您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

92 行
3.8 KiB

  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-2022 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 = { { 16.0f, 14.0f, 16.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  26. Image image = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
  27. Texture2D cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM)
  28. Mesh mesh = GenMeshCubicmap(image, (Vector3){ 1.0f, 1.0f, 1.0f });
  29. Model model = LoadModelFromMesh(mesh);
  30. // NOTE: By default each cube is mapped to one part of texture atlas
  31. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
  32. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  33. Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
  34. UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM
  35. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  36. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  37. //--------------------------------------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //----------------------------------------------------------------------------------
  43. UpdateCamera(&camera);
  44. //----------------------------------------------------------------------------------
  45. // Draw
  46. //----------------------------------------------------------------------------------
  47. BeginDrawing();
  48. ClearBackground(RAYWHITE);
  49. BeginMode3D(camera);
  50. DrawModel(model, mapPosition, 1.0f, WHITE);
  51. EndMode3D();
  52. DrawTextureEx(cubicmap, (Vector2){ screenWidth - cubicmap.width*4.0f - 20, 20.0f }, 0.0f, 4.0f, WHITE);
  53. DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
  54. DrawText("cubicmap image used to", 658, 90, 10, GRAY);
  55. DrawText("generate map 3d model", 658, 104, 10, GRAY);
  56. DrawFPS(10, 10);
  57. EndDrawing();
  58. //----------------------------------------------------------------------------------
  59. }
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. UnloadTexture(cubicmap); // Unload cubicmap texture
  63. UnloadTexture(texture); // Unload map texture
  64. UnloadModel(model); // Unload map model
  65. CloseWindow(); // Close window and OpenGL context
  66. //--------------------------------------------------------------------------------------
  67. return 0;
  68. }