models_heightmap.c 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-2023 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 = { 0 };
  26. camera.position = (Vector3){ 18.0f, 21.0f, 18.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/heightmap.png"); // Load heightmap image (RAM)
  32. Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
  33. Mesh mesh = GenMeshHeightmap(image, (Vector3){ 16, 8, 16 }); // Generate heightmap mesh (RAM and VRAM)
  34. Model model = LoadModelFromMesh(mesh); // Load model from generated mesh
  35. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  36. Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Define model position
  37. UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
  38. DisableCursor(); // Limit cursor to relative movement inside the window
  39. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  40. //--------------------------------------------------------------------------------------
  41. // Main game loop
  42. while (!WindowShouldClose()) // Detect window close button or ESC key
  43. {
  44. // Update
  45. //----------------------------------------------------------------------------------
  46. UpdateCamera(&camera, CAMERA_ORBITAL);
  47. //----------------------------------------------------------------------------------
  48. // Draw
  49. //----------------------------------------------------------------------------------
  50. BeginDrawing();
  51. ClearBackground(RAYWHITE);
  52. BeginMode3D(camera);
  53. DrawModel(model, mapPosition, 1.0f, RED);
  54. DrawGrid(20, 1.0f);
  55. EndMode3D();
  56. DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE);
  57. DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN);
  58. DrawFPS(10, 10);
  59. EndDrawing();
  60. //----------------------------------------------------------------------------------
  61. }
  62. // De-Initialization
  63. //--------------------------------------------------------------------------------------
  64. UnloadTexture(texture); // Unload texture
  65. UnloadModel(model); // Unload model
  66. CloseWindow(); // Close window and OpenGL context
  67. //--------------------------------------------------------------------------------------
  68. return 0;
  69. }