# HG changeset patch # User John Tsiombikas # Date 1422143968 -7200 # Node ID e6ed6acee42f8f39c4ac90645562c604af3f544d # Parent 64483c640a3849c429248ecf845476007955faf3 more foo diff -r 64483c640a38 -r e6ed6acee42f main.cc --- a/main.cc Sat Jan 24 17:49:35 2015 +0200 +++ b/main.cc Sun Jan 25 01:59:28 2015 +0200 @@ -19,20 +19,27 @@ Vector2 screen_to_world(int x, int y); Vector2 project(const Vector2 &v); Vector2 unproject(const Vector2 &v); +float calc_proj_dist(float fov); int win_xsz, win_ysz; float aspect; Matrix4x4 proj; +float proj_offset; Vector2 cur_point; bool cur_point_valid; float proj_near = 0.5, proj_far = 50.0; -float proj_vfov = 50.0; +float proj_vfov = 60.0; bool keystate[256]; +bool bnstate[16]; +int prev_x, prev_y; +unsigned int modkeys; + dtx_font *font; -float pan[2] = {0, 0.85}, zoom = 0.4; +float pan[2] = {0, 0.85}, zoom = 0.3; +float snap_dist = 0.25; int main(int argc, char **argv) { @@ -65,6 +72,7 @@ glEnable(GL_MULTISAMPLE); proj.set_perspective(DEG_TO_RAD(proj_vfov), 1, 1.0, 50.0); + proj.translate(Vector3(proj_offset, 0, 0)); return true; } @@ -103,9 +111,9 @@ glEnd(); glDisable(GL_LINE_STIPPLE); - glColor3f(0.15, 0.5, 0.15); + glColor3f(0.2, 0.5, 0.2); draw_label(cur_point, "(%.2f, %.2f)", cur_point.x, cur_point.y); - glColor3f(0.4, 0.1, 0.1); + glColor3f(0.5, 0.2, 0.2); draw_label(ppt, "(%.2f, %.2f)", ppt.x, ppt.y); } @@ -146,22 +154,36 @@ void draw_frustum() { - Vector2 v0, v1; + Vector2 left[2], right[2]; glBegin(GL_LINES); glColor3f(0.2, 0.4, 0.8); - v0 = unproject(Vector2(1, 0)); - v1 = unproject(Vector2(1, 1)); - LINE(v0.x, v0.y, v1.x, v1.y); + right[0] = unproject(Vector2(1, 0)); + right[1] = unproject(Vector2(1, 1)); + LINE(right[0].x, right[0].y, right[1].x, right[1].y); - v0 = unproject(Vector2(-1, 0)); - v1 = unproject(Vector2(-1, 1)); - LINE(v0.x, v0.y, v1.x, v1.y); + left[0] = unproject(Vector2(-1, 0)); + left[1] = unproject(Vector2(-1, 1)); + LINE(left[0].x, left[0].y, left[1].x, left[1].y); - LINE(-0.5, proj_near, 0.5, proj_near); + float dist = calc_proj_dist(proj_vfov); + LINE(-1, dist, 1, dist); + glEnd(); + /* find the points where the frustum lines hit zero */ + glEnable(GL_LINE_STIPPLE); + glLineStipple(3, 0x0101); + + glBegin(GL_LINES); + for(int i=0; i<2; i++) { + Vector2 *v = i ? left : right; + float t = -v[1].y / (v[0].y - v[1].y); + float x = v[1].x + (v[0].x - v[1].x) * t; + LINE(v[0].x, v[0].y, x, 0); + } glEnd(); + glDisable(GL_LINE_STIPPLE); } void draw_label(const Vector2 &pos, const char *fmt, ...) @@ -199,6 +221,7 @@ void keyb(unsigned char key, int x, int y) { keystate[key] = 1; + modkeys = glutGetModifiers(); switch(key) { case 27: @@ -209,16 +232,15 @@ void keyb_up(unsigned char key, int x, int y) { keystate[key] = 0; + modkeys = glutGetModifiers(); } -bool bnstate[16]; -int prev_x, prev_y; - void mouse(int bn, int st, int x, int y) { bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; prev_x = x; prev_y = y; + modkeys = glutGetModifiers(); if(bn == GLUT_LEFT_BUTTON && st == GLUT_DOWN) { cur_point = screen_to_world(x, y); @@ -236,6 +258,10 @@ if(bnstate[0]) { cur_point = screen_to_world(x, y); + if(keystate['s'] || keystate['S']) { + cur_point.x = round(cur_point.x / snap_dist) * snap_dist; + cur_point.y = round(cur_point.y / snap_dist) * snap_dist; + } glutPostRedisplay(); } if(bnstate[1]) { @@ -268,7 +294,7 @@ Vector2 project(const Vector2 &v) { Vector4 ppt = Vector4(v.x, 0, -v.y, 1).transformed(proj); - return Vector2(ppt.x / ppt.w, ppt.z / ppt.w); + return Vector2(ppt.x / ppt.w, calc_proj_dist(proj_vfov)); } Vector2 unproject(const Vector2 &v) @@ -279,3 +305,8 @@ Vector4 res4 = v4.transformed(inv_proj); return Vector2(res4.x / res4.w, -res4.z / res4.w); } + +float calc_proj_dist(float fov) +{ + return 1.0 / tan(DEG_TO_RAD(fov) / 2.0); +}