Polygon Dithering and Color Usage

SGDK only sub forum

Moderator: Stef

Post Reply
kubilus1
Very interested
Posts: 237
Joined: Thu Aug 16, 2012 2:25 am
Contact:

Polygon Dithering and Color Usage

Post by kubilus1 » Sat Nov 08, 2014 9:32 pm

So Stef, I notice that the BMP_drawPolygon function will always draw the polygons dithered. I'm wondering how I would go about changing this.

I would like to be able retain more color choices but still some sort of lighting effect. Since the current lighting mechanism cycles through a number of color entries and only can use one palette, that does not leave a lot of choice.

What I would like to see is something along the lines of increasing the dithering of the fill color to have a shading effect. Completely filled is full brightness, every other pixel the next step down, every 3rd(4th?) pixel the next step, etc. Is this feasible?

I've dug a bit through the drawPolygon assembly and I don't think I've quite wrapped my head around it yet, any pointers would be appreciated.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Sun Nov 09, 2014 9:41 am

Basically what you want is sort of gouraud shading right ?
The drawPolygon method is really done for flat rendering (and allow dithering) and having gouraud shading is a total different matter and also require more time. I did that a long time ago but to be honest the genesis palette is not suited for gouraud shading as you need to fit to a single 16 colors. You can define a special 128 colors palette using dithering but it will even require more time to implement polygon fill this way.

kubilus1
Very interested
Posts: 237
Joined: Thu Aug 16, 2012 2:25 am
Contact:

Post by kubilus1 » Sun Nov 09, 2014 2:07 pm

I don't think I explained myself well. I'm not talking about gouraud shading.

In SGDK starting with the cube_flat example, when rendering the polygon for flat shading each surface is drawn by first calculating the lighting, and indexing a color based on this lighting calculation. Then the BMP_drawPolygon function is called.

This always results in a dithered color for some reason. Also this means that 6 color variations in a palette are set aside to show lighting for the polygon. In effect there can only be two colors for lighting!

So I would like to see something like this. I would pick a single color in the palette. Full intensity would be the color undithered. Next lighting step down would be the color dithered one step. Next lighting step dithered two steps. etc.

This would all be flat shading like currently.

Even getting a non-dithered flat polygon fill would be nice!

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Mon Nov 10, 2014 10:22 am

Oh sorry i got it, well the cube flat does it very simply, the dithering is always present because the drawPolygon function expect a byte value to fill the polygon and in my light calculation i only use value from 0 to 15 (so 4 bits). This is incorrect but it was just for the demo so and i did not bothered much about it. Just thing the drawPolygon method expect a real byte value to fill the polygon and correctly handle dithering by switching color nibbles at each scanline. So you can prepare a virtual palette containing 128 bytes entries for each 2 nibbles combination. I said 128 and not 256 because actually the nibble 0-1 is equivalent to 1-0 so you can at least drop down the size of the table by 2. Also the sega megadrive is limited to 8 intensity level per component so you can have only 8 grays level where 0 is black and 7 is white.
In this case you can build a 15 entries virtual palette to make the dither combinations :
pal[0] = $00
pal[1] = $01
pal[2] = $11
pal[3] = $12
pal[4] = $22
pal[5] = $23
pal[6] = $33
pal[7] = $34
pal[8] = $44
pal[9] = $45
pal[10] = $55
pal[11] = $56
pal[12] = $66
pal[13] = $67
pal[14] = $77

Computing the light will give you the according palette index entry : 0 for black and 14 for white. Then you use the palette info to feed the drawPolygon method :

drawPolygon(poly, 4, pal[light_index]);

kubilus1
Very interested
Posts: 237
Joined: Thu Aug 16, 2012 2:25 am
Contact:

Post by kubilus1 » Mon Nov 10, 2014 11:48 pm

Fantastic, thanks for the information!

Post Reply