models_heightmap.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 = {{ 24.0, 18.0, 24.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }};
  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, 32); // Load heightmap model
  24. SetModelTexture(&map, texture); // Bind texture to model
  25. Vector3 mapPosition = { -16, 0.0, -16 }; // 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/4.0f, RED);
  45. End3dMode();
  46. DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE);
  47. DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN);
  48. DrawFPS(10, 10);
  49. EndDrawing();
  50. //----------------------------------------------------------------------------------
  51. }
  52. // De-Initialization
  53. //--------------------------------------------------------------------------------------
  54. UnloadTexture(texture); // Unload texture
  55. UnloadModel(map); // Unload model
  56. CloseWindow(); // Close window and OpenGL context
  57. //--------------------------------------------------------------------------------------
  58. return 0;
  59. }