Fixed canvas Z-index

This commit is contained in:
Michaël Lemaire 2014-06-18 20:10:05 +02:00
parent cf58bea1b7
commit f94d93ae60
7 changed files with 83 additions and 10 deletions

View file

@ -34,7 +34,7 @@ void CanvasPixel::pushFragment(const CanvasFragment &fragment)
} }
else else
{ {
if (fragments[0].getOpaque() and fragment.getZ() > fragments[0].getZ()) if (fragments[0].getOpaque() and fragment.getZ() < fragments[0].getZ())
{ {
// behind opaque fragment, don't bother // behind opaque fragment, don't bother
return; return;
@ -42,7 +42,7 @@ void CanvasPixel::pushFragment(const CanvasFragment &fragment)
// find expected position // find expected position
int i = 0; int i = 0;
while (i < count and fragment.getZ() < fragments[i].getZ()) while (i < count and fragment.getZ() > fragments[i].getZ())
{ {
i++; i++;
} }

View file

@ -44,6 +44,8 @@ void SoftwareCanvasRenderer::render()
render_camera->setRenderSize(canvas->getWidth(), canvas->getHeight()); render_camera->setRenderSize(canvas->getWidth(), canvas->getHeight());
prepare();
rasterize(portion, true); rasterize(portion, true);
postProcess(portion, true); postProcess(portion, true);
} }

View file

@ -238,3 +238,8 @@ void TerrainRasterizer::rasterize()
queue->wait(); queue->wait();
} }
void TerrainRasterizer::rasterizeToCanvas(CanvasPortion *canvas)
{
}

View file

@ -48,6 +48,8 @@ public:
*/ */
virtual void rasterize(); virtual void rasterize();
virtual void rasterizeToCanvas(CanvasPortion* canvas);
private: private:
ParallelQueue* queue; ParallelQueue* queue;
}; };

View file

@ -14,7 +14,7 @@ static Color _postProcessFragment(SoftwareRenderer* renderer, const Vector3 &loc
return renderer->getWaterRenderer()->getResult(location.x, location.z).final; return renderer->getWaterRenderer()->getResult(location.x, location.z).final;
} }
static Vector3 _getFirstPassVertex(SoftwareRenderer* renderer, double x, double z) static inline Vector3 _getFirstPassVertex(SoftwareRenderer* renderer, double x, double z)
{ {
Vector3 result; Vector3 result;
@ -128,3 +128,63 @@ void WaterRasterizer::rasterize()
queue->wait(); queue->wait();
delete queue; delete queue;
} }
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;
}
}

View file

@ -13,7 +13,11 @@ class WaterRasterizer: public Rasterizer
public: public:
WaterRasterizer(SoftwareRenderer* renderer, int client_id); WaterRasterizer(SoftwareRenderer* renderer, int client_id);
void rasterizeQuad(CanvasPortion* canvas, double x, double z, double size);
virtual void rasterize(); virtual void rasterize();
virtual void rasterizeToCanvas(CanvasPortion* canvas);
}; };
} }

View file

@ -47,17 +47,17 @@ TEST(CanvasPortion, pushFragment_opaque)
ASSERT_EQ(1, portion.getFragmentCount(2, 2)); ASSERT_EQ(1, portion.getFragmentCount(2, 2));
EXPECT_DOUBLE_EQ(2.0, portion.getFrontFragment(2, 2)->getZ()); EXPECT_DOUBLE_EQ(2.0, portion.getFrontFragment(2, 2)->getZ());
pushed = CanvasFragment(4.0, VECTOR_ZERO, 0); pushed = CanvasFragment(1.0, VECTOR_ZERO, 0);
portion.pushFragment(2, 2, pushed); portion.pushFragment(2, 2, pushed);
ASSERT_EQ(1, portion.getFragmentCount(2, 2)); ASSERT_EQ(1, portion.getFragmentCount(2, 2));
EXPECT_DOUBLE_EQ(2.0, portion.getFrontFragment(2, 2)->getZ()); EXPECT_DOUBLE_EQ(2.0, portion.getFrontFragment(2, 2)->getZ());
pushed = CanvasFragment(1.0, VECTOR_ZERO, 0); pushed = CanvasFragment(4.0, VECTOR_ZERO, 0);
portion.pushFragment(2, 2, pushed); portion.pushFragment(2, 2, pushed);
ASSERT_EQ(1, portion.getFragmentCount(2, 2)); ASSERT_EQ(1, portion.getFragmentCount(2, 2));
EXPECT_DOUBLE_EQ(1.0, portion.getFrontFragment(2, 2)->getZ()); EXPECT_DOUBLE_EQ(4.0, portion.getFrontFragment(2, 2)->getZ());
} }
TEST(CanvasPortion, pushFragment_transparent) TEST(CanvasPortion, pushFragment_transparent)
@ -67,11 +67,11 @@ TEST(CanvasPortion, pushFragment_transparent)
portion.setSize(10, 10); portion.setSize(10, 10);
pushed = CanvasFragment(4.0, VECTOR_ZERO, 0, false); pushed = CanvasFragment(2.0, VECTOR_ZERO, 0, false);
portion.pushFragment(2, 2, pushed); portion.pushFragment(2, 2, pushed);
ASSERT_EQ(1, portion.getFragmentCount(2, 2)); ASSERT_EQ(1, portion.getFragmentCount(2, 2));
EXPECT_DOUBLE_EQ(4.0, portion.getFrontFragment(2, 2)->getZ()); EXPECT_DOUBLE_EQ(2.0, portion.getFrontFragment(2, 2)->getZ());
pushed = CanvasFragment(3.0, VECTOR_ZERO, 0, true); pushed = CanvasFragment(3.0, VECTOR_ZERO, 0, true);
portion.pushFragment(2, 2, pushed); portion.pushFragment(2, 2, pushed);
@ -79,11 +79,11 @@ TEST(CanvasPortion, pushFragment_transparent)
ASSERT_EQ(1, portion.getFragmentCount(2, 2)); ASSERT_EQ(1, portion.getFragmentCount(2, 2));
EXPECT_DOUBLE_EQ(3.0, portion.getFrontFragment(2, 2)->getZ()); EXPECT_DOUBLE_EQ(3.0, portion.getFrontFragment(2, 2)->getZ());
pushed = CanvasFragment(2.0, VECTOR_ZERO, 0, false); pushed = CanvasFragment(4.0, VECTOR_ZERO, 0, false);
portion.pushFragment(2, 2, pushed); portion.pushFragment(2, 2, pushed);
ASSERT_EQ(2, portion.getFragmentCount(2, 2)); ASSERT_EQ(2, portion.getFragmentCount(2, 2));
EXPECT_DOUBLE_EQ(2.0, portion.getFrontFragment(2, 2)->getZ()); EXPECT_DOUBLE_EQ(4.0, portion.getFrontFragment(2, 2)->getZ());
} }
TEST(CanvasPortion, clear) TEST(CanvasPortion, clear)