vrseasons

annotate src/game.cc @ 1:65c2e37c48b2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 12 Apr 2015 03:39:55 +0300
parents 393ef1143c9c
children
rev   line source
nuclear@0 1 #include <assert.h>
nuclear@0 2 #include "opengl.h"
nuclear@0 3 #include "game.h"
nuclear@0 4 #include <stdio.h>
nuclear@0 5 #include <goatvr.h>
nuclear@0 6 #include "main.h"
nuclear@0 7 #include "scene.h"
nuclear@0 8 #include "timer.h"
nuclear@0 9 #include "rtarg.h"
nuclear@0 10 #include "opt.h"
nuclear@0 11
nuclear@0 12 static bool vr_mode = true;
nuclear@0 13 static int win_width, win_height;
nuclear@0 14 static RenderTarget vr_rtarg;
nuclear@1 15 static Options opt;
nuclear@0 16
nuclear@0 17 bool game_init(int argc, char **argv)
nuclear@0 18 {
nuclear@1 19 if(!parse_args(&opt, argc, argv)) {
nuclear@0 20 return false;
nuclear@0 21 }
nuclear@0 22
nuclear@0 23 init_gl();
nuclear@0 24
nuclear@0 25 if(vr_mode) {
nuclear@0 26 if(vr_init() == -1) {
nuclear@0 27 return false;
nuclear@0 28 }
nuclear@0 29
nuclear@0 30 win_width = vr_geti_def(VR_DISPLAY_WIDTH, 1280);
nuclear@0 31 win_height = vr_geti_def(VR_DISPLAY_HEIGHT, 800);
nuclear@0 32 resize_window(win_width, win_height);
nuclear@0 33
nuclear@0 34 int rt_width = vr_geti_def(VR_RENDER_XRES, win_width);
nuclear@0 35 int rt_height = vr_geti_def(VR_RENDER_YRES, win_height);
nuclear@0 36 vr_rtarg.create(rt_width, rt_height);
nuclear@0 37 }
nuclear@0 38
nuclear@0 39 return true;
nuclear@0 40 }
nuclear@0 41
nuclear@0 42 void game_shutdown()
nuclear@0 43 {
nuclear@0 44 vr_shutdown();
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 void game_draw()
nuclear@0 48 {
nuclear@0 49 static unsigned long prev_msec;
nuclear@0 50 unsigned long msec = get_msec();
nuclear@0 51 float dt = (float)(msec - prev_msec) / 1000.0;
nuclear@0 52
nuclear@0 53 update_scene(msec, dt);
nuclear@0 54
nuclear@0 55 if(vr_mode) {
nuclear@0 56 set_render_target(&vr_rtarg);
nuclear@0 57
nuclear@0 58 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 59
nuclear@0 60 for(int i=0; i<2; i++) {
nuclear@0 61 vr_begin(i);
nuclear@0 62
nuclear@0 63 glViewport(i == VR_EYE_LEFT ? 0 : vr_rtarg.get_width() / 2, 0, vr_rtarg.get_width() / 2, vr_rtarg.get_height());
nuclear@0 64
nuclear@0 65 glMatrixMode(GL_PROJECTION);
nuclear@0 66 float proj[16];
nuclear@0 67 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
nuclear@0 68 glLoadMatrixf(proj);
nuclear@0 69 } else {
nuclear@0 70 glLoadIdentity();
nuclear@0 71 gluPerspective(50.0, (float)win_width / 2.0 / (float)win_height, 0.5, 500.0);
nuclear@0 72 }
nuclear@0 73
nuclear@0 74 float view[16];
nuclear@0 75 vr_view_matrix(i, view);
nuclear@0 76
nuclear@0 77 glMatrixMode(GL_MODELVIEW);
nuclear@0 78 glLoadMatrixf(view);
nuclear@0 79 glTranslatef(0, -vr_getf(VR_EYE_HEIGHT), 0);
nuclear@0 80
nuclear@0 81 draw_scene();
nuclear@0 82
nuclear@0 83 vr_end();
nuclear@0 84 }
nuclear@0 85
nuclear@0 86 set_render_target(0);
nuclear@0 87 glViewport(0, 0, win_width, win_height);
nuclear@0 88
nuclear@0 89 vr_swap_buffers();
nuclear@0 90
nuclear@0 91 } else {
nuclear@0 92 glMatrixMode(GL_MODELVIEW);
nuclear@0 93 glLoadIdentity();
nuclear@0 94
nuclear@0 95 draw_scene();
nuclear@0 96
nuclear@0 97 swap_buffers();
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 assert(glGetError() == GL_NO_ERROR);
nuclear@0 101 }
nuclear@0 102
nuclear@0 103 void game_reshape(int x, int y)
nuclear@0 104 {
nuclear@0 105 glViewport(0, 0, x, y);
nuclear@0 106
nuclear@0 107 if(vr_mode) {
nuclear@0 108 vr_rtarg.resize(x, y);
nuclear@0 109
nuclear@0 110 float umax = (float)vr_rtarg.get_width() / (float)vr_rtarg.get_tex_width();
nuclear@0 111 float vmax = (float)vr_rtarg.get_height() / (float)vr_rtarg.get_tex_height();
nuclear@0 112 vr_output_texture(vr_rtarg.get_texture(), 0, 0, umax, vmax);
nuclear@0 113 }
nuclear@0 114 }
nuclear@0 115
nuclear@0 116 void game_keyboard(int key, bool press)
nuclear@0 117 {
nuclear@0 118 if(press) {
nuclear@0 119 switch(key) {
nuclear@0 120 case 27:
nuclear@0 121 quit();
nuclear@0 122
nuclear@0 123 case ' ':
nuclear@0 124 if(vr_mode) {
nuclear@0 125 vr_recenter();
nuclear@0 126 }
nuclear@0 127 break;
nuclear@0 128 }
nuclear@0 129 }
nuclear@0 130 }
nuclear@0 131
nuclear@0 132 void game_mbutton(int bn, bool press, int x, int y)
nuclear@0 133 {
nuclear@0 134 }
nuclear@0 135
nuclear@0 136 void game_mmotion(int x, int y)
nuclear@0 137 {
nuclear@0 138 }