models_heightmap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Heightmap loading and drawing
  4. *
  5. * This example has been created using raylib 1.3 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing");
  19. // Define our custom camera to look into our 3d world
  20. Camera camera = {{ 18.0f, 16.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  21. Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
  22. Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
  23. Model map = LoadHeightmap(image, (Vector3){ 16, 8, 16 }); // Load heightmap model with defined size
  24. map.material.texDiffuse = texture; // Set map diffuse texture
  25. Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Set model position (depends on model scaling!)
  26. UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
  27. SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode
  28. SetCameraPosition(camera.position); // Set internal camera position to match our custom camera position
  29. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  30. //--------------------------------------------------------------------------------------
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. UpdateCamera(&camera); // Update internal camera and our camera
  37. //----------------------------------------------------------------------------------
  38. // Draw
  39. //----------------------------------------------------------------------------------
  40. BeginDrawing();
  41. ClearBackground(RAYWHITE);
  42. Begin3dMode(camera);
  43. // NOTE: Model is scaled to 1/4 of its original size (128x128 units)
  44. DrawModel(map, mapPosition, 1.0f, RED);
  45. DrawGrid(20, 1.0f);
  46. End3dMode();
  47. DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE);
  48. DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN);
  49. DrawFPS(10, 10);
  50. EndDrawing();
  51. //----------------------------------------------------------------------------------
  52. }
  53. // De-Initialization
  54. //--------------------------------------------------------------------------------------
  55. UnloadTexture(texture); // Unload texture
  56. UnloadModel(map); // Unload model
  57. CloseWindow(); // Close window and OpenGL context
  58. //--------------------------------------------------------------------------------------
  59. return 0;
  60. }