2012-01-23 23:45:33 +00:00
|
|
|
#include "camera.h"
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "euclid.h"
|
2012-01-24 13:16:20 +00:00
|
|
|
#include "render.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
#include "shared/types.h"
|
|
|
|
#include "shared/constants.h"
|
2012-01-29 11:34:49 +00:00
|
|
|
#include "scenery.h"
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "tools.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraInit()
|
|
|
|
{
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-02-12 16:57:29 +00:00
|
|
|
void cameraQuit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraSave(FILE* f, CameraDefinition* camera)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 17:39:56 +00:00
|
|
|
v3Save(f, &camera->location);
|
|
|
|
toolsSaveDouble(f, &camera->yaw);
|
|
|
|
toolsSaveDouble(f, &camera->pitch);
|
|
|
|
toolsSaveDouble(f, &camera->roll);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cameraLoad(FILE* f, CameraDefinition* camera)
|
|
|
|
{
|
2012-01-29 17:39:56 +00:00
|
|
|
v3Load(f, &camera->location);
|
|
|
|
toolsLoadDouble(f, &camera->yaw);
|
|
|
|
toolsLoadDouble(f, &camera->pitch);
|
|
|
|
toolsLoadDouble(f, &camera->roll);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(camera, 0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
CameraDefinition cameraCreateDefinition()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
CameraDefinition definition;
|
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
definition.location.x = 0.0;
|
2012-01-23 23:45:33 +00:00
|
|
|
definition.location.y = 0.0;
|
|
|
|
definition.location.z = 0.0;
|
2012-01-29 11:34:49 +00:00
|
|
|
definition.yaw = 0.0;
|
|
|
|
definition.pitch = 0.0;
|
|
|
|
definition.roll = 0.0;
|
2012-01-31 11:20:52 +00:00
|
|
|
|
|
|
|
definition.width = 1.0;
|
|
|
|
definition.height = 1.0;
|
|
|
|
definition.yfov = 1.57;
|
|
|
|
definition.xratio = 1.0;
|
|
|
|
definition.znear = 1.0;
|
|
|
|
definition.zfar = 1000.0;
|
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(&definition, 0);
|
2012-01-23 23:45:33 +00:00
|
|
|
|
|
|
|
return definition;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraDeleteDefinition(CameraDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraCopyDefinition(CameraDefinition* source, CameraDefinition* destination)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
*destination = *source;
|
2012-01-29 11:34:49 +00:00
|
|
|
|
|
|
|
cameraValidateDefinition(destination, 0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
void cameraValidateDefinition(CameraDefinition* definition, int check_above)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 11:34:49 +00:00
|
|
|
WaterDefinition water;
|
|
|
|
TerrainDefinition terrain;
|
|
|
|
double water_height, terrain_height, diff;
|
|
|
|
Vector3 move;
|
|
|
|
Matrix4 rotation;
|
|
|
|
|
|
|
|
if (check_above)
|
|
|
|
{
|
|
|
|
water = waterCreateDefinition();
|
|
|
|
sceneryGetWater(&water);
|
|
|
|
water_height = water.height + 0.5;
|
|
|
|
waterDeleteDefinition(&water);
|
|
|
|
|
|
|
|
terrain = terrainCreateDefinition();
|
|
|
|
sceneryGetTerrain(&terrain);
|
|
|
|
terrain_height = terrainGetHeight(&terrain, definition->location.x, definition->location.z) + 0.5;
|
|
|
|
terrainDeleteDefinition(&terrain);
|
|
|
|
|
|
|
|
if (definition->location.y < water_height || definition->location.y < terrain_height)
|
|
|
|
{
|
|
|
|
if (water_height > terrain_height)
|
|
|
|
{
|
|
|
|
diff = water_height - definition->location.y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff = terrain_height - definition->location.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
move.x = move.z = 0.0;
|
|
|
|
move.y = diff;
|
|
|
|
definition->location = v3Add(definition->location, move);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
definition->forward.x = 1.0;
|
|
|
|
definition->forward.y = 0.0;
|
|
|
|
definition->forward.z = 0.0;
|
|
|
|
definition->right.x = 0.0;
|
|
|
|
definition->right.y = 0.0;
|
|
|
|
definition->right.z = 1.0;
|
2012-01-23 23:45:33 +00:00
|
|
|
definition->up.x = 0.0;
|
|
|
|
definition->up.y = 1.0;
|
|
|
|
definition->up.z = 0.0;
|
2012-01-29 11:34:49 +00:00
|
|
|
|
|
|
|
rotation = m4NewRotateEuler(definition->yaw, definition->pitch, definition->roll);
|
|
|
|
|
|
|
|
definition->forward = m4MultPoint(rotation, definition->forward);
|
|
|
|
definition->right = m4MultPoint(rotation, definition->right);
|
|
|
|
definition->up = m4MultPoint(rotation, definition->up);
|
|
|
|
|
|
|
|
definition->target = v3Add(definition->location, definition->forward);
|
|
|
|
|
2012-01-31 11:20:52 +00:00
|
|
|
definition->project = m4Mult(m4NewPerspective(definition->yfov, definition->xratio, definition->znear, definition->zfar), m4NewLookAt(definition->location, definition->target, definition->up));
|
2012-01-23 23:45:33 +00:00
|
|
|
definition->unproject = m4Inverse(definition->project);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraSetLocation(CameraDefinition* camera, double x, double y, double z)
|
|
|
|
{
|
|
|
|
camera->location.x = x;
|
|
|
|
camera->location.y = y;
|
|
|
|
camera->location.z = z;
|
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(camera, 0);
|
2012-01-23 23:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cameraSetTarget(CameraDefinition* camera, double x, double y, double z)
|
|
|
|
{
|
2012-01-29 11:34:49 +00:00
|
|
|
Vector3 forward, target;
|
|
|
|
|
|
|
|
target.x = x;
|
|
|
|
target.y = y;
|
|
|
|
target.z = z;
|
|
|
|
|
|
|
|
forward = v3Sub(target, camera->location);
|
|
|
|
if (v3Norm(forward) < 0.0000001)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
forward = v3Normalize(forward);
|
|
|
|
|
|
|
|
if (fabs(forward.x) < 0.0000001 && fabs(forward.z) < 0.0000001)
|
|
|
|
{
|
|
|
|
/* Forward vector is vertical */
|
|
|
|
if (forward.y > 0.0)
|
|
|
|
{
|
|
|
|
camera->pitch = M_PI_2;
|
|
|
|
}
|
|
|
|
else if (forward.y > 0.0)
|
|
|
|
{
|
|
|
|
camera->pitch = -M_PI_2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* TODO Guess angles */
|
|
|
|
}
|
|
|
|
|
|
|
|
cameraValidateDefinition(camera, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cameraSetRoll(CameraDefinition* camera, double angle)
|
|
|
|
{
|
|
|
|
camera->roll = angle;
|
|
|
|
|
|
|
|
cameraValidateDefinition(camera, 0);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-27 16:01:34 +00:00
|
|
|
void cameraStrafeForward(CameraDefinition* camera, double value)
|
|
|
|
{
|
2012-01-29 11:34:49 +00:00
|
|
|
camera->location = v3Add(camera->location, v3Scale(camera->forward, value));
|
2012-01-27 16:01:34 +00:00
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(camera, 0);
|
2012-01-27 16:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cameraStrafeRight(CameraDefinition* camera, double value)
|
|
|
|
{
|
2012-01-29 11:34:49 +00:00
|
|
|
camera->location = v3Add(camera->location, v3Scale(camera->right, value));
|
2012-01-27 16:01:34 +00:00
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(camera, 0);
|
2012-01-27 16:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cameraStrafeUp(CameraDefinition* camera, double value)
|
|
|
|
{
|
2012-01-29 11:34:49 +00:00
|
|
|
camera->location = v3Add(camera->location, v3Scale(camera->up, value));
|
2012-01-27 16:01:34 +00:00
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(camera, 0);
|
2012-01-27 16:01:34 +00:00
|
|
|
}
|
|
|
|
|
2012-01-27 17:21:57 +00:00
|
|
|
void cameraRotateYaw(CameraDefinition* camera, double value)
|
|
|
|
{
|
2012-01-29 11:34:49 +00:00
|
|
|
camera->yaw += value;
|
2012-01-27 17:21:57 +00:00
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(camera, 0);
|
|
|
|
}
|
2012-01-27 17:21:57 +00:00
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
void cameraRotatePitch(CameraDefinition* camera, double value)
|
|
|
|
{
|
|
|
|
camera->pitch += value;
|
|
|
|
|
|
|
|
cameraValidateDefinition(camera, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cameraRotateRoll(CameraDefinition* camera, double value)
|
|
|
|
{
|
|
|
|
camera->roll += value;
|
2012-01-27 17:21:57 +00:00
|
|
|
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(camera, 0);
|
2012-01-27 17:21:57 +00:00
|
|
|
}
|
|
|
|
|
2012-01-31 11:20:52 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
Vector3 cameraProject(CameraDefinition* camera, Renderer* renderer, Vector3 point)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 14:23:10 +00:00
|
|
|
point = m4Transform(camera->project, point);
|
2012-01-31 11:20:52 +00:00
|
|
|
point.x = (point.x + 1.0) * 0.5 * camera->width;
|
|
|
|
point.y = (-point.y + 1.0) * 0.5 * camera->height;
|
2011-12-10 13:25:22 +00:00
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
Vector3 cameraUnproject(CameraDefinition* camera, Renderer* renderer, Vector3 point)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-31 11:20:52 +00:00
|
|
|
point.x = (point.x / (0.5 * camera->width) - 1.0);
|
|
|
|
point.y = -(point.y / (0.5 * camera->height) - 1.0);
|
2012-01-29 14:23:10 +00:00
|
|
|
return m4Transform(camera->unproject, point);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
void cameraProjectToFragment(CameraDefinition* camera, Renderer* renderer, double x, double y, double z, RenderFragment* result)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Vector3 point = {x, y, z};
|
2012-01-29 21:45:58 +00:00
|
|
|
point = cameraProject(camera, renderer, point);
|
2012-01-29 14:23:10 +00:00
|
|
|
result->x = lround(point.x);
|
|
|
|
result->y = lround(point.y);
|
2011-12-10 13:25:22 +00:00
|
|
|
result->z = point.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a quad that will fill the view in front of the camera.
|
|
|
|
* This quad can be used for post-processing.
|
2012-01-23 23:45:33 +00:00
|
|
|
*
|
2011-12-10 13:25:22 +00:00
|
|
|
* @param col Color of the polygon.
|
|
|
|
* @param callback Post-processing callback.
|
|
|
|
*/
|
2012-01-29 21:45:58 +00:00
|
|
|
/*void cameraPushOverlay(CameraDefinition* camera, Color col, f_RenderFragmentCallback callback)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-29 21:45:58 +00:00
|
|
|
Vertex v1, v2, v3, v4;
|
2011-12-10 13:25:22 +00:00
|
|
|
Vector3 v;
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
v.x = 0.0;
|
|
|
|
v.y = 0.0;
|
|
|
|
v.z = 10.0;
|
2012-01-23 23:45:33 +00:00
|
|
|
v1.location = cameraUnproject(camera, v);
|
2011-12-10 13:25:22 +00:00
|
|
|
v1.color = col;
|
|
|
|
v1.callback = callback;
|
|
|
|
|
|
|
|
v.x = 0.0;
|
|
|
|
v.y = (double)render_height;
|
|
|
|
v.z = 10.0;
|
2012-01-23 23:45:33 +00:00
|
|
|
v2.location = cameraUnproject(camera, v);
|
2011-12-10 13:25:22 +00:00
|
|
|
v2.color = col;
|
|
|
|
v2.callback = callback;
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
v.x = (double)render_width;
|
|
|
|
v.y = (double)render_height;
|
|
|
|
v.z = 10.0;
|
2012-01-23 23:45:33 +00:00
|
|
|
v3.location = cameraUnproject(camera, v);
|
2011-12-10 13:25:22 +00:00
|
|
|
v3.color = col;
|
|
|
|
v3.callback = callback;
|
|
|
|
|
|
|
|
v.x = (double)render_width;
|
|
|
|
v.y = 0.0;
|
|
|
|
v.z = 10.0;
|
2012-01-23 23:45:33 +00:00
|
|
|
v4.location = cameraUnproject(camera, v);
|
2011-12-10 13:25:22 +00:00
|
|
|
v4.color = col;
|
|
|
|
v4.callback = callback;
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
renderPushQuad(&v1, &v2, &v3, &v4);
|
|
|
|
}*/
|