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.

422 lines
18 KiB

6 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. * 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-2018 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. #include "rlgl.h" // OpenGL 1.1 immediate-mode style coding
  48. #ifdef __EMSCRIPTEN__
  49. #define GLFW_INCLUDE_ES2
  50. #endif
  51. #include <GLFW/glfw3.h> // Windows/Context and inputs management
  52. #define RED (Color){ 230, 41, 55, 255 } // Red
  53. #define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
  54. #define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
  55. //----------------------------------------------------------------------------------
  56. // Module specific Functions Declaration
  57. //----------------------------------------------------------------------------------
  58. static void ErrorCallback(int error, const char* description);
  59. static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
  60. // Drawing functions (uses rlgl functionality)
  61. static void DrawGrid(int slices, float spacing);
  62. static void DrawCube(Vector3 position, float width, float height, float length, Color color);
  63. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color);
  64. static void DrawRectangleV(Vector2 position, Vector2 size, Color color);
  65. //----------------------------------------------------------------------------------
  66. // Main Entry point
  67. //----------------------------------------------------------------------------------
  68. int main(void)
  69. {
  70. // Initialization
  71. //--------------------------------------------------------------------------------------
  72. const int screenWidth = 800;
  73. const int screenHeight = 450;
  74. // GLFW3 Initialization + OpenGL 3.3 Context + Extensions
  75. //--------------------------------------------------------
  76. glfwSetErrorCallback(ErrorCallback);
  77. if (!glfwInit())
  78. {
  79. TraceLog(LOG_WARNING, "GLFW3: Can not initialize GLFW");
  80. return 1;
  81. }
  82. else TraceLog(LOG_INFO, "GLFW3: GLFW initialized successfully");
  83. glfwWindowHint(GLFW_SAMPLES, 4);
  84. glfwWindowHint(GLFW_DEPTH_BITS, 16);
  85. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  86. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  87. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  88. //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  89. GLFWwindow *window = glfwCreateWindow(screenWidth, screenHeight, "rlgl standalone", NULL, NULL);
  90. if (!window)
  91. {
  92. glfwTerminate();
  93. return 2;
  94. }
  95. else TraceLog(LOG_INFO, "GLFW3: Window created successfully");
  96. glfwSetWindowPos(window, 200, 200);
  97. glfwSetKeyCallback(window, KeyCallback);
  98. glfwMakeContextCurrent(window);
  99. glfwSwapInterval(0);
  100. // Load OpenGL 3.3 supported extensions
  101. rlLoadExtensions(glfwGetProcAddress);
  102. //--------------------------------------------------------
  103. // Initialize OpenGL context (states and resources)
  104. rlglInit(screenWidth, screenHeight);
  105. // Initialize viewport and internal projection/modelview matrices
  106. rlViewport(0, 0, screenWidth, screenHeight);
  107. rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
  108. rlLoadIdentity(); // Reset current matrix (PROJECTION)
  109. rlOrtho(0, screenWidth, screenHeight, 0, 0.0f, 1.0f); // Orthographic projection with top-left corner at (0,0)
  110. rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
  111. rlLoadIdentity(); // Reset current matrix (MODELVIEW)
  112. rlClearColor(245, 245, 245, 255); // Define clear color
  113. rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
  114. Camera camera;
  115. camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
  116. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  117. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  118. camera.fovy = 45.0f; // Camera field-of-view Y
  119. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; // Cube default position (center)
  120. //--------------------------------------------------------------------------------------
  121. // Main game loop
  122. while (!glfwWindowShouldClose(window))
  123. {
  124. // Update
  125. //----------------------------------------------------------------------------------
  126. //camera.position.x += 0.01f;
  127. //----------------------------------------------------------------------------------
  128. // Draw
  129. //----------------------------------------------------------------------------------
  130. rlClearScreenBuffers(); // Clear current framebuffer
  131. // Draw '3D' elements in the scene
  132. //-----------------------------------------------
  133. // Calculate projection matrix (from perspective) and view matrix from camera look at
  134. Matrix matProj = MatrixPerspective(camera.fovy*DEG2RAD, (double)screenWidth/(double)screenHeight, 0.01, 1000.0);
  135. Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
  136. SetMatrixModelview(matView); // Set internal modelview matrix (default shader)
  137. SetMatrixProjection(matProj); // Set internal projection matrix (default shader)
  138. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  139. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, RAYWHITE);
  140. DrawGrid(10, 1.0f);
  141. // NOTE: Internal buffers drawing (3D data)
  142. rlglDraw();
  143. //-----------------------------------------------
  144. // Draw '2D' elements in the scene (GUI)
  145. //-----------------------------------------------
  146. #define RLGL_CREATE_MATRIX_MANUALLY
  147. #if defined(RLGL_CREATE_MATRIX_MANUALLY)
  148. matProj = MatrixOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0);
  149. matView = MatrixIdentity();
  150. SetMatrixModelview(matView); // Set internal modelview matrix (default shader)
  151. SetMatrixProjection(matProj); // Set internal projection matrix (default shader)
  152. #else // Let rlgl generate and multiply matrix internally
  153. rlMatrixMode(RL_PROJECTION); // Enable internal projection matrix
  154. rlLoadIdentity(); // Reset internal projection matrix
  155. rlOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0); // Recalculate internal projection matrix
  156. rlMatrixMode(RL_MODELVIEW); // Enable internal modelview matrix
  157. rlLoadIdentity(); // Reset internal modelview matrix
  158. #endif
  159. DrawRectangleV((Vector2){ 10.0f, 10.0f }, (Vector2){ 780.0f, 20.0f }, DARKGRAY);
  160. // NOTE: Internal buffers drawing (2D data)
  161. rlglDraw();
  162. //-----------------------------------------------
  163. glfwSwapBuffers(window);
  164. glfwPollEvents();
  165. //----------------------------------------------------------------------------------
  166. }
  167. // De-Initialization
  168. //--------------------------------------------------------------------------------------
  169. rlglClose(); // Unload rlgl internal buffers and default shader/texture
  170. glfwDestroyWindow(window); // Close window
  171. glfwTerminate(); // Free GLFW3 resources
  172. //--------------------------------------------------------------------------------------
  173. return 0;
  174. }
  175. //----------------------------------------------------------------------------------
  176. // Module specific Functions Definitions
  177. //----------------------------------------------------------------------------------
  178. // GLFW3: Error callback
  179. static void ErrorCallback(int error, const char* description)
  180. {
  181. TraceLog(LOG_ERROR, description);
  182. }
  183. // GLFW3: Keyboard callback
  184. static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
  185. {
  186. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  187. {
  188. glfwSetWindowShouldClose(window, GL_TRUE);
  189. }
  190. }
  191. // Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
  192. static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
  193. {
  194. rlBegin(RL_TRIANGLES);
  195. rlColor4ub(color.r, color.g, color.b, color.a);
  196. rlVertex2i(position.x, position.y);
  197. rlVertex2i(position.x, position.y + size.y);
  198. rlVertex2i(position.x + size.x, position.y + size.y);
  199. rlVertex2i(position.x, position.y);
  200. rlVertex2i(position.x + size.x, position.y + size.y);
  201. rlVertex2i(position.x + size.x, position.y);
  202. rlEnd();
  203. }
  204. // Draw a grid centered at (0, 0, 0)
  205. static void DrawGrid(int slices, float spacing)
  206. {
  207. int halfSlices = slices / 2;
  208. rlBegin(RL_LINES);
  209. for(int i = -halfSlices; i <= halfSlices; i++)
  210. {
  211. if (i == 0)
  212. {
  213. rlColor3f(0.5f, 0.5f, 0.5f);
  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. }
  218. else
  219. {
  220. rlColor3f(0.75f, 0.75f, 0.75f);
  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. }
  225. rlVertex3f((float)i*spacing, 0.0f, (float)-halfSlices*spacing);
  226. rlVertex3f((float)i*spacing, 0.0f, (float)halfSlices*spacing);
  227. rlVertex3f((float)-halfSlices*spacing, 0.0f, (float)i*spacing);
  228. rlVertex3f((float)halfSlices*spacing, 0.0f, (float)i*spacing);
  229. }
  230. rlEnd();
  231. }
  232. // Draw cube
  233. // NOTE: Cube position is the center position
  234. static void DrawCube(Vector3 position, float width, float height, float length, Color color)
  235. {
  236. float x = 0.0f;
  237. float y = 0.0f;
  238. float z = 0.0f;
  239. rlPushMatrix();
  240. // NOTE: Be careful! Function order matters (rotate -> scale -> translate)
  241. rlTranslatef(position.x, position.y, position.z);
  242. //rlScalef(2.0f, 2.0f, 2.0f);
  243. //rlRotatef(45, 0, 1, 0);
  244. rlBegin(RL_TRIANGLES);
  245. rlColor4ub(color.r, color.g, color.b, color.a);
  246. // Front Face -----------------------------------------------------
  247. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  248. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  249. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  250. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top 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); // Bottom Right
  253. // Back Face ------------------------------------------------------
  254. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  255. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  256. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  257. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  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 Left
  260. // Top Face -------------------------------------------------------
  261. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  262. rlVertex3f(x-width/2, y+height/2, z+length/2); // Bottom Left
  263. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  264. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  265. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  266. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  267. // Bottom Face ----------------------------------------------------
  268. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  269. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  270. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  271. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right
  272. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  273. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  274. // Right face -----------------------------------------------------
  275. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  276. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  277. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  278. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Left
  279. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  280. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  281. // Left Face ------------------------------------------------------
  282. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom 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); // Top Right
  285. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  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. rlEnd();
  289. rlPopMatrix();
  290. }
  291. // Draw cube wires
  292. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color)
  293. {
  294. float x = 0.0f;
  295. float y = 0.0f;
  296. float z = 0.0f;
  297. rlPushMatrix();
  298. rlTranslatef(position.x, position.y, position.z);
  299. //rlRotatef(45, 0, 1, 0);
  300. rlBegin(RL_LINES);
  301. rlColor4ub(color.r, color.g, color.b, color.a);
  302. // Front Face -----------------------------------------------------
  303. // Bottom Line
  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. // Left Line
  307. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  308. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  309. // Top Line
  310. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  311. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  312. // Right Line
  313. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  314. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  315. // Back Face ------------------------------------------------------
  316. // Bottom Line
  317. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  318. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  319. // Left Line
  320. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  321. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  322. // Top Line
  323. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  324. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  325. // Right Line
  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 Left
  328. // Top Face -------------------------------------------------------
  329. // Left Line
  330. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left Front
  331. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Back
  332. // Right Line
  333. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right Front
  334. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Back
  335. // Bottom Face ---------------------------------------------------
  336. // Left Line
  337. rlVertex3f(x-width/2, y-height/2, z+length/2); // Top Left Front
  338. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left Back
  339. // Right Line
  340. rlVertex3f(x+width/2, y-height/2, z+length/2); // Top Right Front
  341. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right Back
  342. rlEnd();
  343. rlPopMatrix();
  344. }