rlgl_standalone.c 18 KB

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