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.

81 lines
3.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Heightmap loading and drawing
  4. *
  5. * This example has been created using raylib 1.8 (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(void)
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. const int screenWidth = 800;
  17. const 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, 0 };
  21. Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
  22. Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
  23. Mesh mesh = GenMeshHeightmap(image, (Vector3){ 16, 8, 16 }); // Generate heightmap mesh (RAM and VRAM)
  24. Model model = LoadModelFromMesh(mesh); // Load model from generated mesh
  25. model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  26. Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Define model position
  27. UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
  28. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  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 camera
  37. //----------------------------------------------------------------------------------
  38. // Draw
  39. //----------------------------------------------------------------------------------
  40. BeginDrawing();
  41. ClearBackground(RAYWHITE);
  42. BeginMode3D(camera);
  43. DrawModel(model, mapPosition, 1.0f, RED);
  44. DrawGrid(20, 1.0f);
  45. EndMode3D();
  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(model); // Unload model
  56. CloseWindow(); // Close window and OpenGL context
  57. //--------------------------------------------------------------------------------------
  58. return 0;
  59. }