Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

178 Zeilen
6.2 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib example - procedural mesh generation
  4. *
  5. * This example has been created using raylib 1.8 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2017 Ramon Santamaria (Ray San)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define NUM_MODELS 9 // Parametric 3d shapes to generate
  13. void AllocateMeshData(Mesh* mesh, int triangleCount)
  14. {
  15. mesh->vertexCount = triangleCount * 3;
  16. mesh->triangleCount = triangleCount;
  17. mesh->vertices = (float*)MemAlloc(mesh->vertexCount * 3 * sizeof(float));
  18. mesh->texcoords = (float*)MemAlloc(mesh->vertexCount * 2 * sizeof(float));
  19. mesh->normals = (float*)MemAlloc(mesh->vertexCount * 3 * sizeof(float));
  20. }
  21. // generate a simple triangle mesh from code
  22. Mesh MakeMesh()
  23. {
  24. Mesh mesh = { 0 };
  25. AllocateMeshData(&mesh, 1);
  26. // vertex at the origin
  27. mesh.vertices[0] = 0;
  28. mesh.vertices[1] = 0;
  29. mesh.vertices[2] = 0;
  30. mesh.normals[0] = 0;
  31. mesh.normals[1] = 1;
  32. mesh.normals[2] = 0;
  33. mesh.texcoords[0] = 0;
  34. mesh.texcoords[1] = 0;
  35. // vertex at 1,0,2
  36. mesh.vertices[3] = 1;
  37. mesh.vertices[4] = 0;
  38. mesh.vertices[5] = 2;
  39. mesh.normals[3] = 0;
  40. mesh.normals[4] = 1;
  41. mesh.normals[5] = 0;
  42. mesh.texcoords[2] = 0.5f;
  43. mesh.texcoords[3] = 1.0f;
  44. // vertex at 2,0,0
  45. mesh.vertices[6] = 2;
  46. mesh.vertices[7] = 0;
  47. mesh.vertices[8] = 0;
  48. mesh.normals[6] = 0;
  49. mesh.normals[7] = 1;
  50. mesh.normals[8] = 0;
  51. mesh.texcoords[4] = 1;
  52. mesh.texcoords[5] =0;
  53. UploadMesh(&mesh, false);
  54. return mesh;
  55. }
  56. int main(void)
  57. {
  58. // Initialization
  59. //--------------------------------------------------------------------------------------
  60. const int screenWidth = 800;
  61. const int screenHeight = 450;
  62. InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
  63. // We generate a checked image for texturing
  64. Image checked = GenImageChecked(2, 2, 1, 1, RED, GREEN);
  65. Texture2D texture = LoadTextureFromImage(checked);
  66. UnloadImage(checked);
  67. Model models[NUM_MODELS] = { 0 };
  68. models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5));
  69. models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f));
  70. models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32));
  71. models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16));
  72. models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16));
  73. models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
  74. models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
  75. models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f));
  76. models[8] = LoadModelFromMesh(MakeMesh());
  77. // Set checked texture as default diffuse component for all models material
  78. for (int i = 0; i < NUM_MODELS; i++) models[i].materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  79. // Define the camera to look into our 3d world
  80. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  81. // Model drawing position
  82. Vector3 position = { 0.0f, 0.0f, 0.0f };
  83. int currentModel = 0;
  84. SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  85. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  86. //--------------------------------------------------------------------------------------
  87. // Main game loop
  88. while (!WindowShouldClose()) // Detect window close button or ESC key
  89. {
  90. // Update
  91. //----------------------------------------------------------------------------------
  92. UpdateCamera(&camera); // Update internal camera and our camera
  93. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  94. {
  95. currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures
  96. }
  97. if (IsKeyPressed(KEY_RIGHT))
  98. {
  99. currentModel++;
  100. if (currentModel >= NUM_MODELS) currentModel = 0;
  101. }
  102. else if (IsKeyPressed(KEY_LEFT))
  103. {
  104. currentModel--;
  105. if (currentModel < 0) currentModel = NUM_MODELS - 1;
  106. }
  107. //----------------------------------------------------------------------------------
  108. // Draw
  109. //----------------------------------------------------------------------------------
  110. BeginDrawing();
  111. ClearBackground(RAYWHITE);
  112. BeginMode3D(camera);
  113. DrawModel(models[currentModel], position, 1.0f, WHITE);
  114. DrawGrid(10, 1.0);
  115. EndMode3D();
  116. DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
  117. DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
  118. DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, BLUE);
  119. switch(currentModel)
  120. {
  121. case 0: DrawText("PLANE", 680, 10, 20, DARKBLUE); break;
  122. case 1: DrawText("CUBE", 680, 10, 20, DARKBLUE); break;
  123. case 2: DrawText("SPHERE", 680, 10, 20, DARKBLUE); break;
  124. case 3: DrawText("HEMISPHERE", 640, 10, 20, DARKBLUE); break;
  125. case 4: DrawText("CYLINDER", 680, 10, 20, DARKBLUE); break;
  126. case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break;
  127. case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break;
  128. case 7: DrawText("POLY", 680, 10, 20, DARKBLUE); break;
  129. case 8: DrawText("Parametric(custom)", 580, 10, 20, DARKBLUE); break;
  130. default: break;
  131. }
  132. EndDrawing();
  133. //----------------------------------------------------------------------------------
  134. }
  135. // De-Initialization
  136. //--------------------------------------------------------------------------------------
  137. UnloadTexture(texture); // Unload texture
  138. // Unload models data (GPU VRAM)
  139. for (int i = 0; i < NUM_MODELS; i++) UnloadModel(models[i]);
  140. CloseWindow(); // Close window and OpenGL context
  141. //--------------------------------------------------------------------------------------
  142. return 0;
  143. }