
; AddMesh Example
; ---------------
Graphics3D 640,480
SetBuffer BackBuffer()
type_ground=1
type_character=2
Dim temp_vertex(4,4)
camera=CreateCamera()
PositionEntity camera,0,10,-10
light=CreateLight()
RotateEntity light,90,0,0
; Create tree mesh (upper half)
tree=CreateMesh()
PositionMesh tree,0,1.5,0
width#=3
height#=3
For i=1 To 50 Step 10
For j=1 To 50 Step 10
Local surface=CreateSurface(tree)
temp_vertex(0,0)=AddVertex(surface,i-width#,0,j-height#)
temp_vertex(0,1)=AddVertex(surface,i-width#,0,j+height#)
temp_vertex(1,0)=AddVertex(surface,i+width#,0,j-height#)
temp_vertex(1,1)=AddVertex(surface,i+width#,0,j+height#)
VertexTexCoords surface,temp_vertex(0,0),0,1,0
VertexTexCoords surface,temp_vertex(0,1),0,0,0
VertexTexCoords surface,temp_vertex(1,0),1,1,0
VertexTexCoords surface,temp_vertex(1,1),1,0,0
AddTriangle(surface,temp_vertex(0,0),temp_vertex(0,1),temp_vertex(1,1))
AddTriangle(surface,temp_vertex(1,1),temp_vertex(1,0),temp_vertex(0,0))
Next
Next
UpdateNormals tree
EntityType tree,type_ground
Player=CreateCube()
PositionEntity player,-7,0,-4
EntityType player,type_character
Collisions type_character,type_ground,2,3
While Not KeyDown( 1 )
MoveEntity player,0.1,0,0.1
PointEntity camera,player
UpdateWorld
RenderWorld
Draw_Wire_2D(tree,camera,255,0,0)
Flip
Wend
End
Function Draw_Wire_2D(mesh,cam,red=0,grn=0,blu=0)
Color red,grn,blu
If EntityInView(mesh,cam)=False Then Return
cnt=CountSurfaces(mesh)
For a=1 To cnt
surf=GetSurface(mesh,a)
For tri=0 To CountTriangles(surf)-1
TFormPoint VertexX(surf,TriangleVertex(surf,tri,0)),VertexY(surf,TriangleVertex(surf,tri,0)),VertexZ(surf,TriangleVertex(surf,tri,0)),mesh,0
CameraProject(cam,TFormedX(),TFormedY(),TFormedZ())
sx1=ProjectedX()
sy1=ProjectedY()
TFormPoint VertexX(surf,TriangleVertex(surf,tri,1)),VertexY(surf,TriangleVertex(surf,tri,1)),VertexZ(surf,TriangleVertex(surf,tri,1)),mesh,0
CameraProject(cam,TFormedX(),TFormedY(),TFormedZ())
sx2=ProjectedX()
sy2=ProjectedY()
TFormPoint VertexX(surf,TriangleVertex(surf,tri,2)),VertexY(surf,TriangleVertex(surf,tri,2)),VertexZ(surf,TriangleVertex(surf,tri,2)),mesh,0
CameraProject(cam,TFormedX(),TFormedY(),TFormedZ())
sx3=ProjectedX()
sy3=ProjectedY()
If Check_Cull_2D(sx1,sy1,sx2,sy2,sx3,sy3)<0
Line sx1,sy1,sx2,sy2
Line sx2,sy2,sx3,sy3
Line sx3,sy3,sx1,sy1
End If
Next
Next
End Function
Function Check_Cull_2D(x1,y1,x2,y2,x3,y3)
Return (x1-x2)*(y3-y2)-(y1-y2)*(x3-x2)
End Function