# HG changeset patch # User John Tsiombikas # Date 1409067773 -10800 # Node ID 782ff06817fb92ba7e1bb283c56f0d53eb64ba3f # Parent c5054e0cd564f4c5695377fcd3bf605f23eb2dee# Parent c814f77d177e09db58db9b2f11ce41a89784e681 merged ... diff -r c5054e0cd564 -r 782ff06817fb Makefile --- a/Makefile Tue Aug 26 18:40:23 2014 +0300 +++ b/Makefile Tue Aug 26 18:42:53 2014 +0300 @@ -11,13 +11,13 @@ ohmd_cflags = -DUSE_OPENHMD ohmd_libs = -lopenhmd -CFLAGS = -pedantic -Wall -g $(ovr_cflags) +CFLAGS = -pedantic -Wall -g $(ovr_cflags) `pkg-config --cflags sdl2` CXXFLAGS = -std=c++11 $(CFLAGS) -LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago -lanim $(ovr_libs) +LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago -lanim $(ovr_libs) `pkg-config --libs sdl2` -libgl_unix = -lGL -lGLU -lglut -lGLEW -libgl_mac = -framework OpenGL -framework GLUT -lGLEW -libgl_win = -lopengl32 -lglu32 -lglut32 -lglew32 +libgl_unix = -lGL -lGLU -lGLEW +libgl_mac = -framework OpenGL -lGLEW +libgl_win = -lopengl32 -lglu32 -lglew32 $(bin): $(obj) $(CXX) -o $@ $(obj) $(LDFLAGS) diff -r c5054e0cd564 -r 782ff06817fb src/game.cc --- a/src/game.cc Tue Aug 26 18:40:23 2014 +0300 +++ b/src/game.cc Tue Aug 26 18:42:53 2014 +0300 @@ -176,7 +176,7 @@ setup_rtarg(rtwidth, rtheight); } -void game_keyboard(int key, bool pressed, int x, int y) +void game_keyboard(int key, bool pressed) { if(pressed) { switch(key) { @@ -338,12 +338,12 @@ } static void draw_pyramid(float basesz, float height) -{ - float hsz = basesz / 2.0; - float theta = atan(hsz / height); - float nx = cos(theta); - float ny = sin(theta); - +{ + float hsz = basesz / 2.0; + float theta = atan(hsz / height); + float nx = cos(theta); + float ny = sin(theta); + glBegin(GL_TRIANGLES); glNormal3f(0, ny, nx); glVertex3f(-hsz, 0, hsz); @@ -362,4 +362,4 @@ glVertex3f(-hsz, 0, hsz); glVertex3f(0, height, 0); glEnd(); -} \ No newline at end of file +} diff -r c5054e0cd564 -r 782ff06817fb src/game.h --- a/src/game.h Tue Aug 26 18:40:23 2014 +0300 +++ b/src/game.h Tue Aug 26 18:42:53 2014 +0300 @@ -8,7 +8,7 @@ void game_render(); void game_reshape(int x, int y); -void game_keyboard(int key, bool pressed, int x, int y); +void game_keyboard(int key, bool pressed); void game_mouse(int bn, bool pressed, int x, int y); void game_motion(int x, int y); void game_mwheel(int dir); diff -r c5054e0cd564 -r 782ff06817fb src/glut/main.cc --- a/src/glut/main.cc Tue Aug 26 18:40:23 2014 +0300 +++ b/src/glut/main.cc Tue Aug 26 18:42:53 2014 +0300 @@ -1,53 +1,64 @@ #include #include +#include #include "opengl.h" #include "game.h" #include "gameopt.h" #include "vr/vr.h" +#define ANYPOS SDL_WINDOWPOS_UNDEFINED + static bool init(); static void cleanup(); +static void handle_event(SDL_Event *ev); -static void display(); -static void idle(); -static void reshape(int x, int y); -static void keyb(unsigned char key, int x, int y); -static void keyb_up(unsigned char key, int x, int y); -static void mouse(int bn, int st, int x, int y); -static void motion(int x, int y); -static void sball_motion(int x, int y, int z); -static void sball_rotate(int x, int y, int z); - -static bool fullscreen_pending; +static SDL_Window *win; +static SDL_GLContext ctx; int main(int argc, char **argv) { - glutInitWindowSize(1024, 600); - glutInit(&argc, argv); - if(!parse_args(argc, argv)) { return 1; } - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0)); - glutCreateWindow("LD48 #30 - connected worlds"); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); - glutDisplayFunc(display); - glutIdleFunc(idle); - glutReshapeFunc(reshape); - glutKeyboardFunc(keyb); - glutKeyboardUpFunc(keyb_up); - glutMouseFunc(mouse); - glutMotionFunc(motion); - glutSpaceballMotionFunc(sball_motion); - glutSpaceballRotateFunc(sball_rotate); + unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; + if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) { + fprintf(stderr, "failed to create window\n"); + return 1; + } + + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); + + if(!(ctx = SDL_GL_CreateContext(win))) { + fprintf(stderr, "failed to create OpenGL context\n"); + return 1; + } if(!init()) { return 1; } atexit(cleanup); - glutMainLoop(); + int xsz, ysz; + SDL_GetWindowSize(win, &xsz, &ysz); + game_reshape(xsz, ysz); + + for(;;) { + SDL_Event ev; + + while(SDL_PollEvent(&ev)) { + handle_event(&ev); + } + + unsigned int msec = SDL_GetTicks(); + + game_update(msec); + game_render(); + } + return 0; } @@ -63,7 +74,7 @@ int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH); int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT); if(win_xsz && win_ysz) { - glutReshapeWindow(win_xsz, win_ysz); + SDL_SetWindowSize(win, win_xsz, win_ysz); } } return true; @@ -74,101 +85,66 @@ game_cleanup(); } -static void display() -{ - unsigned int msec = glutGet(GLUT_ELAPSED_TIME); - - game_update(msec); - game_render(); -} - -static void idle() -{ - if(fullscreen_pending) { - glutFullScreen(); - } - - glutPostRedisplay(); -} - -static void reshape(int x, int y) -{ - game_reshape(x, y); -} - -static void keyb(unsigned char key, int x, int y) +static void handle_event(SDL_Event *ev) { static bool fullscr; static int prev_xpos, prev_ypos; static int prev_xsz, prev_ysz; - switch(key) { - case 'f': - fullscr = !fullscr; - if(fullscr) { - int xoffs, yoffs; + switch(ev->type) { + case SDL_KEYDOWN: + case SDL_KEYUP: + if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) { + fullscr = !fullscr; + if(fullscr) { + int xoffs, yoffs; - prev_xpos = glutGet(GLUT_WINDOW_X); - prev_ypos = glutGet(GLUT_WINDOW_Y); - prev_xsz = glutGet(GLUT_WINDOW_WIDTH); - prev_ysz = glutGet(GLUT_WINDOW_HEIGHT); + SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos); + SDL_GetWindowSize(win, &prev_xsz, &prev_ysz); - xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); - yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); - if(xoffs || yoffs) { - printf("repositioning: %d,%d\n", xoffs, yoffs); - glutPositionWindow(xoffs, yoffs); + xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); + yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); + if(xoffs || yoffs) { + printf("repositioning: %d,%d\n", xoffs, yoffs); + SDL_SetWindowPosition(win, xoffs, yoffs); + } + SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); + } else { + SDL_SetWindowFullscreen(win, 0); + /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos); + SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/ } - fullscreen_pending = true; - } else { - fullscreen_pending = false; - glutPositionWindow(prev_xpos, prev_ypos); - glutReshapeWindow(prev_xsz, prev_ysz); + break; } + + game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); + break; + + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED, + ev->button.x, ev->button.y); + break; + + case SDL_MOUSEWHEEL: + game_mwheel(ev->wheel.y); + break; + + case SDL_MOUSEMOTION: + game_motion(ev->motion.x, ev->motion.y); + break; + + case SDL_WINDOWEVENT: + switch(ev->window.event) { + case SDL_WINDOWEVENT_RESIZED: + game_reshape(ev->window.data1, ev->window.data2); + break; + + default: + break; + } + + default: break; } - game_keyboard(key, true, x, y); } - -static void keyb_up(unsigned char key, int x, int y) -{ - game_keyboard(key, false, x, y); -} - -static void mouse(int bn, int st, int x, int y) -{ - switch(bn) { - case GLUT_RIGHT_BUTTON + 1: - if(st == GLUT_DOWN) { - game_mwheel(1); - } - break; - - case GLUT_RIGHT_BUTTON + 2: - if(st == GLUT_DOWN) { - game_mwheel(-1); - } - break; - - default: - game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y); - } -} - -static void motion(int x, int y) -{ - game_motion(x, y); -} - -#define SBALL_MOVE_SCALE 0.00025 -#define SBALL_ROT_SCALE 0.01 - -static void sball_motion(int x, int y, int z) -{ - game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE); -} - -static void sball_rotate(int x, int y, int z) -{ - game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE); -} diff -r c5054e0cd564 -r 782ff06817fb src/opengl.h --- a/src/opengl.h Tue Aug 26 18:40:23 2014 +0300 +++ b/src/opengl.h Tue Aug 26 18:42:53 2014 +0300 @@ -4,9 +4,14 @@ #include #ifndef __APPLE__ -#include + +#ifdef WIN32 +#include +#endif + +#include #else -#include +#include #endif #define CHECKGLERR \ diff -r c5054e0cd564 -r 782ff06817fb src/vr/mathutil.c --- a/src/vr/mathutil.c Tue Aug 26 18:40:23 2014 +0300 +++ b/src/vr/mathutil.c Tue Aug 26 18:42:53 2014 +0300 @@ -1,78 +1,78 @@ -#include -#include -#include "mathutil.h" - -#define M(i, j) ((i) * 4 + (j)) - -static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; - -void rotation_matrix(const float *quat, float *matrix) -{ - matrix[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2]; - matrix[1] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2]; - matrix[2] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1]; - matrix[3] = 0.0f; - - matrix[4] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2]; - matrix[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2]; - matrix[6] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0]; - matrix[7] = 0.0f; - - matrix[8] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1]; - matrix[9] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0]; - matrix[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1]; - matrix[11] = 0.0f; - - matrix[12] = matrix[13] = matrix[14] = 0.0f; - matrix[15] = 1.0f; - - transpose_matrix(matrix); -} - -void translation_matrix(const float *vec, float *matrix) -{ - memcpy(matrix, idmat, sizeof idmat); - matrix[12] = vec[0]; - matrix[13] = vec[1]; - matrix[14] = vec[2]; -} - -void mult_matrix(float *dest, const float *m1, const float *m2) -{ - int i, j; - float tmp[16]; - - for(i=0; i<4; i++) { - for(j=0; j<4; j++) { - tmp[M(i, j)] = m1[M(i, 0)] * m2[M(0, j)] + m1[M(i, 1)] * m2[M(1, j)] + - m1[M(i, 2)] * m2[M(2, j)] + m1[M(i, 3)] * m2[M(3, j)]; - } - } - memcpy(dest, tmp, sizeof tmp); -} - -void transpose_matrix(float *matrix) -{ - int i, j; - float tmp[16]; - - for(i=0; i<4; i++) { - for(j=0; j<4; j++) { - tmp[M(i, j)] = matrix[M(j, i)]; - } - } - memcpy(matrix, tmp, sizeof tmp); -} - -void print_matrix(const float *matrix) -{ - int i, j; - - for(i=0; i<4; i++) { - putchar('['); - for(j=0; j<4; j++) { - printf("%6.3f ", matrix[M(i, j)]); - } - printf("]\n"); - } -} \ No newline at end of file +#include +#include +#include "mathutil.h" + +#define M(i, j) ((i) * 4 + (j)) + +static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; + +void rotation_matrix(const float *quat, float *matrix) +{ + matrix[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2]; + matrix[1] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2]; + matrix[2] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1]; + matrix[3] = 0.0f; + + matrix[4] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2]; + matrix[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2]; + matrix[6] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0]; + matrix[7] = 0.0f; + + matrix[8] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1]; + matrix[9] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0]; + matrix[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1]; + matrix[11] = 0.0f; + + matrix[12] = matrix[13] = matrix[14] = 0.0f; + matrix[15] = 1.0f; + + transpose_matrix(matrix); +} + +void translation_matrix(const float *vec, float *matrix) +{ + memcpy(matrix, idmat, sizeof idmat); + matrix[12] = vec[0]; + matrix[13] = vec[1]; + matrix[14] = vec[2]; +} + +void mult_matrix(float *dest, const float *m1, const float *m2) +{ + int i, j; + float tmp[16]; + + for(i=0; i<4; i++) { + for(j=0; j<4; j++) { + tmp[M(i, j)] = m1[M(i, 0)] * m2[M(0, j)] + m1[M(i, 1)] * m2[M(1, j)] + + m1[M(i, 2)] * m2[M(2, j)] + m1[M(i, 3)] * m2[M(3, j)]; + } + } + memcpy(dest, tmp, sizeof tmp); +} + +void transpose_matrix(float *matrix) +{ + int i, j; + float tmp[16]; + + for(i=0; i<4; i++) { + for(j=0; j<4; j++) { + tmp[M(i, j)] = matrix[M(j, i)]; + } + } + memcpy(matrix, tmp, sizeof tmp); +} + +void print_matrix(const float *matrix) +{ + int i, j; + + for(i=0; i<4; i++) { + putchar('['); + for(j=0; j<4; j++) { + printf("%6.3f ", matrix[M(i, j)]); + } + printf("]\n"); + } +} diff -r c5054e0cd564 -r 782ff06817fb src/vr/mathutil.h --- a/src/vr/mathutil.h Tue Aug 26 18:40:23 2014 +0300 +++ b/src/vr/mathutil.h Tue Aug 26 18:42:53 2014 +0300 @@ -1,13 +1,13 @@ -#ifndef MATHUTIL_H_ -#define MATHUTIL_H_ - -void rotation_matrix(const float *quat, float *matrix); -void translation_matrix(const float *vec, float *matrix); - -void mult_matrix(float *dest, const float *m1, const float *m2); - -void transpose_matrix(float *matrix); - -void print_matrix(const float *matrix); - -#endif /* MATHUTIL_H_ */ \ No newline at end of file +#ifndef MATHUTIL_H_ +#define MATHUTIL_H_ + +void rotation_matrix(const float *quat, float *matrix); +void translation_matrix(const float *vec, float *matrix); + +void mult_matrix(float *dest, const float *m1, const float *m2); + +void transpose_matrix(float *matrix); + +void print_matrix(const float *matrix); + +#endif /* MATHUTIL_H_ */