models_mesh_generation.bmx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. SuperStrict
  2. Framework Ray.Lib
  3. Const NUM_MODELS:Int = 9 ' Parametric 3d shapes to generate
  4. ' Initialization
  5. '--------------------------------------------------------------------------------------
  6. Const screenWidth:Int = 800
  7. Const screenHeight:Int = 450
  8. InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation")
  9. ' We generate a checked image for texturing
  10. Local checked:RImage = GenImageChecked(2, 2, 1, 1, RED, GREEN)
  11. Local texture:RTexture2D = LoadTextureFromImage(checked)
  12. UnloadImage(checked)
  13. Local models:RModel[NUM_MODELS]
  14. models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 4, 3))
  15. models[1] = LoadModelFromMesh(GenMeshCube(2.0, 1.0, 2.0))
  16. models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32))
  17. models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16))
  18. models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16))
  19. models[5] = LoadModelFromMesh(GenMeshTorus(0.25, 4.0, 16, 32))
  20. models[6] = LoadModelFromMesh(GenMeshKnot(1.0, 2.0, 16, 128))
  21. models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0))
  22. models[8] = LoadModelFromMesh(GenMeshCustom())
  23. ' Set checked texture as default diffuse component for all models material
  24. For Local i:Int = 0 Until NUM_MODELS
  25. models[i].materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture
  26. Next
  27. ' Define the camera to look into our 3d world
  28. Local camera:RCamera = New RCamera( New RVector3(5.0, 5.0, 5.0), New RVector3(0.0, 0.0, 0.0), New RVector3(0.0, 1.0, 0.0), 45.0, 0)
  29. ' Model drawing position
  30. Local position:RVector3 = New RVector3(0.0, 0.0, 0.0)
  31. Local currentModel:Int = 0
  32. SetTargetFPS(60) ' Set our game to run at 60 frames-per-second
  33. '--------------------------------------------------------------------------------------
  34. ' Main game loop
  35. While Not WindowShouldClose() ' Detect window close button or ESC key
  36. ' Update
  37. '----------------------------------------------------------------------------------
  38. UpdateCamera(camera, CAMERA_ORBITAL) ' Update internal camera and our camera
  39. If IsMouseButtonPressed(MOUSE_LEFT_BUTTON) Then
  40. currentModel = (currentModel + 1) Mod NUM_MODELS ' Cycle between the textures
  41. End If
  42. If IsKeyPressed(KEY_RIGHT) Then
  43. currentModel :+ 1
  44. If currentModel >= NUM_MODELS Then
  45. currentModel = 0
  46. End If
  47. Else If IsKeyPressed(KEY_LEFT) Then
  48. currentModel :- 1
  49. If currentModel < 0 Then
  50. currentModel = NUM_MODELS - 1
  51. End If
  52. End If
  53. '----------------------------------------------------------------------------------
  54. ' Draw
  55. '----------------------------------------------------------------------------------
  56. BeginDrawing()
  57. ClearBackground(RAYWHITE)
  58. BeginMode3D(camera)
  59. DrawModel(models[currentModel], position, 1.0, WHITE)
  60. DrawGrid(10, 1.0)
  61. EndMode3D()
  62. DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5))
  63. DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5))
  64. DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, BLUE)
  65. Select currentModel
  66. Case 0
  67. DrawText("PLANE", 680, 10, 20, DARKBLUE)
  68. Case 1
  69. DrawText("CUBE", 680, 10, 20, DARKBLUE)
  70. Case 2
  71. DrawText("SPHERE", 680, 10, 20, DARKBLUE)
  72. Case 3
  73. DrawText("HEMISPHERE", 640, 10, 20, DARKBLUE)
  74. Case 4
  75. DrawText("CYLINDER", 680, 10, 20, DARKBLUE)
  76. Case 5
  77. DrawText("TORUS", 680, 10, 20, DARKBLUE)
  78. Case 6
  79. DrawText("KNOT", 680, 10, 20, DARKBLUE)
  80. Case 7
  81. DrawText("POLY", 680, 10, 20, DARKBLUE)
  82. Case 8
  83. DrawText("Custom (triangle)", 580, 10, 20, DARKBLUE)
  84. End Select
  85. EndDrawing()
  86. '----------------------------------------------------------------------------------
  87. Wend
  88. ' De-Initialization
  89. '--------------------------------------------------------------------------------------
  90. UnloadTexture(texture) ' Unload texture
  91. ' Unload models data (GPU VRAM)
  92. For Local i:Int = 0 Until NUM_MODELS
  93. UnloadModel(models[i])
  94. Next
  95. CloseWindow() ' Close window and OpenGL context
  96. '--------------------------------------------------------------------------------------
  97. ' Generate a simple triangle mesh from code
  98. Function GenMeshCustom:RMesh()
  99. Local mesh:RMesh = New RMesh()
  100. mesh.triangleCount = 1
  101. mesh.vertexCount = mesh.triangleCount*3
  102. mesh.vertices = RMemAlloc(UInt(mesh.vertexCount*3*4)) ' 3 vertices, 3 coordinates each (x, y, z)
  103. mesh.texcoords = RMemAlloc(UInt(mesh.vertexCount*2*4)) ' 3 vertices, 2 coordinates each (x, y)
  104. mesh.normals = RMemAlloc(UInt(mesh.vertexCount*3*4)) ' 3 vertices, 3 coordinates each (x, y, z)
  105. ' Vertex at (0, 0, 0)
  106. mesh.vertices[0] = 0
  107. mesh.vertices[1] = 0
  108. mesh.vertices[2] = 0
  109. mesh.normals[0] = 0
  110. mesh.normals[1] = 1
  111. mesh.normals[2] = 0
  112. mesh.texcoords[0] = 0
  113. mesh.texcoords[1] = 0
  114. ' Vertex at (1, 0, 2)
  115. mesh.vertices[3] = 1
  116. mesh.vertices[4] = 0
  117. mesh.vertices[5] = 2
  118. mesh.normals[3] = 0
  119. mesh.normals[4] = 1
  120. mesh.normals[5] = 0
  121. mesh.texcoords[2] = 0.5
  122. mesh.texcoords[3] = 1.0
  123. ' Vertex at (2, 0, 0)
  124. mesh.vertices[6] = 2
  125. mesh.vertices[7] = 0
  126. mesh.vertices[8] = 0
  127. mesh.normals[6] = 0
  128. mesh.normals[7] = 1
  129. mesh.normals[8] = 0
  130. mesh.texcoords[4] = 1
  131. mesh.texcoords[5] = 0
  132. UploadMesh(mesh, False)
  133. Return mesh
  134. End Function