From 16383a2465c235afa336634dcecb77e03e47024f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Tue, 31 Jan 2012 11:20:52 +0000 Subject: [PATCH] paysages : Fixed camera ratio aspect. git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@256 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- TODO | 1 - gui_qt/mainwindow.cpp | 4 +++- gui_qt/widgetwanderer.cpp | 6 +++--- lib_paysages/camera.c | 28 ++++++++++++++++++++++------ lib_paysages/camera.h | 22 ++-------------------- lib_paysages/renderer.c | 6 +++++- lib_paysages/renderer.h | 1 + lib_paysages/scenery.c | 9 +++++---- lib_paysages/shared/types.h | 23 +++++++++++++++++++++++ 9 files changed, 64 insertions(+), 36 deletions(-) diff --git a/TODO b/TODO index ff2a26b..2dbe878 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,3 @@ -- Camera should respect ratio aspect of render area. - All noises should use the same entropy pool (saved separately), and avoid reallocs. - Implement light multi-sampling (mainly for skydome). - Implement scaling and scrolling on previews. diff --git a/gui_qt/mainwindow.cpp b/gui_qt/mainwindow.cpp index c21b183..24fe4ab 100644 --- a/gui_qt/mainwindow.cpp +++ b/gui_qt/mainwindow.cpp @@ -140,8 +140,10 @@ void MainWindow::explore3D() sceneryGetCamera(&camera); - DialogWanderer* dialog = new DialogWanderer(this, &camera, false); + DialogWanderer* dialog = new DialogWanderer(this, &camera, true); dialog->exec(); + + scenerySetCamera(&camera); delete dialog; } diff --git a/gui_qt/widgetwanderer.cpp b/gui_qt/widgetwanderer.cpp index 24966b3..b5760e6 100644 --- a/gui_qt/widgetwanderer.cpp +++ b/gui_qt/widgetwanderer.cpp @@ -190,13 +190,13 @@ void WidgetWanderer::initializeGL() void WidgetWanderer::resizeGL(int w, int h) { - double ratio = (double)h / (double)w; - + cameraSetRenderSize(&_current_camera, w, h); + glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glFrustum(-2.5, 2.5, -2.5 * ratio, 2.5 * ratio, 2.0, 1000.0); + gluPerspective(_current_camera.yfov * 180.0 / M_PI, _current_camera.xratio, _current_camera.znear, _current_camera.zfar); } static inline void _renderTerrainQuad(TerrainDefinition* terrain, double x, double z, double size) diff --git a/lib_paysages/camera.c b/lib_paysages/camera.c index 2954367..8bb3cb8 100644 --- a/lib_paysages/camera.c +++ b/lib_paysages/camera.c @@ -41,7 +41,14 @@ CameraDefinition cameraCreateDefinition() definition.yaw = 0.0; definition.pitch = 0.0; definition.roll = 0.0; - + + definition.width = 1.0; + definition.height = 1.0; + definition.yfov = 1.57; + definition.xratio = 1.0; + definition.znear = 1.0; + definition.zfar = 1000.0; + cameraValidateDefinition(&definition, 0); return definition; @@ -113,7 +120,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(definition->location, definition->target, definition->up)); + definition->project = m4Mult(m4NewPerspective(definition->yfov, definition->xratio, definition->znear, definition->zfar), m4NewLookAt(definition->location, definition->target, definition->up)); definition->unproject = m4Inverse(definition->project); } @@ -210,18 +217,27 @@ void cameraRotateRoll(CameraDefinition* camera, double value) cameraValidateDefinition(camera, 0); } +void cameraSetRenderSize(CameraDefinition* camera, int width, int height) +{ + camera->width = (double)width; + camera->height = (double)height; + camera->xratio = camera->width / camera->height; + + cameraValidateDefinition(camera, 0); +} + Vector3 cameraProject(CameraDefinition* camera, Renderer* renderer, Vector3 point) { point = m4Transform(camera->project, point); - point.x = (point.x + 1.0) * 0.5 * (double)renderer->render_width; - point.y = (-point.y + 1.0) * 0.5 * (double)renderer->render_height; + point.x = (point.x + 1.0) * 0.5 * camera->width; + point.y = (-point.y + 1.0) * 0.5 * camera->height; return point; } Vector3 cameraUnproject(CameraDefinition* camera, Renderer* renderer, Vector3 point) { - point.x = (point.x / (0.5 * (double)renderer->render_width) - 1.0); - point.y = -(point.y / (0.5 * (double)renderer->render_height) - 1.0); + point.x = (point.x / (0.5 * camera->width) - 1.0); + point.y = -(point.y / (0.5 * camera->height) - 1.0); return m4Transform(camera->unproject, point); } diff --git a/lib_paysages/camera.h b/lib_paysages/camera.h index 9d1c7f9..af14607 100644 --- a/lib_paysages/camera.h +++ b/lib_paysages/camera.h @@ -9,26 +9,6 @@ extern "C" { #endif -/* Definition of a 3D scene camera. - * - * Don't modify this structure directly, use the provided functions. - */ -typedef struct -{ - Vector3 location; - double yaw; - double pitch; - double roll; - - Vector3 target; - Vector3 forward; - Vector3 right; - Vector3 up; - - Matrix4 project; - Matrix4 unproject; -} CameraDefinition; - void cameraInit(); void cameraSave(FILE* f, CameraDefinition* camera); void cameraLoad(FILE* f, CameraDefinition* camera); @@ -49,6 +29,8 @@ void cameraRotateYaw(CameraDefinition* camera, double value); void cameraRotatePitch(CameraDefinition* camera, double value); void cameraRotateRoll(CameraDefinition* camera, double value); +void cameraSetRenderSize(CameraDefinition* camera, int width, int height); + Vector3 cameraProject(CameraDefinition* camera, Renderer* renderer, Vector3 point); Vector3 cameraUnproject(CameraDefinition* camera, Renderer* renderer, Vector3 point); void cameraProjectToFragment(CameraDefinition* camera, Renderer* renderer, double x, double y, double z, RenderFragment* result); diff --git a/lib_paysages/renderer.c b/lib_paysages/renderer.c index 172307f..2bb96e6 100644 --- a/lib_paysages/renderer.c +++ b/lib_paysages/renderer.c @@ -109,7 +109,8 @@ Renderer rendererCreate() result.render_interrupt = 0; result.render_progress = 0.0; result.is_rendering = 0; - result.camera_location = VECTOR_ZERO; + result.render_camera = cameraCreateDefinition(); + result.camera_location = result.render_camera.location; result.render_area = renderCreateArea(); renderSetSize(result.render_area, 1, 1); @@ -155,6 +156,9 @@ void rendererStart(Renderer* renderer, int width, int height, int quality) renderer->render_height = height; renderer->render_interrupt = 0; renderer->render_progress = 0.0; + + cameraSetRenderSize(&renderer->render_camera, width, height); + renderer->camera_location = renderer->render_camera.location; renderSetBackgroundColor(renderer->render_area, &COLOR_BLACK); renderSetSize(renderer->render_area, width, height); diff --git a/lib_paysages/renderer.h b/lib_paysages/renderer.h index 6c1032f..c1fa463 100644 --- a/lib_paysages/renderer.h +++ b/lib_paysages/renderer.h @@ -15,6 +15,7 @@ struct Renderer int render_quality; int render_width; int render_height; + CameraDefinition render_camera; Vector3 camera_location; /* Render related */ diff --git a/lib_paysages/scenery.c b/lib_paysages/scenery.c index 48a94da..a01d5d9 100644 --- a/lib_paysages/scenery.c +++ b/lib_paysages/scenery.c @@ -269,23 +269,23 @@ static Color _applyClouds(Renderer* renderer, Color base, Vector3 start, Vector3 static Vector3 _projectPoint(Renderer* renderer, Vector3 point) { - return cameraProject(&_camera, renderer, point); + return cameraProject(&renderer->render_camera, renderer, point); } static Vector3 _unprojectPoint(Renderer* renderer, Vector3 point) { - return cameraUnproject(&_camera, renderer, point); + return cameraUnproject(&renderer->render_camera, renderer, point); } static double _getPrecision(Renderer* renderer, Vector3 location) { Vector3 projected; - projected = cameraProject(&_camera, renderer, location); + projected = cameraProject(&renderer->render_camera, renderer, location); projected.x += 1.0; //projected.y += 1.0; - return v3Norm(v3Sub(cameraUnproject(&_camera, renderer, projected), location)); // / (double)render_quality; + return v3Norm(v3Sub(cameraUnproject(&renderer->render_camera, renderer, projected), location)); // / (double)render_quality; } Renderer sceneryCreateStandardRenderer() @@ -294,6 +294,7 @@ Renderer sceneryCreateStandardRenderer() result = rendererCreate(); + cameraCopyDefinition(&_camera, &result.render_camera); result.camera_location = _camera.location; result.filterLight = _filterLight; diff --git a/lib_paysages/shared/types.h b/lib_paysages/shared/types.h index 7c10f7c..ca9a8e3 100644 --- a/lib_paysages/shared/types.h +++ b/lib_paysages/shared/types.h @@ -121,6 +121,29 @@ typedef struct double base_height; } HeightInfo; +typedef struct +{ + Vector3 location; + double yaw; + double pitch; + double roll; + + Vector3 target; + Vector3 forward; + Vector3 right; + Vector3 up; + + double width; + double height; + double yfov; + double xratio; + double znear; + double zfar; + + Matrix4 project; + Matrix4 unproject; +} CameraDefinition; + #ifdef __cplusplus } #endif