models_heightmap.lua 3.0 KB

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