proj_plot
changeset 1:64483c640a38
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 24 Jan 2015 17:49:35 +0200 |
parents | e467998dcc64 |
children | e6ed6acee42f |
files | main.cc |
diffstat | 1 files changed, 76 insertions(+), 14 deletions(-) [+] |
line diff
1.1 --- a/main.cc Sat Jan 24 16:36:34 2015 +0200 1.2 +++ b/main.cc Sat Jan 24 17:49:35 2015 +0200 1.3 @@ -9,6 +9,7 @@ 1.4 bool init(); 1.5 void display(); 1.6 void draw_grid(); 1.7 +void draw_frustum(); 1.8 void draw_label(const Vector2 &pos, const char *fmt, ...); 1.9 void reshape(int x, int y); 1.10 void keyb(unsigned char key, int x, int y); 1.11 @@ -16,6 +17,8 @@ 1.12 void mouse(int bn, int st, int x, int y); 1.13 void motion(int x, int y); 1.14 Vector2 screen_to_world(int x, int y); 1.15 +Vector2 project(const Vector2 &v); 1.16 +Vector2 unproject(const Vector2 &v); 1.17 1.18 int win_xsz, win_ysz; 1.19 float aspect; 1.20 @@ -24,9 +27,12 @@ 1.21 Vector2 cur_point; 1.22 bool cur_point_valid; 1.23 1.24 +float proj_near = 0.5, proj_far = 50.0; 1.25 +float proj_vfov = 50.0; 1.26 + 1.27 bool keystate[256]; 1.28 dtx_font *font; 1.29 -float pan[2], zoom = 1.0; 1.30 +float pan[2] = {0, 0.85}, zoom = 0.4; 1.31 1.32 int main(int argc, char **argv) 1.33 { 1.34 @@ -58,7 +64,7 @@ 1.35 } 1.36 1.37 glEnable(GL_MULTISAMPLE); 1.38 - proj.set_perspective(DEG_TO_RAD(60), 1, 0.5, 500.0); 1.39 + proj.set_perspective(DEG_TO_RAD(proj_vfov), 1, 1.0, 50.0); 1.40 return true; 1.41 } 1.42 1.43 @@ -73,15 +79,34 @@ 1.44 1.45 draw_grid(); 1.46 1.47 + draw_frustum(); 1.48 + 1.49 if(cur_point_valid) { 1.50 + Vector2 ppt = project(cur_point); 1.51 + 1.52 glPointSize(7.0); 1.53 glBegin(GL_POINTS); 1.54 glColor3f(0.2, 1, 0.2); 1.55 glVertex2f(cur_point.x, cur_point.y); 1.56 + glColor3f(0.7, 0.2, 0.2); 1.57 + glVertex2f(ppt.x, ppt.y); 1.58 glEnd(); 1.59 1.60 + glEnable(GL_LINE_STIPPLE); 1.61 + glLineStipple(4, 0xe0e0); 1.62 + 1.63 + glBegin(GL_LINES); 1.64 + glColor3f(0.2, 0.5, 0.2); 1.65 + glVertex2f(cur_point.x, cur_point.y); 1.66 + glColor3f(0.5, 0.2, 0.2); 1.67 + glVertex2f(ppt.x, ppt.y); 1.68 + glEnd(); 1.69 + glDisable(GL_LINE_STIPPLE); 1.70 + 1.71 glColor3f(0.15, 0.5, 0.15); 1.72 draw_label(cur_point, "(%.2f, %.2f)", cur_point.x, cur_point.y); 1.73 + glColor3f(0.4, 0.1, 0.1); 1.74 + draw_label(ppt, "(%.2f, %.2f)", ppt.x, ppt.y); 1.75 } 1.76 1.77 glutSwapBuffers(); 1.78 @@ -92,20 +117,20 @@ 1.79 1.80 void draw_grid() 1.81 { 1.82 - float ymin = -1; 1.83 - float ymax = 1; 1.84 - float xmin = -1; 1.85 - float xmax = 1; 1.86 - float ticksz = 0.015; 1.87 + float ymin = -10; 1.88 + float ymax = 10; 1.89 + float xmin = -10; 1.90 + float xmax = 10; 1.91 + float ticksz = 0.015 / zoom; 1.92 1.93 glBegin(GL_LINES); 1.94 glColor3f(1, 1, 1); 1.95 - LINE(0, -1, 0, 1); 1.96 - LINE(-1, 0, 1, 0); 1.97 + LINE(0, -10, 0, 10); 1.98 + LINE(-10, 0, 10, 0); 1.99 1.100 for(int i=1; i<11; i++) { 1.101 for(int j=0; j<2; j++) { 1.102 - float x = (float)i / 10.0 * (j ? -1.0 : 1.0); 1.103 + float x = 10.0 * (float)i / 10.0 * (j ? -1.0 : 1.0); 1.104 1.105 glColor3f(0.15, 0.15, 0.15); 1.106 LINE(x, ymin, x, ymax); 1.107 @@ -119,6 +144,26 @@ 1.108 glEnd(); 1.109 } 1.110 1.111 +void draw_frustum() 1.112 +{ 1.113 + Vector2 v0, v1; 1.114 + 1.115 + glBegin(GL_LINES); 1.116 + glColor3f(0.2, 0.4, 0.8); 1.117 + 1.118 + v0 = unproject(Vector2(1, 0)); 1.119 + v1 = unproject(Vector2(1, 1)); 1.120 + LINE(v0.x, v0.y, v1.x, v1.y); 1.121 + 1.122 + v0 = unproject(Vector2(-1, 0)); 1.123 + v1 = unproject(Vector2(-1, 1)); 1.124 + LINE(v0.x, v0.y, v1.x, v1.y); 1.125 + 1.126 + LINE(-0.5, proj_near, 0.5, proj_near); 1.127 + 1.128 + glEnd(); 1.129 +} 1.130 + 1.131 void draw_label(const Vector2 &pos, const char *fmt, ...) 1.132 { 1.133 static char buf[512]; 1.134 @@ -209,11 +254,28 @@ 1.135 1.136 Vector2 screen_to_world(int px, int py) 1.137 { 1.138 - float sx = 2.0 * aspect * (1.0 / zoom) / (float)win_xsz; 1.139 - float sy = 2.0 * (1.0 / zoom) / (float)win_ysz; 1.140 + /* canonical [-1, 1] system */ 1.141 + float x = aspect * ((float)px * 2.0 / (float)win_xsz - 1.0); 1.142 + float y = (float)(win_ysz - py) * 2.0 / (float)win_ysz - 1.0; 1.143 1.144 - float x = (float)px * sx - (aspect + pan[0]) / zoom; 1.145 - float y = (float)(win_ysz - py) * sy - (1.0 + pan[1]) / zoom; 1.146 + /* account for pan & zoom */ 1.147 + x = (x + pan[0]) / zoom; 1.148 + y = (y + pan[1]) / zoom; 1.149 1.150 return Vector2(x, y); 1.151 } 1.152 + 1.153 +Vector2 project(const Vector2 &v) 1.154 +{ 1.155 + Vector4 ppt = Vector4(v.x, 0, -v.y, 1).transformed(proj); 1.156 + return Vector2(ppt.x / ppt.w, ppt.z / ppt.w); 1.157 +} 1.158 + 1.159 +Vector2 unproject(const Vector2 &v) 1.160 +{ 1.161 + Vector4 v4 = Vector4(v.x, 0.0, v.y, 1.0); 1.162 + Matrix4x4 inv_proj = proj.inverse(); 1.163 + 1.164 + Vector4 res4 = v4.transformed(inv_proj); 1.165 + return Vector2(res4.x / res4.w, -res4.z / res4.w); 1.166 +}