2012-12-09 17:49:28 +00:00
|
|
|
#include "public.h"
|
|
|
|
#include "private.h"
|
2013-06-26 15:28:21 +00:00
|
|
|
#include "ter_raster.h"
|
2012-12-09 17:49:28 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2013-06-27 09:47:16 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include "../tools/boundingbox.h"
|
2012-12-09 17:49:28 +00:00
|
|
|
#include "../renderer.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Terrain rasterization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline Vector3 _getPoint(TerrainDefinition* definition, Renderer* renderer, double x, double z)
|
|
|
|
{
|
|
|
|
Vector3 result;
|
|
|
|
|
|
|
|
result.x = x;
|
2013-01-14 12:08:38 +00:00
|
|
|
result.y = renderer->terrain->getHeight(renderer, x, z, 1);
|
2012-12-09 17:49:28 +00:00
|
|
|
result.z = z;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Color _postProcessFragment(Renderer* renderer, Vector3 point, void* data)
|
|
|
|
{
|
|
|
|
double precision;
|
|
|
|
|
|
|
|
point = _getPoint(renderer->terrain->definition, renderer, point.x, point.z);
|
|
|
|
|
|
|
|
precision = renderer->getPrecision(renderer, point);
|
|
|
|
return renderer->terrain->getFinalColor(renderer, point, precision);
|
|
|
|
}
|
|
|
|
|
2013-04-04 20:44:49 +00:00
|
|
|
static void _renderQuad(Renderer* renderer, double x, double z, double size, double water_height)
|
2012-12-09 17:49:28 +00:00
|
|
|
{
|
2013-04-06 13:29:12 +00:00
|
|
|
Vector3 ov1, ov2, ov3, ov4;
|
|
|
|
Vector3 dv1, dv2, dv3, dv4;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-06-26 15:28:21 +00:00
|
|
|
ov1.y = ov2.y = ov3.y = ov4.y = 0.0;
|
2013-04-04 20:44:49 +00:00
|
|
|
|
2013-04-06 13:29:12 +00:00
|
|
|
ov1.x = x;
|
|
|
|
ov1.z = z;
|
|
|
|
dv1 = renderer->terrain->getResult(renderer, x, z, 1, 1).location;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-04-06 13:29:12 +00:00
|
|
|
ov2.x = x;
|
|
|
|
ov2.z = z + size;
|
|
|
|
dv2 = renderer->terrain->getResult(renderer, x, z + size, 1, 1).location;
|
|
|
|
|
|
|
|
ov3.x = x + size;
|
|
|
|
ov3.z = z + size;
|
|
|
|
dv3 = renderer->terrain->getResult(renderer, x + size, z + size, 1, 1).location;
|
|
|
|
|
|
|
|
ov4.x = x + size;
|
|
|
|
ov4.z = z;
|
|
|
|
dv4 = renderer->terrain->getResult(renderer, x + size, z, 1, 1).location;
|
|
|
|
|
|
|
|
if (dv1.y > water_height || dv2.y > water_height || dv3.y > water_height || dv4.y > water_height)
|
2012-12-09 17:49:28 +00:00
|
|
|
{
|
2013-04-06 13:29:12 +00:00
|
|
|
renderer->pushDisplacedQuad(renderer, dv1, dv2, dv3, dv4, ov1, ov2, ov3, ov4, _postProcessFragment, NULL);
|
2012-12-09 17:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 15:28:21 +00:00
|
|
|
void terrainTessellateChunk(Renderer* renderer, TerrainChunkInfo* chunk, int detail)
|
|
|
|
{
|
2013-06-27 09:47:16 +00:00
|
|
|
if (detail < 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
double water_height = renderer->water->getHeightInfo(renderer).min_height;
|
|
|
|
|
|
|
|
double startx = chunk->point_nw.x;
|
|
|
|
double startz = chunk->point_nw.z;
|
|
|
|
double size = (chunk->point_ne.x - chunk->point_nw.x) / (double)detail;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < detail; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < detail; j++)
|
|
|
|
{
|
|
|
|
_renderQuad(renderer, startx + (double)i * size, startz + (double)j * size, size, water_height);
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 15:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _getChunk(Renderer* renderer, TerrainChunkInfo* chunk, double x, double z, double size, double water_height, int displaced)
|
|
|
|
{
|
|
|
|
chunk->point_nw = renderer->terrain->getResult(renderer, x, z, 1, displaced).location;
|
|
|
|
chunk->point_sw = renderer->terrain->getResult(renderer, x, z + size, 1, displaced).location;
|
|
|
|
chunk->point_se = renderer->terrain->getResult(renderer, x + size, z + size, 1, displaced).location;
|
|
|
|
chunk->point_ne = renderer->terrain->getResult(renderer, x + size, z, 1, displaced).location;
|
|
|
|
|
2013-06-27 09:47:16 +00:00
|
|
|
double displacement_power;
|
|
|
|
if (displaced)
|
|
|
|
{
|
|
|
|
displacement_power = 0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
displacement_power = texturesGetMaximalDisplacement(renderer->textures->definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundingBox box;
|
|
|
|
boundingBoxReset(&box);
|
|
|
|
if (displacement_power > 0.0)
|
|
|
|
{
|
|
|
|
boundingBoxPushPoint(&box, v3Add(chunk->point_nw, v3(-displacement_power, displacement_power, -displacement_power)));
|
|
|
|
boundingBoxPushPoint(&box, v3Add(chunk->point_nw, v3(-displacement_power, -displacement_power, -displacement_power)));
|
|
|
|
boundingBoxPushPoint(&box, v3Add(chunk->point_sw, v3(-displacement_power, displacement_power, displacement_power)));
|
|
|
|
boundingBoxPushPoint(&box, v3Add(chunk->point_sw, v3(-displacement_power, -displacement_power, displacement_power)));
|
|
|
|
boundingBoxPushPoint(&box, v3Add(chunk->point_se, v3(displacement_power, displacement_power, displacement_power)));
|
|
|
|
boundingBoxPushPoint(&box, v3Add(chunk->point_se, v3(displacement_power, -displacement_power, displacement_power)));
|
|
|
|
boundingBoxPushPoint(&box, v3Add(chunk->point_ne, v3(displacement_power, displacement_power, -displacement_power)));
|
|
|
|
boundingBoxPushPoint(&box, v3Add(chunk->point_ne, v3(displacement_power, -displacement_power, -displacement_power)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
boundingBoxPushPoint(&box, chunk->point_nw);
|
|
|
|
boundingBoxPushPoint(&box, chunk->point_sw);
|
|
|
|
boundingBoxPushPoint(&box, chunk->point_se);
|
|
|
|
boundingBoxPushPoint(&box, chunk->point_ne);
|
|
|
|
}
|
2013-06-26 15:28:21 +00:00
|
|
|
|
2013-06-27 09:47:16 +00:00
|
|
|
int coverage = cameraIsUnprojectedBoxInView(renderer->render_camera, &box);
|
|
|
|
if (coverage > 0)
|
2013-06-26 15:28:21 +00:00
|
|
|
{
|
2013-06-27 09:47:16 +00:00
|
|
|
chunk->detail_hint = (int)ceil(sqrt((double)coverage) / (double)(21 - 2 * renderer->render_quality));
|
|
|
|
if (chunk->detail_hint > 5 * renderer->render_quality)
|
|
|
|
{
|
|
|
|
chunk->detail_hint = 5 * renderer->render_quality;
|
|
|
|
}
|
2013-06-26 15:28:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-27 09:47:16 +00:00
|
|
|
/* Not in view */
|
2013-06-26 15:28:21 +00:00
|
|
|
chunk->detail_hint = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void terrainGetTessellationInfo(Renderer* renderer, FuncTerrainTessellationCallback callback, int displaced)
|
2012-12-09 17:49:28 +00:00
|
|
|
{
|
2013-06-26 15:28:21 +00:00
|
|
|
TerrainChunkInfo chunk;
|
2012-12-09 17:49:28 +00:00
|
|
|
int chunk_factor, chunk_count, i;
|
2013-01-31 15:10:11 +00:00
|
|
|
Vector3 cam = renderer->getCameraLocation(renderer, VECTOR_ZERO);
|
2013-06-26 15:28:21 +00:00
|
|
|
double progress;
|
|
|
|
double radius_int, radius_ext;
|
|
|
|
double base_chunk_size, chunk_size;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-06-26 15:28:21 +00:00
|
|
|
base_chunk_size = 5.0 / (double)renderer->render_quality;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
|
|
|
chunk_factor = 1;
|
|
|
|
chunk_count = 2;
|
|
|
|
radius_int = 0.0;
|
2013-06-26 15:28:21 +00:00
|
|
|
radius_ext = base_chunk_size;
|
|
|
|
chunk_size = base_chunk_size;
|
|
|
|
progress = 0.0; /* TODO */
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-06-27 09:47:16 +00:00
|
|
|
double cx = cam.x - fmod(cam.x, base_chunk_size);
|
|
|
|
double cz = cam.z - fmod(cam.x, base_chunk_size);
|
|
|
|
|
|
|
|
double water_height = renderer->water->getHeightInfo(renderer).min_height;
|
2012-12-09 17:49:28 +00:00
|
|
|
|
2013-01-16 15:26:33 +00:00
|
|
|
while (radius_int < 5000.0)
|
2012-12-09 17:49:28 +00:00
|
|
|
{
|
|
|
|
for (i = 0; i < chunk_count - 1; i++)
|
|
|
|
{
|
2013-06-26 15:28:21 +00:00
|
|
|
_getChunk(renderer, &chunk, cx - radius_ext + chunk_size * i, cz - radius_ext, chunk_size, water_height, displaced);
|
|
|
|
if (!callback(renderer, &chunk, progress))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getChunk(renderer, &chunk, cx + radius_int, cz - radius_ext + chunk_size * i, chunk_size, water_height, displaced);
|
|
|
|
if (!callback(renderer, &chunk, progress))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getChunk(renderer, &chunk, cx + radius_int - chunk_size * i, cz + radius_int, chunk_size, water_height, displaced);
|
|
|
|
if (!callback(renderer, &chunk, progress))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getChunk(renderer, &chunk, cx - radius_ext, cz + radius_int - chunk_size * i, chunk_size, water_height, displaced);
|
|
|
|
if (!callback(renderer, &chunk, progress))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-12-09 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 15:28:21 +00:00
|
|
|
if (radius_int > 20.0 && chunk_count % 64 == 0 && (double)chunk_factor < radius_int / 20.0)
|
2012-12-09 17:49:28 +00:00
|
|
|
{
|
|
|
|
chunk_count /= 2;
|
|
|
|
chunk_factor *= 2;
|
|
|
|
}
|
|
|
|
chunk_count += 2;
|
2013-06-26 15:28:21 +00:00
|
|
|
chunk_size = base_chunk_size * chunk_factor;
|
2012-12-09 17:49:28 +00:00
|
|
|
radius_int = radius_ext;
|
|
|
|
radius_ext += chunk_size;
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 15:28:21 +00:00
|
|
|
|
|
|
|
static int _standardTessellationCallback(Renderer* renderer, TerrainChunkInfo* chunk, double progress)
|
|
|
|
{
|
|
|
|
terrainTessellateChunk(renderer, chunk, chunk->detail_hint);
|
|
|
|
return !renderer->render_interrupt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void terrainRenderSurface(Renderer* renderer)
|
|
|
|
{
|
|
|
|
terrainGetTessellationInfo(renderer, _standardTessellationCallback, 0);
|
|
|
|
}
|