859e6f2e937dab3424b2268a3b045edc782af957
[vrfileman] / src / app.cc
1 #include <stdio.h>
2 #include <assert.h>
3 #include "opengl.h"
4 #include "app.h"
5 #include "gmath/gmath.h"
6 #include "mesh.h"
7 #include "meshgen.h"
8 #include "backdrop.h"
9
10 int win_width, win_height;
11 float win_aspect;
12 long time_msec;
13
14 static float cam_theta, cam_phi;
15 static Mesh *mesh_torus;
16
17 static bool bnstate[16];
18 static int prev_x, prev_y;
19
20 bool app_init(int argc, char **argv)
21 {
22         if(init_opengl() == -1) {
23                 return false;
24         }
25
26         glEnable(GL_MULTISAMPLE);
27
28         int aasamples = 0;
29         glGetIntegerv(GL_SAMPLES, &aasamples);
30         printf("got %d samples per pixel\n", aasamples);
31
32         glEnable(GL_CULL_FACE);
33         glEnable(GL_DEPTH_TEST);
34         //glEnable(GL_LIGHTING);
35         glEnable(GL_LIGHT0);
36
37         Mesh::use_custom_sdr_attr = false;
38
39         mesh_torus = new Mesh;
40         gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
41
42         if(!init_backdrop()) {
43                 return false;
44         }
45
46         return true;
47 }
48
49 void app_cleanup()
50 {
51         delete mesh_torus;
52         cleanup_backdrop();
53 }
54
55 void app_draw()
56 {
57         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
58
59         Mat4 view_mat;
60         view_mat.pre_rotate_x(deg_to_rad(cam_phi));
61         view_mat.pre_rotate_y(deg_to_rad(cam_theta));
62         view_mat.pre_translate(0, -1.65, 0);
63         glMatrixMode(GL_MODELVIEW);
64         glLoadMatrixf(view_mat[0]);
65
66         //mesh_torus->draw();
67         draw_backdrop();
68
69         app_swap_buffers();
70         assert(glGetError() == GL_NO_ERROR);
71 }
72
73 void app_reshape(int x, int y)
74 {
75         glViewport(0, 0, x, y);
76
77         Mat4 mat;
78         mat.perspective(deg_to_rad(50), win_aspect, 0.5, 500.0);
79
80         glMatrixMode(GL_PROJECTION);
81         glLoadMatrixf(mat[0]);
82 }
83
84 void app_keyboard(int key, bool pressed)
85 {
86         if(pressed) {
87                 switch(key) {
88                 case 27:
89                         app_quit();
90                         break;
91                 }
92         }
93 }
94
95 void app_mouse_button(int bn, bool pressed, int x, int y)
96 {
97         bnstate[bn] = pressed;
98         prev_x = x;
99         prev_y = y;
100 }
101
102 void app_mouse_motion(int x, int y)
103 {
104         int dx = x - prev_x;
105         int dy = y - prev_y;
106         prev_x = x;
107         prev_y = y;
108
109         if(!dx && !dy) return;
110
111         if(bnstate[0]) {
112                 cam_theta += dx * 0.5;
113                 cam_phi += dy * 0.5;
114
115                 if(cam_phi < -90) cam_phi = -90;
116                 if(cam_phi > 90) cam_phi = 90;
117         }
118         app_redraw();
119 }