Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

451 рядки
19 KiB

3 роки тому
8 роки тому
6 роки тому
6 роки тому
5 роки тому
5 роки тому
5 роки тому
3 роки тому
5 роки тому
3 роки тому
3 роки тому
5 роки тому
5 роки тому
3 роки тому
6 роки тому
3 роки тому
3 роки тому
5 роки тому
3 роки тому
3 роки тому
3 роки тому
6 роки тому
3 роки тому
8 роки тому
3 роки тому
3 роки тому
3 роки тому
8 роки тому
3 роки тому
3 роки тому
6 роки тому
3 роки тому
3 роки тому
3 роки тому
3 роки тому
8 роки тому
3 роки тому
5 роки тому
5 роки тому
3 роки тому
6 роки тому
6 роки тому
  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. // Color, 4 components, R8G8B8A8 (32bit)
  63. typedef struct Color {
  64. unsigned char r; // Color red value
  65. unsigned char g; // Color green value
  66. unsigned char b; // Color blue value
  67. unsigned char a; // Color alpha value
  68. } Color;
  69. // Camera type, defines a camera position/orientation in 3d space
  70. typedef struct Camera {
  71. Vector3 position; // Camera position
  72. Vector3 target; // Camera target it looks-at
  73. Vector3 up; // Camera up vector (rotation over its axis)
  74. float fovy; // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic
  75. int projection; // Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
  76. } Camera;
  77. //----------------------------------------------------------------------------------
  78. // Module specific Functions Declaration
  79. //----------------------------------------------------------------------------------
  80. static void ErrorCallback(int error, const char *description);
  81. static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
  82. // Drawing functions (uses rlgl functionality)
  83. static void DrawGrid(int slices, float spacing);
  84. static void DrawCube(Vector3 position, float width, float height, float length, Color color);
  85. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color);
  86. static void DrawRectangleV(Vector2 position, Vector2 size, Color color);
  87. //----------------------------------------------------------------------------------
  88. // Main Entry point
  89. //----------------------------------------------------------------------------------
  90. int main(void)
  91. {
  92. // Initialization
  93. //--------------------------------------------------------------------------------------
  94. const int screenWidth = 800;
  95. const int screenHeight = 450;
  96. // GLFW3 Initialization + OpenGL 3.3 Context + Extensions
  97. //--------------------------------------------------------
  98. glfwSetErrorCallback(ErrorCallback);
  99. if (!glfwInit())
  100. {
  101. printf("GLFW3: Can not initialize GLFW\n");
  102. return 1;
  103. }
  104. else printf("GLFW3: GLFW initialized successfully\n");
  105. glfwWindowHint(GLFW_SAMPLES, 4);
  106. glfwWindowHint(GLFW_DEPTH_BITS, 16);
  107. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  108. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  109. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  110. //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  111. #if defined(__APPLE__)
  112. glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
  113. #endif
  114. GLFWwindow *window = glfwCreateWindow(screenWidth, screenHeight, "rlgl standalone", NULL, NULL);
  115. if (!window)
  116. {
  117. glfwTerminate();
  118. return 2;
  119. }
  120. else printf("GLFW3: Window created successfully\n");
  121. glfwSetWindowPos(window, 200, 200);
  122. glfwSetKeyCallback(window, KeyCallback);
  123. glfwMakeContextCurrent(window);
  124. glfwSwapInterval(0);
  125. // Load OpenGL 3.3 supported extensions
  126. rlLoadExtensions(glfwGetProcAddress);
  127. //--------------------------------------------------------
  128. // Initialize OpenGL context (states and resources)
  129. rlglInit(screenWidth, screenHeight);
  130. // Initialize viewport and internal projection/modelview matrices
  131. rlViewport(0, 0, screenWidth, screenHeight);
  132. rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
  133. rlLoadIdentity(); // Reset current matrix (PROJECTION)
  134. rlOrtho(0, screenWidth, screenHeight, 0, 0.0f, 1.0f); // Orthographic projection with top-left corner at (0,0)
  135. rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
  136. rlLoadIdentity(); // Reset current matrix (MODELVIEW)
  137. rlClearColor(245, 245, 245, 255); // Define clear color
  138. rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
  139. Camera camera = { 0 };
  140. camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
  141. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  142. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  143. camera.fovy = 45.0f; // Camera field-of-view Y
  144. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; // Cube default position (center)
  145. //--------------------------------------------------------------------------------------
  146. // Main game loop
  147. while (!glfwWindowShouldClose(window))
  148. {
  149. // Update
  150. //----------------------------------------------------------------------------------
  151. //camera.position.x += 0.01f;
  152. //----------------------------------------------------------------------------------
  153. // Draw
  154. //----------------------------------------------------------------------------------
  155. rlClearScreenBuffers(); // Clear current framebuffer
  156. // Draw '3D' elements in the scene
  157. //-----------------------------------------------
  158. // Calculate projection matrix (from perspective) and view matrix from camera look at
  159. Matrix matProj = MatrixPerspective((double)(camera.fovy*DEG2RAD), (double)screenWidth/(double)screenHeight, 0.01, 1000.0);
  160. Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
  161. rlSetMatrixModelview(matView); // Set internal modelview matrix (default shader)
  162. rlSetMatrixProjection(matProj); // Set internal projection matrix (default shader)
  163. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  164. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, RAYWHITE);
  165. DrawGrid(10, 1.0f);
  166. // Draw internal render batch buffers (3D data)
  167. rlDrawRenderBatchActive();
  168. //-----------------------------------------------
  169. // Draw '2D' elements in the scene (GUI)
  170. //-----------------------------------------------
  171. #define RLGL_CREATE_MATRIX_MANUALLY
  172. #if defined(RLGL_CREATE_MATRIX_MANUALLY)
  173. matProj = MatrixOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0);
  174. matView = MatrixIdentity();
  175. rlSetMatrixModelview(matView); // Set internal modelview matrix (default shader)
  176. rlSetMatrixProjection(matProj); // Set internal projection matrix (default shader)
  177. #else // Let rlgl generate and multiply matrix internally
  178. rlMatrixMode(RL_PROJECTION); // Enable internal projection matrix
  179. rlLoadIdentity(); // Reset internal projection matrix
  180. rlOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0); // Recalculate internal projection matrix
  181. rlMatrixMode(RL_MODELVIEW); // Enable internal modelview matrix
  182. rlLoadIdentity(); // Reset internal modelview matrix
  183. #endif
  184. DrawRectangleV((Vector2){ 10.0f, 10.0f }, (Vector2){ 780.0f, 20.0f }, DARKGRAY);
  185. // Draw internal render batch buffers (3D data)
  186. rlDrawRenderBatchActive();
  187. //-----------------------------------------------
  188. glfwSwapBuffers(window);
  189. glfwPollEvents();
  190. //----------------------------------------------------------------------------------
  191. }
  192. // De-Initialization
  193. //--------------------------------------------------------------------------------------
  194. rlglClose(); // Unload rlgl internal buffers and default shader/texture
  195. glfwDestroyWindow(window); // Close window
  196. glfwTerminate(); // Free GLFW3 resources
  197. //--------------------------------------------------------------------------------------
  198. return 0;
  199. }
  200. //----------------------------------------------------------------------------------
  201. // Module specific Functions Definitions
  202. //----------------------------------------------------------------------------------
  203. // GLFW3: Error callback
  204. static void ErrorCallback(int error, const char *description)
  205. {
  206. fprintf(stderr, "%s", description);
  207. }
  208. // GLFW3: Keyboard callback
  209. static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
  210. {
  211. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  212. {
  213. glfwSetWindowShouldClose(window, GL_TRUE);
  214. }
  215. }
  216. // Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
  217. static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
  218. {
  219. rlBegin(RL_TRIANGLES);
  220. rlColor4ub(color.r, color.g, color.b, color.a);
  221. rlVertex2f(position.x, position.y);
  222. rlVertex2f(position.x, position.y + size.y);
  223. rlVertex2f(position.x + size.x, position.y + size.y);
  224. rlVertex2f(position.x, position.y);
  225. rlVertex2f(position.x + size.x, position.y + size.y);
  226. rlVertex2f(position.x + size.x, position.y);
  227. rlEnd();
  228. }
  229. // Draw a grid centered at (0, 0, 0)
  230. static void DrawGrid(int slices, float spacing)
  231. {
  232. int halfSlices = slices / 2;
  233. rlBegin(RL_LINES);
  234. for (int i = -halfSlices; i <= halfSlices; i++)
  235. {
  236. if (i == 0)
  237. {
  238. rlColor3f(0.5f, 0.5f, 0.5f);
  239. rlColor3f(0.5f, 0.5f, 0.5f);
  240. rlColor3f(0.5f, 0.5f, 0.5f);
  241. rlColor3f(0.5f, 0.5f, 0.5f);
  242. }
  243. else
  244. {
  245. rlColor3f(0.75f, 0.75f, 0.75f);
  246. rlColor3f(0.75f, 0.75f, 0.75f);
  247. rlColor3f(0.75f, 0.75f, 0.75f);
  248. rlColor3f(0.75f, 0.75f, 0.75f);
  249. }
  250. rlVertex3f((float)i*spacing, 0.0f, (float)-halfSlices*spacing);
  251. rlVertex3f((float)i*spacing, 0.0f, (float)halfSlices*spacing);
  252. rlVertex3f((float)-halfSlices*spacing, 0.0f, (float)i*spacing);
  253. rlVertex3f((float)halfSlices*spacing, 0.0f, (float)i*spacing);
  254. }
  255. rlEnd();
  256. }
  257. // Draw cube
  258. // NOTE: Cube position is the center position
  259. static void DrawCube(Vector3 position, float width, float height, float length, Color color)
  260. {
  261. float x = 0.0f;
  262. float y = 0.0f;
  263. float z = 0.0f;
  264. rlPushMatrix();
  265. // NOTE: Be careful! Function order matters (rotate -> scale -> translate)
  266. rlTranslatef(position.x, position.y, position.z);
  267. //rlScalef(2.0f, 2.0f, 2.0f);
  268. //rlRotatef(45, 0, 1, 0);
  269. rlBegin(RL_TRIANGLES);
  270. rlColor4ub(color.r, color.g, color.b, color.a);
  271. // Front Face -----------------------------------------------------
  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 Left
  275. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  276. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  277. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  278. // Back Face ------------------------------------------------------
  279. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  280. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  281. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  282. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  283. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  284. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  285. // Top Face -------------------------------------------------------
  286. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  287. rlVertex3f(x-width/2, y+height/2, z+length/2); // Bottom Left
  288. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  289. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  290. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  291. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  292. // Bottom Face ----------------------------------------------------
  293. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  294. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom 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 Right
  297. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  298. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  299. // Right face -----------------------------------------------------
  300. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  301. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  302. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  303. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Left
  304. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  305. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  306. // Left Face ------------------------------------------------------
  307. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right
  308. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  309. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Right
  310. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  311. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  312. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right
  313. rlEnd();
  314. rlPopMatrix();
  315. }
  316. // Draw cube wires
  317. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color)
  318. {
  319. float x = 0.0f;
  320. float y = 0.0f;
  321. float z = 0.0f;
  322. rlPushMatrix();
  323. rlTranslatef(position.x, position.y, position.z);
  324. //rlRotatef(45, 0, 1, 0);
  325. rlBegin(RL_LINES);
  326. rlColor4ub(color.r, color.g, color.b, color.a);
  327. // Front Face -----------------------------------------------------
  328. // Bottom Line
  329. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  330. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  331. // Left Line
  332. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  333. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  334. // Top Line
  335. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  336. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  337. // Right Line
  338. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  339. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  340. // Back Face ------------------------------------------------------
  341. // Bottom Line
  342. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  343. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  344. // Left Line
  345. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  346. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  347. // Top Line
  348. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  349. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  350. // Right Line
  351. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  352. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  353. // Top Face -------------------------------------------------------
  354. // Left Line
  355. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left Front
  356. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Back
  357. // Right Line
  358. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right Front
  359. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Back
  360. // Bottom Face ---------------------------------------------------
  361. // Left Line
  362. rlVertex3f(x-width/2, y-height/2, z+length/2); // Top Left Front
  363. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left Back
  364. // Right Line
  365. rlVertex3f(x+width/2, y-height/2, z+length/2); // Top Right Front
  366. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right Back
  367. rlEnd();
  368. rlPopMatrix();
  369. }