paysages : Fixed camera ratio aspect.

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@256 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2012-01-31 11:20:52 +00:00 committed by ThunderK
parent dc88cc5f47
commit 16383a2465
9 changed files with 64 additions and 36 deletions

1
TODO
View file

@ -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. - All noises should use the same entropy pool (saved separately), and avoid reallocs.
- Implement light multi-sampling (mainly for skydome). - Implement light multi-sampling (mainly for skydome).
- Implement scaling and scrolling on previews. - Implement scaling and scrolling on previews.

View file

@ -140,8 +140,10 @@ void MainWindow::explore3D()
sceneryGetCamera(&camera); sceneryGetCamera(&camera);
DialogWanderer* dialog = new DialogWanderer(this, &camera, false); DialogWanderer* dialog = new DialogWanderer(this, &camera, true);
dialog->exec(); dialog->exec();
scenerySetCamera(&camera);
delete dialog; delete dialog;
} }

View file

@ -190,13 +190,13 @@ void WidgetWanderer::initializeGL()
void WidgetWanderer::resizeGL(int w, int h) void WidgetWanderer::resizeGL(int w, int h)
{ {
double ratio = (double)h / (double)w; cameraSetRenderSize(&_current_camera, w, h);
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); 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) static inline void _renderTerrainQuad(TerrainDefinition* terrain, double x, double z, double size)

View file

@ -42,6 +42,13 @@ CameraDefinition cameraCreateDefinition()
definition.pitch = 0.0; definition.pitch = 0.0;
definition.roll = 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); cameraValidateDefinition(&definition, 0);
return definition; return definition;
@ -113,7 +120,7 @@ void cameraValidateDefinition(CameraDefinition* definition, int check_above)
definition->target = v3Add(definition->location, definition->forward); 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); definition->unproject = m4Inverse(definition->project);
} }
@ -210,18 +217,27 @@ void cameraRotateRoll(CameraDefinition* camera, double value)
cameraValidateDefinition(camera, 0); 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) Vector3 cameraProject(CameraDefinition* camera, Renderer* renderer, Vector3 point)
{ {
point = m4Transform(camera->project, point); point = m4Transform(camera->project, point);
point.x = (point.x + 1.0) * 0.5 * (double)renderer->render_width; point.x = (point.x + 1.0) * 0.5 * camera->width;
point.y = (-point.y + 1.0) * 0.5 * (double)renderer->render_height; point.y = (-point.y + 1.0) * 0.5 * camera->height;
return point; return point;
} }
Vector3 cameraUnproject(CameraDefinition* camera, Renderer* renderer, Vector3 point) Vector3 cameraUnproject(CameraDefinition* camera, Renderer* renderer, Vector3 point)
{ {
point.x = (point.x / (0.5 * (double)renderer->render_width) - 1.0); point.x = (point.x / (0.5 * camera->width) - 1.0);
point.y = -(point.y / (0.5 * (double)renderer->render_height) - 1.0); point.y = -(point.y / (0.5 * camera->height) - 1.0);
return m4Transform(camera->unproject, point); return m4Transform(camera->unproject, point);
} }

View file

@ -9,26 +9,6 @@
extern "C" { extern "C" {
#endif #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 cameraInit();
void cameraSave(FILE* f, CameraDefinition* camera); void cameraSave(FILE* f, CameraDefinition* camera);
void cameraLoad(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 cameraRotatePitch(CameraDefinition* camera, double value);
void cameraRotateRoll(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 cameraProject(CameraDefinition* camera, Renderer* renderer, Vector3 point);
Vector3 cameraUnproject(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); void cameraProjectToFragment(CameraDefinition* camera, Renderer* renderer, double x, double y, double z, RenderFragment* result);

View file

@ -109,7 +109,8 @@ Renderer rendererCreate()
result.render_interrupt = 0; result.render_interrupt = 0;
result.render_progress = 0.0; result.render_progress = 0.0;
result.is_rendering = 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(); result.render_area = renderCreateArea();
renderSetSize(result.render_area, 1, 1); renderSetSize(result.render_area, 1, 1);
@ -156,6 +157,9 @@ void rendererStart(Renderer* renderer, int width, int height, int quality)
renderer->render_interrupt = 0; renderer->render_interrupt = 0;
renderer->render_progress = 0.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); renderSetBackgroundColor(renderer->render_area, &COLOR_BLACK);
renderSetSize(renderer->render_area, width, height); renderSetSize(renderer->render_area, width, height);
renderClear(renderer->render_area); renderClear(renderer->render_area);

View file

@ -15,6 +15,7 @@ struct Renderer
int render_quality; int render_quality;
int render_width; int render_width;
int render_height; int render_height;
CameraDefinition render_camera;
Vector3 camera_location; Vector3 camera_location;
/* Render related */ /* Render related */

View file

@ -269,23 +269,23 @@ static Color _applyClouds(Renderer* renderer, Color base, Vector3 start, Vector3
static Vector3 _projectPoint(Renderer* renderer, Vector3 point) 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) 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) static double _getPrecision(Renderer* renderer, Vector3 location)
{ {
Vector3 projected; Vector3 projected;
projected = cameraProject(&_camera, renderer, location); projected = cameraProject(&renderer->render_camera, renderer, location);
projected.x += 1.0; projected.x += 1.0;
//projected.y += 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() Renderer sceneryCreateStandardRenderer()
@ -294,6 +294,7 @@ Renderer sceneryCreateStandardRenderer()
result = rendererCreate(); result = rendererCreate();
cameraCopyDefinition(&_camera, &result.render_camera);
result.camera_location = _camera.location; result.camera_location = _camera.location;
result.filterLight = _filterLight; result.filterLight = _filterLight;

View file

@ -121,6 +121,29 @@ typedef struct
double base_height; double base_height;
} HeightInfo; } 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 #ifdef __cplusplus
} }
#endif #endif