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.

468 wiersze
21 KiB

6 lat temu
11 miesięcy temu
4 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. * rlgl library is an abstraction layer for multiple OpenGL versions (1.1, 2.1, 3.3 Core, ES 2.0)
  6. * that provides a pseudo-OpenGL 1.1 immediate-mode style API (rlVertex, rlTranslate, rlRotate...)
  7. *
  8. * WARNING: This example is intended only for PLATFORM_DESKTOP and OpenGL 3.3 Core profile.
  9. * It could work on other platforms if redesigned for those platforms (out-of-scope)
  10. *
  11. * DEPENDENCIES:
  12. * glfw3 - Windows and context initialization library
  13. * rlgl.h - OpenGL abstraction layer to OpenGL 1.1, 3.3 or ES2
  14. * glad.h - OpenGL extensions initialization library (required by rlgl)
  15. * raymath.h - 3D math library
  16. *
  17. * WINDOWS COMPILATION:
  18. * gcc -o rlgl_standalone.exe rlgl_standalone.c -s -Iexternal\include -I..\..\src \
  19. * -L. -Lexternal\lib -lglfw3 -lopengl32 -lgdi32 -Wall -std=c99 -DGRAPHICS_API_OPENGL_33
  20. *
  21. * APPLE COMPILATION:
  22. * gcc -o rlgl_standalone rlgl_standalone.c -I../../src -Iexternal/include -Lexternal/lib \
  23. * -lglfw3 -framework CoreVideo -framework OpenGL -framework IOKit -framework Cocoa
  24. * -Wno-deprecated-declarations -std=c99 -DGRAPHICS_API_OPENGL_33
  25. *
  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-2024 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. // NOTE: rlgl can be configured just re-defining the following values:
  51. //#define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 8192 // Default internal render batch elements limits
  52. //#define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering)
  53. //#define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture)
  54. //#define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture())
  55. //#define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack
  56. //#define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported
  57. //#define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance
  58. //#define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance
  59. #define RLGL_IMPLEMENTATION
  60. #include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 3.3+ or ES2
  61. #define RAYMATH_STATIC_INLINE
  62. #include "raymath.h" // Vector2, Vector3, Quaternion and Matrix functionality
  63. #include "GLFW/glfw3.h" // Windows/Context and inputs management
  64. #include <stdio.h> // Required for: printf()
  65. #define RED (Color){ 230, 41, 55, 255 } // Red
  66. #define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
  67. #define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
  68. //----------------------------------------------------------------------------------
  69. // Structures Definition
  70. //----------------------------------------------------------------------------------
  71. // Color, 4 components, R8G8B8A8 (32bit)
  72. typedef struct Color {
  73. unsigned char r; // Color red value
  74. unsigned char g; // Color green value
  75. unsigned char b; // Color blue value
  76. unsigned char a; // Color alpha value
  77. } Color;
  78. // Camera type, defines a camera position/orientation in 3d space
  79. typedef struct Camera {
  80. Vector3 position; // Camera position
  81. Vector3 target; // Camera target it looks-at
  82. Vector3 up; // Camera up vector (rotation over its axis)
  83. float fovy; // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic
  84. int projection; // Camera projection: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
  85. } Camera;
  86. //----------------------------------------------------------------------------------
  87. // Module specific Functions Declaration
  88. //----------------------------------------------------------------------------------
  89. static void ErrorCallback(int error, const char *description);
  90. static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
  91. // Drawing functions (uses rlgl functionality)
  92. static void DrawGrid(int slices, float spacing);
  93. static void DrawCube(Vector3 position, float width, float height, float length, Color color);
  94. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color);
  95. static void DrawRectangleV(Vector2 position, Vector2 size, Color color);
  96. // NOTE: We use raymath to get this functionality but it could be implemented in this module
  97. //static Matrix MatrixIdentity(void);
  98. //static Matrix MatrixOrtho(double left, double right, double bottom, double top, double near, double far);
  99. //static Matrix MatrixPerspective(double fovy, double aspect, double near, double far);
  100. //static Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up);
  101. //------------------------------------------------------------------------------------
  102. // Program main entry point
  103. //------------------------------------------------------------------------------------
  104. int main(void)
  105. {
  106. // Initialization
  107. //--------------------------------------------------------------------------------------
  108. const int screenWidth = 800;
  109. const int screenHeight = 450;
  110. // GLFW3 Initialization + OpenGL 3.3 Context + Extensions
  111. //--------------------------------------------------------
  112. glfwSetErrorCallback(ErrorCallback);
  113. if (!glfwInit())
  114. {
  115. printf("GLFW3: Can not initialize GLFW\n");
  116. return 1;
  117. }
  118. else printf("GLFW3: GLFW initialized successfully\n");
  119. glfwWindowHint(GLFW_SAMPLES, 4);
  120. glfwWindowHint(GLFW_DEPTH_BITS, 16);
  121. // WARNING: OpenGL 3.3 Core profile only
  122. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  123. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  124. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  125. //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  126. #if defined(__APPLE__)
  127. glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
  128. #endif
  129. GLFWwindow *window = glfwCreateWindow(screenWidth, screenHeight, "rlgl standalone", NULL, NULL);
  130. if (!window)
  131. {
  132. glfwTerminate();
  133. return 2;
  134. }
  135. else printf("GLFW3: Window created successfully\n");
  136. glfwSetWindowPos(window, 200, 200);
  137. glfwSetKeyCallback(window, KeyCallback);
  138. glfwMakeContextCurrent(window);
  139. glfwSwapInterval(0);
  140. // Load OpenGL 3.3 supported extensions
  141. rlLoadExtensions(glfwGetProcAddress);
  142. //--------------------------------------------------------
  143. // Initialize OpenGL context (states and resources)
  144. rlglInit(screenWidth, screenHeight);
  145. // Initialize viewport and internal projection/modelview matrices
  146. rlViewport(0, 0, screenWidth, screenHeight);
  147. rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
  148. rlLoadIdentity(); // Reset current matrix (PROJECTION)
  149. rlOrtho(0, screenWidth, screenHeight, 0, 0.0f, 1.0f); // Orthographic projection with top-left corner at (0,0)
  150. rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
  151. rlLoadIdentity(); // Reset current matrix (MODELVIEW)
  152. rlClearColor(245, 245, 245, 255); // Define clear color
  153. rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
  154. Camera camera = { 0 };
  155. camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
  156. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  157. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  158. camera.fovy = 45.0f; // Camera field-of-view Y
  159. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; // Cube default position (center)
  160. //--------------------------------------------------------------------------------------
  161. // Main game loop
  162. while (!glfwWindowShouldClose(window))
  163. {
  164. // Update
  165. //----------------------------------------------------------------------------------
  166. //camera.position.x += 0.01f;
  167. //----------------------------------------------------------------------------------
  168. // Draw
  169. //----------------------------------------------------------------------------------
  170. rlClearScreenBuffers(); // Clear current framebuffer
  171. // Draw '3D' elements in the scene
  172. //-----------------------------------------------
  173. // Calculate projection matrix (from perspective) and view matrix from camera look at
  174. Matrix matProj = MatrixPerspective((double)(camera.fovy*DEG2RAD), (double)screenWidth/(double)screenHeight, 0.01, 1000.0);
  175. Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
  176. rlSetMatrixModelview(matView); // Set internal modelview matrix (default shader)
  177. rlSetMatrixProjection(matProj); // Set internal projection matrix (default shader)
  178. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  179. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, RAYWHITE);
  180. DrawGrid(10, 1.0f);
  181. // Draw internal render batch buffers (3D data)
  182. rlDrawRenderBatchActive();
  183. //-----------------------------------------------
  184. // Draw '2D' elements in the scene (GUI)
  185. //-----------------------------------------------
  186. #define RLGL_SET_MATRIX_MANUALLY
  187. #if defined(RLGL_SET_MATRIX_MANUALLY)
  188. matProj = MatrixOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0);
  189. matView = MatrixIdentity();
  190. rlSetMatrixModelview(matView); // Set internal modelview matrix (default shader)
  191. rlSetMatrixProjection(matProj); // Set internal projection matrix (default shader)
  192. #else // Let rlgl generate and multiply matrix internally
  193. rlMatrixMode(RL_PROJECTION); // Enable internal projection matrix
  194. rlLoadIdentity(); // Reset internal projection matrix
  195. rlOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0); // Recalculate internal projection matrix
  196. rlMatrixMode(RL_MODELVIEW); // Enable internal modelview matrix
  197. rlLoadIdentity(); // Reset internal modelview matrix
  198. #endif
  199. DrawRectangleV((Vector2){ 10.0f, 10.0f }, (Vector2){ 780.0f, 20.0f }, DARKGRAY);
  200. // Draw internal render batch buffers (2D data)
  201. rlDrawRenderBatchActive();
  202. //-----------------------------------------------
  203. glfwSwapBuffers(window);
  204. glfwPollEvents();
  205. //----------------------------------------------------------------------------------
  206. }
  207. // De-Initialization
  208. //--------------------------------------------------------------------------------------
  209. rlglClose(); // Unload rlgl internal buffers and default shader/texture
  210. glfwDestroyWindow(window); // Close window
  211. glfwTerminate(); // Free GLFW3 resources
  212. //--------------------------------------------------------------------------------------
  213. return 0;
  214. }
  215. //----------------------------------------------------------------------------------
  216. // Module specific Functions Definitions
  217. //----------------------------------------------------------------------------------
  218. // GLFW3: Error callback
  219. static void ErrorCallback(int error, const char *description)
  220. {
  221. fprintf(stderr, "%s", description);
  222. }
  223. // GLFW3: Keyboard callback
  224. static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
  225. {
  226. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  227. {
  228. glfwSetWindowShouldClose(window, GL_TRUE);
  229. }
  230. }
  231. // Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
  232. static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
  233. {
  234. rlBegin(RL_TRIANGLES);
  235. rlColor4ub(color.r, color.g, color.b, color.a);
  236. rlVertex2f(position.x, position.y);
  237. rlVertex2f(position.x, position.y + size.y);
  238. rlVertex2f(position.x + size.x, position.y + size.y);
  239. rlVertex2f(position.x, position.y);
  240. rlVertex2f(position.x + size.x, position.y + size.y);
  241. rlVertex2f(position.x + size.x, position.y);
  242. rlEnd();
  243. }
  244. // Draw a grid centered at (0, 0, 0)
  245. static void DrawGrid(int slices, float spacing)
  246. {
  247. int halfSlices = slices / 2;
  248. rlBegin(RL_LINES);
  249. for (int i = -halfSlices; i <= halfSlices; i++)
  250. {
  251. if (i == 0)
  252. {
  253. rlColor3f(0.5f, 0.5f, 0.5f);
  254. rlColor3f(0.5f, 0.5f, 0.5f);
  255. rlColor3f(0.5f, 0.5f, 0.5f);
  256. rlColor3f(0.5f, 0.5f, 0.5f);
  257. }
  258. else
  259. {
  260. rlColor3f(0.75f, 0.75f, 0.75f);
  261. rlColor3f(0.75f, 0.75f, 0.75f);
  262. rlColor3f(0.75f, 0.75f, 0.75f);
  263. rlColor3f(0.75f, 0.75f, 0.75f);
  264. }
  265. rlVertex3f((float)i*spacing, 0.0f, (float)-halfSlices*spacing);
  266. rlVertex3f((float)i*spacing, 0.0f, (float)halfSlices*spacing);
  267. rlVertex3f((float)-halfSlices*spacing, 0.0f, (float)i*spacing);
  268. rlVertex3f((float)halfSlices*spacing, 0.0f, (float)i*spacing);
  269. }
  270. rlEnd();
  271. }
  272. // Draw cube
  273. // NOTE: Cube position is the center position
  274. static void DrawCube(Vector3 position, float width, float height, float length, Color color)
  275. {
  276. float x = 0.0f;
  277. float y = 0.0f;
  278. float z = 0.0f;
  279. rlPushMatrix();
  280. // NOTE: Be careful! Function order matters (rotate -> scale -> translate)
  281. rlTranslatef(position.x, position.y, position.z);
  282. //rlScalef(2.0f, 2.0f, 2.0f);
  283. //rlRotatef(45, 0, 1, 0);
  284. rlBegin(RL_TRIANGLES);
  285. rlColor4ub(color.r, color.g, color.b, color.a);
  286. // Front Face -----------------------------------------------------
  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 Left
  290. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  291. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  292. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  293. // Back Face ------------------------------------------------------
  294. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  295. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  296. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  297. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  298. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  299. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  300. // Top Face -------------------------------------------------------
  301. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  302. rlVertex3f(x-width/2, y+height/2, z+length/2); // Bottom Left
  303. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  304. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  305. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  306. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  307. // Bottom Face ----------------------------------------------------
  308. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  309. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom 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 Right
  312. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  313. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  314. // Right face -----------------------------------------------------
  315. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  316. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  317. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  318. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Left
  319. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  320. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  321. // Left Face ------------------------------------------------------
  322. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right
  323. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  324. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Right
  325. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  326. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  327. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right
  328. rlEnd();
  329. rlPopMatrix();
  330. }
  331. // Draw cube wires
  332. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color)
  333. {
  334. float x = 0.0f;
  335. float y = 0.0f;
  336. float z = 0.0f;
  337. rlPushMatrix();
  338. rlTranslatef(position.x, position.y, position.z);
  339. //rlRotatef(45, 0, 1, 0);
  340. rlBegin(RL_LINES);
  341. rlColor4ub(color.r, color.g, color.b, color.a);
  342. // Front Face -----------------------------------------------------
  343. // Bottom Line
  344. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  345. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  346. // Left Line
  347. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  348. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  349. // Top Line
  350. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  351. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  352. // Right Line
  353. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  354. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  355. // Back Face ------------------------------------------------------
  356. // Bottom Line
  357. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  358. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  359. // Left Line
  360. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  361. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  362. // Top Line
  363. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  364. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  365. // Right Line
  366. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  367. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  368. // Top Face -------------------------------------------------------
  369. // Left Line
  370. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left Front
  371. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Back
  372. // Right Line
  373. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right Front
  374. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Back
  375. // Bottom Face ---------------------------------------------------
  376. // Left Line
  377. rlVertex3f(x-width/2, y-height/2, z+length/2); // Top Left Front
  378. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left Back
  379. // Right Line
  380. rlVertex3f(x+width/2, y-height/2, z+length/2); // Top Right Front
  381. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right Back
  382. rlEnd();
  383. rlPopMatrix();
  384. }