models_cubicmap.lua 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [models] example - Cubicmap 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 - cubesmap loading and drawing")
  16. -- Define the camera to look into our 3d world
  17. local camera = Camera(Vector3(16.0, 14.0, 16.0), Vector3(0.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), 45.0)
  18. local image = LoadImage("resources/cubicmap.png") -- Load cubicmap image (RAM)
  19. local cubicmap = LoadTextureFromImage(image) -- Convert image to texture to display (VRAM)
  20. local map = LoadCubicmap(image) -- Load cubicmap model (generate model from image)
  21. -- NOTE: By default each cube is mapped to one part of texture atlas
  22. local texture = LoadTexture("resources/cubicmap_atlas.png") -- Load map texture
  23. map.material.texDiffuse = texture -- Set map diffuse texture
  24. local mapPosition = Vector3(-16.0, 0.0, -8.0) -- Set model position
  25. UnloadImage(image) -- Unload cubesmap image from RAM, already uploaded to VRAM
  26. SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode
  27. SetCameraPosition(camera.position) -- Set internal camera position to match our custom camera position
  28. SetCameraFovy(camera.fovy) -- Set internal camera field-of-view Y
  29. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  30. -------------------------------------------------------------------------------------------
  31. -- Main game loop
  32. while not WindowShouldClose() do -- Detect window close button or ESC key
  33. -- Update
  34. ---------------------------------------------------------------------------------------
  35. camera = UpdateCamera(camera) -- Update internal camera and our camera
  36. ---------------------------------------------------------------------------------------
  37. -- Draw
  38. ---------------------------------------------------------------------------------------
  39. BeginDrawing()
  40. ClearBackground(RAYWHITE)
  41. Begin3dMode(camera)
  42. DrawModel(map, mapPosition, 1.0, WHITE)
  43. End3dMode()
  44. DrawTextureEx(cubicmap, (Vector2)(screenWidth - cubicmap.width*4 - 20, 20), 0.0, 4.0, WHITE)
  45. DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN)
  46. DrawText("cubicmap image used to", 658, 90, 10, GRAY)
  47. DrawText("generate map 3d model", 658, 104, 10, GRAY)
  48. DrawFPS(10, 10)
  49. EndDrawing()
  50. ---------------------------------------------------------------------------------------
  51. end
  52. -- De-Initialization
  53. -------------------------------------------------------------------------------------------
  54. UnloadTexture(cubicmap) -- Unload cubicmap texture
  55. UnloadTexture(texture) -- Unload map texture
  56. UnloadModel(map) -- Unload map model
  57. CloseWindow() -- Close window and OpenGL context
  58. -------------------------------------------------------------------------------------------