models_cubicmap.c 4.3 KB

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