models_mesh_generation.c 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. //------------------------------------------------------------------------------------
  13. // Program main entry point
  14. //------------------------------------------------------------------------------------
  15. int main()
  16. {
  17. // Initialization
  18. //--------------------------------------------------------------------------------------
  19. int screenWidth = 800;
  20. int screenHeight = 450;
  21. InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
  22. //Model model = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5)); // Texture coordinates must be divided by resX, resZ
  23. //Model model = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f)); // OK!
  24. //Model model = LoadModelFromMesh(GenMeshSphere(2, 32, 32)); // OK! (par_shapes)
  25. //Model model = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16)); // OK! (par_shapes)
  26. //Model model = LoadModelFromMesh(GenMeshCylinder(1, 2, 16)); // OK! (par_shapes)
  27. Model model = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
  28. //Model model = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
  29. model.material.maps[MAP_DIFFUSE].texture = LoadTexture("resources/pixels.png");
  30. // Debug information
  31. /*
  32. printf("model.mesh.vertexCount: %i\n", model.mesh.vertexCount);
  33. printf("model.mesh.triangleCount: %i\n", model.mesh.triangleCount);
  34. printf("model.mesh.vboId (position): %i\n", model.mesh.vboId[0]);
  35. printf("model.mesh.vboId (texcoords): %i\n", model.mesh.vboId[1]);
  36. printf("model.mesh.vboId (normals): %i\n", model.mesh.vboId[2]);
  37. printf("model.mesh.vboId (indices): %i\n", model.mesh.vboId[6]);
  38. */
  39. // Define the camera to look into our 3d world
  40. Camera camera = {{ 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  41. Vector3 position = { 0.0f, 0.0f, 0.0f };
  42. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. UpdateCamera(&camera); // Update internal camera and our camera
  51. //----------------------------------------------------------------------------------
  52. // Draw
  53. //----------------------------------------------------------------------------------
  54. BeginDrawing();
  55. ClearBackground(RAYWHITE);
  56. Begin3dMode(camera);
  57. DrawModel(model, position, 1.0f, WHITE);
  58. DrawGrid(10, 1.0);
  59. End3dMode();
  60. EndDrawing();
  61. //----------------------------------------------------------------------------------
  62. }
  63. // De-Initialization
  64. //--------------------------------------------------------------------------------------
  65. UnloadModel(model);
  66. CloseWindow(); // Close window and OpenGL context
  67. //--------------------------------------------------------------------------------------
  68. return 0;
  69. }