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