models_heightmap.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Heightmap loading and drawing (adapted for HTML5 platform)
  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. #if defined(PLATFORM_WEB)
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. //----------------------------------------------------------------------------------
  16. // Global Variables Definition
  17. //----------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. // Define our custom camera to look into our 3d world
  21. Camera camera = {{ 18.0f, 16.0f, 18.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  22. Texture2D texture;
  23. Model map;
  24. Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Set model position (depends on model scaling!)
  25. //----------------------------------------------------------------------------------
  26. // Module Functions Declaration
  27. //----------------------------------------------------------------------------------
  28. void UpdateDrawFrame(void); // Update and Draw one frame
  29. //----------------------------------------------------------------------------------
  30. // Main Enry Point
  31. //----------------------------------------------------------------------------------
  32. int main()
  33. {
  34. // Initialization
  35. //--------------------------------------------------------------------------------------
  36. InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing");
  37. Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
  38. texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
  39. map = LoadHeightmap(image, (Vector3){ 16, 8, 16 }); // Load heightmap model with defined size
  40. map.material.texDiffuse = texture; // Set map diffuse texture
  41. UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
  42. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  43. #if defined(PLATFORM_WEB)
  44. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  45. #else
  46. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  47. //--------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. UpdateDrawFrame();
  52. }
  53. #endif
  54. // De-Initialization
  55. //--------------------------------------------------------------------------------------
  56. UnloadTexture(texture); // Unload texture
  57. UnloadModel(map); // Unload model
  58. CloseWindow(); // Close window and OpenGL context
  59. //--------------------------------------------------------------------------------------
  60. return 0;
  61. }
  62. //----------------------------------------------------------------------------------
  63. // Module Functions Definition
  64. //----------------------------------------------------------------------------------
  65. void UpdateDrawFrame(void)
  66. {
  67. // Update
  68. //----------------------------------------------------------------------------------
  69. UpdateCamera(&camera); // Update internal camera and our camera
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. Begin3dMode(camera);
  76. // NOTE: Model is scaled to 1/4 of its original size (128x128 units)
  77. DrawModel(map, mapPosition, 1.0f, RED);
  78. DrawGrid(20, 1.0f);
  79. End3dMode();
  80. DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE);
  81. DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN);
  82. DrawFPS(10, 10);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }