- THE OUTLAW TRIAD DEMO-SERIES - ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄþ PART XI þÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Written by : Vulture/OT Code in : Pascal Topics : 3d glenzing, flatshading ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄþ Introduction þÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Welcome to the Outlaw Triad demo-series! In these series we will be talking about programming demo-effects in either pascal or assembler. Theory behind the effects shall be discussed while a full sourcecode is also provided. Right, we will continue our discussions on 3d graphics. This time we'll be talking about some relatively simple concepts. 3d glenzing and flatshading! These effects are pretty easy to implement if you know your 3d. This doc is an add-on to part 10 in which we created a basic 3d engine. Ya really should read that text first before reading this. Otherwise you might get a little confused. Or maybe not... or maybe you will... naah, enough said... Enjoy! ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄþ Theory þÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ - GLENZING - First of all, let me explain what glenzing means. To put it simply, imagine a cube made out of colored glass. Whenever you look through a side, you see the sides lying behind it. But... in a slightly different color than that side really has. Why? Obviously, because the color of the side which you are looking through effects the color of the sides behind it. Ok, with that clear, how do we code this stuff? It's real easy! Here we go: Most important, in the previous tutorial (on face sorting) we sorted all our polygons on their average z value. We don't do this when showing a glenzing 3d object because it isn't necessary! It doesn't matter in which order you draw the polygons. Ok, as you probably know, a polygon is drawn by drawing each horizontal scanline it is consists of. In the previous tutorial (#10) we just used one constant color for each polygon. This time, while drawing, we don't just put one color of the polygon on the screen but we'll also use the background color of the pixel of the current scanline we are plotting. So, the algorithm for drawing one pixel of one scanline looks like: - grab background color - add polygon color - plot pixel on the screen - point to next vga spot All you have to do is change a few lines of code in your horizontal line routine and you're done. Of course this is quite a bit slower than just plotting a row of pixels of one constant color. Ok, here's a part of the horizontal line routine. Examine it. Shouldn't be too hard to understand. Of course you have to calculate the exact vga position first before using this code. Check the complete source code for the total routine. mov cx,x2 sub cx,x1 { Calculate # of bytes to draw } cmp cx,0 jle @No_Draw { Quit if no pixels to draw } @Draw_Loop: mov al,[byte ptr es:di] { Grab background color } add al,Color { Add color } stosb { And store the byte } loop @Draw_Loop @No_Draw: There you go! Easy, eh? First check to see if pixels have to be drawn and if so, get the backgroundcolor, add the polygon color and store the byte! Do this for all pixels in the horizontal line. That's all! The source code is simply the 3d system used in the previous trainer so if you got that one already, this should be a breeze. - FLATSHADING - Ok, next step in 3d is of course shading of the objects. There are many different ways of shading, some of which are very complicated and difficult to understand. We will start with the most simple and basic shading method around and that's flatshading (also called Lambart shading). What's that? Imagize a cube rotating in front of the sun. When a face turns towards the sun, it will get a brighter color. And when the face turns away from the sun, it will get a darker color. Important: the entire face will get another color using this method. This is called flatshading and is of course more realistic than just having faces with one constant color. The light is coming from the viewers side in this example (and in most demos anyway). How do we code this stuff... Well, there are different ways of implementing this effect and I'm going to show the easiest one. We simply use the average z-value of the polygon to determine the color of this polygon. If you read part 10 of these series, you know we need to determine the drawing order of the polygons. This is done by adding the z-values of the 4 3d points of the polygons. The resulting values are then sorted and the object is drawn from back to front. For a complete explanation, go and read part 10. Anyway, the z-values we sorted can also be used to determine the color of the polygons. Again, the values in the list are the 4 z-values of each polygon added. The resulting values are then sorted from small to large. Now, while drawing, grab the z-value of the poly (the absolute z-value) from the list and divide this by a certain number to get it into the color range you want (I divided the value by 8). This is the color of the face. So, for each frame: - Rotate polygons - Store added z-values - Sort polygons on these z-values - While drawing: - Get absolute z-value from list - Divide it by color factor - Draw the polygon with resulting color For example: if I reserved colors 0 - 18 for the shading and the z-value can be 280 or less (the 4 z-values of the polygon added), the dividing factor should be around 16. Because 280/16=17.5 and that fits into the color range we wanted. Before this, you should have setup a nice palette for the shading. In the source I used a gray-scale palette. And of course, the more colors you use, the more realistic the shading will be. This method is very easy to implement but it's also the most unrealistic one. It's good enough for cubes and other simple objects but for more complicated objects you'll need a better method of calculating the color of the polygon. Flatshading can also be realised by using face-normals and other complicated vector calculations. This method will result in better shading. Hmm, yeah, so you would like to know about that too, rite? Be prepared for future trainers from OT including textfiles on improved flatshading, gouraud, texture, phong, z-buffering and a lot more! For questions, chatting or for suggestions for future trainers, mail me. Ya know where to find me... I'm always in for a chat so don't hesitate to contact me (or any other member of OT). That's all for now. Happy coding! -Vulture/Outlaw Triad- ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄþ Distro Sites þÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Call our distribution sites! All our releases are available at: BlueNose World HQ +31 (0)345-619401 The Force Distrosite +31 (0)36-5346967 Bugs'R'Us Distrosite +31 (0)252-686092 More distros wanted! The 7 Angels Distrosite +31 (0)715-148377 (preferably outside ShockWave South African HQ +27 (011)888-6345 of the Netherlands) Society HQ United States HQ +1 (518)465-6721 ACe World Brazilian HQ +55 (21)-259-8847 Corps Elite Canadian HQ +403 (ITS)-PRIVATE Also check the major FTP/WWW sites for Outlaw Triad productions. ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄþ Contact þÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Want to contact Outlaw Triad for some reason? You can reach us at our distrosites in Holland. Or if you have e-mail access, mail us: Vulture (coder/pr) comma400@tem.nhl.nl Our internet homepage: http://www.tem.nhl.nl/~comma400/vulture.html These internet adresses should be valid at least till june 1997. ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Quote: Toe = part of the foot used to find furniture in the dark