proj_plot

view main.cc @ 2:e6ed6acee42f

more foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 25 Jan 2015 01:59:28 +0200
parents 64483c640a38
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <assert.h>
5 #include <GL/glut.h>
6 #include <vmath/vmath.h>
7 #include <drawtext.h>
9 bool init();
10 void display();
11 void draw_grid();
12 void draw_frustum();
13 void draw_label(const Vector2 &pos, const char *fmt, ...);
14 void reshape(int x, int y);
15 void keyb(unsigned char key, int x, int y);
16 void keyb_up(unsigned char key, int x, int y);
17 void mouse(int bn, int st, int x, int y);
18 void motion(int x, int y);
19 Vector2 screen_to_world(int x, int y);
20 Vector2 project(const Vector2 &v);
21 Vector2 unproject(const Vector2 &v);
22 float calc_proj_dist(float fov);
24 int win_xsz, win_ysz;
25 float aspect;
27 Matrix4x4 proj;
28 float proj_offset;
29 Vector2 cur_point;
30 bool cur_point_valid;
32 float proj_near = 0.5, proj_far = 50.0;
33 float proj_vfov = 60.0;
35 bool keystate[256];
36 bool bnstate[16];
37 int prev_x, prev_y;
38 unsigned int modkeys;
40 dtx_font *font;
41 float pan[2] = {0, 0.85}, zoom = 0.3;
42 float snap_dist = 0.25;
44 int main(int argc, char **argv)
45 {
46 glutInit(&argc, argv);
47 glutInitWindowSize(1024, 768);
48 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
49 glutCreateWindow("Interactive projection diagram");
51 glutDisplayFunc(display);
52 glutReshapeFunc(reshape);
53 glutKeyboardFunc(keyb);
54 glutKeyboardUpFunc(keyb_up);
55 glutMouseFunc(mouse);
56 glutMotionFunc(motion);
58 if(!init()) {
59 return 1;
60 }
62 glutMainLoop();
63 return 0;
64 }
66 bool init()
67 {
68 if(!(font = dtx_open_font("data/font.ttf", 16))) {
69 fprintf(stderr, "failed to open font\n");
70 return false;
71 }
73 glEnable(GL_MULTISAMPLE);
74 proj.set_perspective(DEG_TO_RAD(proj_vfov), 1, 1.0, 50.0);
75 proj.translate(Vector3(proj_offset, 0, 0));
76 return true;
77 }
79 void display()
80 {
81 glClear(GL_COLOR_BUFFER_BIT);
83 glMatrixMode(GL_MODELVIEW);
84 glLoadIdentity();
85 glTranslatef(-pan[0], -pan[1], 0);
86 glScalef(zoom, zoom, zoom);
88 draw_grid();
90 draw_frustum();
92 if(cur_point_valid) {
93 Vector2 ppt = project(cur_point);
95 glPointSize(7.0);
96 glBegin(GL_POINTS);
97 glColor3f(0.2, 1, 0.2);
98 glVertex2f(cur_point.x, cur_point.y);
99 glColor3f(0.7, 0.2, 0.2);
100 glVertex2f(ppt.x, ppt.y);
101 glEnd();
103 glEnable(GL_LINE_STIPPLE);
104 glLineStipple(4, 0xe0e0);
106 glBegin(GL_LINES);
107 glColor3f(0.2, 0.5, 0.2);
108 glVertex2f(cur_point.x, cur_point.y);
109 glColor3f(0.5, 0.2, 0.2);
110 glVertex2f(ppt.x, ppt.y);
111 glEnd();
112 glDisable(GL_LINE_STIPPLE);
114 glColor3f(0.2, 0.5, 0.2);
115 draw_label(cur_point, "(%.2f, %.2f)", cur_point.x, cur_point.y);
116 glColor3f(0.5, 0.2, 0.2);
117 draw_label(ppt, "(%.2f, %.2f)", ppt.x, ppt.y);
118 }
120 glutSwapBuffers();
121 assert(glGetError() == GL_NO_ERROR);
122 }
124 #define LINE(x0, y0, x1, y1) (glVertex2f(x0, y0), glVertex2f(x1, y1))
126 void draw_grid()
127 {
128 float ymin = -10;
129 float ymax = 10;
130 float xmin = -10;
131 float xmax = 10;
132 float ticksz = 0.015 / zoom;
134 glBegin(GL_LINES);
135 glColor3f(1, 1, 1);
136 LINE(0, -10, 0, 10);
137 LINE(-10, 0, 10, 0);
139 for(int i=1; i<11; i++) {
140 for(int j=0; j<2; j++) {
141 float x = 10.0 * (float)i / 10.0 * (j ? -1.0 : 1.0);
143 glColor3f(0.15, 0.15, 0.15);
144 LINE(x, ymin, x, ymax);
145 LINE(xmin, x, xmax, x);
147 glColor3f(0.4, 0.4, 0.4);
148 LINE(x, -ticksz, x, ticksz);
149 LINE(-ticksz, x, ticksz, x);
150 }
151 }
152 glEnd();
153 }
155 void draw_frustum()
156 {
157 Vector2 left[2], right[2];
159 glBegin(GL_LINES);
160 glColor3f(0.2, 0.4, 0.8);
162 right[0] = unproject(Vector2(1, 0));
163 right[1] = unproject(Vector2(1, 1));
164 LINE(right[0].x, right[0].y, right[1].x, right[1].y);
166 left[0] = unproject(Vector2(-1, 0));
167 left[1] = unproject(Vector2(-1, 1));
168 LINE(left[0].x, left[0].y, left[1].x, left[1].y);
170 float dist = calc_proj_dist(proj_vfov);
171 LINE(-1, dist, 1, dist);
172 glEnd();
174 /* find the points where the frustum lines hit zero */
175 glEnable(GL_LINE_STIPPLE);
176 glLineStipple(3, 0x0101);
178 glBegin(GL_LINES);
179 for(int i=0; i<2; i++) {
180 Vector2 *v = i ? left : right;
181 float t = -v[1].y / (v[0].y - v[1].y);
182 float x = v[1].x + (v[0].x - v[1].x) * t;
183 LINE(v[0].x, v[0].y, x, 0);
184 }
185 glEnd();
186 glDisable(GL_LINE_STIPPLE);
187 }
189 void draw_label(const Vector2 &pos, const char *fmt, ...)
190 {
191 static char buf[512];
192 va_list ap;
194 va_start(ap, fmt);
195 vsnprintf(buf, sizeof buf - 1, fmt, ap);
196 va_end(ap);
198 glPushMatrix();
199 glTranslatef(pos.x + 0.01, pos.y + 0.01, 0);
200 float s = 2.0 / (win_ysz * zoom);
201 glScalef(s, s, s);
203 dtx_string(buf);
205 glPopMatrix();
206 }
208 void reshape(int x, int y)
209 {
210 win_xsz = x;
211 win_ysz = y;
212 aspect = (float)x / (float)y;
214 glViewport(0, 0, x, y);
216 glMatrixMode(GL_PROJECTION);
217 glLoadIdentity();
218 glScalef(1.0 / aspect, 1.0, 1.0);
219 }
221 void keyb(unsigned char key, int x, int y)
222 {
223 keystate[key] = 1;
224 modkeys = glutGetModifiers();
226 switch(key) {
227 case 27:
228 exit(0);
229 }
230 }
232 void keyb_up(unsigned char key, int x, int y)
233 {
234 keystate[key] = 0;
235 modkeys = glutGetModifiers();
236 }
238 void mouse(int bn, int st, int x, int y)
239 {
240 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
241 prev_x = x;
242 prev_y = y;
243 modkeys = glutGetModifiers();
245 if(bn == GLUT_LEFT_BUTTON && st == GLUT_DOWN) {
246 cur_point = screen_to_world(x, y);
247 cur_point_valid = true;
248 glutPostRedisplay();
249 }
250 }
252 void motion(int x, int y)
253 {
254 float dx = 2.0 * aspect * (x - prev_x) / (float)win_xsz;
255 float dy = 2.0 * (y - prev_y) / (float)win_ysz;
256 prev_x = x;
257 prev_y = y;
259 if(bnstate[0]) {
260 cur_point = screen_to_world(x, y);
261 if(keystate['s'] || keystate['S']) {
262 cur_point.x = round(cur_point.x / snap_dist) * snap_dist;
263 cur_point.y = round(cur_point.y / snap_dist) * snap_dist;
264 }
265 glutPostRedisplay();
266 }
267 if(bnstate[1]) {
268 pan[0] -= dx;
269 pan[1] += dy;
271 glutPostRedisplay();
272 }
273 if(bnstate[2]) {
274 zoom += dy;
276 if(zoom < 1e-4) zoom = 1e-4;
277 glutPostRedisplay();
278 }
279 }
281 Vector2 screen_to_world(int px, int py)
282 {
283 /* canonical [-1, 1] system */
284 float x = aspect * ((float)px * 2.0 / (float)win_xsz - 1.0);
285 float y = (float)(win_ysz - py) * 2.0 / (float)win_ysz - 1.0;
287 /* account for pan & zoom */
288 x = (x + pan[0]) / zoom;
289 y = (y + pan[1]) / zoom;
291 return Vector2(x, y);
292 }
294 Vector2 project(const Vector2 &v)
295 {
296 Vector4 ppt = Vector4(v.x, 0, -v.y, 1).transformed(proj);
297 return Vector2(ppt.x / ppt.w, calc_proj_dist(proj_vfov));
298 }
300 Vector2 unproject(const Vector2 &v)
301 {
302 Vector4 v4 = Vector4(v.x, 0.0, v.y, 1.0);
303 Matrix4x4 inv_proj = proj.inverse();
305 Vector4 res4 = v4.transformed(inv_proj);
306 return Vector2(res4.x / res4.w, -res4.z / res4.w);
307 }
309 float calc_proj_dist(float fov)
310 {
311 return 1.0 / tan(DEG_TO_RAD(fov) / 2.0);
312 }