paysages3d/src/render/software/WaterRasterizer.cpp

88 lines
2.6 KiB
C++
Raw Normal View History

2013-12-08 17:05:18 +00:00
#include "WaterRasterizer.h"
2013-12-08 17:05:18 +00:00
#include "SoftwareRenderer.h"
#include "WaterRenderer.h"
2014-08-18 10:17:16 +00:00
#include "CanvasFragment.h"
2013-12-08 17:05:18 +00:00
2014-06-12 15:45:59 +00:00
WaterRasterizer::WaterRasterizer(SoftwareRenderer* renderer, int client_id):
Rasterizer(renderer, client_id, Color(0.9, 0.95, 1.0))
2013-12-08 17:05:18 +00:00
{
}
2014-06-18 18:10:05 +00:00
static inline Vector3 _getFirstPassVertex(SoftwareRenderer* renderer, double x, double z)
{
Vector3 result;
result.x = x;
result.y = renderer->getWaterRenderer()->getHeight(x, z);
result.z = z;
return result;
}
2014-06-18 18:10:05 +00:00
void WaterRasterizer::rasterizeQuad(CanvasPortion* canvas, double x, double z, double size)
{
Vector3 v1, v2, v3, v4;
v1 = _getFirstPassVertex(renderer, x, z);
v2 = _getFirstPassVertex(renderer, x, z + size);
v3 = _getFirstPassVertex(renderer, x + size, z + size);
v4 = _getFirstPassVertex(renderer, x + size, z);
pushQuad(canvas, v1, v2, v3, v4);
}
void WaterRasterizer::rasterizeToCanvas(CanvasPortion *canvas)
{
int chunk_factor, chunk_count, i;
Vector3 cam = renderer->getCameraLocation(VECTOR_ZERO);
double radius_int, radius_ext, base_chunk_size, chunk_size;
base_chunk_size = 2.0 / (double)renderer->render_quality;
if (renderer->render_quality > 7)
{
base_chunk_size *= 0.5;
}
chunk_factor = 1;
chunk_count = 2;
radius_int = 0.0;
radius_ext = base_chunk_size;
chunk_size = base_chunk_size;
double cx = cam.x - fmod(cam.x, base_chunk_size);
double cz = cam.z - fmod(cam.x, base_chunk_size);
while (radius_int < 20000.0)
{
if (!renderer->addRenderProgress(0.0))
{
return;
}
for (i = 0; i < chunk_count - 1; i++)
{
rasterizeQuad(canvas, cx - radius_ext + chunk_size * i, cz - radius_ext, chunk_size);
rasterizeQuad(canvas, cx + radius_int, cz - radius_ext + chunk_size * i, chunk_size);
rasterizeQuad(canvas, cx + radius_int - chunk_size * i, cz + radius_int, chunk_size);
rasterizeQuad(canvas, cx - radius_ext, cz + radius_int - chunk_size * i, chunk_size);
}
if (radius_int > 20.0 && chunk_count % 64 == 0 && (double)chunk_factor < radius_int / 20.0)
{
chunk_count /= 2;
chunk_factor *= 2;
}
chunk_count += 2;
chunk_size = base_chunk_size * chunk_factor;
radius_int = radius_ext;
radius_ext += chunk_size;
}
}
2014-08-18 10:17:16 +00:00
Color WaterRasterizer::shadeFragment(const CanvasFragment &fragment) const
{
Vector3 location = fragment.getLocation();
return renderer->getWaterRenderer()->getResult(location.x, location.z).final;
}