You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.2 KiB

8 months ago
8 months ago
8 months ago
8 months ago
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Cubicmap loading and drawing
  4. *
  5. * Example originally created with raylib 1.8, last time updated with raylib 3.5
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. //------------------------------------------------------------------------------------
  15. // Program main entry point
  16. //------------------------------------------------------------------------------------
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [models] example - cubesmap loading and drawing");
  24. // Define the camera to look into our 3d world
  25. Camera camera = { 0 };
  26. camera.position = (Vector3){ 16.0f, 14.0f, 16.0f }; // Camera position
  27. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  28. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  29. camera.fovy = 45.0f; // Camera field-of-view Y
  30. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  31. Image image = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
  32. Texture2D cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM)
  33. Mesh mesh = GenMeshCubicmap(image, (Vector3){ 1.0f, 1.0f, 1.0f });
  34. Model model = LoadModelFromMesh(mesh);
  35. // NOTE: By default each cube is mapped to one part of texture atlas
  36. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
  37. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  38. Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
  39. UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM
  40. bool pause = false; // Pause camera orbital rotation (and zoom)
  41. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  42. //--------------------------------------------------------------------------------------
  43. // Main game loop
  44. while (!WindowShouldClose()) // Detect window close button or ESC key
  45. {
  46. // Update
  47. //----------------------------------------------------------------------------------
  48. if (IsKeyPressed(KEY_P)) pause = !pause;
  49. if (!pause) UpdateCamera(&camera, CAMERA_ORBITAL);
  50. //----------------------------------------------------------------------------------
  51. // Draw
  52. //----------------------------------------------------------------------------------
  53. BeginDrawing();
  54. ClearBackground(RAYWHITE);
  55. BeginMode3D(camera);
  56. DrawModel(model, mapPosition, 1.0f, WHITE);
  57. EndMode3D();
  58. DrawTextureEx(cubicmap, (Vector2){ screenWidth - cubicmap.width*4.0f - 20, 20.0f }, 0.0f, 4.0f, WHITE);
  59. DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
  60. DrawText("cubicmap image used to", 658, 90, 10, GRAY);
  61. DrawText("generate map 3d model", 658, 104, 10, GRAY);
  62. DrawFPS(10, 10);
  63. EndDrawing();
  64. //----------------------------------------------------------------------------------
  65. }
  66. // De-Initialization
  67. //--------------------------------------------------------------------------------------
  68. UnloadTexture(cubicmap); // Unload cubicmap texture
  69. UnloadTexture(texture); // Unload map texture
  70. UnloadModel(model); // Unload map model
  71. CloseWindow(); // Close window and OpenGL context
  72. //--------------------------------------------------------------------------------------
  73. return 0;
  74. }