3D Math, sorting faces/drawing order

Started by Supermonkey, July 20, 2004, 06:13:46 AM

Previous topic - Next topic

Supermonkey

I've noticed I've been posting alot of questions regarding the same topic of 3D math so I'll try to keep most of my questions in this one thread from now on. Ok so I have a camera working and thats about as far as I've done before. As expected the faces of the cube are not been drawn in the correct order, along with this I need some method of checking if one of the cubes is visible i.e if it is infront of the camera. Anyone care to explain how to go about doing these to things?

cheers

Jonny

P.S code below ;)

PlayBASIC Code: [Select]
;---------------------------------------------------------------------
Rem create a camera
Dim camerapos#(3)
Dim cameraspin#(3)
Dim cameramatrix#(3,3)
viewscale#=100
nearplane#=0.001
Camerapos#(1) = 320
Camerapos#(2) = 240

pcount=16 :Rem total number of points
fcount=12 :Rem total number of faces
ocount=2 :Rem total number of objects

Rem create some space for object information
Dim object#(ocount,11)
Rem variables to say which array entry hold what data
obj_pcount=1
obj_fcount=2
obj_xpos=3
obj_ypos=4
obj_zpos=5
obj_tilt=6
obj_turn=7
obj_roll=8
obj_xscale=9
obj_yscale=10
obj_zscale=11

Rem object 1 has 8 vertices and 6 faces (the cube)
object#(1,obj_pcount)=8
object#(1,obj_fcount)=6
object#(1,obj_xpos)=200
object#(1,obj_ypos)=240
object#(1,obj_zpos)=400
object#(1,obj_tilt)=0
object#(1,obj_turn)=0
object#(1,obj_roll)=0
object#(1,obj_xscale)=100
object#(1,obj_yscale)=100
object#(1,obj_zscale)=100

Rem object 2 has 8 vertices and 6 faces (the pyramid or tetrahedron)
object#(2,obj_pcount)=8
object#(2,obj_fcount)=6
object#(2,obj_xpos)=-200
object#(2,obj_ypos)=240
object#(2,obj_zpos)=300
object#(2,obj_tilt)=0
object#(2,obj_turn)=0
object#(2,obj_roll)=0
object#(2,obj_xscale)=100
object#(2,obj_yscale)=100
object#(2,obj_zscale)=100

Rem create some space for the vertex and face data
Dim points#(pcount,3)
Dim faces(fcount,4)
Dim facecols(fcount,3)

Rem create space for rotation matrices
Dim matrix#(3,3)
Dim objmatrix#(3,3)

Rem read in the vertex data
Restore point_data
For p = 1 To pcount
points#(p,1) = ReadData()
points#(p,2) = ReadData()
points#(p,3) = ReadData()
Next p

Rem read in the face data
Restore face_data
For f = 1 To fcount
faces(f,1) = ReadData()
faces(f,2) = ReadData()
faces(f,3) = ReadData()
faces(f, 4) = ReadData()
Next f

Rem read in the face colours
Restore face_colours
For f = 1 To fcount
facecols(f,1) = ReadData()
facecols(f,2) = ReadData()
facecols(f,3) = ReadData()
Next f

Rem vertex data
point_data:
Data 1,1,1 :Rem cube 1
Data 1,1,-1
Data 1,-1,1
Data 1,-1,-1
Data -1,1,1
Data -1,1,-1
Data -1,-1,1
Data -1,-1,-1

Data 1,1,1 :Rem cube 2
Data 1,1,-1
Data 1,-1,1
Data 1,-1,-1
Data -1,1,1
Data -1,1,-1
Data -1,-1,1
Data -1,-1,-1

Rem face data (defined in a clockwise order when looking at the face)
face_data:
Data 1,3,7,5 :Rem front
Data 1,2,4,3 :Rem right side
Data 5,7,8,6 :Rem left side
Data 2,1,5,6 :Rem top
Data 3,4,8,7 :Rem bottom
Data 4,2,6,8 :Rem back

Data 9,11,15,13 :Rem front
Data 9,10,12,11 :Rem right side
Data 13,15,16,14 :Rem left side
Data 10,9,13,14 :Rem top
Data 11,12,16,15 :Rem bottom
Data 12,10,14,16 :Rem back

Rem the face colours (R,G,B)
face_colours:
Data 255,0,0
Data 255,128,0
Data 255,255,0
Data 0,255,0
Data 0,0,255
Data 255,0,255

Data 255,0,0
Data 255,128,0
Data 255,255,0
Data 0,255,0
Data 0,0,255
Data 255,0,255

Rem some space to put the rotated points
Dim rotated#(pcount,3)

Rem set the initial rotation angles to 0
tilt=0
turn=0
roll=0

Login required to view complete source code



kevin

#1
To remove back faces, ensure the surfaces are defined in either clockwise or anti clockwise order.  So in other words, the join list for each surface connects the points in order.   So if you were to look at any surface directly, the vertex would all be joined in the same way.  

There's a few ways to cull the back faces, the approach that I've always used,  is via performing a 2D cross product on the translated polygon.  If the polygon edges are connected in clockwise order, then the direction of the face when visible will point out of the monitor, when facing away, it's on the back side of the object and doesn't need to be drawn.   That's how the scroll box example works..  

When a polygon is found to be visible, rather than draw it there and then.  We add this definition to the scenes visible polygon list.    The visible polygon list  contains all the info needed to render this surface later.. Ie. it's translated  X1,y1,x2,y2,x3,y3 and Z depth (from the camera)

Once all objects have been processed to visible polygons list.  The list has to be sorted, then the sorted list is drawn from Back to Front.   Each polygon is sorted upon it's average Z depth from the camera.   Be warned though, since surfaces are drawn without taking into account the actual Z depth of each pixel.  Polygon popping will occur.

Another thing to consider is that some polygons will need to be clipped against the camera view before they're allowed to the be placed in the Visible polygons list.

well, there's something to chew on.

Supermonkey

cheers for the help kevin I got the backface bit done last night, very small adjustment  :)  I'll look at the sorting/drawing order bit tonight when I have some time.

oh and I saw the new scrolling cube example I was dead chuffed to see my name at the top of a piece of source code released with PB  :P

kevin

we need all the examples we can get at this point :)