2012-01-23 23:45:33 +00:00
|
|
|
#include "renderer.h"
|
2012-05-29 13:32:23 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
#include "lighting.h"
|
2012-01-29 21:45:58 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "render.h"
|
|
|
|
#include "scenery.h"
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
RayCastingResult _RAYCASTING_NULL = {0};
|
2012-01-29 17:53:12 +00:00
|
|
|
HeightInfo _WATER_HEIGHT_INFO = {-1000000.0, -1000000.0, -1000000.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;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2012-01-30 13:10:16 +00:00
|
|
|
return !renderer->render_interrupt;
|
2012-01-29 21:45:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static double _getPrecision(Renderer* renderer, Vector3 location)
|
2012-01-29 21:45:58 +00:00
|
|
|
{
|
|
|
|
return 0.001;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vector3 _projectPoint(Renderer* renderer, Vector3 point)
|
|
|
|
{
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vector3 _unprojectPoint(Renderer* renderer, Vector3 point)
|
|
|
|
{
|
|
|
|
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-06-17 09:40:40 +00:00
|
|
|
p1 = renderer->projectPoint(renderer, v1);
|
|
|
|
p2 = renderer->projectPoint(renderer, v2);
|
|
|
|
p3 = renderer->projectPoint(renderer, v3);
|
2012-01-29 21:45:58 +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-06-05 20:22:12 +00:00
|
|
|
static void _alterLight(Renderer* renderer, LightDefinition* light, Vector3 location)
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-05 20:22:12 +00:00
|
|
|
static void _getLightStatus(Renderer* renderer, LightStatus* status, Vector3 location)
|
2012-01-25 17:31:36 +00:00
|
|
|
{
|
2012-06-05 20:22:12 +00:00
|
|
|
status->nblights = 0;
|
2012-01-25 17:31:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 20:22:12 +00:00
|
|
|
static Color _applyLightStatus(Renderer* renderer, LightStatus* status, Vector3 location, Vector3 normal, SurfaceMaterial material)
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
2012-06-05 20:22:12 +00:00
|
|
|
return lightingApplyStatusToSurface(renderer, status, location, normal, material);
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static double _getTerrainHeight(Renderer* renderer, double x, double z)
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:53:12 +00:00
|
|
|
static HeightInfo _getWaterHeightInfo(Renderer* renderer)
|
|
|
|
{
|
|
|
|
return _WATER_HEIGHT_INFO;
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static Color _applyTextures(Renderer* renderer, Vector3 location, double precision)
|
2012-01-24 13:16:20 +00:00
|
|
|
{
|
|
|
|
return COLOR_TRANSPARENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Color _applyAtmosphere(Renderer* renderer, Vector3 location, Color base)
|
|
|
|
{
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Color _applyClouds(Renderer* renderer, Color base, Vector3 start, Vector3 end)
|
|
|
|
{
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2012-01-29 21:45:58 +00:00
|
|
|
Renderer rendererCreate()
|
2012-01-23 23:45:33 +00:00
|
|
|
{
|
|
|
|
Renderer result;
|
2012-06-13 15:38:11 +00:00
|
|
|
RenderParams params = {1, 1, 1, 5};
|
2012-01-23 23:45:33 +00:00
|
|
|
|
|
|
|
result.render_quality = 5;
|
2012-01-29 21:45:58 +00:00
|
|
|
result.render_width = 1;
|
|
|
|
result.render_height = 1;
|
|
|
|
result.render_interrupt = 0;
|
|
|
|
result.render_progress = 0.0;
|
|
|
|
result.is_rendering = 0;
|
2012-01-31 11:20:52 +00:00
|
|
|
result.render_camera = cameraCreateDefinition();
|
|
|
|
result.camera_location = result.render_camera.location;
|
2012-01-29 21:45:58 +00:00
|
|
|
result.render_area = renderCreateArea();
|
2012-06-13 15:38:11 +00:00
|
|
|
|
|
|
|
renderSetParams(result.render_area, params);
|
2012-01-29 21:45:58 +00:00
|
|
|
|
|
|
|
result.addRenderProgress = _addRenderProgress;
|
|
|
|
result.getPrecision = _getPrecision;
|
|
|
|
result.projectPoint = _projectPoint;
|
|
|
|
result.unprojectPoint = _unprojectPoint;
|
|
|
|
result.pushTriangle = _pushTriangle;
|
|
|
|
result.pushQuad = _pushQuad;
|
2012-01-23 23:45:33 +00:00
|
|
|
|
2012-01-24 13:16:20 +00:00
|
|
|
result.rayWalking = _rayWalking;
|
|
|
|
result.getTerrainHeight = _getTerrainHeight;
|
2012-01-29 17:53:12 +00:00
|
|
|
result.getWaterHeightInfo = _getWaterHeightInfo;
|
2012-01-24 13:16:20 +00:00
|
|
|
result.applyTextures = _applyTextures;
|
|
|
|
result.applyAtmosphere = _applyAtmosphere;
|
|
|
|
result.applyClouds = _applyClouds;
|
2012-01-29 21:45:58 +00:00
|
|
|
|
2012-06-05 20:22:12 +00:00
|
|
|
result.alterLight = _alterLight;
|
|
|
|
result.getLightStatus = _getLightStatus;
|
|
|
|
result.applyLightStatus = _applyLightStatus;
|
2012-01-23 23:45:33 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-01-29 21:45:58 +00:00
|
|
|
|
|
|
|
void rendererDelete(Renderer* renderer)
|
|
|
|
{
|
|
|
|
renderDeleteArea(renderer->render_area);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-01-31 11:20:52 +00:00
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
cameraSetRenderSize(&renderer->render_camera, renderer->render_width, renderer->render_height);
|
2012-01-31 11:20:52 +00:00
|
|
|
renderer->camera_location = renderer->render_camera.location;
|
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);
|
|
|
|
|
|
|
|
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
|
|
|
}
|