Optimized texture rendering

Texture layers fully masked by another are not shaded anymore
This commit is contained in:
Michaël Lemaire 2015-09-21 00:42:58 +02:00
parent 6e45fc4c2f
commit aacb4addd5
3 changed files with 29 additions and 25 deletions

View file

@ -32,7 +32,7 @@ int main(int argc, char** argv)
{ {
SoftwareCanvasRenderer* renderer; SoftwareCanvasRenderer* renderer;
char* conf_file_path = NULL; char* conf_file_path = NULL;
RenderConfig conf_render_params(400, 300, 1, 3); RenderConfig conf_render_params(480, 270, 1, 3);
int conf_first_picture = 0; int conf_first_picture = 0;
int conf_nb_pictures = 1; int conf_nb_pictures = 1;
double conf_daytime_start = 0.4; double conf_daytime_start = 0.4;

View file

@ -67,7 +67,6 @@ double TexturesRenderer::getMaximalDisplacement(TexturesDefinition *textures)
static inline Vector3 _getNormal4(Vector3 center, Vector3 north, Vector3 east, Vector3 south, Vector3 west) static inline Vector3 _getNormal4(Vector3 center, Vector3 north, Vector3 east, Vector3 south, Vector3 west)
{ {
/* TODO This is duplicated in terrain/main.c */
Vector3 dnorth, deast, dsouth, dwest, normal; Vector3 dnorth, deast, dsouth, dwest, normal;
dnorth = north.sub(center); dnorth = north.sub(center);
@ -85,7 +84,6 @@ static inline Vector3 _getNormal4(Vector3 center, Vector3 north, Vector3 east, V
static inline Vector3 _getNormal2(Vector3 center, Vector3 east, Vector3 south) static inline Vector3 _getNormal2(Vector3 center, Vector3 east, Vector3 south)
{ {
/* TODO This is duplicated in terrain/main.c */
return south.sub(center).crossProduct(east.sub(center)).normalize(); return south.sub(center).crossProduct(east.sub(center)).normalize();
} }
@ -175,28 +173,23 @@ TexturesRenderer::TexturesResult TexturesRenderer::applyToTerrain(double x, doub
TexturesDefinition* textures = parent->getScenery()->getTextures(); TexturesDefinition* textures = parent->getScenery()->getTextures();
TexturesResult result; TexturesResult result;
/* Displacement */ // Displacement
TerrainRenderer::TerrainResult terrain = parent->getTerrainRenderer()->getResult(x, z, 1, 1); TerrainRenderer::TerrainResult terrain = parent->getTerrainRenderer()->getResult(x, z, 1, 1);
/* TODO Displaced textures had their presence already computed before, store that result and use it */ // TODO Displaced textures had their presence already computed before, store that result and use it
/* Find presence of each layer */ // Find presence of each layer
int i, n; int n = textures->count();
n = textures->count(); int start = 0;
for (i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
TexturesLayerResult* info = result.layers + i; TexturesLayerResult &layer = result.layers[i];
info->layer = textures->getTextureLayer(i);
info->presence = getBasePresence(i, terrain); layer.definition = textures->getTextureLayer(i);
if (info->presence > 0.0) layer.presence = getBasePresence(i, terrain);
if (layer.presence > 0.9999)
{ {
Vector3 normal = _getDetailNormal(parent, terrain.location, terrain.normal, info->layer); start = i;
Vector3 location(x, terrain.location.y, z);
info->color = parent->applyLightingToSurface(location, normal, *info->layer->material);
}
else
{
info->color = COLOR_TRANSPARENT;
} }
} }
result.layer_count = n; result.layer_count = n;
@ -205,12 +198,23 @@ TexturesRenderer::TexturesResult TexturesRenderer::applyToTerrain(double x, doub
result.base_normal = terrain.normal; result.base_normal = terrain.normal;
result.final_location = terrain.location; result.final_location = terrain.location;
result.final_color = COLOR_GREEN; result.final_color = COLOR_GREEN;
for (i = 0; i < n; i++)
// Compute and merge colors of visible layers
for (int i = start; i < n; i++)
{ {
if (result.layers[i].presence > 0.0) TexturesLayerResult &layer = result.layers[i];
if (layer.presence > 0.0)
{ {
result.layers[i].color.a = result.layers[i].presence; Vector3 normal = _getDetailNormal(parent, terrain.location, terrain.normal, layer.definition);
result.final_color.mask(result.layers[i].color); Vector3 location(x, terrain.location.y, z);
layer.color = parent->applyLightingToSurface(location, normal, *layer.definition->material);
layer.color.a = layer.presence;
result.final_color.mask(layer.color);
}
else
{
layer.color = COLOR_TRANSPARENT;
} }
} }

View file

@ -15,7 +15,7 @@ class SOFTWARESHARED_EXPORT TexturesRenderer
public: public:
typedef struct typedef struct
{ {
TextureLayerDefinition* layer; TextureLayerDefinition* definition;
double presence; double presence;
Color color; Color color;
} TexturesLayerResult; } TexturesLayerResult;