Guest User

Untitled

a guest
Aug 12th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <windows.h>
  6. #include <GL/glew.h>
  7. #include <GLFW/glfw3.h>
  8. #include <GLFW/glfw3native.h>
  9. #include <SOIL.h>
  10.  
  11. #include "Kernel/OVR_Math.h"
  12. #include <OVR_CAPI_GL.h>
  13. #include "OVR_CAPI.h"
  14. #include "CRender.h"
  15.  
  16. #include "OvrShader.h" // Header with vertex and fragment shader
  17.  
  18. using namespace OVR;
  19.  
  20. static void error_callback(int error, const char* description)
  21. {
  22. fputs(description, stderr);
  23. }
  24.  
  25. static void key_callback(GLFWwindow* glfwWnd, int key, int scancode, int action, int mods)
  26. {
  27. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  28. glfwSetWindowShouldClose(glfwWnd, GL_TRUE);
  29. }
  30.  
  31. // Render class functions
  32. int oculusVr::ovrInit(int mode)
  33. {
  34. // initialise ovr framework
  35. ovr_Initialize();
  36.  
  37. // create hmd handle
  38. switch (mode) {
  39. case 0:
  40. pHmd = ovrHmd_Create(0);
  41. break;
  42. case 1:
  43. pHmd = ovrHmd_CreateDebug(ovrHmd_DK1);
  44. break;
  45. case 2:
  46. pHmd = ovrHmd_CreateDebug(ovrHmd_DK2);
  47. break;
  48. default:
  49. // if no mode assume dummyDK2
  50. pHmd = ovrHmd_CreateDebug(ovrHmd_DK2);
  51. break;
  52. }
  53.  
  54. if (!pHmd) {
  55. printf("ovrInit() failed");
  56. return NULL;
  57. }
  58.  
  59. // Configure tracking
  60. ovrHmd_ConfigureTracking(pHmd, ovrTrackingCap_Orientation |
  61. ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position, 0);
  62.  
  63. // Configure stereo settings
  64. OVR::Sizei recommenedTex0Size =
  65. ovrHmd_GetFovTextureSize(pHmd, ovrEye_Left, pHmd->DefaultEyeFov[0], 1.0f);
  66. OVR::Sizei recommenedTex1Size =
  67. ovrHmd_GetFovTextureSize(pHmd, ovrEye_Right, pHmd->DefaultEyeFov[1], 1.0f);
  68.  
  69. // Get recomended resolution
  70. recomendedTargetSize.w = recommenedTex0Size.w + recommenedTex1Size.w;
  71. recomendedTargetSize.h = max(recommenedTex0Size.h, recommenedTex1Size.h);
  72.  
  73. return 0;
  74. }
  75.  
  76. int oculusVr::ovrSetParams(int width, int height)
  77. {
  78. // Use recomended resolution unless
  79. if(!(width & height)) {
  80. renderTargetSize.w = recomendedTargetSize.w;
  81. renderTargetSize.h = recomendedTargetSize.h;
  82. } else {
  83. renderTargetSize.w = width;
  84. renderTargetSize.h = height;
  85. }
  86.  
  87. // Config eyeRenderDesc with default FOV only
  88. eyeRenderDesc[0] = ovrHmd_GetRenderDesc(pHmd, ovrEye_Left, pHmd->DefaultEyeFov[0]);
  89. eyeRenderDesc[1] = ovrHmd_GetRenderDesc(pHmd, ovrEye_Right, pHmd->DefaultEyeFov[1]);
  90.  
  91. // Set the viewports based on the recomended resolution
  92. eyeRenderViewport[0].Pos = Vector2i(0, 0);
  93. eyeRenderViewport[0].Size = Sizei(renderTargetSize.w / 2, renderTargetSize.h);
  94. eyeRenderViewport[1].Pos = Vector2i((renderTargetSize.w + 1) / 2, 0);
  95. eyeRenderViewport[1].Size = eyeRenderViewport[0].Size;
  96.  
  97. return 0;
  98. }
  99.  
  100. int oculusVr::ovrMeshVbo()
  101. {
  102. // delete and generate 2 new VBOs
  103. glDeleteBuffers(2, vboId);
  104. glGenBuffers(2, &vboId[0]);
  105.  
  106. // generate 2 IBOs
  107. glDeleteBuffers(2, iboId);
  108. glGenBuffers(2, &iboId[0]);
  109.  
  110. // generate 1 VAO format is the same for 2 VBOs
  111. glDeleteVertexArrays(2, vaoId);
  112. glGenVertexArrays(2, &vaoId[0]);
  113.  
  114. for (unsigned int eyeNum = 0; eyeNum < ovrEye_Count; eyeNum++) {
  115. // Generate distortion mesh vertices for eyeNum
  116. ovrDistortionMesh meshData;
  117. ovrHmd_CreateDistortionMesh(pHmd, eyeRenderDesc[eyeNum].Eye,
  118. eyeRenderDesc[eyeNum].Fov, ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette,
  119. &meshData);
  120.  
  121. // Get render scale and offset for this eyeNum
  122. ovrHmd_GetRenderScaleAndOffset(eyeRenderDesc[eyeNum].Fov,
  123. renderTargetSize, eyeRenderViewport[eyeNum], (ovrVector2f*)UVScaleOffset[eyeNum]);
  124.  
  125. // VBO
  126. glBindBuffer(GL_ARRAY_BUFFER, vboId[eyeNum]);
  127. glBufferData(GL_ARRAY_BUFFER, sizeof(ovrDistortionVertex)*meshData.VertexCount,
  128. meshData.pVertexData, GL_STATIC_DRAW);
  129.  
  130. // IBO
  131. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId[eyeNum]); // for indices
  132. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*meshData.IndexCount,
  133. meshData.pIndexData, GL_STATIC_DRAW);
  134.  
  135. // VAO
  136. // Set vertex pointer to address of eyeNum's vertex data in mesh
  137. ovrDistortionVertex *pData = meshData.pVertexData;
  138. glBindVertexArray(vaoId[eyeNum]);
  139.  
  140. // TimeWarpFactor and VignetteFade encoded into Pos.z and Pos.w
  141. glEnableVertexAttribArray(0);
  142. glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex),
  143. (GLvoid*)offsetof(ovrDistortionVertex, ScreenPosNDC));
  144.  
  145. glEnableVertexAttribArray(1);
  146. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex),
  147. (GLvoid*)offsetof(ovrDistortionVertex, TanEyeAnglesR));
  148.  
  149. glEnableVertexAttribArray(2);
  150. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex),
  151. (GLvoid*)offsetof(ovrDistortionVertex, TanEyeAnglesG));
  152.  
  153. glEnableVertexAttribArray(3);
  154. glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex),
  155. (GLvoid*)offsetof(ovrDistortionVertex, TanEyeAnglesB));
  156.  
  157. glBindVertexArray(0);
  158.  
  159. // store total index count
  160. indexCount[eyeNum] = meshData.IndexCount;
  161.  
  162. // destroy the mesh
  163. ovrHmd_DestroyDistortionMesh(&meshData);
  164. }
  165.  
  166. return 0;
  167. }
  168.  
  169. int oculusVr::ovrDeinit()
  170. {
  171. if (pHmd) {
  172. ovrHmd_Destroy(pHmd);
  173. }
  174. ovr_Shutdown();
  175. return 0;
  176. }
  177.  
  178. int oculusVr::oglRender()
  179. {
  180. // use vertex and fragment shader
  181. glUseProgram(shaderProg);
  182.  
  183. while (!glfwWindowShouldClose(glfwWindow) ) {
  184.  
  185. // Perform operations for window 0
  186. glfwMakeContextCurrent(glfwWindow);
  187. glClear(GL_COLOR_BUFFER_BIT);
  188.  
  189. // Draw left Eye
  190. glBindVertexArray(vaoId[0]);
  191. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId[0]);
  192.  
  193. glUniform2f(uvScale, UVScaleOffset[0][0].x, UVScaleOffset[0][0].y);
  194. glUniform2f(uvOffset, UVScaleOffset[0][1].x, UVScaleOffset[0][1].y);
  195. glDrawElements(GL_LINES, indexCount[0], GL_UNSIGNED_SHORT, 0);
  196.  
  197. // Draw right Eye
  198. glBindVertexArray(vaoId[1]);
  199. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId[1]);
  200.  
  201. glUniform2f(uvScale, UVScaleOffset[1][0].x, UVScaleOffset[1][0].y);
  202. glUniform2f(uvOffset, UVScaleOffset[1][1].x, UVScaleOffset[1][1].y);
  203. glDrawElements(GL_TRIANGLES, indexCount[1], GL_UNSIGNED_SHORT, 0);
  204.  
  205. glfwSwapBuffers(glfwWindow);
  206.  
  207. // Check if 'esc' has been pressed
  208. glfwPollEvents();
  209. }
  210. return 0;
  211. }
  212.  
  213. int oculusVr::oglInit()
  214. {
  215. glfwSetErrorCallback(error_callback);
  216.  
  217. // Intialize GLFW
  218. if (!glfwInit()) {
  219. getchar();
  220. exit(EXIT_FAILURE);
  221. }
  222.  
  223. // Make undercorated
  224. glfwWindowHint(GLFW_DECORATED, 0);
  225.  
  226. glfwWindow = glfwCreateWindow(renderTargetSize.w, renderTargetSize.h, "OVR - Mesh debug", NULL, NULL);
  227.  
  228. if (!glfwWindow) {
  229. glfwTerminate();
  230. getchar();
  231. exit(EXIT_FAILURE);
  232. }
  233.  
  234. glfwMakeContextCurrent(glfwWindow);
  235. glfwSetKeyCallback(glfwWindow, key_callback);
  236.  
  237. glewExperimental = true; // Needed for core profile
  238.  
  239. ovrHmd_AttachToWindow(pHmd, glfwGetWin32Window(glfwWindow), NULL, NULL);
  240.  
  241. // Initialize GLEW
  242. if (glewInit() != GLEW_OK) {
  243. fprintf(stderr, "Failed to initialize GLEW\n");
  244. getchar();
  245. return -1;
  246. }
  247.  
  248. // get version info
  249. const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
  250. const GLubyte* version = glGetString(GL_VERSION); // version as a string
  251. printf("Renderer: %s\n", renderer);
  252. printf("OpenGL version supported %s\n", version);
  253.  
  254. return 0;
  255. }
  256.  
  257. int oculusVr::oglDeinit()
  258. {
  259. // destroy both windows
  260. glfwDestroyWindow(glfwWindow);
  261. glfwTerminate();
  262. return 0;
  263. }
  264.  
  265. int oculusVr::loadShader()
  266. {
  267. GLint length, result;
  268. GLuint vShaderId, fShaderId;
  269.  
  270. shaderProg = glCreateProgram();
  271.  
  272. // Bind the attributes
  273. glBindAttribLocation(shaderProg, 0, "Position");
  274. glBindAttribLocation(shaderProg, 1, "TexCoord0");
  275. glBindAttribLocation(shaderProg, 2, "TexCoord1");
  276. glBindAttribLocation(shaderProg, 3, "TexCoord2");
  277.  
  278. // vertexShader
  279. vShaderId = glCreateShader(GL_VERTEX_SHADER);
  280. length = strlen(vertexShader);
  281. glShaderSource(vShaderId, 1, &vertexShader, &length);
  282. glCompileShader(vShaderId);
  283.  
  284. // make sure the vertexShader compilation was successful
  285. glGetShaderiv(vShaderId, GL_COMPILE_STATUS, &result);
  286. if(result == GL_FALSE) {
  287. char *log;
  288.  
  289. // get the shader info log
  290. glGetShaderiv(vShaderId, GL_INFO_LOG_LENGTH, &length);
  291. log = (char*)malloc(length);
  292. glGetShaderInfoLog(vShaderId, length, &result, log);
  293.  
  294. // print an error message and the info log
  295. fprintf(stderr, "shaderCompileFromFile(): Unable to compile vShader: %s\n", log);
  296. free(log);
  297.  
  298. glDeleteShader(vShaderId);
  299. return 0;
  300. }
  301. // Attach vertexShader
  302. glAttachShader(shaderProg, vShaderId);
  303. glDeleteShader(vShaderId);
  304.  
  305. // fragmentShader
  306. fShaderId = glCreateShader(GL_FRAGMENT_SHADER);
  307. length = strlen(fragmentShader);
  308. glShaderSource(fShaderId, 1, &fragmentShader, &length);
  309. glCompileShader(fShaderId);
  310.  
  311. // make sure the fragmentShader compilation was successful
  312. glGetShaderiv(fShaderId, GL_COMPILE_STATUS, &result);
  313. if(result == GL_FALSE) {
  314. char *log;
  315.  
  316. // get the shader info log
  317. glGetShaderiv(vShaderId, GL_INFO_LOG_LENGTH, &length);
  318. log = (char*)malloc(length);
  319. glGetShaderInfoLog(fShaderId, length, &result, log);
  320.  
  321. // print an error message and the info log
  322. fprintf(stderr, "shaderCompileFromFile(): Unable to compile fShader: %s\n", log);
  323. free(log);
  324.  
  325. glDeleteShader(fShaderId);
  326. return 0;
  327. }
  328.  
  329. // attach fragmentShader
  330. glAttachShader(shaderProg, fShaderId);
  331. glDeleteShader(fShaderId);
  332.  
  333.  
  334. // Link the program
  335. glLinkProgram(shaderProg);
  336. glGetProgramiv(shaderProg, GL_LINK_STATUS, &result);
  337. if(result == GL_FALSE) {
  338. GLint length;
  339. char *log;
  340.  
  341. // get the program info log
  342. glGetProgramiv(shaderProg, GL_INFO_LOG_LENGTH, &length);
  343. log = (char*)malloc(length);
  344. glGetProgramInfoLog(shaderProg, length, &result, log);
  345.  
  346. // print an error message and the info log
  347. fprintf(stderr, "sceneInit(): Program linking failed: %s\n", log);
  348. free(log);
  349.  
  350. /* delete the program */
  351. glDeleteProgram(shaderProg);
  352. shaderProg = 0;
  353. return 0;
  354. }
  355.  
  356. // setup uniforms
  357. uvScale = glGetUniformLocation(shaderProg, "EyeToSourceUVScale");
  358. uvOffset = glGetUniformLocation(shaderProg, "EyeToSourceUVOffset");
  359.  
  360. return 0;
  361. };
  362.  
  363. int oculusVr::loadTexture()
  364. {
  365. int width, height;
  366. unsigned char* image;
  367.  
  368. // Enable context for window 0
  369. glfwMakeContextCurrent(glfwWindow);
  370.  
  371. // Load texture
  372. GLuint tex1;
  373. glGenTextures(1, &tex1);
  374.  
  375. glActiveTexture(GL_TEXTURE0);
  376. glBindTexture(GL_TEXTURE_2D, tex1);
  377. image = SOIL_load_image("rgb.png", &width, &height, 0, SOIL_LOAD_RGB);
  378. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  379. SOIL_free_image_data(image);
  380.  
  381. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  382. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  383. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  384. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  385.  
  386. return 0;
  387. }
Advertisement
Add Comment
Please, Sign In to add comment