lmesh.bb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;Lod Mesh
  2. ;David Bird
  3. ;[email protected]
  4. Graphics3D 640,480
  5. SetBuffer BackBuffer()
  6. lit=CreateLight()
  7. cam=CreateCamera()
  8. CameraRange cam,1,1000
  9. PositionEntity cam,0,0,-100
  10. ;Load in mesh and texture
  11. mesh=LoadMesh("dolphin.x")
  12. tex=LoadTexture("dolphin.bmp")
  13. EntityTexture mesh,tex
  14. ScaleMesh mesh,.2,.2,.2
  15. Get_Data(mesh) ;Store mesh inforamtion
  16. ;mesh=Create_New_Mesh(mesh,1) ;Reduce mesh by 1 triangle
  17. ;WireFrame True
  18. While Not KeyDown(1)
  19. TurnEntity mesh,0,1,0
  20. If KeyDown(57) Then
  21. mesh=Create_New_Mesh(mesh,1) ; Reduce mesh by 1 triangle
  22. EntityTexture mesh,tex
  23. End If
  24. tri_s=CountTriangles(GetSurface(mesh,CountSurfaces(mesh)))
  25. UpdateWorld
  26. RenderWorld
  27. Text 0,0,"Lod Mesh. (c)2001 David Bird, [email protected]"
  28. Text 0,16,"Press space to reduce triangles in mesh"
  29. Text 0,32,"Current triangle count="+tri_s
  30. Flip
  31. Wend
  32. FreeEntity cam
  33. EndGraphics
  34. End
  35. Type vert
  36. Field x#
  37. Field y#
  38. Field z#
  39. Field u#
  40. Field v#
  41. Field oindex
  42. Field index
  43. Field nindex
  44. Field use
  45. Field cost#
  46. Field nei.vert
  47. End Type
  48. Type tri
  49. Field ind[3]
  50. Field mv[2]
  51. Field index
  52. Field tcost#
  53. Field use
  54. End Type
  55. Function Get_Data(mesh)
  56. surf=GetSurface(mesh,CountSurfaces(mesh))
  57. For index=0 To CountVertices(surf)-1
  58. a.vert=New vert
  59. a\index=index
  60. a\oindex=index
  61. a\use=True
  62. a\cost=1000000
  63. a\x=VertexX(surf,index)
  64. a\y=VertexY(surf,index)
  65. a\z=VertexZ(surf,index)
  66. a\u=VertexU(surf,index)
  67. a\v=VertexV(surf,index)
  68. Next
  69. For index=0 To CountTriangles(surf)-1
  70. b.tri=New tri
  71. b\use=True
  72. For c=0 To 2
  73. b\ind[c]=TriangleVertex(surf,index,c)
  74. Next
  75. Get_Tri_Cost#(b)
  76. cnt=cnt+1
  77. b\index=index
  78. Next
  79. End Function
  80. Function Find_Vert.vert(index)
  81. For a.vert=Each vert
  82. If a\index=index Then Return a
  83. Next
  84. Return Null
  85. End Function
  86. Function Create_New_Mesh(mesh,red)
  87. For it=1 To red
  88. del.tri=Get_Lowest_Cost_Tri()
  89. If del<>Null Then
  90. del\use=False
  91. reindex_tris(del\ind[del\mv[0]],del\ind[del\mv[1]])
  92. End If
  93. Next
  94. ;find the lowest cost (used vert)
  95. rol#=EntityRoll(mesh)
  96. pit#=EntityPitch(mesh)
  97. yaw#=EntityYaw(mesh)
  98. px#=EntityX(mesh,True)
  99. py#=EntityY(mesh,True)
  100. pz#=EntityZ(mesh,True)
  101. surf=GetSurface(mesh,1)
  102. ClearSurface surf,False,True
  103. For b.tri=Each tri
  104. If b\use=True Then
  105. AddTriangle surf,b\ind[0],b\ind[1],b\ind[2]
  106. End If
  107. Next
  108. UpdateNormals mesh
  109. RotateEntity mesh,pit,yaw,rol
  110. PositionEntity mesh,px,py,pz
  111. Return mesh
  112. End Function
  113. Function Get_Lowest_Cost_Tri.tri()
  114. cost#=100000
  115. pnt.tri=Null
  116. For a.tri=Each tri
  117. If a\use=True Then
  118. If a\tcost<cost Then
  119. cost=a\tcost
  120. pnt.tri=a
  121. End If
  122. End If
  123. Next
  124. Return pnt.tri
  125. End Function
  126. Function ReIndex_Tris(oldtri,newtri)
  127. For a.tri=Each tri
  128. p=0
  129. If a\ind[0]=oldtri Then a\ind[0]=newtri:p=1
  130. If a\ind[1]=oldtri Then a\ind[1]=newtri:p=1
  131. If a\ind[2]=oldtri Then a\ind[2]=newtri:p=1
  132. If p=1 Then Get_Tri_Cost(a)
  133. Next
  134. End Function
  135. Function Get_Tri_Cost#(a.tri)
  136. v0.vert=Find_Vert(a\ind[0])
  137. v1.vert=Find_Vert(a\ind[1])
  138. v2.vert=Find_Vert(a\ind[2])
  139. x0#=v0\x:y0#=v0\y:z0#=v0\z
  140. x1#=v1\x:y1#=v1\y:z1#=v1\z
  141. x2#=v2\x:y2#=v2\y:z2#=v2\z
  142. coord0#=Sqr((x0-x1)^2+(y0-y1)^2+(z0-z1)^2)
  143. coord1#=Sqr((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)
  144. coord2#=Sqr((x2-x0)^2+(y2-y0)^2+(z2-z0)^2)
  145. If coord0<coord1 And coord0<coord2 Then p=0
  146. If coord1<coord0 And coord1<coord2 Then p=1
  147. If coord2<coord0 And coord2<coord1 Then p=2
  148. Select p
  149. Case 0
  150. a\mv[0]=1:a\mv[1]=0
  151. Case 1
  152. a\mv[0]=2:a\mv[1]=1
  153. Case 2
  154. a\mv[0]=0:a\mv[1]=2
  155. End Select
  156. a\tcost=coord1+coord2+coord3
  157. Return a\tcost
  158. End Function