This is a simplified version of what I use, I came up with it while trying to emulate what B3D does. The directions of rotations are probably different and one of the rotation functions in B3D resets the rotation first which mine doesn't (I find it more useful that way).
Type vertex
Field x#,y#,z#
Field rx#,ry#,rz#
Field sx,sy
End Type
Type triangle
Field vertex.vertex[3]
End Type
Type entity
Field x#,y#,z#
Field ux0#,uy0#,uz0#
Field ux1#,uy1#,uz1#
Field ux2#,uy2#,uz2#
Field vertex.vertex[8]
Field vertex_count
Field triangle.triangle[12]
Field triangle_count
End Type
;*******************************************************************************************************************************
;entity creation functions
Function add_vertex(entity_h,x#,y#,z#)
entity.entity=Object.entity(entity_h)
entity\vertex[entity\vertex_count]=New vertex
entity\vertex[entity\vertex_count]\x=x
entity\vertex[entity\vertex_count]\y=y
entity\vertex[entity\vertex_count]\z=z
entity\vertex_count=entity\vertex_count+1
Return Handle(entity\vertex[entity\vertex_count-1])
End Function
Function add_triangle(entity_h,v0_h,v1_h,v2_h)
entity.entity=Object.entity(entity_h)
entity\triangle[entity\triangle_count]=New triangle
entity\triangle[entity\triangle_count]\vertex[0]=Object.vertex(v0_h)
entity\triangle[entity\triangle_count]\vertex[1]=Object.vertex(v1_h)
entity\triangle[entity\triangle_count]\vertex[2]=Object.vertex(v2_h)
entity\triangle_count=entity\triangle_count+1
End Function
Function create_entity()
new_entity.entity=New entity
;set up the initial matrix (1,0,0),(0,1,0),(0,0,1)
new_entity\ux0=1.0
new_entity\uy1=1.0
new_entity\uz2=1.0
Return Handle(new_entity)
End Function
;*******************************************************************************************************************************
;entity control functions
;rotate the entity relative to it's own axis
Function rotate_entity(entity_h,a#,b#,c#)
entity.entity=Object.entity(entity_h)
csa#=Cos(-a)
sna#=Sin(-a)
csb#=Cos(-b)
snb#=Sin(-b)
csc#=Cos(c)
snc#=Sin(c)
x#=entity\ux0*csb+entity\ux2*snb
z#=entity\ux2*csb-entity\ux0*snb
entity\ux2=z*csa+entity\ux1*sna
y#=entity\ux1*csa-z*sna
entity\ux0=x*csc+y*snc
entity\ux1=y*csc-x*snc
x=entity\uy0*csb+entity\uy2*snb
z=entity\uy2*csb-entity\uy0*snb
entity\uy2=z*csa+entity\uy1*sna
y=entity\uy1*csa-z*sna
entity\uy0=x*csc+y*snc
entity\uy1=y*csc-x*snc
x=entity\uz0*csb+entity\uz2*snb
z=entity\uz2*csb-entity\uz0*snb
entity\uz2=z*csa+entity\uz1*sna
y=entity\uz1*csa-z*sna
entity\uz0=x*csc+y*snc
entity\uz1=y*csc-x*snc
End Function
;rotate the entity relative to the world axis
Function turn_entity(entity_h,a#,b#,c#)
entity.entity=Object.entity(entity_h)
csa#=Cos(a)
sna#=Sin(a)
csb#=Cos(b)
snb#=Sin(b)
csc#=Cos(-c)
snc#=Sin(-c)
x#=entity\ux0*csb+entity\uz0*snb
z#=entity\uz0*csb-entity\ux0*snb
entity\uz0=z*csa+entity\uy0*sna
y#=entity\uy0*csa-z*sna
entity\ux0=x*csc+y*snc
entity\uy0=y*csc-x*snc
x=entity\ux1*csb+entity\uz1*snb
z=entity\uz1*csb-entity\ux1*snb
entity\uz1=z*csa+entity\uy1*sna
y=entity\uy1*csa-z*sna
entity\ux1=x*csc+y*snc
entity\uy1=y*csc-x*snc
x=entity\ux2*csb+entity\uz2*snb
z=entity\uz2*csb-entity\ux2*snb
entity\uz2=z*csa+entity\uy2*sna
y=entity\uy2*csa-z*sna
entity\ux2=x*csc+y*snc
entity\uy2=y*csc-x*snc
End Function
;move the entity to world coordinates
Function position_entity(entity_h,x#,y#,z#)
entity.entity=Object.entity(entity_h)
entity\x=x
entity\y=y
entity\z=z
End Function
;move the entity relative to the world axis
Function translate_entity(entity_h,x#,y#,z#)
entity.entity=Object.entity(entity_h)
entity\x=entity\x+x
entity\y=entity\y+y
entity\z=entity\z+z
End Function
;move the entity relative to it's own axis
Function move_entity(entity_h,x#,y#,z#)
entity.entity=Object.entity(entity_h)
entity\x=entity\x+x*entity\ux0+y*entity\ux1+z*entity\ux2
entity\y=entity\y+x*entity\uy0+y*entity\uy1+z*entity\uy2
entity\z=entity\z+x*entity\uz0+y*entity\uz1+z*entity\uz2
End Function
;********************************************************************************************************************************
;entity rendering functions
Function rotate_vertices(entity.entity)
i=0
While i<entity\vertex_count
vertex.vertex=entity\vertex[i]
vertex\rx= vertex\x*entity\ux0 + vertex\y*entity\ux1 + vertex\z*entity\ux2 + entity\x
vertex\ry= vertex\x*entity\uy0 + vertex\y*entity\uy1 + vertex\z*entity\uy2 + entity\y
vertex\rz= vertex\x*entity\uz0 + vertex\y*entity\uz1 + vertex\z*entity\uz2 + entity\z
zr#=640.0/vertex\rz
vertex\sx=320.0+vertex\rx*zr
vertex\sy=240.0-vertex\ry*zr
i=i+1
Wend
End Function
Function draw_triangles(entity.entity)
i=0
While i<entity\triangle_count
triangle.triangle=entity\triangle[i]
v0.vertex=triangle\vertex[0]
v1.vertex=triangle\vertex[1]
v2.vertex=triangle\vertex[2]
If ((v1\sx-v0\sx)*(v2\sy-v0\sy))-((v2\sx-v0\sx)*(v1\sy-v0\sy))>0.0 Then
Line v0\sx,v0\sy,v1\sx,v1\sy
Line v1\sx,v1\sy,v2\sx,v2\sy
Line v2\sx,v2\sy,v0\sx,v0\sy
End If
i=i+1
Wend
End Function
Function draw_entity(entity_h)
entity.entity=Object.entity(entity_h)
rotate_vertices(entity)
draw_triangles(entity)
End Function
;*****************************************************************************************************************************
;main code
Function create_cube(size#)
cube=create_entity()
v0=add_vertex(cube,-size,-size,-size)
v1=add_vertex(cube,-size,-size, size)
v2=add_vertex(cube, size,-size, size)
v3=add_vertex(cube, size,-size,-size)
v4=add_vertex(cube,-size, size,-size)
v5=add_vertex(cube,-size, size, size)
v6=add_vertex(cube, size, size, size)
v7=add_vertex(cube, size, size,-size)
add_triangle cube,v0,v1,v5
add_triangle cube,v5,v4,v0
add_triangle cube,v1,v2,v6
add_triangle cube,v6,v5,v1
add_triangle cube,v2,v3,v7
add_triangle cube,v7,v6,v2
add_triangle cube,v3,v0,v4
add_triangle cube,v4,v7,v3
add_triangle cube,v0,v3,v2
add_triangle cube,v2,v1,v0
add_triangle cube,v4,v5,v6
add_triangle cube,v6,v7,v4
Return cube
End Function
Function main()
Graphics 640,480,32,2
cube=create_cube(100.0)
position_entity cube,0,0,1000
SetBuffer BackBuffer()
Color 255,255,255
While Not(KeyHit(1))
rotate_entity cube,2,2,2
;turn_entity cube,0.1,0,0
;move_entity cube,0,0,10
draw_entity cube
Flip
Cls
Wend
End Function
main()
end