-_--_--_--_--_--_--_--_--_--_--_--_--_--_-
-_--_--_--_--_--_--_--_-
-¯--¯--¯--¯--¯--¯--¯--¯--¯--¯--¯--¯--¯--¯-
-¯--¯--¯--¯--¯--¯--¯--¯-
AS:3Dimensions - Intermediate
---------
in this thread im going to discuss using fills in your 3d engine with basic flat-lighting and back-face culling (along with some advancements for a better rendering)
Sample file
note that fills are set to 85 transparency so you can see the back-face culling at work
//
in my basic thread, i discussed the floating balls sample, each ball was simply a coordinate being rotated and projected onto the screen.
fills (polygons) are simply a group of coordinates joined together filled with a colour
//
there are two ways you could go about setting up your 3d models:
the first is to create a list of polygons with the coordinates for each vertices listed for each coordinate:
the second is to create 2 lists:
the first being a list of the coordinate of each vertex
the second being a list of polygons - but instead of listing the coordiantes of each vertex, you give a reference to a vertex in the first list
the advantages of the second is it requires less computation on rotations and projection
but the disadvantages are when you need to clip polygons to the viewing frustum (dont worry about what i just said for this thread :p)
//
i like to use the second way but ill give an example for both using a pyramid model:
the first way:
var polygons:Array = [
[[-100,0,100],[100,0,100],[100,0,-100],[-1
00,0,-100]],
[[-100,0,100],[0,200,0],[100,0,100]],
[[100,0,100],[0,200,0],[100,0-100]],
[100,0,-100],[0,200,0],[-100,0,-100]],
[[-100,0,-100],[0,200,0],[-100,0,100]]
]
the second way:
var vertex:Array = [[-100,0,100],[100,0,100],[100,0,-100],[-1
00,0,-100],[0,200,0]];
var polygon:Array = [[4,0,1,2,3],[3,0,4,1],[3,1,4,2],[3,2,4,3]
,[3,3,4,0]];
(note that the first number in the polygon array is the number of vertices, its not neccesary but it keeps things a bit neater in code)
again its what youd rather use, i think that the second way is far neater
//
at the end of each polygon index you can then add other info for the polygon like the colour of its fill etc. for example [4,0,2,5,1,0x00ff00]
//
since from my other thread you will already know how to rotate coordinates and to project them, the next thing is how do you do z-sorting on polygons?
in a proper 3d engine for proper games, each pixel of each polygon is tested called a z-buffer, since this isnt possible (and would be far far far too slow) you can only z-sort the polygons
the most effective way is by finding the average distance to the camera for each vertex and sort the polygons by this
you then draw the polygons from the back to the front overlapping polygons behind with the ones infront:
you need to use the array function sortOn for this:
you need to go through each polygon and find the average distance to the camera (using pythagoreus theorem extended to 3dimensions - hyp^2 = x^2+y^2+z^2)
you then store it in an array with a refernence to the polygon: ie <arrayname>.push({dis:<the average distance>, poly:<the index in the polygon array for that polygon>});
with that array you call <arrayname>.sortOn("dis",18); (18 means it is in descending order so the first item in the array will be the back polygon)
//