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-24 13:16:20 +00:00
|
|
|
#include "render.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
#include "shared/types.h"
|
|
|
|
#include "shared/globals.h"
|
|
|
|
#include "shared/constants.h"
|
|
|
|
#include "shared/functions.h"
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraInit()
|
|
|
|
{
|
|
|
|
}
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraSave(FILE* f, CameraDefinition* camera)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
v3Save(camera->location, f);
|
|
|
|
v3Save(camera->target, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cameraLoad(FILE* f, CameraDefinition* camera)
|
|
|
|
{
|
|
|
|
camera->location = v3Load(f);
|
|
|
|
camera->target = v3Load(f);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
cameraValidateDefinition(camera);
|
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;
|
|
|
|
|
|
|
|
definition.location.x = -1.0;
|
|
|
|
definition.location.y = 0.0;
|
|
|
|
definition.location.z = 0.0;
|
|
|
|
definition.target.x = 0.0;
|
|
|
|
definition.target.y = 0.0;
|
|
|
|
definition.target.z = 0.0;
|
|
|
|
|
|
|
|
cameraValidateDefinition(&definition);
|
|
|
|
|
|
|
|
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;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraValidateDefinition(CameraDefinition* definition)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
/* TODO Recompute up vector */
|
|
|
|
definition->up.x = 0.0;
|
|
|
|
definition->up.y = 1.0;
|
|
|
|
definition->up.z = 0.0;
|
|
|
|
|
|
|
|
definition->project = m4Mult(m4NewPerspective(1.57, 1.333333, 1.0, 1000.0), m4NewLookAt(VECTOR_ZERO, v3Sub(definition->target, definition->location), definition->up));
|
|
|
|
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;
|
|
|
|
|
|
|
|
cameraValidateDefinition(camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cameraSetTarget(CameraDefinition* camera, double x, double y, double z)
|
|
|
|
{
|
|
|
|
camera->target.x = x;
|
|
|
|
camera->target.y = y;
|
|
|
|
camera->target.z = z;
|
|
|
|
|
|
|
|
cameraValidateDefinition(camera);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cameraSetAngle(CameraDefinition* camera, double angle)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
Vector3 cameraProject(CameraDefinition* camera, Vector3 point)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-23 23:45:33 +00:00
|
|
|
point = m4Transform(camera->project, v3Sub(point, camera->location));
|
2011-12-10 13:25:22 +00:00
|
|
|
point.x = (-point.x + 1.0) * 0.5 * (double)render_width;
|
|
|
|
point.y = (-point.y + 1.0) * 0.5 * (double)render_height;
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
Vector3 cameraUnproject(CameraDefinition* camera, Vector3 point)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
point.x = -(point.x / (0.5 * (double)render_width) - 1.0);
|
|
|
|
point.y = -(point.y / (0.5 * (double)render_height) - 1.0);
|
2012-01-23 23:45:33 +00:00
|
|
|
return v3Add(m4Transform(camera->unproject, point), camera->location);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-23 23:45:33 +00:00
|
|
|
void cameraProjectToFragment(CameraDefinition* camera, double x, double y, double z, RenderFragment* result)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Vector3 point = {x, y, z};
|
2012-01-23 23:45:33 +00:00
|
|
|
point = m4Transform(camera->project, v3Sub(point, camera->location));
|
2011-12-10 13:25:22 +00:00
|
|
|
result->x = lround((-point.x + 1.0) * 0.5 * (double)render_width);
|
|
|
|
result->y = lround((-point.y + 1.0) * 0.5 * (double)render_height);
|
|
|
|
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-23 23:45:33 +00:00
|
|
|
void cameraPushOverlay(CameraDefinition* camera, Color col, f_RenderFragmentCallback callback)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-01-24 13:16:20 +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-24 13:16:20 +00:00
|
|
|
renderPushQuad(&v1, &v2, &v3, &v4);*/
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|