models_draw_cube_texture.c 13 KB

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