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

86 行
3.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Heightmap 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 - heightmap loading and drawing");
  24. // Define our custom camera to look into our 3d world
  25. Camera camera = { { 18.0f, 18.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  26. Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
  27. Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
  28. Mesh mesh = GenMeshHeightmap(image, (Vector3){ 16, 8, 16 }); // Generate heightmap mesh (RAM and VRAM)
  29. Model model = LoadModelFromMesh(mesh); // Load model from generated mesh
  30. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  31. Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Define model position
  32. UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
  33. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  34. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  35. //--------------------------------------------------------------------------------------
  36. // Main game loop
  37. while (!WindowShouldClose()) // Detect window close button or ESC key
  38. {
  39. // Update
  40. //----------------------------------------------------------------------------------
  41. UpdateCamera(&camera);
  42. //----------------------------------------------------------------------------------
  43. // Draw
  44. //----------------------------------------------------------------------------------
  45. BeginDrawing();
  46. ClearBackground(RAYWHITE);
  47. BeginMode3D(camera);
  48. DrawModel(model, mapPosition, 1.0f, RED);
  49. DrawGrid(20, 1.0f);
  50. EndMode3D();
  51. DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE);
  52. DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN);
  53. DrawFPS(10, 10);
  54. EndDrawing();
  55. //----------------------------------------------------------------------------------
  56. }
  57. // De-Initialization
  58. //--------------------------------------------------------------------------------------
  59. UnloadTexture(texture); // Unload texture
  60. UnloadModel(model); // Unload model
  61. CloseWindow(); // Close window and OpenGL context
  62. //--------------------------------------------------------------------------------------
  63. return 0;
  64. }