main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * OpenHMD - Free and Open Source API and drivers for immersive technology.
  3. * Copyright (C) 2013 Fredrik Hultin.
  4. * Copyright (C) 2013 Jakob Bornecrantz.
  5. * Distributed under the Boost 1.0 licence, see LICENSE for full text.
  6. */
  7. /* OpenGL Test - Main Implementation */
  8. #include <openhmd.h>
  9. #include <stdbool.h>
  10. #include <assert.h>
  11. #include <math.h>
  12. #include "gl.h"
  13. #define OVERSAMPLE_SCALE 2.0
  14. char* read_file(const char* filename)
  15. {
  16. FILE* f = fopen(filename, "rb");
  17. fseek(f, 0, SEEK_END);
  18. long len = ftell(f);
  19. fseek(f, 0, SEEK_SET);
  20. char* buffer = calloc(1, len + 1);
  21. assert(buffer);
  22. size_t ret = fread(buffer, len, 1, f);
  23. assert(ret);
  24. fclose(f);
  25. return buffer;
  26. }
  27. float randf()
  28. {
  29. return (float)rand() / (float)RAND_MAX;
  30. }
  31. GLuint gen_cubes()
  32. {
  33. GLuint list = glGenLists(1);
  34. // Set the random seed.
  35. srand(42);
  36. glNewList(list, GL_COMPILE);
  37. for(float a = 0.0f; a < 360.0f; a += 20.0f){
  38. glPushMatrix();
  39. glRotatef(a, 0, 1, 0);
  40. glTranslatef(0, 0, -1);
  41. glScalef(0.2, 0.2, 0.2);
  42. glRotatef(randf() * 360, randf(), randf(), randf());
  43. glColor4f(randf(), randf(), randf(), randf() * .5f + .5f);
  44. draw_cube();
  45. glPopMatrix();
  46. }
  47. // draw floor
  48. glColor4f(0, 1.0f, .25f, .25f);
  49. glTranslatef(0, -2.5f, 0);
  50. draw_cube();
  51. glEndList();
  52. return list;
  53. }
  54. void draw_scene(GLuint list)
  55. {
  56. // draw cubes
  57. glCallList(list);
  58. }
  59. static inline void
  60. print_matrix(float m[])
  61. {
  62. printf("[[%0.4f, %0.4f, %0.4f, %0.4f],\n"
  63. "[%0.4f, %0.4f, %0.4f, %0.4f],\n"
  64. "[%0.4f, %0.4f, %0.4f, %0.4f],\n"
  65. "[%0.4f, %0.4f, %0.4f, %0.4f]]\n",
  66. m[0], m[4], m[8], m[12],
  67. m[1], m[5], m[9], m[13],
  68. m[2], m[6], m[10], m[14],
  69. m[3], m[7], m[11], m[15]);
  70. }
  71. int main(int argc, char** argv)
  72. {
  73. int hmd_w, hmd_h;
  74. ohmd_context* ctx = ohmd_ctx_create();
  75. int num_devices = ohmd_ctx_probe(ctx);
  76. if(num_devices < 0){
  77. printf("failed to probe devices: %s\n", ohmd_ctx_get_error(ctx));
  78. return 1;
  79. }
  80. ohmd_device_settings* settings = ohmd_device_settings_create(ctx);
  81. // If OHMD_IDS_AUTOMATIC_UPDATE is set to 0, ohmd_ctx_update() must be called at least 10 times per second.
  82. // It is enabled by default.
  83. int auto_update = 1;
  84. ohmd_device_settings_seti(settings, OHMD_IDS_AUTOMATIC_UPDATE, &auto_update);
  85. ohmd_device* hmd = ohmd_list_open_device_s(ctx, 0, settings);
  86. ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, &hmd_w);
  87. ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, &hmd_h);
  88. float viewport_scale[2];
  89. float distortion_coeffs[4];
  90. float aberr_scale[3];
  91. float sep;
  92. float left_lens_center[2];
  93. float right_lens_center[2];
  94. //viewport is half the screen
  95. ohmd_device_getf(hmd, OHMD_SCREEN_HORIZONTAL_SIZE, &(viewport_scale[0]));
  96. viewport_scale[0] /= 2.0f;
  97. ohmd_device_getf(hmd, OHMD_SCREEN_VERTICAL_SIZE, &(viewport_scale[1]));
  98. //distortion coefficients
  99. ohmd_device_getf(hmd, OHMD_UNIVERSAL_DISTORTION_K, &(distortion_coeffs[0]));
  100. ohmd_device_getf(hmd, OHMD_UNIVERSAL_ABERRATION_K, &(aberr_scale[0]));
  101. //calculate lens centers (assuming the eye separation is the distance betweenteh lense centers)
  102. ohmd_device_getf(hmd, OHMD_LENS_HORIZONTAL_SEPARATION, &sep);
  103. ohmd_device_getf(hmd, OHMD_LENS_VERTICAL_POSITION, &(left_lens_center[1]));
  104. ohmd_device_getf(hmd, OHMD_LENS_VERTICAL_POSITION, &(right_lens_center[1]));
  105. left_lens_center[0] = viewport_scale[0] - sep/2.0f;
  106. right_lens_center[0] = sep/2.0f;
  107. //asume calibration was for lens view to which ever edge of screen is further away from lens center
  108. float warp_scale = (left_lens_center[0] > right_lens_center[0]) ? left_lens_center[0] : right_lens_center[0];
  109. ohmd_device_settings_destroy(settings);
  110. if(!hmd){
  111. printf("failed to open device: %s\n", ohmd_ctx_get_error(ctx));
  112. return 1;
  113. }
  114. gl_ctx gl;
  115. init_gl(&gl, hmd_w, hmd_h);
  116. SDL_ShowCursor(SDL_DISABLE);
  117. const char* vertex;
  118. ohmd_gets(OHMD_GLSL_DISTORTION_VERT_SRC, &vertex);
  119. const char* fragment;
  120. ohmd_gets(OHMD_GLSL_DISTORTION_FRAG_SRC, &fragment);
  121. GLuint shader = compile_shader(vertex, fragment);
  122. glUseProgram(shader);
  123. glUniform1i(glGetUniformLocation(shader, "warpTexture"), 0);
  124. glUniform2fv(glGetUniformLocation(shader, "ViewportScale"), 1, viewport_scale);
  125. glUniform1f(glGetUniformLocation(shader, "WarpScale"), warp_scale);
  126. glUniform4fv(glGetUniformLocation(shader, "HmdWarpParam"), 1, distortion_coeffs);
  127. glUniform3fv(glGetUniformLocation(shader, "aberr"), 1, aberr_scale);
  128. glUseProgram(0);
  129. GLuint list = gen_cubes();
  130. int eye_w = hmd_w/2*OVERSAMPLE_SCALE;
  131. int eye_h = hmd_h*OVERSAMPLE_SCALE;
  132. GLuint left_color_tex = 0, left_depth_tex = 0, left_fbo = 0;
  133. create_fbo(eye_w, eye_h, &left_fbo, &left_color_tex, &left_depth_tex);
  134. GLuint right_color_tex = 0, right_depth_tex = 0, right_fbo = 0;
  135. create_fbo(eye_w, eye_h, &right_fbo, &right_color_tex, &right_depth_tex);
  136. bool done = false;
  137. while(!done){
  138. ohmd_ctx_update(ctx);
  139. SDL_Event event;
  140. while(SDL_PollEvent(&event)){
  141. if(event.type == SDL_KEYDOWN){
  142. switch(event.key.keysym.sym){
  143. case SDLK_ESCAPE:
  144. done = true;
  145. break;
  146. case SDLK_F1:
  147. SDL_WM_ToggleFullScreen(gl.screen);
  148. break;
  149. case SDLK_F2:
  150. {
  151. // reset rotation and position
  152. float zero[] = {0, 0, 0, 1};
  153. ohmd_device_setf(hmd, OHMD_ROTATION_QUAT, zero);
  154. ohmd_device_setf(hmd, OHMD_POSITION_VECTOR, zero);
  155. }
  156. break;
  157. case SDLK_F3:
  158. {
  159. float mat[16];
  160. ohmd_device_getf(hmd, OHMD_LEFT_EYE_GL_PROJECTION_MATRIX, mat);
  161. printf("Projection L: ");
  162. print_matrix(mat);
  163. printf("\n");
  164. ohmd_device_getf(hmd, OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX, mat);
  165. printf("Projection R: ");
  166. print_matrix(mat);
  167. printf("\n");
  168. ohmd_device_getf(hmd, OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX, mat);
  169. printf("View: ");
  170. print_matrix(mat);
  171. printf("\n");
  172. printf("viewport_scale: [%0.4f, %0.4f]\n", viewport_scale[0], viewport_scale[1]);
  173. printf("warp_scale: %0.4f\r\n", warp_scale);
  174. printf("distoriton coeffs: [%0.4f, %0.4f, %0.4f, %0.4f]\n", distortion_coeffs[0], distortion_coeffs[1], distortion_coeffs[2], distortion_coeffs[3]);
  175. printf("aberration coeffs: [%0.4f, %0.4f, %0.4f]\n", aberr_scale[0], aberr_scale[1], aberr_scale[2]);
  176. printf("left_lens_center: [%0.4f, %0.4f]\n", left_lens_center[0], left_lens_center[1]);
  177. printf("right_lens_center: [%0.4f, %0.4f]\n", left_lens_center[0], left_lens_center[1]);
  178. }
  179. break;
  180. default:
  181. break;
  182. }
  183. }
  184. }
  185. // Common scene state
  186. glEnable(GL_BLEND);
  187. glEnable(GL_DEPTH_TEST);
  188. float matrix[16];
  189. // set hmd rotation, for left eye.
  190. glMatrixMode(GL_PROJECTION);
  191. ohmd_device_getf(hmd, OHMD_LEFT_EYE_GL_PROJECTION_MATRIX, matrix);
  192. glLoadMatrixf(matrix);
  193. glMatrixMode(GL_MODELVIEW);
  194. ohmd_device_getf(hmd, OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX, matrix);
  195. glLoadMatrixf(matrix);
  196. // Draw scene into framebuffer.
  197. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, left_fbo);
  198. glViewport(0, 0, eye_w, eye_h);
  199. glClearColor(0.0, 0.0, 0.0, 1.0);
  200. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  201. draw_scene(list);
  202. // set hmd rotation, for right eye.
  203. glMatrixMode(GL_PROJECTION);
  204. ohmd_device_getf(hmd, OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX, matrix);
  205. glLoadMatrixf(matrix);
  206. glMatrixMode(GL_MODELVIEW);
  207. ohmd_device_getf(hmd, OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX, matrix);
  208. glLoadMatrixf(matrix);
  209. // Draw scene into framebuffer.
  210. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, right_fbo);
  211. glViewport(0, 0, eye_w, eye_h);
  212. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  213. draw_scene(list);
  214. // Clean up common draw state
  215. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  216. glDisable(GL_BLEND);
  217. glDisable(GL_DEPTH_TEST);
  218. // Setup ortho state.
  219. glUseProgram(shader);
  220. glViewport(0, 0, hmd_w, hmd_h);
  221. glEnable(GL_TEXTURE_2D);
  222. glColor4d(1, 1, 1, 1);
  223. // Setup simple render state
  224. glMatrixMode(GL_PROJECTION);
  225. glLoadIdentity();
  226. glMatrixMode(GL_MODELVIEW);
  227. glLoadIdentity();
  228. // Draw left eye
  229. glUniform2fv(glGetUniformLocation(shader, "LensCenter"), 1, left_lens_center);
  230. glBindTexture(GL_TEXTURE_2D, left_color_tex);
  231. glBegin(GL_QUADS);
  232. glTexCoord2d( 0, 0);
  233. glVertex3d( -1, -1, 0);
  234. glTexCoord2d( 1, 0);
  235. glVertex3d( 0, -1, 0);
  236. glTexCoord2d( 1, 1);
  237. glVertex3d( 0, 1, 0);
  238. glTexCoord2d( 0, 1);
  239. glVertex3d( -1, 1, 0);
  240. glEnd();
  241. // Draw right eye
  242. glUniform2fv(glGetUniformLocation(shader, "LensCenter"), 1, right_lens_center);
  243. glBindTexture(GL_TEXTURE_2D, right_color_tex);
  244. glBegin(GL_QUADS);
  245. glTexCoord2d( 0, 0);
  246. glVertex3d( 0, -1, 0);
  247. glTexCoord2d( 1, 0);
  248. glVertex3d( 1, -1, 0);
  249. glTexCoord2d( 1, 1);
  250. glVertex3d( 1, 1, 0);
  251. glTexCoord2d( 0, 1);
  252. glVertex3d( 0, 1, 0);
  253. glEnd();
  254. // Clean up state.
  255. glBindTexture(GL_TEXTURE_2D, 0);
  256. glDisable(GL_TEXTURE_2D);
  257. glUseProgram(0);
  258. // Da swap-dawup!
  259. SDL_GL_SwapBuffers();
  260. SDL_Delay(10);
  261. }
  262. ohmd_ctx_destroy(ctx);
  263. return 0;
  264. }