models_cubicmap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Cubicmap 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 the camera to look into our 3d world
  21. Camera camera = {{ 16.0f, 14.0f, 16.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  22. Texture2D cubicmap;
  23. Model map;
  24. Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
  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 - cubesmap loading and drawing");
  37. Image image = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
  38. cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM)
  39. map = LoadCubicmap(image); // Load cubicmap model (generate model from image)
  40. // NOTE: By default each cube is mapped to one part of texture atlas
  41. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
  42. map.material.texDiffuse = texture; // Set map diffuse texture
  43. UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM
  44. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  45. #if defined(PLATFORM_WEB)
  46. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  47. #else
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. UpdateDrawFrame();
  54. }
  55. #endif
  56. // De-Initialization
  57. //--------------------------------------------------------------------------------------
  58. UnloadTexture(cubicmap); // Unload cubicmap texture
  59. UnloadTexture(texture); // Unload map texture
  60. UnloadModel(map); // Unload map model
  61. CloseWindow(); // Close window and OpenGL context
  62. //--------------------------------------------------------------------------------------
  63. return 0;
  64. }
  65. //----------------------------------------------------------------------------------
  66. // Module Functions Definition
  67. //----------------------------------------------------------------------------------
  68. void UpdateDrawFrame(void)
  69. {
  70. // Update
  71. //----------------------------------------------------------------------------------
  72. UpdateCamera(&camera); // Update internal camera and our camera
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. Begin3dMode(camera);
  79. DrawModel(map, mapPosition, 1.0f, WHITE);
  80. End3dMode();
  81. DrawTextureEx(cubicmap, (Vector2){ screenWidth - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE);
  82. DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
  83. DrawText("cubicmap image used to", 658, 90, 10, GRAY);
  84. DrawText("generate map 3d model", 658, 104, 10, GRAY);
  85. DrawFPS(10, 10);
  86. EndDrawing();
  87. //----------------------------------------------------------------------------------
  88. }