2013-03-31 20:27:21 +00:00
|
|
|
#include "private.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "../tools.h"
|
|
|
|
#include "../renderer.h"
|
|
|
|
|
|
|
|
/******************** Fake ********************/
|
2013-04-03 20:37:29 +00:00
|
|
|
static Vector3 _fakeDisplaceTerrain(Renderer* renderer, TerrainResult terrain)
|
2013-03-31 20:27:21 +00:00
|
|
|
{
|
|
|
|
UNUSED(renderer);
|
|
|
|
|
2013-04-03 20:37:29 +00:00
|
|
|
return terrain.location;
|
2013-03-31 20:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 20:37:29 +00:00
|
|
|
static TexturesResult _fakeApplyToTerrain(Renderer* renderer, double x, double z)
|
2013-03-31 20:27:21 +00:00
|
|
|
{
|
|
|
|
TexturesResult result;
|
|
|
|
|
2013-04-03 20:37:29 +00:00
|
|
|
result.base_location.x = x;
|
|
|
|
result.base_location.y = 0.0;
|
|
|
|
result.base_location.z = z;
|
|
|
|
result.base_normal = VECTOR_UP;
|
2013-03-31 20:27:21 +00:00
|
|
|
result.layer_count = 0;
|
2013-04-03 20:37:29 +00:00
|
|
|
result.final_location = result.base_location;
|
2013-03-31 20:27:21 +00:00
|
|
|
result.final_color = COLOR_WHITE;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************** Real ********************/
|
2013-04-03 20:37:29 +00:00
|
|
|
static Vector3 _realDisplaceTerrain(Renderer* renderer, TerrainResult terrain)
|
2013-03-31 20:27:21 +00:00
|
|
|
{
|
2013-04-03 20:37:29 +00:00
|
|
|
TexturesDefinition* textures = renderer->textures->definition;
|
|
|
|
double offset = 0.0;
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
n = layersCount(textures->layers);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
TexturesLayerDefinition* layer = layersGetLayer(textures->layers, i);
|
|
|
|
|
|
|
|
if (layer->displacement_height > 0.0)
|
|
|
|
{
|
|
|
|
double presence = texturesGetLayerBasePresence(layer, terrain);
|
|
|
|
offset += texturesGetTriplanarNoise(layer->_displacement_noise, terrain.location, terrain.normal) * presence * layer->displacement_height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v3Add(terrain.location, v3Scale(v3Normalize(terrain.normal), offset));
|
2013-03-31 20:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 20:37:29 +00:00
|
|
|
static TexturesResult _realApplyToTerrain(Renderer* renderer, double x, double z)
|
2013-03-31 20:27:21 +00:00
|
|
|
{
|
2013-04-03 20:37:29 +00:00
|
|
|
TexturesResult result;
|
|
|
|
|
|
|
|
/* Displacement */
|
|
|
|
TerrainResult terrain = renderer->terrain->getResult(renderer, x, z, 1, 1);
|
|
|
|
|
|
|
|
/* TODO Detail */
|
|
|
|
|
|
|
|
/* TEMP */
|
|
|
|
SurfaceMaterial temp;
|
|
|
|
temp.base.r = 0.6;
|
|
|
|
temp.base.g = 0.55;
|
|
|
|
temp.base.b = 0.57;
|
|
|
|
temp.reflection = 0.02;
|
|
|
|
temp.shininess = 3.0;
|
|
|
|
|
|
|
|
result.base_location = terrain.location;
|
|
|
|
result.base_normal = terrain.normal;
|
|
|
|
result.final_location = terrain.location;
|
|
|
|
result.final_color = renderer->applyLightingToSurface(renderer, terrain.location, terrain.normal, &temp);
|
|
|
|
return result;
|
2013-03-31 20:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************** Renderer ********************/
|
|
|
|
static TexturesRenderer* _createRenderer()
|
|
|
|
{
|
|
|
|
TexturesRenderer* result;
|
|
|
|
|
|
|
|
result = malloc(sizeof(TexturesRenderer));
|
|
|
|
result->definition = TexturesDefinitionClass.create();
|
|
|
|
|
|
|
|
result->displaceTerrain = _fakeDisplaceTerrain;
|
|
|
|
result->applyToTerrain = _fakeApplyToTerrain;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _deleteRenderer(TexturesRenderer* renderer)
|
|
|
|
{
|
|
|
|
TexturesDefinitionClass.destroy(renderer->definition);
|
|
|
|
free(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _bindRenderer(Renderer* renderer, TexturesDefinition* definition)
|
|
|
|
{
|
|
|
|
TexturesDefinitionClass.copy(definition, renderer->textures->definition);
|
|
|
|
|
|
|
|
renderer->textures->displaceTerrain = _realDisplaceTerrain;
|
|
|
|
renderer->textures->applyToTerrain = _realApplyToTerrain;
|
|
|
|
}
|
|
|
|
|
|
|
|
StandardRenderer TexturesRendererClass = {
|
|
|
|
(FuncObjectCreate)_createRenderer,
|
|
|
|
(FuncObjectDelete)_deleteRenderer,
|
|
|
|
(FuncObjectBind)_bindRenderer
|
|
|
|
};
|