proj_plot
changeset 2:e6ed6acee42f tip
more foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 25 Jan 2015 01:59:28 +0200 |
parents | 64483c640a38 |
children | |
files | main.cc |
diffstat | 1 files changed, 47 insertions(+), 16 deletions(-) [+] |
line diff
1.1 --- a/main.cc Sat Jan 24 17:49:35 2015 +0200 1.2 +++ b/main.cc Sun Jan 25 01:59:28 2015 +0200 1.3 @@ -19,20 +19,27 @@ 1.4 Vector2 screen_to_world(int x, int y); 1.5 Vector2 project(const Vector2 &v); 1.6 Vector2 unproject(const Vector2 &v); 1.7 +float calc_proj_dist(float fov); 1.8 1.9 int win_xsz, win_ysz; 1.10 float aspect; 1.11 1.12 Matrix4x4 proj; 1.13 +float proj_offset; 1.14 Vector2 cur_point; 1.15 bool cur_point_valid; 1.16 1.17 float proj_near = 0.5, proj_far = 50.0; 1.18 -float proj_vfov = 50.0; 1.19 +float proj_vfov = 60.0; 1.20 1.21 bool keystate[256]; 1.22 +bool bnstate[16]; 1.23 +int prev_x, prev_y; 1.24 +unsigned int modkeys; 1.25 + 1.26 dtx_font *font; 1.27 -float pan[2] = {0, 0.85}, zoom = 0.4; 1.28 +float pan[2] = {0, 0.85}, zoom = 0.3; 1.29 +float snap_dist = 0.25; 1.30 1.31 int main(int argc, char **argv) 1.32 { 1.33 @@ -65,6 +72,7 @@ 1.34 1.35 glEnable(GL_MULTISAMPLE); 1.36 proj.set_perspective(DEG_TO_RAD(proj_vfov), 1, 1.0, 50.0); 1.37 + proj.translate(Vector3(proj_offset, 0, 0)); 1.38 return true; 1.39 } 1.40 1.41 @@ -103,9 +111,9 @@ 1.42 glEnd(); 1.43 glDisable(GL_LINE_STIPPLE); 1.44 1.45 - glColor3f(0.15, 0.5, 0.15); 1.46 + glColor3f(0.2, 0.5, 0.2); 1.47 draw_label(cur_point, "(%.2f, %.2f)", cur_point.x, cur_point.y); 1.48 - glColor3f(0.4, 0.1, 0.1); 1.49 + glColor3f(0.5, 0.2, 0.2); 1.50 draw_label(ppt, "(%.2f, %.2f)", ppt.x, ppt.y); 1.51 } 1.52 1.53 @@ -146,22 +154,36 @@ 1.54 1.55 void draw_frustum() 1.56 { 1.57 - Vector2 v0, v1; 1.58 + Vector2 left[2], right[2]; 1.59 1.60 glBegin(GL_LINES); 1.61 glColor3f(0.2, 0.4, 0.8); 1.62 1.63 - v0 = unproject(Vector2(1, 0)); 1.64 - v1 = unproject(Vector2(1, 1)); 1.65 - LINE(v0.x, v0.y, v1.x, v1.y); 1.66 + right[0] = unproject(Vector2(1, 0)); 1.67 + right[1] = unproject(Vector2(1, 1)); 1.68 + LINE(right[0].x, right[0].y, right[1].x, right[1].y); 1.69 1.70 - v0 = unproject(Vector2(-1, 0)); 1.71 - v1 = unproject(Vector2(-1, 1)); 1.72 - LINE(v0.x, v0.y, v1.x, v1.y); 1.73 + left[0] = unproject(Vector2(-1, 0)); 1.74 + left[1] = unproject(Vector2(-1, 1)); 1.75 + LINE(left[0].x, left[0].y, left[1].x, left[1].y); 1.76 1.77 - LINE(-0.5, proj_near, 0.5, proj_near); 1.78 + float dist = calc_proj_dist(proj_vfov); 1.79 + LINE(-1, dist, 1, dist); 1.80 + glEnd(); 1.81 1.82 + /* find the points where the frustum lines hit zero */ 1.83 + glEnable(GL_LINE_STIPPLE); 1.84 + glLineStipple(3, 0x0101); 1.85 + 1.86 + glBegin(GL_LINES); 1.87 + for(int i=0; i<2; i++) { 1.88 + Vector2 *v = i ? left : right; 1.89 + float t = -v[1].y / (v[0].y - v[1].y); 1.90 + float x = v[1].x + (v[0].x - v[1].x) * t; 1.91 + LINE(v[0].x, v[0].y, x, 0); 1.92 + } 1.93 glEnd(); 1.94 + glDisable(GL_LINE_STIPPLE); 1.95 } 1.96 1.97 void draw_label(const Vector2 &pos, const char *fmt, ...) 1.98 @@ -199,6 +221,7 @@ 1.99 void keyb(unsigned char key, int x, int y) 1.100 { 1.101 keystate[key] = 1; 1.102 + modkeys = glutGetModifiers(); 1.103 1.104 switch(key) { 1.105 case 27: 1.106 @@ -209,16 +232,15 @@ 1.107 void keyb_up(unsigned char key, int x, int y) 1.108 { 1.109 keystate[key] = 0; 1.110 + modkeys = glutGetModifiers(); 1.111 } 1.112 1.113 -bool bnstate[16]; 1.114 -int prev_x, prev_y; 1.115 - 1.116 void mouse(int bn, int st, int x, int y) 1.117 { 1.118 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; 1.119 prev_x = x; 1.120 prev_y = y; 1.121 + modkeys = glutGetModifiers(); 1.122 1.123 if(bn == GLUT_LEFT_BUTTON && st == GLUT_DOWN) { 1.124 cur_point = screen_to_world(x, y); 1.125 @@ -236,6 +258,10 @@ 1.126 1.127 if(bnstate[0]) { 1.128 cur_point = screen_to_world(x, y); 1.129 + if(keystate['s'] || keystate['S']) { 1.130 + cur_point.x = round(cur_point.x / snap_dist) * snap_dist; 1.131 + cur_point.y = round(cur_point.y / snap_dist) * snap_dist; 1.132 + } 1.133 glutPostRedisplay(); 1.134 } 1.135 if(bnstate[1]) { 1.136 @@ -268,7 +294,7 @@ 1.137 Vector2 project(const Vector2 &v) 1.138 { 1.139 Vector4 ppt = Vector4(v.x, 0, -v.y, 1).transformed(proj); 1.140 - return Vector2(ppt.x / ppt.w, ppt.z / ppt.w); 1.141 + return Vector2(ppt.x / ppt.w, calc_proj_dist(proj_vfov)); 1.142 } 1.143 1.144 Vector2 unproject(const Vector2 &v) 1.145 @@ -279,3 +305,8 @@ 1.146 Vector4 res4 = v4.transformed(inv_proj); 1.147 return Vector2(res4.x / res4.w, -res4.z / res4.w); 1.148 } 1.149 + 1.150 +float calc_proj_dist(float fov) 1.151 +{ 1.152 + return 1.0 / tan(DEG_TO_RAD(fov) / 2.0); 1.153 +}