2012-01-23 23:45:33 +00:00
|
|
|
#include "renderer.h"
|
2012-05-29 13:32:23 +00:00
|
|
|
|
2013-01-19 22:42:50 +00:00
|
|
|
#include "tools/lighting.h"
|
2012-01-29 21:45:58 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "render.h"
|
|
|
|
#include "scenery.h"
|
2013-01-31 15:10:11 +00:00
|
|
|
#include "tools.h"
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2013-01-31 15:10:11 +00:00
|
|
|
static RayCastingResult _RAYCASTING_NULL = {0};
|
2012-01-24 13:16:20 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
static void* _renderFirstPass(void* data)
|
|
|
|
{
|
|
|
|
Renderer* renderer = (Renderer*)data;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
sceneryRenderFirstPass(renderer);
|
|
|
|
renderer->is_rendering = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static int _addRenderProgress(Renderer* renderer, double progress)
|
2012-01-29 21:45:58 +00:00
|
|
|
{
|
2013-01-31 15:10:11 +00:00
|
|
|
UNUSED(progress);
|
2012-01-30 13:10:16 +00:00
|
|
|
return !renderer->render_interrupt;
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 15:10:11 +00:00
|
|
|
static Vector3 _getCameraLocation(Renderer* renderer, Vector3 target)
|
|
|
|
{
|
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(target);
|
|
|
|
return renderer->render_camera.location;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vector3 _getCameraDirection(Renderer* renderer, Vector3 target)
|
|
|
|
{
|
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(target);
|
|
|
|
return renderer->render_camera.forward;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static double _getPrecision(Renderer* renderer, Vector3 location)
|
2012-01-29 21:45:58 +00:00
|
|
|
{
|
2013-01-31 15:10:11 +00:00
|
|
|
UNUSED(renderer);
|
|
|
|
UNUSED(location);
|
2012-11-11 10:52:03 +00:00
|
|
|
return 0.0;
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Vector3 _projectPoint(Renderer* renderer, Vector3 point)
|
|
|
|
{
|
2013-01-31 15:10:11 +00:00
|
|
|
UNUSED(renderer);
|
2012-01-29 21:45:58 +00:00
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vector3 _unprojectPoint(Renderer* renderer, Vector3 point)
|
|
|
|
{
|
2013-01-31 15:10:11 +00:00
|
|
|
UNUSED(renderer);
|
2012-01-29 21:45:58 +00:00
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _pushTriangle(Renderer* renderer, Vector3 v1, Vector3 v2, Vector3 v3, f_RenderFragmentCallback callback, void* callback_data)
|
2012-01-29 21:45:58 +00:00
|
|
|
{
|
|
|
|
Vector3 p1, p2, p3;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
p1 = renderer->projectPoint(renderer, v1);
|
|
|
|
p2 = renderer->projectPoint(renderer, v2);
|
|
|
|
p3 = renderer->projectPoint(renderer, v3);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
renderPushTriangle(renderer->render_area, p1, p2, p3, v1, v2, v3, callback, callback_data);
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static void _pushQuad(Renderer* renderer, Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4, f_RenderFragmentCallback callback, void* callback_data)
|
2012-01-29 21:45:58 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
renderer->pushTriangle(renderer, v2, v3, v1, callback, callback_data);
|
|
|
|
renderer->pushTriangle(renderer, v4, v1, v3, callback, callback_data);
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
static RayCastingResult _rayWalking(Renderer* renderer, Vector3 location, Vector3 direction, int terrain, int water, int sky, int clouds)
|
|
|
|
{
|
|
|
|
return _RAYCASTING_NULL;
|
|
|
|
}
|
|
|
|
|
2013-02-03 21:18:32 +00:00
|
|
|
static Color _applyLightingToSurface(Renderer* renderer, Vector3 location, Vector3 normal, SurfaceMaterial* material)
|
2013-01-22 13:32:39 +00:00
|
|
|
{
|
2013-01-31 15:10:11 +00:00
|
|
|
LightStatus* light = lightingCreateStatus(renderer->lighting, location, renderer->getCameraLocation(renderer, location));
|
2013-01-22 13:32:39 +00:00
|
|
|
renderer->atmosphere->getLightingStatus(renderer, light, normal, 0);
|
|
|
|
Color result = lightingApplyStatus(light, normal, material);
|
|
|
|
lightingDeleteStatus(light);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-02-03 21:18:32 +00:00
|
|
|
static Color _applyMediumTraversal(Renderer* renderer, Vector3 location, Color color)
|
|
|
|
{
|
2013-03-14 17:29:12 +00:00
|
|
|
color = renderer->atmosphere->applyAerialPerspective(renderer, location, color).final;
|
2013-02-03 21:18:32 +00:00
|
|
|
color = renderer->clouds->getColor(renderer, color, renderer->getCameraLocation(renderer, location), location);
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
Renderer* rendererCreate()
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
2013-01-20 15:07:45 +00:00
|
|
|
Renderer* result = malloc(sizeof(Renderer));
|
2012-06-13 15:38:11 +00:00
|
|
|
RenderParams params = {1, 1, 1, 5};
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
result->render_quality = 5;
|
|
|
|
result->render_width = 1;
|
|
|
|
result->render_height = 1;
|
|
|
|
result->render_interrupt = 0;
|
|
|
|
result->render_progress = 0.0;
|
|
|
|
result->is_rendering = 0;
|
|
|
|
result->render_camera = cameraCreateDefinition();
|
|
|
|
result->render_area = renderCreateArea();
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
renderSetParams(result->render_area, params);
|
2012-01-29 21:45:58 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
result->addRenderProgress = _addRenderProgress;
|
2013-01-31 15:10:11 +00:00
|
|
|
result->getCameraLocation = _getCameraLocation;
|
|
|
|
result->getCameraDirection = _getCameraDirection;
|
2013-01-20 15:07:45 +00:00
|
|
|
result->getPrecision = _getPrecision;
|
|
|
|
result->projectPoint = _projectPoint;
|
|
|
|
result->unprojectPoint = _unprojectPoint;
|
|
|
|
result->pushTriangle = _pushTriangle;
|
|
|
|
result->pushQuad = _pushQuad;
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
result->rayWalking = _rayWalking;
|
2012-01-29 21:45:58 +00:00
|
|
|
|
2013-01-22 13:32:39 +00:00
|
|
|
result->applyLightingToSurface = _applyLightingToSurface;
|
2013-02-03 21:18:32 +00:00
|
|
|
result->applyMediumTraversal = _applyMediumTraversal;
|
2013-01-22 13:32:39 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
result->lighting = lightingManagerCreate();
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-01-20 15:07:45 +00:00
|
|
|
result->atmosphere = AtmosphereRendererClass.create();
|
2013-01-22 20:50:37 +00:00
|
|
|
result->clouds = CloudsRendererClass.create();
|
2013-01-20 15:07:45 +00:00
|
|
|
result->terrain = TerrainRendererClass.create();
|
2013-03-31 20:27:21 +00:00
|
|
|
result->textures = TexturesRendererClass.create();
|
2013-02-27 16:38:27 +00:00
|
|
|
result->water = WaterRendererClass.create();
|
2012-01-23 23:45:33 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
|
|
|
|
void rendererDelete(Renderer* renderer)
|
|
|
|
{
|
2013-01-19 22:42:50 +00:00
|
|
|
lightingManagerDelete(renderer->lighting);
|
|
|
|
|
2012-11-25 21:53:01 +00:00
|
|
|
AtmosphereRendererClass.destroy(renderer->atmosphere);
|
2013-01-22 20:50:37 +00:00
|
|
|
CloudsRendererClass.destroy(renderer->clouds);
|
2012-12-09 17:49:28 +00:00
|
|
|
TerrainRendererClass.destroy(renderer->terrain);
|
2013-03-31 20:27:21 +00:00
|
|
|
TexturesRendererClass.destroy(renderer->textures);
|
2013-02-27 16:38:27 +00:00
|
|
|
WaterRendererClass.destroy(renderer->water);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
renderDeleteArea(renderer->render_area);
|
2013-01-20 15:07:45 +00:00
|
|
|
|
|
|
|
free(renderer);
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rendererSetPreviewCallbacks(Renderer* renderer, RenderCallbackStart start, RenderCallbackDraw draw, RenderCallbackUpdate update)
|
|
|
|
{
|
|
|
|
renderSetPreviewCallbacks(renderer->render_area, start, draw, update);
|
|
|
|
}
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
void rendererStart(Renderer* renderer, RenderParams params)
|
2012-01-29 21:45:58 +00:00
|
|
|
{
|
|
|
|
Thread* thread;
|
|
|
|
int loops;
|
|
|
|
int core_count = systemGetCoreCount();
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
params.antialias = (params.antialias < 1) ? 1 : params.antialias;
|
|
|
|
params.antialias = (params.antialias > 4) ? 4 : params.antialias;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
renderer->render_quality = params.quality;
|
|
|
|
renderer->render_width = params.width * params.antialias;
|
|
|
|
renderer->render_height = params.height * params.antialias;
|
2012-01-29 21:45:58 +00:00
|
|
|
renderer->render_interrupt = 0;
|
|
|
|
renderer->render_progress = 0.0;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
cameraSetRenderSize(&renderer->render_camera, renderer->render_width, renderer->render_height);
|
2012-01-29 21:45:58 +00:00
|
|
|
|
|
|
|
renderSetBackgroundColor(renderer->render_area, &COLOR_BLACK);
|
2012-06-13 15:38:11 +00:00
|
|
|
renderSetParams(renderer->render_area, params);
|
2012-01-29 21:45:58 +00:00
|
|
|
renderClear(renderer->render_area);
|
|
|
|
|
|
|
|
renderer->is_rendering = 1;
|
|
|
|
thread = threadCreate(_renderFirstPass, renderer);
|
|
|
|
loops = 0;
|
|
|
|
|
|
|
|
while (renderer->is_rendering)
|
|
|
|
{
|
|
|
|
timeSleepMs(100);
|
|
|
|
|
|
|
|
if (++loops >= 10)
|
|
|
|
{
|
|
|
|
renderUpdate(renderer->render_area);
|
|
|
|
loops = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
threadJoin(thread);
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
renderer->is_rendering = 1;
|
|
|
|
renderPostProcess(renderer->render_area, renderer, core_count);
|
|
|
|
renderer->is_rendering = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rendererInterrupt(Renderer* renderer)
|
|
|
|
{
|
2012-01-30 13:10:16 +00:00
|
|
|
renderer->render_interrupt = 1;
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|