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.

443 lines
19 KiB

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