1de3f891b4503ab624000c0be4eb6ffcf507c295
[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 "sdr.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 static unsigned int sdr_grid;
21
22 bool app_init(int argc, char **argv)
23 {
24         if(init_opengl() == -1) {
25                 return false;
26         }
27
28         glEnable(GL_MULTISAMPLE);
29
30         int aasamples = 0;
31         glGetIntegerv(GL_SAMPLES, &aasamples);
32         printf("got %d samples per pixel\n", aasamples);
33
34         glEnable(GL_CULL_FACE);
35         glEnable(GL_DEPTH_TEST);
36         //glEnable(GL_LIGHTING);
37         glEnable(GL_LIGHT0);
38
39         Mesh::use_custom_sdr_attr = false;
40
41         mesh_torus = new Mesh;
42         gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
43
44         if(!(sdr_grid = create_program_load("sdr/grid.v.glsl", "sdr/grid.p.glsl"))) {
45                 return false;
46         }
47
48         return true;
49 }
50
51 void app_cleanup()
52 {
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
68         Mat4 xform;
69         xform.scaling(500.0);
70         glPushMatrix();
71         glMultMatrixf(xform[0]);
72
73         bind_program(sdr_grid);
74         glBegin(GL_QUADS);
75         glNormal3f(0, 1, 0);
76         glVertex3f(-1, 0, 1);
77         glVertex3f(1, 0, 1);
78         glVertex3f(1, 0, -1);
79         glVertex3f(-1, 0, -1);
80         glEnd();
81         bind_program(0);
82
83         glPopMatrix();
84
85         app_swap_buffers();
86         assert(glGetError() == GL_NO_ERROR);
87 }
88
89 void app_reshape(int x, int y)
90 {
91         glViewport(0, 0, x, y);
92
93         Mat4 mat;
94         mat.perspective(deg_to_rad(50), win_aspect, 0.5, 500.0);
95
96         glMatrixMode(GL_PROJECTION);
97         glLoadMatrixf(mat[0]);
98 }
99
100 void app_keyboard(int key, bool pressed)
101 {
102         if(pressed) {
103                 switch(key) {
104                 case 27:
105                         app_quit();
106                         break;
107                 }
108         }
109 }
110
111 void app_mouse_button(int bn, bool pressed, int x, int y)
112 {
113         bnstate[bn] = pressed;
114         prev_x = x;
115         prev_y = y;
116 }
117
118 void app_mouse_motion(int x, int y)
119 {
120         int dx = x - prev_x;
121         int dy = y - prev_y;
122         prev_x = x;
123         prev_y = y;
124
125         if(!dx && !dy) return;
126
127         if(bnstate[0]) {
128                 cam_theta += dx * 0.5;
129                 cam_phi += dy * 0.5;
130
131                 if(cam_phi < -90) cam_phi = -90;
132                 if(cam_phi > 90) cam_phi = 90;
133         }
134         app_redraw();
135 }