You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
13 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Draw textured cube
  4. *
  5. * Example originally created with raylib 4.5, last time updated with raylib 4.5
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2022-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include "rlgl.h" // Required to define vertex data (immediate-mode style)
  15. //------------------------------------------------------------------------------------
  16. // Custom Functions Declaration
  17. //------------------------------------------------------------------------------------
  18. void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color); // Draw cube textured
  19. void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, float width, float height, float length, Color color); // Draw cube with a region of a texture
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [models] example - draw cube texture");
  30. // Define the camera to look into our 3d world
  31. Camera camera = { 0 };
  32. camera.position = (Vector3){ 0.0f, 10.0f, 10.0f };
  33. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  34. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  35. camera.fovy = 45.0f;
  36. camera.projection = CAMERA_PERSPECTIVE;
  37. // Load texture to be applied to the cubes sides
  38. Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
  39. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  40. //--------------------------------------------------------------------------------------
  41. // Main game loop
  42. while (!WindowShouldClose()) // Detect window close button or ESC key
  43. {
  44. // Update
  45. //----------------------------------------------------------------------------------
  46. // TODO: Update your variables here
  47. //----------------------------------------------------------------------------------
  48. // Draw
  49. //----------------------------------------------------------------------------------
  50. BeginDrawing();
  51. ClearBackground(RAYWHITE);
  52. BeginMode3D(camera);
  53. // Draw cube with an applied texture
  54. DrawCubeTexture(texture, (Vector3){ -2.0f, 2.0f, 0.0f }, 2.0f, 4.0f, 2.0f, WHITE);
  55. // Draw cube with an applied texture, but only a defined rectangle piece of the texture
  56. DrawCubeTextureRec(texture, (Rectangle){ 0.0f, texture.height/2.0f, texture.width/2.0f, texture.height/2.0f },
  57. (Vector3){ 2.0f, 1.0f, 0.0f }, 2.0f, 2.0f, 2.0f, WHITE);
  58. DrawGrid(10, 1.0f); // Draw a grid
  59. EndMode3D();
  60. DrawFPS(10, 10);
  61. EndDrawing();
  62. //----------------------------------------------------------------------------------
  63. }
  64. // De-Initialization
  65. //--------------------------------------------------------------------------------------
  66. UnloadTexture(texture); // Unload texture
  67. CloseWindow(); // Close window and OpenGL context
  68. //--------------------------------------------------------------------------------------
  69. return 0;
  70. }
  71. //------------------------------------------------------------------------------------
  72. // Custom Functions Definition
  73. //------------------------------------------------------------------------------------
  74. // Draw cube textured
  75. // NOTE: Cube position is the center position
  76. void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color)
  77. {
  78. float x = position.x;
  79. float y = position.y;
  80. float z = position.z;
  81. // Set desired texture to be enabled while drawing following vertex data
  82. rlSetTexture(texture.id);
  83. // Vertex data transformation can be defined with the commented lines,
  84. // but in this example we calculate the transformed vertex data directly when calling rlVertex3f()
  85. //rlPushMatrix();
  86. // NOTE: Transformation is applied in inverse order (scale -> rotate -> translate)
  87. //rlTranslatef(2.0f, 0.0f, 0.0f);
  88. //rlRotatef(45, 0, 1, 0);
  89. //rlScalef(2.0f, 2.0f, 2.0f);
  90. rlBegin(RL_QUADS);
  91. rlColor4ub(color.r, color.g, color.b, color.a);
  92. // Front Face
  93. rlNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
  94. rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left Of The Texture and Quad
  95. rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right Of The Texture and Quad
  96. rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Right Of The Texture and Quad
  97. rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left Of The Texture and Quad
  98. // Back Face
  99. rlNormal3f(0.0f, 0.0f, - 1.0f); // Normal Pointing Away From Viewer
  100. rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Right Of The Texture and Quad
  101. rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Right Of The Texture and Quad
  102. rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Left Of The Texture and Quad
  103. rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Left Of The Texture and Quad
  104. // Top Face
  105. rlNormal3f(0.0f, 1.0f, 0.0f); // Normal Pointing Up
  106. rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left Of The Texture and Quad
  107. rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y + height/2, z + length/2); // Bottom Left Of The Texture and Quad
  108. rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y + height/2, z + length/2); // Bottom Right Of The Texture and Quad
  109. rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right Of The Texture and Quad
  110. // Bottom Face
  111. rlNormal3f(0.0f, - 1.0f, 0.0f); // Normal Pointing Down
  112. rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Top Right Of The Texture and Quad
  113. rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y - height/2, z - length/2); // Top Left Of The Texture and Quad
  114. rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Left Of The Texture and Quad
  115. rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Right Of The Texture and Quad
  116. // Right face
  117. rlNormal3f(1.0f, 0.0f, 0.0f); // Normal Pointing Right
  118. rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right Of The Texture and Quad
  119. rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right Of The Texture and Quad
  120. rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Left Of The Texture and Quad
  121. rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Left Of The Texture and Quad
  122. // Left Face
  123. rlNormal3f( - 1.0f, 0.0f, 0.0f); // Normal Pointing Left
  124. rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Left Of The Texture and Quad
  125. rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Right Of The Texture and Quad
  126. rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Right Of The Texture and Quad
  127. rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left Of The Texture and Quad
  128. rlEnd();
  129. //rlPopMatrix();
  130. rlSetTexture(0);
  131. }
  132. // Draw cube with texture piece applied to all faces
  133. void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, float width, float height, float length, Color color)
  134. {
  135. float x = position.x;
  136. float y = position.y;
  137. float z = position.z;
  138. float texWidth = (float)texture.width;
  139. float texHeight = (float)texture.height;
  140. // Set desired texture to be enabled while drawing following vertex data
  141. rlSetTexture(texture.id);
  142. // We calculate the normalized texture coordinates for the desired texture-source-rectangle
  143. // It means converting from (tex.width, tex.height) coordinates to [0.0f, 1.0f] equivalent
  144. rlBegin(RL_QUADS);
  145. rlColor4ub(color.r, color.g, color.b, color.a);
  146. // Front face
  147. rlNormal3f(0.0f, 0.0f, 1.0f);
  148. rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
  149. rlVertex3f(x - width/2, y - height/2, z + length/2);
  150. rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
  151. rlVertex3f(x + width/2, y - height/2, z + length/2);
  152. rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
  153. rlVertex3f(x + width/2, y + height/2, z + length/2);
  154. rlTexCoord2f(source.x/texWidth, source.y/texHeight);
  155. rlVertex3f(x - width/2, y + height/2, z + length/2);
  156. // Back face
  157. rlNormal3f(0.0f, 0.0f, - 1.0f);
  158. rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
  159. rlVertex3f(x - width/2, y - height/2, z - length/2);
  160. rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
  161. rlVertex3f(x - width/2, y + height/2, z - length/2);
  162. rlTexCoord2f(source.x/texWidth, source.y/texHeight);
  163. rlVertex3f(x + width/2, y + height/2, z - length/2);
  164. rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
  165. rlVertex3f(x + width/2, y - height/2, z - length/2);
  166. // Top face
  167. rlNormal3f(0.0f, 1.0f, 0.0f);
  168. rlTexCoord2f(source.x/texWidth, source.y/texHeight);
  169. rlVertex3f(x - width/2, y + height/2, z - length/2);
  170. rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
  171. rlVertex3f(x - width/2, y + height/2, z + length/2);
  172. rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
  173. rlVertex3f(x + width/2, y + height/2, z + length/2);
  174. rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
  175. rlVertex3f(x + width/2, y + height/2, z - length/2);
  176. // Bottom face
  177. rlNormal3f(0.0f, - 1.0f, 0.0f);
  178. rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
  179. rlVertex3f(x - width/2, y - height/2, z - length/2);
  180. rlTexCoord2f(source.x/texWidth, source.y/texHeight);
  181. rlVertex3f(x + width/2, y - height/2, z - length/2);
  182. rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
  183. rlVertex3f(x + width/2, y - height/2, z + length/2);
  184. rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
  185. rlVertex3f(x - width/2, y - height/2, z + length/2);
  186. // Right face
  187. rlNormal3f(1.0f, 0.0f, 0.0f);
  188. rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
  189. rlVertex3f(x + width/2, y - height/2, z - length/2);
  190. rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
  191. rlVertex3f(x + width/2, y + height/2, z - length/2);
  192. rlTexCoord2f(source.x/texWidth, source.y/texHeight);
  193. rlVertex3f(x + width/2, y + height/2, z + length/2);
  194. rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
  195. rlVertex3f(x + width/2, y - height/2, z + length/2);
  196. // Left face
  197. rlNormal3f( - 1.0f, 0.0f, 0.0f);
  198. rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
  199. rlVertex3f(x - width/2, y - height/2, z - length/2);
  200. rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
  201. rlVertex3f(x - width/2, y - height/2, z + length/2);
  202. rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
  203. rlVertex3f(x - width/2, y + height/2, z + length/2);
  204. rlTexCoord2f(source.x/texWidth, source.y/texHeight);
  205. rlVertex3f(x - width/2, y + height/2, z - length/2);
  206. rlEnd();
  207. rlSetTexture(0);
  208. }