models_cubicmap.lua 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(camera, CameraMode.ORBITAL) -- Set an orbital camera mode
  27. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  28. -------------------------------------------------------------------------------------------
  29. -- Main game loop
  30. while not WindowShouldClose() do -- Detect window close button or ESC key
  31. -- Update
  32. ---------------------------------------------------------------------------------------
  33. camera = UpdateCamera(camera) -- Update camera
  34. ---------------------------------------------------------------------------------------
  35. -- Draw
  36. ---------------------------------------------------------------------------------------
  37. BeginDrawing()
  38. ClearBackground(RAYWHITE)
  39. Begin3dMode(camera)
  40. DrawModel(map, mapPosition, 1.0, WHITE)
  41. End3dMode()
  42. DrawTextureEx(cubicmap, (Vector2)(screenWidth - cubicmap.width*4 - 20, 20), 0.0, 4.0, WHITE)
  43. DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN)
  44. DrawText("cubicmap image used to", 658, 90, 10, GRAY)
  45. DrawText("generate map 3d model", 658, 104, 10, GRAY)
  46. DrawFPS(10, 10)
  47. EndDrawing()
  48. ---------------------------------------------------------------------------------------
  49. end
  50. -- De-Initialization
  51. -------------------------------------------------------------------------------------------
  52. UnloadTexture(cubicmap) -- Unload cubicmap texture
  53. UnloadTexture(texture) -- Unload map texture
  54. UnloadModel(map) -- Unload map model
  55. CloseWindow() -- Close window and OpenGL context
  56. -------------------------------------------------------------------------------------------