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.

89 lines
3.8 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-2024 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. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. UpdateCamera(&camera, CAMERA_ORBITAL);
  46. //----------------------------------------------------------------------------------
  47. // Draw
  48. //----------------------------------------------------------------------------------
  49. BeginDrawing();
  50. ClearBackground(RAYWHITE);
  51. BeginMode3D(camera);
  52. DrawModel(model, mapPosition, 1.0f, RED);
  53. DrawGrid(20, 1.0f);
  54. EndMode3D();
  55. DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE);
  56. DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN);
  57. DrawFPS(10, 10);
  58. EndDrawing();
  59. //----------------------------------------------------------------------------------
  60. }
  61. // De-Initialization
  62. //--------------------------------------------------------------------------------------
  63. UnloadTexture(texture); // Unload texture
  64. UnloadModel(model); // Unload model
  65. CloseWindow(); // Close window and OpenGL context
  66. //--------------------------------------------------------------------------------------
  67. return 0;
  68. }