models_cubicmap.c 3.6 KB

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