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.

392 lines
16 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [rlgl] example - Oculus minimum sample
  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 -DRLGL_OCULUS_SUPPORT
  10. *
  11. * NOTE 1: 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. * NOTE 2: rlgl requires LibOVR (Oculus PC SDK) to support Oculus Rift CV1
  18. *
  19. * Compile example using:
  20. * gcc -o rlgl_oculus_rift.exe rlgl_oculus_rift.c rlgl.o -L. -lLibOVRRT32_1 -lglfw3 -lopengl32 -lgdi32 -std=c99
  21. *
  22. * NOTE: Example must be linked against LibOVRRT32_1.dll that comes with Oculus Rift runtime.
  23. *
  24. * This example has been created using raylib 1.5 (www.raylib.com)
  25. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  26. *
  27. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  28. *
  29. ********************************************************************************************/
  30. #include <stdlib.h>
  31. #include <stdarg.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <math.h>
  35. #include <GLFW/glfw3.h> // Windows/Context and inputs management
  36. #define RLGL_STANDALONE
  37. #include "rlgl.h" // rlgl library: OpenGL 1.1 immediate-mode style coding
  38. #define RED (Color){ 230, 41, 55, 255 } // Red
  39. #define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
  40. #define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
  41. //----------------------------------------------------------------------------------
  42. // Module specific Functions Declaration
  43. //----------------------------------------------------------------------------------
  44. static void ErrorCallback(int error, const char* description);
  45. static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
  46. // Drawing functions (uses rlgl functionality)
  47. static void DrawGrid(int slices, float spacing);
  48. static void DrawCube(Vector3 position, float width, float height, float length, Color color);
  49. static void DrawCubeWires(Vector3 position, float width, float height, float length, Color color);
  50. static void DrawRectangleV(Vector2 position, Vector2 size, Color color);
  51. //----------------------------------------------------------------------------------
  52. // Main Entry point
  53. //----------------------------------------------------------------------------------
  54. int main(void)
  55. {
  56. // Initialization
  57. //--------------------------------------------------------------------------------------
  58. int screenWidth = 1080; // Mirror screen width (set to hmdDesc.Resolution.w/2)
  59. int screenHeight = 600; // Mirror screen height (set to hmdDesc.Resolution.h/2)
  60. // NOTE: Mirror screen size can be set to any desired resolution!
  61. // GLFW3 Initialization + OpenGL 3.3 Context + Extensions
  62. //--------------------------------------------------------
  63. glfwSetErrorCallback(ErrorCallback);
  64. if (!glfwInit())
  65. {
  66. TraceLog(WARNING, "GLFW3: Can not initialize GLFW");
  67. return 1;
  68. }
  69. else TraceLog(INFO, "GLFW3: GLFW initialized successfully");
  70. glfwWindowHint(GLFW_SAMPLES, 4);
  71. glfwWindowHint(GLFW_DEPTH_BITS, 16);
  72. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  73. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  74. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  75. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
  76. GLFWwindow *window = glfwCreateWindow(screenWidth, screenHeight, "rlgl oculus rift", NULL, NULL);
  77. if (!window)
  78. {
  79. glfwTerminate();
  80. return 2;
  81. }
  82. else TraceLog(INFO, "GLFW3: Window created successfully");
  83. glfwSetKeyCallback(window, KeyCallback);
  84. glfwMakeContextCurrent(window);
  85. glfwSwapInterval(0);
  86. // Load OpenGL 3.3 supported extensions
  87. rlglLoadExtensions(glfwGetProcAddress);
  88. //--------------------------------------------------------
  89. // Initialize OpenGL context (states and resources)
  90. rlglInit(screenWidth, screenHeight);
  91. rlClearColor(245, 245, 245, 255); // Define clear color
  92. rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
  93. // Define custom camera to initialize projection and view matrices
  94. Camera camera;
  95. camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
  96. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  97. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  98. camera.fovy = 45.0f; // Camera field-of-view Y
  99. // Initialize viewport and internal projection/modelview matrices
  100. rlViewport(0, 0, screenWidth, screenHeight);
  101. rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
  102. rlLoadIdentity(); // Reset current matrix (PROJECTION)
  103. // Setup perspective projection
  104. float aspect = (float)screenWidth/(float)screenHeight;
  105. double top = 0.01*tan(camera.fovy*PI/360.0);
  106. double right = top*aspect;
  107. rlFrustum(-right, right, -top, top, 0.01, 1000.0);
  108. rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
  109. rlLoadIdentity(); // Reset current matrix (MODELVIEW)
  110. // Setup Camera view
  111. Matrix cameraView = MatrixLookAt(camera.position, camera.target, camera.up);
  112. rlMultMatrixf(MatrixToFloat(cameraView)); // Multiply MODELVIEW matrix by view matrix (camera)
  113. InitOculusDevice(); // Initialize Oculus Rift CV1
  114. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  115. //--------------------------------------------------------------------------------------
  116. // Main game loop
  117. while (!glfwWindowShouldClose(window))
  118. {
  119. // Update
  120. //----------------------------------------------------------------------------------
  121. UpdateOculusTracking(&camera);
  122. //----------------------------------------------------------------------------------
  123. // Draw
  124. //----------------------------------------------------------------------------------
  125. BeginOculusDrawing();
  126. rlClearScreenBuffers(); // Clear current framebuffer(s)
  127. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  128. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, RAYWHITE);
  129. DrawGrid(10, 1.0f);
  130. // NOTE: Internal buffers drawing (3D data)
  131. rlglDraw();
  132. EndOculusDrawing();
  133. glfwSwapBuffers(window);
  134. glfwPollEvents();
  135. //----------------------------------------------------------------------------------
  136. }
  137. // De-Initialization
  138. //--------------------------------------------------------------------------------------
  139. CloseOculusDevice(); // Close Oculus device and clear resources
  140. rlglClose(); // Unload rlgl internal buffers and default shader/texture
  141. glfwDestroyWindow(window); // Close window
  142. glfwTerminate(); // Free GLFW3 resources
  143. //--------------------------------------------------------------------------------------
  144. return 0;
  145. }
  146. //----------------------------------------------------------------------------------
  147. // Module specific Functions Definitions
  148. //----------------------------------------------------------------------------------
  149. // GLFW3: Error callback
  150. static void ErrorCallback(int error, const char* description)
  151. {
  152. TraceLog(ERROR, description);
  153. }
  154. // GLFW3: Keyboard callback
  155. static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
  156. {
  157. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  158. {
  159. glfwSetWindowShouldClose(window, GL_TRUE);
  160. }
  161. }
  162. // Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
  163. static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
  164. {
  165. rlBegin(RL_TRIANGLES);
  166. rlColor4ub(color.r, color.g, color.b, color.a);
  167. rlVertex2i(position.x, position.y);
  168. rlVertex2i(position.x, position.y + size.y);
  169. rlVertex2i(position.x + size.x, position.y + size.y);
  170. rlVertex2i(position.x, position.y);
  171. rlVertex2i(position.x + size.x, position.y + size.y);
  172. rlVertex2i(position.x + size.x, position.y);
  173. rlEnd();
  174. }
  175. // Draw a grid centered at (0, 0, 0)
  176. static void DrawGrid(int slices, float spacing)
  177. {
  178. int halfSlices = slices / 2;
  179. rlBegin(RL_LINES);
  180. for(int i = -halfSlices; i <= halfSlices; i++)
  181. {
  182. if (i == 0)
  183. {
  184. rlColor3f(0.5f, 0.5f, 0.5f);
  185. rlColor3f(0.5f, 0.5f, 0.5f);
  186. rlColor3f(0.5f, 0.5f, 0.5f);
  187. rlColor3f(0.5f, 0.5f, 0.5f);
  188. }
  189. else
  190. {
  191. rlColor3f(0.75f, 0.75f, 0.75f);
  192. rlColor3f(0.75f, 0.75f, 0.75f);
  193. rlColor3f(0.75f, 0.75f, 0.75f);
  194. rlColor3f(0.75f, 0.75f, 0.75f);
  195. }
  196. rlVertex3f((float)i*spacing, 0.0f, (float)-halfSlices*spacing);
  197. rlVertex3f((float)i*spacing, 0.0f, (float)halfSlices*spacing);
  198. rlVertex3f((float)-halfSlices*spacing, 0.0f, (float)i*spacing);
  199. rlVertex3f((float)halfSlices*spacing, 0.0f, (float)i*spacing);
  200. }
  201. rlEnd();
  202. }
  203. // Draw cube
  204. // NOTE: Cube position is the center position
  205. void DrawCube(Vector3 position, float width, float height, float length, Color color)
  206. {
  207. float x = 0.0f;
  208. float y = 0.0f;
  209. float z = 0.0f;
  210. rlPushMatrix();
  211. // NOTE: Be careful! Function order matters (rotate -> scale -> translate)
  212. rlTranslatef(position.x, position.y, position.z);
  213. //rlScalef(2.0f, 2.0f, 2.0f);
  214. //rlRotatef(45, 0, 1, 0);
  215. rlBegin(RL_TRIANGLES);
  216. rlColor4ub(color.r, color.g, color.b, color.a);
  217. // Front Face -----------------------------------------------------
  218. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  219. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  220. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  221. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  222. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  223. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  224. // Back 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); // Top Left
  227. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  228. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  229. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  230. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  231. // Top Face -------------------------------------------------------
  232. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  233. rlVertex3f(x-width/2, y+height/2, z+length/2); // Bottom 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); // Top Left
  237. rlVertex3f(x+width/2, y+height/2, z+length/2); // Bottom Right
  238. // Bottom 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 Right
  241. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  242. rlVertex3f(x+width/2, y-height/2, z-length/2); // Top Right
  243. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  244. rlVertex3f(x-width/2, y-height/2, z-length/2); // Top Left
  245. // Right face -----------------------------------------------------
  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 Right
  248. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Left
  249. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Left
  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. // Left 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 Left
  255. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Right
  256. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  257. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  258. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Right
  259. rlEnd();
  260. rlPopMatrix();
  261. }
  262. // Draw cube wires
  263. void DrawCubeWires(Vector3 position, float width, float height, float length, Color color)
  264. {
  265. float x = 0.0f;
  266. float y = 0.0f;
  267. float z = 0.0f;
  268. rlPushMatrix();
  269. rlTranslatef(position.x, position.y, position.z);
  270. //rlRotatef(45, 0, 1, 0);
  271. rlBegin(RL_LINES);
  272. rlColor4ub(color.r, color.g, color.b, color.a);
  273. // Front Face -----------------------------------------------------
  274. // Bottom Line
  275. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  276. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  277. // Left Line
  278. rlVertex3f(x+width/2, y-height/2, z+length/2); // Bottom Right
  279. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  280. // Top Line
  281. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right
  282. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  283. // Right Line
  284. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left
  285. rlVertex3f(x-width/2, y-height/2, z+length/2); // Bottom Left
  286. // Back Face ------------------------------------------------------
  287. // Bottom Line
  288. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  289. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  290. // Left Line
  291. rlVertex3f(x+width/2, y-height/2, z-length/2); // Bottom Right
  292. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right
  293. // Top Line
  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. // Right Line
  297. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left
  298. rlVertex3f(x-width/2, y-height/2, z-length/2); // Bottom Left
  299. // Top Face -------------------------------------------------------
  300. // Left Line
  301. rlVertex3f(x-width/2, y+height/2, z+length/2); // Top Left Front
  302. rlVertex3f(x-width/2, y+height/2, z-length/2); // Top Left Back
  303. // Right Line
  304. rlVertex3f(x+width/2, y+height/2, z+length/2); // Top Right Front
  305. rlVertex3f(x+width/2, y+height/2, z-length/2); // Top Right Back
  306. // Bottom 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. rlEnd();
  314. rlPopMatrix();
  315. }