models_heightmap.c 3.8 KB

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