CreateMesh.bb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ; CreateMesh Example
  2. ; ------------------
  3. ; In this example, we will create a custom mesh. This custom mesh will be in the shape of a ramp.
  4. Graphics3D 640,480
  5. SetBuffer BackBuffer()
  6. camera=CreateCamera()
  7. light=CreateLight()
  8. RotateEntity light,45,0,0
  9. ; Create blank mesh
  10. ramp=CreateMesh()
  11. ; Create blank surface which is attached to mesh (surfaces must always be attached to a mesh)
  12. surf=CreateSurface(ramp)
  13. ; Now we have our blank mesh and surface, we can start adding vertices to it, to form the shape of our
  14. ; ramp.
  15. ; Vertices are invisible 'points' in a 3D object that we can attach triangles too later.
  16. ; To create a single triangle, you need three vertices, one for each corner.
  17. ; However, you can share vertices between triangles, so you do not always need 3 new vertices per
  18. ; triangle.
  19. ; In the case of our ramp mesh, we will require 6 vertices, one for each corner
  20. v0=AddVertex(surf,0,0,0) ; bottom corner 1
  21. v1=AddVertex(surf,0,0,1) ; bottom corner 2
  22. v2=AddVertex(surf,4,0,1) ; bottom corner 3
  23. v3=AddVertex(surf,4,0,0) ; bottom corner 4
  24. v4=AddVertex(surf,0,2,0) ; top corner 1
  25. v5=AddVertex(surf,0,2,1) ; top corner 2
  26. ; Having created our blank mesh and surface, and added our vertices to form the shape of the mesh, we
  27. ; now need to add triangles to it in order to make it solid and visible to the user. We create
  28. ; triangles simply by connecting vertices up, very much like a 3D dot-to-dot.
  29. ; When adding triangles, we need to remember that they are only one sided, and the side they are
  30. ; visible from is determined by the order in which we specify the vertices when using AddTriangle.
  31. ; If the vertices, in the order that they are specified, are aligned in a clockwise fashion relative
  32. ; to the camera then they will appear visible, otherwise they won't.
  33. ; So, to make our ramp visible from the outside, we will be connecting all vertices in a clockwise
  34. ; fashion, relative to the camera.
  35. t0=AddTriangle(surf,v0,v3,v2) ; bottom triangle 1
  36. t1=AddTriangle(surf,v0,v2,v1) ; bottom triangle 2
  37. t2=AddTriangle(surf,v0,v4,v3) ; front triangle
  38. t3=AddTriangle(surf,v1,v2,v5) ; back triangle
  39. t4=AddTriangle(surf,v0,v1,v5) ; side triangle 1
  40. t5=AddTriangle(surf,v0,v5,v4) ; side triangle 2
  41. t6=AddTriangle(surf,v2,v4,v5) ; top triangle 1
  42. t7=AddTriangle(surf,v2,v3,v4) ; top triangle 2
  43. ; Now we will position our ramp in front of the camera so we can see it!
  44. PositionEntity ramp,0,-4,10
  45. ; Enable wireframe mode so we can see structure of model more clearly
  46. WireFrame True
  47. ; And a quick loop that renders the scene and displays the contents on the screen until we press esc
  48. While Not KeyDown(1)
  49. ; Constantly turn our ramp entity to show it off a bit
  50. TurnEntity ramp,0,1,0
  51. RenderWorld
  52. Flip
  53. Wend
  54. ; The end!
  55. End