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.

395 lines
17 KiB

  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. * Compile rlgl module using:
  9. * gcc -c rlgl.c -Wall -std=c99 -DRLGL_STANDALONE -DRAYMATH_IMPLEMENTATION -DGRAPHICS_API_OPENGL_33
  10. *
  11. * NOTE: rlgl module requires the following header-only files:
  12. * external/glad.h - OpenGL extensions loader (stripped to only required extensions)
  13. * shader_standard.h - Standard shader for materials and lighting
  14. * shader_distortion.h - Distortion shader for VR
  15. * raymath.h - Vector and matrix math functions
  16. *
  17. * Compile example using:
  18. * gcc -o rlgl_standalone.exe rlgl_standalone.c rlgl.o -lglfw3 -lopengl32 -lgdi32 -std=c99
  19. *
  20. * This example has been created using raylib 1.5 (www.raylib.com)
  21. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  22. *
  23. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  24. *
  25. ********************************************************************************************/
  26. #include <GLFW/glfw3.h> // Windows/Context and inputs management
  27. #define RLGL_STANDALONE
  28. #include "rlgl.h" // rlgl library: OpenGL 1.1 immediate-mode style coding
  29. #define RED (Color){ 230, 41, 55, 255 } // Red
  30. #define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
  31. #define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
  32. //----------------------------------------------------------------------------------
  33. // Module specific Functions Declaration
  34. //----------------------------------------------------------------------------------
  35. static void ErrorCallback(int error, const char* description);
  36. static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
  37. // Drawing functions (uses rlgl functionality)
  38. static void DrawGrid(int slices, float spacing);
  39. static void DrawCube(Vector3 position, float width, float height, float length, Color color);
  40. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color);
  41. static void DrawRectangleV(Vector2 position, Vector2 size, Color color);
  42. //----------------------------------------------------------------------------------
  43. // Main Entry point
  44. //----------------------------------------------------------------------------------
  45. int main(void)
  46. {
  47. // Initialization
  48. //--------------------------------------------------------------------------------------
  49. const int screenWidth = 800;
  50. const int screenHeight = 450;
  51. // GLFW3 Initialization + OpenGL 3.3 Context + Extensions
  52. //--------------------------------------------------------
  53. glfwSetErrorCallback(ErrorCallback);
  54. if (!glfwInit())
  55. {
  56. TraceLog(WARNING, "GLFW3: Can not initialize GLFW");
  57. return 1;
  58. }
  59. else TraceLog(INFO, "GLFW3: GLFW initialized successfully");
  60. glfwWindowHint(GLFW_SAMPLES, 4);
  61. glfwWindowHint(GLFW_DEPTH_BITS, 16);
  62. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  63. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  64. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  65. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  66. GLFWwindow *window = glfwCreateWindow(screenWidth, screenHeight, "rlgl standalone", NULL, NULL);
  67. if (!window)
  68. {
  69. glfwTerminate();
  70. return 2;
  71. }
  72. else TraceLog(INFO, "GLFW3: Window created successfully");
  73. glfwSetWindowPos(window, 200, 200);
  74. glfwSetKeyCallback(window, KeyCallback);
  75. glfwMakeContextCurrent(window);
  76. glfwSwapInterval(1);
  77. // Load OpenGL 3.3 supported extensions
  78. rlglLoadExtensions(glfwGetProcAddress);
  79. //--------------------------------------------------------
  80. // Initialize OpenGL context (states and resources)
  81. rlglInit(screenWidth, screenHeight);
  82. // Initialize viewport and internal projection/modelview matrices
  83. rlViewport(0, 0, screenWidth, screenHeight);
  84. rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
  85. rlLoadIdentity(); // Reset current matrix (PROJECTION)
  86. rlOrtho(0, screenWidth, screenHeight, 0, 0.0f, 1.0f); // Orthographic projection with top-left corner at (0,0)
  87. rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
  88. rlLoadIdentity(); // Reset current matrix (MODELVIEW)
  89. rlClearColor(245, 245, 245, 255); // Define clear color
  90. rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
  91. Camera camera;
  92. camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
  93. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  94. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  95. camera.fovy = 45.0f; // Camera field-of-view Y
  96. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f }; // Cube default position (center)
  97. //--------------------------------------------------------------------------------------
  98. // Main game loop
  99. while (!glfwWindowShouldClose(window))
  100. {
  101. // Update
  102. //----------------------------------------------------------------------------------
  103. // ...
  104. //----------------------------------------------------------------------------------
  105. // Draw
  106. //----------------------------------------------------------------------------------
  107. rlClearScreenBuffers(); // Clear current framebuffer
  108. // Calculate projection matrix (from perspective) and view matrix from camera look at
  109. Matrix matProj = MatrixPerspective(camera.fovy, (double)screenWidth/(double)screenHeight, 0.01, 1000.0);
  110. MatrixTranspose(&matProj);
  111. Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
  112. SetMatrixModelview(matView); // Replace internal modelview matrix by a custom one
  113. SetMatrixProjection(matProj); // Replace internal projection matrix by a custom one
  114. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  115. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, RAYWHITE);
  116. DrawGrid(10, 1.0f);
  117. // NOTE: Internal buffers drawing (3D data)
  118. rlglDraw();
  119. // Draw '2D' elements in the scene (GUI)
  120. #define RLGL_CREATE_MATRIX_MANUALLY
  121. #if defined(RLGL_CREATE_MATRIX_MANUALLY)
  122. matProj = MatrixOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0);
  123. MatrixTranspose(&matProj);
  124. matView = MatrixIdentity();
  125. SetMatrixModelview(matView); // Replace internal modelview matrix by a custom one
  126. SetMatrixProjection(matProj); // Replace internal projection matrix by a custom one
  127. #else // Let rlgl generate and multiply matrix internally
  128. rlMatrixMode(RL_PROJECTION); // Enable internal projection matrix
  129. rlLoadIdentity(); // Reset internal projection matrix
  130. rlOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0); // Recalculate internal projection matrix
  131. rlMatrixMode(RL_MODELVIEW); // Enable internal modelview matrix
  132. rlLoadIdentity(); // Reset internal modelview matrix
  133. #endif
  134. DrawRectangleV((Vector2){ 10.0f, 10.0f }, (Vector2){ 780.0f, 20.0f }, DARKGRAY);
  135. // NOTE: Internal buffers drawing (2D data)
  136. rlglDraw();
  137. glfwSwapBuffers(window);
  138. glfwPollEvents();
  139. //----------------------------------------------------------------------------------
  140. }
  141. // De-Initialization
  142. //--------------------------------------------------------------------------------------
  143. rlglClose(); // Unload rlgl internal buffers and default shader/texture
  144. glfwDestroyWindow(window); // Close window
  145. glfwTerminate(); // Free GLFW3 resources
  146. //--------------------------------------------------------------------------------------
  147. return 0;
  148. }
  149. //----------------------------------------------------------------------------------
  150. // Module specific Functions Definitions
  151. //----------------------------------------------------------------------------------
  152. // GLFW3: Error callback
  153. static void ErrorCallback(int error, const char* description)
  154. {
  155. TraceLog(ERROR, description);
  156. }
  157. // GLFW3: Keyboard callback
  158. static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
  159. {
  160. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  161. {
  162. glfwSetWindowShouldClose(window, GL_TRUE);
  163. }
  164. }
  165. // Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
  166. static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
  167. {
  168. rlBegin(RL_TRIANGLES);
  169. rlColor4ub(color.r, color.g, color.b, color.a);
  170. rlVertex2i(position.x, position.y);
  171. rlVertex2i(position.x, position.y + size.y);
  172. rlVertex2i(position.x + size.x, position.y + size.y);
  173. rlVertex2i(position.x, position.y);
  174. rlVertex2i(position.x + size.x, position.y + size.y);
  175. rlVertex2i(position.x + size.x, position.y);
  176. rlEnd();
  177. }
  178. // Draw a grid centered at (0, 0, 0)
  179. static void DrawGrid(int slices, float spacing)
  180. {
  181. int halfSlices = slices / 2;
  182. rlBegin(RL_LINES);
  183. for(int i = -halfSlices; i <= halfSlices; i++)
  184. {
  185. if (i == 0)
  186. {
  187. rlColor3f(0.5f, 0.5f, 0.5f);
  188. rlColor3f(0.5f, 0.5f, 0.5f);
  189. rlColor3f(0.5f, 0.5f, 0.5f);
  190. rlColor3f(0.5f, 0.5f, 0.5f);
  191. }
  192. else
  193. {
  194. rlColor3f(0.75f, 0.75f, 0.75f);
  195. rlColor3f(0.75f, 0.75f, 0.75f);
  196. rlColor3f(0.75f, 0.75f, 0.75f);
  197. rlColor3f(0.75f, 0.75f, 0.75f);
  198. }
  199. rlVertex3f((float)i*spacing, 0.0f, (float)-halfSlices*spacing);
  200. rlVertex3f((float)i*spacing, 0.0f, (float)halfSlices*spacing);
  201. rlVertex3f((float)-halfSlices*spacing, 0.0f, (float)i*spacing);
  202. rlVertex3f((float)halfSlices*spacing, 0.0f, (float)i*spacing);
  203. }
  204. rlEnd();
  205. }
  206. // Draw cube
  207. // NOTE: Cube position is the center position
  208. void DrawCube(Vector3 position, float width, float height, float length, Color color)
  209. {
  210. float x = 0.0f;
  211. float y = 0.0f;
  212. float z = 0.0f;
  213. rlPushMatrix();
  214. // NOTE: Be careful! Function order matters (rotate -> scale -> translate)
  215. rlTranslatef(position.x, position.y, position.z);
  216. //rlScalef(2.0f, 2.0f, 2.0f);
  217. //rlRotatef(45, 0, 1, 0);
  218. rlBegin(RL_TRIANGLES);
  219. rlColor4ub(color.r, color.g, color.b, color.a);
  220. // Front Face -----------------------------------------------------
  221. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  222. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  223. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  224. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  225. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  226. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  227. // Back Face ------------------------------------------------------
  228. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  229. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  230. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  231. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  232. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  233. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  234. // Top Face -------------------------------------------------------
  235. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  236. rlVertex3f(x-width/2, y+height/2, z+length/2); // Bottom Left
  237. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  238. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  239. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  240. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  241. // Bottom Face ----------------------------------------------------
  242. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  243. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  244. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  245. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right
  246. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  247. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  248. // Right face -----------------------------------------------------
  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 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 Left
  253. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  254. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  255. // Left Face ------------------------------------------------------
  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 Left
  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 Left
  260. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  261. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right
  262. rlEnd();
  263. rlPopMatrix();
  264. }
  265. // Draw cube wires
  266. void DrawCubeWires(Vector3 position, float width, float height, float length, Color color)
  267. {
  268. float x = 0.0f;
  269. float y = 0.0f;
  270. float z = 0.0f;
  271. rlPushMatrix();
  272. rlTranslatef(position.x, position.y, position.z);
  273. //rlRotatef(45, 0, 1, 0);
  274. rlBegin(RL_LINES);
  275. rlColor4ub(color.r, color.g, color.b, color.a);
  276. // Front Face -----------------------------------------------------
  277. // Bottom Line
  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. // Left Line
  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. // Top Line
  284. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  285. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  286. // Right Line
  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 Left
  289. // Back Face ------------------------------------------------------
  290. // Bottom Line
  291. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  292. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  293. // Left Line
  294. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  295. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  296. // Top Line
  297. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  298. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  299. // Right Line
  300. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  301. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  302. // Top Face -------------------------------------------------------
  303. // Left Line
  304. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left Front
  305. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Back
  306. // Right Line
  307. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right Front
  308. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Back
  309. // Bottom Face ---------------------------------------------------
  310. // Left Line
  311. rlVertex3f(x-width/2, y-height/2, z+length/2); // Top Left Front
  312. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left Back
  313. // Right Line
  314. rlVertex3f(x+width/2, y-height/2, z+length/2); // Top Right Front
  315. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right Back
  316. rlEnd();
  317. rlPopMatrix();
  318. }