You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

425 line
18 KiB

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