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.

87 lines
3.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Cubicmap loading and drawing
  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) 2015 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main(void)
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. const int screenWidth = 800;
  17. const int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [models] example - cubesmap loading and drawing");
  19. // Define the camera to look into our 3d world
  20. Camera camera = { { 16.0f, 14.0f, 16.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  21. Image image = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
  22. Texture2D cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM)
  23. Mesh mesh = GenMeshCubicmap(image, (Vector3){ 1.0f, 1.0f, 1.0f });
  24. Model model = LoadModelFromMesh(mesh);
  25. // NOTE: By default each cube is mapped to one part of texture atlas
  26. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
  27. model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  28. Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
  29. UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM
  30. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  31. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  32. //--------------------------------------------------------------------------------------
  33. // Main game loop
  34. while (!WindowShouldClose()) // Detect window close button or ESC key
  35. {
  36. // Update
  37. //----------------------------------------------------------------------------------
  38. UpdateCamera(&camera); // Update camera
  39. //----------------------------------------------------------------------------------
  40. // Draw
  41. //----------------------------------------------------------------------------------
  42. BeginDrawing();
  43. ClearBackground(RAYWHITE);
  44. BeginMode3D(camera);
  45. DrawModel(model, mapPosition, 1.0f, WHITE);
  46. EndMode3D();
  47. DrawTextureEx(cubicmap, (Vector2){ screenWidth - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE);
  48. DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
  49. DrawText("cubicmap image used to", 658, 90, 10, GRAY);
  50. DrawText("generate map 3d model", 658, 104, 10, GRAY);
  51. DrawFPS(10, 10);
  52. EndDrawing();
  53. //----------------------------------------------------------------------------------
  54. }
  55. // De-Initialization
  56. //--------------------------------------------------------------------------------------
  57. UnloadTexture(cubicmap); // Unload cubicmap texture
  58. UnloadTexture(texture); // Unload map texture
  59. UnloadModel(model); // Unload map model
  60. CloseWindow(); // Close window and OpenGL context
  61. //--------------------------------------------------------------------------------------
  62. return 0;
  63. }