Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

434 wiersze
19 KiB

6 lat temu
5 lat temu
5 lat temu
6 lat temu
6 lat temu
6 lat temu
6 lat temu
6 lat temu
  1. /*******************************************************************************************
  2. *
  3. * raylib [rlgl] example - Using rlgl module as standalone module
  4. *
  5. * NOTE: This example requires OpenGL 3.3 or ES2 versions for shaders support,
  6. * OpenGL 1.1 does not support shaders but it can also be used.
  7. *
  8. * DEPENDENCIES:
  9. * rlgl.h - OpenGL 1.1 immediate-mode style coding translation layer
  10. * glad.h - OpenGL extensions initialization library (required by rlgl)
  11. * raymath.h - 3D math library (required by rlgl)
  12. * glfw3 - Windows and context initialization library
  13. *
  14. * rlgl library is provided as a single-file header-only library, this library
  15. * allows coding in a pseudo-OpenGL 1.1 style while translating calls to multiple
  16. * OpenGL versions backends (1.1, 2.1, 3.3, ES 2.0).
  17. *
  18. * WINDOWS COMPILATION:
  19. * gcc -o rlgl_standalone.exe rlgl_standalone.c -s -Iexternal\include -I..\..\src \
  20. * -L. -Lexternal\lib -lglfw3 -lopengl32 -lgdi32 -Wall -std=c99 -DGRAPHICS_API_OPENGL_33
  21. *
  22. * APPLE COMPILATION:
  23. * gcc -o rlgl_standalone rlgl_standalone.c -I../../src -Iexternal/include -Lexternal/lib \
  24. * -lglfw3 -std=c99 -framework CoreVideo -framework OpenGL -framework OpenAL \
  25. * -framework IOKit -framework Cocoa -Wno-deprecated-declarations
  26. *
  27. * LICENSE: zlib/libpng
  28. *
  29. * This example is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  30. * BSD-like license that allows static linking with closed source software:
  31. *
  32. * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
  33. *
  34. * This software is provided "as-is", without any express or implied warranty. In no event
  35. * will the authors be held liable for any damages arising from the use of this software.
  36. *
  37. * Permission is granted to anyone to use this software for any purpose, including commercial
  38. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  39. *
  40. * 1. The origin of this software must not be misrepresented; you must not claim that you
  41. * wrote the original software. If you use this software in a product, an acknowledgment
  42. * in the product documentation would be appreciated but is not required.
  43. *
  44. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  45. * as being the original software.
  46. *
  47. * 3. This notice may not be removed or altered from any source distribution.
  48. *
  49. ********************************************************************************************/
  50. #define RLGL_IMPLEMENTATION
  51. #define RLGL_STANDALONE
  52. #define RLGL_SUPPORT_TRACELOG
  53. #include "rlgl.h" // OpenGL 1.1 immediate-mode style coding
  54. #if defined(__EMSCRIPTEN__)
  55. #define GLFW_INCLUDE_ES2
  56. #endif
  57. #include <GLFW/glfw3.h> // Windows/Context and inputs management
  58. #include <stdio.h> // Required for: printf()
  59. #define RED (Color){ 230, 41, 55, 255 } // Red
  60. #define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
  61. #define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
  62. //----------------------------------------------------------------------------------
  63. // Module specific Functions Declaration
  64. //----------------------------------------------------------------------------------
  65. static void ErrorCallback(int error, const char *description);
  66. static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
  67. // Drawing functions (uses rlgl functionality)
  68. static void DrawGrid(int slices, float spacing);
  69. static void DrawCube(Vector3 position, float width, float height, float length, Color color);
  70. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color);
  71. static void DrawRectangleV(Vector2 position, Vector2 size, Color color);
  72. //----------------------------------------------------------------------------------
  73. // Main Entry point
  74. //----------------------------------------------------------------------------------
  75. int main(void)
  76. {
  77. // Initialization
  78. //--------------------------------------------------------------------------------------
  79. const int screenWidth = 800;
  80. const int screenHeight = 450;
  81. // GLFW3 Initialization + OpenGL 3.3 Context + Extensions
  82. //--------------------------------------------------------
  83. glfwSetErrorCallback(ErrorCallback);
  84. if (!glfwInit())
  85. {
  86. printf("GLFW3: Can not initialize GLFW\n");
  87. return 1;
  88. }
  89. else printf("GLFW3: GLFW initialized successfully\n");
  90. glfwWindowHint(GLFW_SAMPLES, 4);
  91. glfwWindowHint(GLFW_DEPTH_BITS, 16);
  92. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  93. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  94. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  95. //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  96. #if defined(__APPLE__)
  97. glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
  98. #endif
  99. GLFWwindow *window = glfwCreateWindow(screenWidth, screenHeight, "rlgl standalone", NULL, NULL);
  100. if (!window)
  101. {
  102. glfwTerminate();
  103. return 2;
  104. }
  105. else printf("GLFW3: Window created successfully\n");
  106. glfwSetWindowPos(window, 200, 200);
  107. glfwSetKeyCallback(window, KeyCallback);
  108. glfwMakeContextCurrent(window);
  109. glfwSwapInterval(0);
  110. // Load OpenGL 3.3 supported extensions
  111. rlLoadExtensions(glfwGetProcAddress);
  112. //--------------------------------------------------------
  113. // Initialize OpenGL context (states and resources)
  114. rlglInit(screenWidth, screenHeight);
  115. // Initialize viewport and internal projection/modelview matrices
  116. rlViewport(0, 0, screenWidth, screenHeight);
  117. rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
  118. rlLoadIdentity(); // Reset current matrix (PROJECTION)
  119. rlOrtho(0, screenWidth, screenHeight, 0, 0.0f, 1.0f); // Orthographic projection with top-left corner at (0,0)
  120. rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
  121. rlLoadIdentity(); // Reset current matrix (MODELVIEW)
  122. rlClearColor(245, 245, 245, 255); // Define clear color
  123. rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
  124. Camera camera = { 0 };
  125. camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
  126. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  127. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  128. camera.fovy = 45.0f; // Camera field-of-view Y
  129. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; // Cube default position (center)
  130. //--------------------------------------------------------------------------------------
  131. // Main game loop
  132. while (!glfwWindowShouldClose(window))
  133. {
  134. // Update
  135. //----------------------------------------------------------------------------------
  136. //camera.position.x += 0.01f;
  137. //----------------------------------------------------------------------------------
  138. // Draw
  139. //----------------------------------------------------------------------------------
  140. rlClearScreenBuffers(); // Clear current framebuffer
  141. // Draw '3D' elements in the scene
  142. //-----------------------------------------------
  143. // Calculate projection matrix (from perspective) and view matrix from camera look at
  144. Matrix matProj = MatrixPerspective(camera.fovy*DEG2RAD, (double)screenWidth/(double)screenHeight, 0.01, 1000.0);
  145. Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
  146. SetMatrixModelview(matView); // Set internal modelview matrix (default shader)
  147. SetMatrixProjection(matProj); // Set internal projection matrix (default shader)
  148. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  149. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, RAYWHITE);
  150. DrawGrid(10, 1.0f);
  151. // NOTE: Internal buffers drawing (3D data)
  152. rlglDraw();
  153. //-----------------------------------------------
  154. // Draw '2D' elements in the scene (GUI)
  155. //-----------------------------------------------
  156. #define RLGL_CREATE_MATRIX_MANUALLY
  157. #if defined(RLGL_CREATE_MATRIX_MANUALLY)
  158. matProj = MatrixOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0);
  159. matView = MatrixIdentity();
  160. SetMatrixModelview(matView); // Set internal modelview matrix (default shader)
  161. SetMatrixProjection(matProj); // Set internal projection matrix (default shader)
  162. #else // Let rlgl generate and multiply matrix internally
  163. rlMatrixMode(RL_PROJECTION); // Enable internal projection matrix
  164. rlLoadIdentity(); // Reset internal projection matrix
  165. rlOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0); // Recalculate internal projection matrix
  166. rlMatrixMode(RL_MODELVIEW); // Enable internal modelview matrix
  167. rlLoadIdentity(); // Reset internal modelview matrix
  168. #endif
  169. DrawRectangleV((Vector2){ 10.0f, 10.0f }, (Vector2){ 780.0f, 20.0f }, DARKGRAY);
  170. // NOTE: Internal buffers drawing (2D data)
  171. rlglDraw();
  172. //-----------------------------------------------
  173. glfwSwapBuffers(window);
  174. glfwPollEvents();
  175. //----------------------------------------------------------------------------------
  176. }
  177. // De-Initialization
  178. //--------------------------------------------------------------------------------------
  179. rlglClose(); // Unload rlgl internal buffers and default shader/texture
  180. glfwDestroyWindow(window); // Close window
  181. glfwTerminate(); // Free GLFW3 resources
  182. //--------------------------------------------------------------------------------------
  183. return 0;
  184. }
  185. //----------------------------------------------------------------------------------
  186. // Module specific Functions Definitions
  187. //----------------------------------------------------------------------------------
  188. // GLFW3: Error callback
  189. static void ErrorCallback(int error, const char *description)
  190. {
  191. fprintf(stderr, "%s", description);
  192. }
  193. // GLFW3: Keyboard callback
  194. static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
  195. {
  196. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  197. {
  198. glfwSetWindowShouldClose(window, GL_TRUE);
  199. }
  200. }
  201. // Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
  202. static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
  203. {
  204. rlBegin(RL_TRIANGLES);
  205. rlColor4ub(color.r, color.g, color.b, color.a);
  206. rlVertex2i(position.x, position.y);
  207. rlVertex2i(position.x, position.y + size.y);
  208. rlVertex2i(position.x + size.x, position.y + size.y);
  209. rlVertex2i(position.x, position.y);
  210. rlVertex2i(position.x + size.x, position.y + size.y);
  211. rlVertex2i(position.x + size.x, position.y);
  212. rlEnd();
  213. }
  214. // Draw a grid centered at (0, 0, 0)
  215. static void DrawGrid(int slices, float spacing)
  216. {
  217. int halfSlices = slices / 2;
  218. rlBegin(RL_LINES);
  219. for(int i = -halfSlices; i <= halfSlices; i++)
  220. {
  221. if (i == 0)
  222. {
  223. rlColor3f(0.5f, 0.5f, 0.5f);
  224. rlColor3f(0.5f, 0.5f, 0.5f);
  225. rlColor3f(0.5f, 0.5f, 0.5f);
  226. rlColor3f(0.5f, 0.5f, 0.5f);
  227. }
  228. else
  229. {
  230. rlColor3f(0.75f, 0.75f, 0.75f);
  231. rlColor3f(0.75f, 0.75f, 0.75f);
  232. rlColor3f(0.75f, 0.75f, 0.75f);
  233. rlColor3f(0.75f, 0.75f, 0.75f);
  234. }
  235. rlVertex3f((float)i*spacing, 0.0f, (float)-halfSlices*spacing);
  236. rlVertex3f((float)i*spacing, 0.0f, (float)halfSlices*spacing);
  237. rlVertex3f((float)-halfSlices*spacing, 0.0f, (float)i*spacing);
  238. rlVertex3f((float)halfSlices*spacing, 0.0f, (float)i*spacing);
  239. }
  240. rlEnd();
  241. }
  242. // Draw cube
  243. // NOTE: Cube position is the center position
  244. static void DrawCube(Vector3 position, float width, float height, float length, Color color)
  245. {
  246. float x = 0.0f;
  247. float y = 0.0f;
  248. float z = 0.0f;
  249. rlPushMatrix();
  250. // NOTE: Be careful! Function order matters (rotate -> scale -> translate)
  251. rlTranslatef(position.x, position.y, position.z);
  252. //rlScalef(2.0f, 2.0f, 2.0f);
  253. //rlRotatef(45, 0, 1, 0);
  254. rlBegin(RL_TRIANGLES);
  255. rlColor4ub(color.r, color.g, color.b, color.a);
  256. // Front Face -----------------------------------------------------
  257. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  258. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  259. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  260. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  261. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  262. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  263. // Back Face ------------------------------------------------------
  264. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  265. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  266. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  267. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  268. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  269. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  270. // Top Face -------------------------------------------------------
  271. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  272. rlVertex3f(x-width/2, y+height/2, z+length/2); // Bottom Left
  273. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  274. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  275. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  276. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  277. // Bottom Face ----------------------------------------------------
  278. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  279. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  280. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  281. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right
  282. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  283. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  284. // Right face -----------------------------------------------------
  285. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  286. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  287. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  288. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Left
  289. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  290. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  291. // Left Face ------------------------------------------------------
  292. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right
  293. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  294. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Right
  295. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  296. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  297. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right
  298. rlEnd();
  299. rlPopMatrix();
  300. }
  301. // Draw cube wires
  302. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color)
  303. {
  304. float x = 0.0f;
  305. float y = 0.0f;
  306. float z = 0.0f;
  307. rlPushMatrix();
  308. rlTranslatef(position.x, position.y, position.z);
  309. //rlRotatef(45, 0, 1, 0);
  310. rlBegin(RL_LINES);
  311. rlColor4ub(color.r, color.g, color.b, color.a);
  312. // Front Face -----------------------------------------------------
  313. // Bottom Line
  314. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  315. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  316. // Left Line
  317. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  318. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  319. // Top Line
  320. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  321. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  322. // Right Line
  323. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  324. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  325. // Back Face ------------------------------------------------------
  326. // Bottom Line
  327. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  328. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  329. // Left Line
  330. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  331. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  332. // Top Line
  333. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  334. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  335. // Right Line
  336. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  337. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  338. // Top Face -------------------------------------------------------
  339. // Left Line
  340. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left Front
  341. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Back
  342. // Right Line
  343. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right Front
  344. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Back
  345. // Bottom Face ---------------------------------------------------
  346. // Left Line
  347. rlVertex3f(x-width/2, y-height/2, z+length/2); // Top Left Front
  348. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left Back
  349. // Right Line
  350. rlVertex3f(x+width/2, y-height/2, z+length/2); // Top Right Front
  351. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right Back
  352. rlEnd();
  353. rlPopMatrix();
  354. }