您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

469 行
21 KiB

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