# HG changeset patch # User John Tsiombikas # Date 1411634685 -10800 # Node ID b49461618f61827474cb3468f5b7e80041b36015 # Parent d06e4e24f922fb37561a3ad5704f7e940b902e66 starting point diff -r d06e4e24f922 -r b49461618f61 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Thu Sep 25 11:44:45 2014 +0300 @@ -0,0 +1,10 @@ +\.o$ +\.d$ +\.swp$ +Makefile$ +^vrheights$ +^Debug +^Release +\.suo$ +\.opensdf$ +\.sdf$ diff -r d06e4e24f922 -r b49461618f61 src/game.cc --- a/src/game.cc Wed Sep 24 11:34:07 2014 +0300 +++ b/src/game.cc Thu Sep 25 11:44:45 2014 +0300 @@ -1,101 +1,204 @@ -#include -#include -#include -#include "opengl.h" -#include "game.h" -#include "goatvr.h" - -static void create_rtarg(int x, int y); -static int next_pow2(int x); - -static int win_width, win_height; -static unsigned int fb_tex; -static unsigned int fbo, fb_depth; -static int fb_xsz, fb_ysz; -static int fb_tex_xsz, fb_tex_ysz; - -bool game_init() -{ - init_opengl(); - - if(vr_init() == -1) { - return false; - } - - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - - int sdk_fb_xsz = vr_getf(VR_LEYE_XRES) + vr_getf(VR_REYE_XRES); - int sdk_fb_ysz = std::max(vr_getf(VR_LEYE_YRES), vr_getf(VR_REYE_YRES)); - create_rtarg(sdk_fb_xsz, sdk_fb_ysz); - - return true; -} - -void game_cleanup() -{ - vr_shutdown(); -} - -void game_update(long tm) -{ -} - -void game_display() -{ -} - -void game_reshape(int x, int y) -{ -} - -void game_keyboard(int key, bool pressed); -void game_mouse_button(int bn, bool state, int x, int y); -void game_mouse_motion(int x, int y); - - -static void create_rtarg(int x, int y) -{ - if(x == fb_xsz && y == fb_ysz) { - return; // nothing changed - } - - fb_xsz = x; - fb_ysz = y; - fb_tex_xsz = next_pow2(fb_xsz); - fb_tex_ysz = next_pow2(fb_ysz); - - if(!fbo) { - glGenFramebuffers(1, &fbo); - - glGenTextures(1, &fb_tex); - glBindTexture(GL_TEXTURE_2D, fb_tex); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - glGenRenderbuffers(1, &fb_depth); - } - - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - - glBindTexture(GL_TEXTURE_2D, fb_tex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0); - - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth); - - assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); - glBindFramebuffer(GL_FRAMEBUFFER, 0); -} - -static int next_pow2(int x) -{ - x -= 1; - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; - return x + 1; -} +#include +#include +#include +#include "opengl.h" +#include "game.h" +#include "goatvr.h" + +static void draw_scene(); +static void toggle_hmd_fullscr(); +static void create_rtarg(int x, int y); +static int next_pow2(int x); + +static int win_width, win_height; +static unsigned int fb_tex; +static unsigned int fbo, fb_depth; +static int fb_xsz, fb_ysz; +static int fb_tex_xsz, fb_tex_ysz; + +bool game_init() +{ + init_opengl(); + + if(vr_init() == -1) { + return false; + } + + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + return true; +} + +void game_cleanup() +{ + vr_shutdown(); +} + +void game_update(long tm) +{ +} + +void game_display() +{ + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + for(int i=0; i<2; i++) { + glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz); + vr_begin(i); + + float proj[16]; + if(vr_proj_matrix(i, 0.5, 500.0, proj)) { + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(proj); + } + + glMatrixMode(GL_MODELVIEW); + + float view[16]; + vr_view_matrix(i, view); + glLoadMatrixf(view); + /* move the camera to the eye level of the user */ + glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0); + + draw_scene(); + + vr_end(); + } + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(0, 0, win_width, win_height); + + vr_swap_buffers(); + assert(glGetError() == GL_NO_ERROR); +} + +void game_reshape(int x, int y) +{ + win_width = x; + win_height = y; + + create_rtarg(vr_geti_def(VR_RENDER_XRES, x), vr_geti_def(VR_RENDER_YRES, y)); + vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz); + + /* these might be overriden in VR mode (see game_display) */ + glViewport(0, 0, x, y); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); +} + +void game_keyboard(int key, bool pressed) +{ + if(pressed) { + switch(key) { + case 27: + exit_game(); + break; + + case 'f': + toggle_hmd_fullscr(); + break; + + case 'r': + vr_recenter(); + break; + + default: + break; + } + } +} + +void game_mouse_button(int bn, bool state, int x, int y) +{ +} + +void game_mouse_motion(int x, int y) +{ +} + +static void draw_scene() +{ +} + +static void toggle_hmd_fullscr() +{ + static bool fullscr; + static int prev_x, prev_y; + //static int prev_xsz, prev_ysz; + + fullscr = !fullscr; + if(fullscr) { + /* entering fullscreen on the HMD */ + int xoffs = vr_geti_def(VR_WIN_XOFFS, -1); + int yoffs = vr_geti_def(VR_WIN_YOFFS, -1); + if(xoffs != -1) { + get_window_pos(&prev_x, &prev_y); + move_window(xoffs, yoffs); + } + + int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1); + int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1); + if(xsz != -1) { + //prev_xsz = win_width; + //prev_ysz = win_height; + resize_window(xsz, ysz); + } + enter_fullscreen(); + + } else { + /* leaving fullscreen */ + leave_fullscreen(); + /*move_window(prev_x, prev_y); + resize_window(prev_xsz, prev_ysz);*/ + } +} + +static void create_rtarg(int x, int y) +{ + if(x == fb_xsz && y == fb_ysz) { + return; // nothing changed + } + + fb_xsz = x; + fb_ysz = y; + fb_tex_xsz = next_pow2(fb_xsz); + fb_tex_ysz = next_pow2(fb_ysz); + + if(!fbo) { + glGenFramebuffers(1, &fbo); + + glGenTextures(1, &fb_tex); + glBindTexture(GL_TEXTURE_2D, fb_tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glGenRenderbuffers(1, &fb_depth); + } + + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + glBindTexture(GL_TEXTURE_2D, fb_tex); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0); + + glBindRenderbuffer(GL_RENDERBUFFER, fb_depth); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth); + + assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +static int next_pow2(int x) +{ + x -= 1; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return x + 1; +} diff -r d06e4e24f922 -r b49461618f61 src/game.h --- a/src/game.h Wed Sep 24 11:34:07 2014 +0300 +++ b/src/game.h Thu Sep 25 11:44:45 2014 +0300 @@ -1,16 +1,22 @@ -#ifndef GAME_H_ -#define GAME_H_ - -bool game_init(); -void game_cleanup(); - -void game_update(long tm); -void game_display(); -void game_reshape(int x, int y); -void game_keyboard(int key, bool pressed); -void game_mouse_button(int bn, bool state, int x, int y); -void game_mouse_motion(int x, int y); - -void exit_game(); /* defined in main.cc */ - -#endif /* GAME_H_ */ \ No newline at end of file +#ifndef GAME_H_ +#define GAME_H_ + +bool game_init(); +void game_cleanup(); + +void game_update(long tm); +void game_display(); +void game_reshape(int x, int y); +void game_keyboard(int key, bool pressed); +void game_mouse_button(int bn, bool state, int x, int y); +void game_mouse_motion(int x, int y); + +/* defined in main.cc */ +void exit_game(); +void move_window(int x, int y); +void resize_window(int x, int y); +void get_window_pos(int *x, int *y); +void enter_fullscreen(); +void leave_fullscreen(); + +#endif /* GAME_H_ */ diff -r d06e4e24f922 -r b49461618f61 src/main.cc --- a/src/main.cc Wed Sep 24 11:34:07 2014 +0300 +++ b/src/main.cc Thu Sep 25 11:44:45 2014 +0300 @@ -1,94 +1,127 @@ -#include -#include -#include -#include "game.h" - -static bool init(); -static void cleanup(); -static void handle_event(SDL_Event *ev); - -static bool done; -static SDL_Window *win; -static SDL_GLContext ctx; - -int main(int argc, char **argv) -{ - if(!init()) { - return 1; - } - - for(;;) { - SDL_Event ev; - while(SDL_PollEvent(&ev)) { - handle_event(&ev); - if(done) break; - } - - game_update(SDL_GetTicks()); - game_display(); - } - - cleanup(); - return 0; -} - -void exit_game() -{ - done = true; -} - -static bool init() -{ - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { - fprintf(stderr, "failed to initialize SDL\n"); - return false; - } - - win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - 1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); - if(!win) { - fprintf(stderr, "failed to create window\n"); - return false; - } - - if(!(ctx = SDL_GL_CreateContext(win))) { - fprintf(stderr, "failed to create OpenGL context\n"); - return false; - } - - return game_init(); -} - -static void cleanup() -{ - game_cleanup(); - SDL_Quit(); -} - -static void handle_event(SDL_Event *ev) -{ - switch(ev->type) { - case SDL_WINDOWEVENT: - if(ev->window.event == SDL_WINDOWEVENT_RESIZED) { - game_reshape(ev->window.data1, ev->window.data2); - } - break; - - case SDL_KEYDOWN: - case SDL_KEYUP: - game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); - break; - - case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEBUTTONUP: - game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y); - break; - - case SDL_MOUSEMOTION: - game_mouse_motion(ev->motion.x, ev->motion.y); - break; - - default: - break; - } -} \ No newline at end of file +#include +#include +#include +#include "game.h" + +static bool init(); +static void cleanup(); +static void handle_event(SDL_Event *ev); + +static bool done; +static SDL_Window *win; +static SDL_GLContext ctx; + +int main(int argc, char **argv) +{ + if(!init()) { + return 1; + } + + while(!done) { + SDL_Event ev; + while(SDL_PollEvent(&ev)) { + handle_event(&ev); + if(done) goto end_main_loop; + } + + game_update(SDL_GetTicks()); + game_display(); + } + +end_main_loop: + cleanup(); + return 0; +} + +void exit_game() +{ + done = true; +} + +void move_window(int x, int y) +{ + SDL_SetWindowPosition(win, x, y); +} + +void get_window_pos(int *x, int *y) +{ + SDL_GetWindowPosition(win, x, y); +} + +void resize_window(int x, int y) +{ + SDL_SetWindowSize(win, x, y); +} + +void enter_fullscreen() +{ + SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); +} + +void leave_fullscreen() +{ + SDL_SetWindowFullscreen(win, 0); +} + +static bool init() +{ + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { + fprintf(stderr, "failed to initialize SDL\n"); + return false; + } + + win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + 1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + if(!win) { + fprintf(stderr, "failed to create window\n"); + return false; + } + + if(!(ctx = SDL_GL_CreateContext(win))) { + fprintf(stderr, "failed to create OpenGL context\n"); + return false; + } + + if(!game_init()) { + return false; + } + + int w, h; + SDL_GetWindowSize(win, &w, &h); + game_reshape(w, h); + return true; +} + +static void cleanup() +{ + game_cleanup(); + SDL_Quit(); +} + +static void handle_event(SDL_Event *ev) +{ + switch(ev->type) { + case SDL_WINDOWEVENT: + if(ev->window.event == SDL_WINDOWEVENT_RESIZED) { + game_reshape(ev->window.data1, ev->window.data2); + } + break; + + case SDL_KEYDOWN: + case SDL_KEYUP: + game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); + break; + + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y); + break; + + case SDL_MOUSEMOTION: + game_mouse_motion(ev->motion.x, ev->motion.y); + break; + + default: + break; + } +} diff -r d06e4e24f922 -r b49461618f61 src/opengl.cc --- a/src/opengl.cc Wed Sep 24 11:34:07 2014 +0300 +++ b/src/opengl.cc Thu Sep 25 11:44:45 2014 +0300 @@ -1,7 +1,7 @@ -#include "opengl.h" - -bool init_opengl() -{ - glewInit(); - return true; -} \ No newline at end of file +#include "opengl.h" + +bool init_opengl() +{ + glewInit(); + return true; +} diff -r d06e4e24f922 -r b49461618f61 src/opengl.h --- a/src/opengl.h Wed Sep 24 11:34:07 2014 +0300 +++ b/src/opengl.h Thu Sep 25 11:44:45 2014 +0300 @@ -1,8 +1,8 @@ -#ifndef OPENGL_H_ -#define OPENGL_H_ - -#include - -bool init_opengl(); - -#endif /* OPENGL_H_ */ \ No newline at end of file +#ifndef OPENGL_H_ +#define OPENGL_H_ + +#include + +bool init_opengl(); + +#endif /* OPENGL_H_ */