Create_Custom_Cube_Mesh.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //
  2. // I took this from a example source and slapped it together.
  3. //
  4. // It kind of works... I am going to need to study this more though.
  5. #include "raylib.h"
  6. #include "rlgl.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define MAX_MESH_VBO 7
  10. static float *GetCubeVertices(float x, float y, float z);
  11. static float texcoordsRef[];
  12. static float normalsRef[];
  13. int main(void)
  14. {
  15. // Initialization
  16. //--------------------------------------------------------------------------------------
  17. const int screenWidth = 800;
  18. const int screenHeight = 450;
  19. InitWindow(screenWidth, screenHeight, "raylib example.");
  20. // Define the camera to look into our 3d world
  21. Camera camera = { 0 };
  22. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  23. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  24. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  25. camera.fovy = 45.0f; // Camera field-of-view Y
  26. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
  27. //SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  28. Mesh mesh = {0};
  29. mesh.vboId = (unsigned int *)RL_CALLOC(MAX_MESH_VBO, sizeof(unsigned int));
  30. float *vertices = RL_MALLOC(36 * 3 * 1 * sizeof(float));
  31. float *texcoords = RL_MALLOC(36 * 2 * 1 * sizeof(float));
  32. float *normals = RL_MALLOC(36 * 3 * 1 * sizeof(float));
  33. int verticesCount = 0;
  34. int texcoordsCount = 0;
  35. int normalsCount = 0;
  36. int x=0,y=0,z=0;
  37. float *blockVertices = GetCubeVertices(x, y, z);
  38. for (int v = 0; v < 36 * 3; v++)
  39. {
  40. vertices[verticesCount + v] = blockVertices[v];
  41. }
  42. for (int t = 0; t < 36 * 2; t++)
  43. {
  44. texcoords[texcoordsCount + t] = texcoordsRef[t];
  45. }
  46. for (int n = 0; n < 36 * 3; n++)
  47. {
  48. normals[normalsCount + n] = normalsRef[n];
  49. }
  50. verticesCount += 36 * 3;
  51. texcoordsCount += 36 * 2;
  52. normalsCount += 36 * 3;
  53. mesh.vertices = (float *)RL_MALLOC(verticesCount * sizeof(float));
  54. memcpy(mesh.vertices, vertices, verticesCount * sizeof(float));
  55. mesh.texcoords = (float *)RL_MALLOC(texcoordsCount * sizeof(float));
  56. memcpy(mesh.texcoords, texcoords, texcoordsCount * sizeof(float));
  57. mesh.normals = (float *)RL_MALLOC(normalsCount * sizeof(float));
  58. memcpy(mesh.normals, normals, normalsCount * sizeof(float));
  59. mesh.vertexCount = verticesCount / 3; // fixme: Why divide by 3 ???
  60. mesh.triangleCount = (verticesCount / 3) / 2; // fixme: Why divide by 3 and 2 ???
  61. RL_FREE(vertices);
  62. RL_FREE(texcoords);
  63. RL_FREE(normals);
  64. rlLoadMesh(&mesh, false);
  65. Model worldModel = LoadModelFromMesh(mesh);
  66. // We generate a checked image for texturing
  67. Image checked = GenImageChecked(4, 4, 1, 1, RED, GREEN);
  68. Texture2D texture = LoadTextureFromImage(checked);
  69. UnloadImage(checked);
  70. worldModel.materials[0].maps[MAP_DIFFUSE].texture = texture;
  71. //worldModel.materials[0].maps[MAP_DIFFUSE].texture = LoadTexture("resources/grass.png");
  72. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  73. //--------------------------------------------------------------------------------------
  74. // Main game loop
  75. while (!WindowShouldClose()) // Detect window close button or ESC key
  76. {
  77. // Update
  78. //----------------------------------------------------------------------------------
  79. UpdateCamera(&camera); // Update internal camera and our camera
  80. //----------------------------------------------------------------------------------
  81. // Draw
  82. //----------------------------------------------------------------------------------
  83. BeginDrawing();
  84. ClearBackground(RAYWHITE);
  85. BeginMode3D(camera);
  86. Vector3 pos;
  87. pos.x = 9;
  88. pos.y = 2;
  89. pos.z = 13;
  90. DrawModel(worldModel, pos, 1.0f, WHITE);
  91. EndMode3D();
  92. DrawFPS(0,0);
  93. EndDrawing();
  94. //----------------------------------------------------------------------------------
  95. }
  96. //--------------------------------------------------------------------------------------
  97. UnloadModel(worldModel);
  98. UnloadTexture(texture); // Unload texture
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }
  103. static float texcoordsRef[] = {
  104. // face 1
  105. 0.5f, 1.0f,
  106. 0.25f, 1.0f,
  107. 0.25f, 0.0f,
  108. 0.25f, 0.0f,
  109. 0.5f, 0.0f,
  110. 0.5f, 1.0f,
  111. // face 2
  112. 0.25f, 1.0f,
  113. 0.25f, 0.0f,
  114. 0.5f, 0.0f,
  115. 0.5f, 0.0f,
  116. 0.5f, 1.0f,
  117. 0.25f, 1.0f,
  118. // face 3 (top)
  119. 0.0f, 0.0f,
  120. 0.25f, 0.0f,
  121. 0.25f, 1.0f,
  122. 0.25f, 1.0f,
  123. 0.0f, 1.0f,
  124. 0.0f, 0.0f,
  125. // face 4 (bottom)
  126. 0.0f, 0.0f,
  127. 0.25f, 0.0f,
  128. 0.25f, 1.0f,
  129. 0.25f, 1.0f,
  130. 0.0f, 1.0f,
  131. 0.0f, 0.0f,
  132. // face 5
  133. 0.25f, 1.0f,
  134. 0.25f, 0.0f,
  135. 0.5f, 0.0f,
  136. 0.5f, 0.0f,
  137. 0.5f, 1.0f,
  138. 0.25f, 1.0f,
  139. // face 6
  140. 0.5f, 1.0f,
  141. 0.25f, 1.0f,
  142. 0.25f, 0.0f,
  143. 0.25f, 0.0f,
  144. 0.5f, 0.0f,
  145. 0.5f, 1.0f,
  146. };
  147. static float normalsRef[] = {
  148. // face 1
  149. 0.0f, 0.0f, 1.0f,
  150. 0.0f, 0.0f, 1.0f,
  151. 0.0f, 0.0f, 1.0f,
  152. 0.0f, 0.0f, 1.0f,
  153. 0.0f, 0.0f, 1.0f,
  154. 0.0f, 0.0f, 1.0f,
  155. // face 2
  156. 0.0f, 0.0f, -1.0f,
  157. 0.0f, 0.0f, -1.0f,
  158. 0.0f, 0.0f, -1.0f,
  159. 0.0f, 0.0f, -1.0f,
  160. 0.0f, 0.0f, -1.0f,
  161. 0.0f, 0.0f, -1.0f,
  162. // face 3
  163. 0.0f, 1.0f, 0.0f,
  164. 0.0f, 1.0f, 0.0f,
  165. 0.0f, 1.0f, 0.0f,
  166. 0.0f, 1.0f, 0.0f,
  167. 0.0f, 1.0f, 0.0f,
  168. 0.0f, 1.0f, 0.0f,
  169. // face 4
  170. 0.0f, -1.0f, 0.0f,
  171. 0.0f, -1.0f, 0.0f,
  172. 0.0f, -1.0f, 0.0f,
  173. 0.0f, -1.0f, 0.0f,
  174. 0.0f, -1.0f, 0.0f,
  175. 0.0f, -1.0f, 0.0f,
  176. // face 5
  177. 1.0f, 0.0f, 0.0f,
  178. 1.0f, 0.0f, 0.0f,
  179. 1.0f, 0.0f, 0.0f,
  180. 1.0f, 0.0f, 0.0f,
  181. 1.0f, 0.0f, 0.0f,
  182. 1.0f, 0.0f, 0.0f,
  183. // face 6
  184. -1.0f, 0.0f, 0.0f,
  185. -1.0f, 0.0f, 0.0f,
  186. -1.0f, 0.0f, 0.0f,
  187. -1.0f, 0.0f, 0.0f,
  188. -1.0f, 0.0f, 0.0f,
  189. -1.0f, 0.0f, 0.0f};
  190. // note: Yes, the implementation of the world model is dirty.
  191. // todo: reimplement as chunks
  192. static float *GetCubeVertices(float x, float y, float z)
  193. {
  194. // not using indices
  195. float width = 1.0f;
  196. float height = 1.0f;
  197. float length = 1.0f;
  198. float *cubeVertices = malloc(36 * 3 * sizeof(float));
  199. float ref[] = {
  200. // face 1
  201. -width / 2 + x, -height / 2 + y, length / 2 + z,
  202. width / 2 + x, -height / 2 + y, length / 2 + z,
  203. width / 2 + x, height / 2 + y, length / 2 + z,
  204. width / 2 + x, height / 2 + y, length / 2 + z,
  205. -width / 2 + x, height / 2 + y, length / 2 + z,
  206. -width / 2 + x, -height / 2 + y, length / 2 + z,
  207. // face 2
  208. -width / 2 + x, -height / 2 + y, -length / 2 + z,
  209. -width / 2 + x, height / 2 + y, -length / 2 + z,
  210. width / 2 + x, height / 2 + y, -length / 2 + z,
  211. width / 2 + x, height / 2 + y, -length / 2 + z,
  212. width / 2 + x, -height / 2 + y, -length / 2 + z,
  213. -width / 2 + x, -height / 2 + y, -length / 2 + z,
  214. // face 3
  215. -width / 2 + x, height / 2 + y, -length / 2 + z,
  216. -width / 2 + x, height / 2 + y, length / 2 + z,
  217. width / 2 + x, height / 2 + y, length / 2 + z,
  218. width / 2 + x, height / 2 + y, length / 2 + z,
  219. width / 2 + x, height / 2 + y, -length / 2 + z,
  220. -width / 2 + x, height / 2 + y, -length / 2 + z,
  221. // face 4
  222. -width / 2 + x, -height / 2 + y, -length / 2 + z,
  223. width / 2 + x, -height / 2 + y, -length / 2 + z,
  224. width / 2 + x, -height / 2 + y, length / 2 + z,
  225. width / 2 + x, -height / 2 + y, length / 2 + z,
  226. -width / 2 + x, -height / 2 + y, length / 2 + z,
  227. -width / 2 + x, -height / 2 + y, -length / 2 + z,
  228. // face 5
  229. width / 2 + x, -height / 2 + y, -length / 2 + z,
  230. width / 2 + x, height / 2 + y, -length / 2 + z,
  231. width / 2 + x, height / 2 + y, length / 2 + z,
  232. width / 2 + x, height / 2 + y, length / 2 + z,
  233. width / 2 + x, -height / 2 + y, length / 2 + z,
  234. width / 2 + x, -height / 2 + y, -length / 2 + z,
  235. // face 6
  236. -width / 2 + x, -height / 2 + y, -length / 2 + z,
  237. -width / 2 + x, -height / 2 + y, length / 2 + z,
  238. -width / 2 + x, height / 2 + y, length / 2 + z,
  239. -width / 2 + x, height / 2 + y, length / 2 + z,
  240. -width / 2 + x, height / 2 + y, -length / 2 + z,
  241. -width / 2 + x, -height / 2 + y, -length / 2 + z};
  242. for (int i = 0; i < 36 * 3; i++)
  243. {
  244. cubeVertices[i] = ref[i];
  245. }
  246. return cubeVertices;
  247. }