Gouraud Vectors by Tumblin / Bodies In Motion ================================ Hi there everybody. I'm back with another source code release, and this time its on gouraud shading. Actually I learned how to do gouraud shading when I was at NAID '95. (Incidentally, try checking out squ.zip, Squishtro by Dungeon Dwellers Design's production for the intro competition at NAID... there is a picture of me getting squished all around with a coding carrot in my mouth! hahahahah, long story :) Anyway, I am not going to try to describe how to do the 3d polygon stuff that you see on the screen... it would take too long, so I will just try to explain a little bit about the gouraud shading algorithm. Lets get started. Gouraud shading is a multiple step process. When you call a routine that would draw a gouraud shaded triangle, you pass the x, y, and color of all three tips of the triangle. Now, with this information you have to trace each of the three edges and record the coordinates and the color information along the way. What you are trying to do is interpolate from color A to color B. The way you do this is you calculate the amount of change in color divided by the amount of change in the distance you have to trace the edge. This will give you something similar to the slope of a line, only you are dealing with color rather than just plain x's and y's. What you do with the slope is you add it to your screen coordinate and when it comes time to record the information, truncate off the fractional part. You can do this by using floating point (as I have used in my source code, because I was a little lazy) or fixedpoint. I didn't bother to optimize the routine because it would just get in the way of learning how the algorithm works, which is my purpose for releasing this anyway (: After you have all three edges of the triangle traced and the coordinates, and color information are recorded for each scanline (horizontally or vertically, your choice... I chose to fill my scanlines horizontally in the source code that I have included here), you have to fill between one edge of the scanline and the opposite edge of the scanline. The filling is again done the same way as above, only you actually draw the point rather than record it into a buffer. Once you have all of the scanlines filled, you have drawn a gouraud shaded triangle! Of course you could take this many steps further and create a routine that draws arbitrarily sided polygons, or many other interesting things. Okay, so how do you come up with the color of the vertices of the polygons you are trying to draw? Well, in my code I did a simple little hack that not entirely correct, but still produces some funky gouraud shaded objects (: What I did was take each vertex in the object and multiply it by 2 to generate vertex normals. Then when I spin the object around, I also spin the normal vertices around as well. When it comes time to draw the polygons, I take the dot product of the vertex normals and the light source and calculate a color with a little formula. This is the same idea as lambert shading, only you apply it to each vertex normal, rather than to the one normal for the whole polygon. I hope this becomes more clear when you look at the source code. Oh, I thought I might let you know that I use a tab setting of 2 in all my source code, so you may want to set your editor's tab setting to match it. Here are some tips to get it to compile: - Unzip all the files from this release into a directory - Open a project file (I used Turbo C++ 3.0 to compile the demo) - In the project file, include gvectors.cpp, fixedl.asm, and xlib61l.lib - Make sure that you have the compiler set to the LARGE memory model - One thing that I did with the way I organized my copy of the XLIB library was that I copied all the *.h files into the directory I made called c:\tc\xlib61, then I added it to compiler's include and link directories in the Options/Directories menu. This allows me to simply put #include at the top of my programs, rather than having all those *.h files cluttering up my source code directory. I still copied the xlib61l.lib file to my source code directory however. Works out great! - That should do it (: If you have any problems, just email me. The V10 objects were all created in my vector editor called VED v1.0, which I released at NAID'95. Here's where you can find it: ftp.cdrom.com:/pub/demos/code/utils/ved10.zip The gvectors.h file which contains all the palette data was created by my PCX to C++ source code convertor. You can find that on: ftp.cdrom.com:/pub/demos/code/images/pcx2csrc.zip. I used Deluxe Paint to create a PCX file with the color number range 16 to 96 to be bright red to black. Then I deleted the information about the bitmap at the top of the file and renamed the palette buffer for this program. Quite a useful little utility I wrote! I guess I'll wrap up this text file with some greets, so here they go: Dungeon Dwellers Design - Thanks for squishing my face at NAID! hehe. CORE - Your demo has been an inspiration to me. Thanks for letting me hang with you guys at NAID. Ronski - glad to hear that you're alive and well, give me a ring more often! Necros - too bad I didn't get to meet you, but saw you on the big stage... congratulations for your sucess at NAID'95, I loved your song! Boggart - thanks for showing me your code and for explaining gouraud shading Voltaire - your release on gouraud shading helped a lot in making this MikMak - I like your music system, very nice Complex - your DOPE demo rocks (: Everyone I talk to on IRC in #coders : ae, johnson, fYSx, ReDDoG, anixter, MrBeach, Kodiak, Addict, Snowman, ior, Hasty, and Lord Logics. Have fun with the code and we might have some more sources for ya in the not too distant future. Until then, keep on coding, and keep on eating your coding carrots! Tumblin / Bodies In Motion '95 tumblin@mi.net