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.

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