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.

79 lines
3.3 KiB

  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, CAMERA_ORBITAL); // Set an orbital camera mode
  28. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. UpdateCamera(&camera); // Update camera
  36. //----------------------------------------------------------------------------------
  37. // Draw
  38. //----------------------------------------------------------------------------------
  39. BeginDrawing();
  40. ClearBackground(RAYWHITE);
  41. Begin3dMode(camera);
  42. // NOTE: Model is scaled to 1/4 of its original size (128x128 units)
  43. DrawModel(map, mapPosition, 1.0f, RED);
  44. DrawGrid(20, 1.0f);
  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. }