From b46d25628aef169bc0e82af55248a7e98b4d2ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 29 Jan 2012 14:23:10 +0000 Subject: [PATCH] paysages: Finally fixed camera transformation ! git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@249 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- gui_qt/widgetwanderer.cpp | 2 +- lib_paysages/camera.c | 16 ++++++++-------- lib_paysages/euclid.c | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/gui_qt/widgetwanderer.cpp b/gui_qt/widgetwanderer.cpp index 4a3186f..29e226c 100644 --- a/gui_qt/widgetwanderer.cpp +++ b/gui_qt/widgetwanderer.cpp @@ -189,7 +189,7 @@ void WidgetWanderer::resizeGL(int w, int h) { double ratio = (double)h / (double)w; - glViewport(0, 0, (GLint)w, (GLint)h); + glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); diff --git a/lib_paysages/camera.c b/lib_paysages/camera.c index f8e1b72..9db35cc 100644 --- a/lib_paysages/camera.c +++ b/lib_paysages/camera.c @@ -114,7 +114,7 @@ void cameraValidateDefinition(CameraDefinition* definition, int check_above) definition->target = v3Add(definition->location, definition->forward); - definition->project = m4Mult(m4NewPerspective(1.57, 1.333333, 1.0, 1000.0), m4NewLookAt(VECTOR_ZERO, definition->forward, definition->up)); + definition->project = m4Mult(m4NewPerspective(1.57, 1.333333, 1.0, 1000.0), m4NewLookAt(definition->location, definition->target, definition->up)); definition->unproject = m4Inverse(definition->project); } @@ -213,25 +213,25 @@ void cameraRotateRoll(CameraDefinition* camera, double value) Vector3 cameraProject(CameraDefinition* camera, Vector3 point) { - point = m4Transform(camera->project, v3Sub(point, camera->location)); - point.x = (-point.x + 1.0) * 0.5 * (double)render_width; + point = m4Transform(camera->project, point); + point.x = (point.x + 1.0) * 0.5 * (double)render_width; point.y = (-point.y + 1.0) * 0.5 * (double)render_height; return point; } Vector3 cameraUnproject(CameraDefinition* camera, Vector3 point) { - point.x = -(point.x / (0.5 * (double)render_width) - 1.0); + point.x = (point.x / (0.5 * (double)render_width) - 1.0); point.y = -(point.y / (0.5 * (double)render_height) - 1.0); - return v3Add(m4Transform(camera->unproject, point), camera->location); + return m4Transform(camera->unproject, point); } void cameraProjectToFragment(CameraDefinition* camera, double x, double y, double z, RenderFragment* result) { Vector3 point = {x, y, z}; - point = m4Transform(camera->project, v3Sub(point, camera->location)); - result->x = lround((-point.x + 1.0) * 0.5 * (double)render_width); - result->y = lround((-point.y + 1.0) * 0.5 * (double)render_height); + point = cameraProject(camera, point); + result->x = lround(point.x); + result->y = lround(point.y); result->z = point.z; } diff --git a/lib_paysages/euclid.c b/lib_paysages/euclid.c index 9864adf..ceafb98 100644 --- a/lib_paysages/euclid.c +++ b/lib_paysages/euclid.c @@ -331,14 +331,14 @@ Matrix4 m4NewRotateTripleAxis(Vector3 x, Vector3 y, Vector3 z) Matrix4 m4NewLookAt(Vector3 eye, Vector3 at, Vector3 up) { - Vector3 z = v3Normalize(v3Sub(eye, at)); + Vector3 z = v3Normalize(v3Sub(at, eye)); Vector3 x = v3Normalize(v3Cross(up, z)); Vector3 y = v3Cross(z, x); - Matrix4 result = m4NewRotateTripleAxis(x, y, v3Neg(z)); + Matrix4 result = m4NewRotateTripleAxis(x, y, z); result.d = eye.x; result.h = eye.y; result.l = eye.z; - return result; + return m4Inverse(result); } Matrix4 m4NewPerspective(double fov_y, double aspect, double near, double far)