f99e034b9a9653017b10122bda766b6652e4ab7e
[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 #include "texture.h"
10
11 int win_width, win_height;
12 float win_aspect;
13 long time_msec;
14
15 static float cam_theta, cam_phi;
16 static Mesh *mesh_torus;
17 static Texture *tex_grid;
18
19 static bool bnstate[16];
20 static int prev_x, prev_y;
21
22 static unsigned int sdr_grid;
23
24 bool app_init(int argc, char **argv)
25 {
26         if(init_opengl() == -1) {
27                 return false;
28         }
29
30         glEnable(GL_MULTISAMPLE);
31
32         int aasamples = 0;
33         glGetIntegerv(GL_SAMPLES, &aasamples);
34         printf("got %d samples per pixel\n", aasamples);
35
36         glEnable(GL_CULL_FACE);
37         glEnable(GL_DEPTH_TEST);
38         //glEnable(GL_LIGHTING);
39         glEnable(GL_LIGHT0);
40
41         Mesh::use_custom_sdr_attr = false;
42
43         mesh_torus = new Mesh;
44         gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
45
46         if(!(sdr_grid = create_program_load("sdr/grid.v.glsl", "sdr/grid.p.glsl"))) {
47                 return false;
48         }
49         if(!(tex_grid = load_texture("data/purple_grid.png"))) {
50                 delete tex_grid;
51                 return false;
52         }
53
54         return true;
55 }
56
57 void app_cleanup()
58 {
59 }
60
61 void app_draw()
62 {
63         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
64
65         Mat4 view_mat;
66         view_mat.pre_rotate_x(deg_to_rad(cam_phi));
67         view_mat.pre_rotate_y(deg_to_rad(cam_theta));
68         view_mat.pre_translate(0, -1.65, 0);
69         glMatrixMode(GL_MODELVIEW);
70         glLoadMatrixf(view_mat[0]);
71
72         //mesh_torus->draw();
73
74         Mat4 xform;
75         xform.scaling(500.0);
76         glPushMatrix();
77         glMultMatrixf(xform[0]);
78
79         bind_program(sdr_grid);
80         bind_texture(tex_grid);
81
82         glBegin(GL_QUADS);
83         glNormal3f(0, 1, 0);
84         glVertex3f(-1, 0, 1);
85         glVertex3f(1, 0, 1);
86         glVertex3f(1, 0, -1);
87         glVertex3f(-1, 0, -1);
88         glEnd();
89
90         bind_texture(0);
91         bind_program(0);
92
93         glPopMatrix();
94
95         app_swap_buffers();
96         assert(glGetError() == GL_NO_ERROR);
97 }
98
99 void app_reshape(int x, int y)
100 {
101         glViewport(0, 0, x, y);
102
103         Mat4 mat;
104         mat.perspective(deg_to_rad(50), win_aspect, 0.5, 500.0);
105
106         glMatrixMode(GL_PROJECTION);
107         glLoadMatrixf(mat[0]);
108 }
109
110 void app_keyboard(int key, bool pressed)
111 {
112         if(pressed) {
113                 switch(key) {
114                 case 27:
115                         app_quit();
116                         break;
117                 }
118         }
119 }
120
121 void app_mouse_button(int bn, bool pressed, int x, int y)
122 {
123         bnstate[bn] = pressed;
124         prev_x = x;
125         prev_y = y;
126 }
127
128 void app_mouse_motion(int x, int y)
129 {
130         int dx = x - prev_x;
131         int dy = y - prev_y;
132         prev_x = x;
133         prev_y = y;
134
135         if(!dx && !dy) return;
136
137         if(bnstate[0]) {
138                 cam_theta += dx * 0.5;
139                 cam_phi += dy * 0.5;
140
141                 if(cam_phi < -90) cam_phi = -90;
142                 if(cam_phi > 90) cam_phi = 90;
143         }
144         app_redraw();
145 }