paysages : Lighting refactoring (WIP).

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@501 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2013-01-20 15:07:45 +00:00 committed by ThunderK
parent b46060f3c4
commit ae91a39729
33 changed files with 297 additions and 224 deletions

View file

@ -39,7 +39,7 @@ void _previewUpdate(double progress)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Renderer renderer; Renderer* renderer;
char* conf_file_path = NULL; char* conf_file_path = NULL;
RenderParams conf_render_params = {800, 600, 1, 5}; RenderParams conf_render_params = {800, 600, 1, 5};
int conf_nb_pictures = 1; int conf_nb_pictures = 1;
@ -127,20 +127,20 @@ int main(int argc, char** argv)
} }
renderer = sceneryCreateStandardRenderer(); renderer = sceneryCreateStandardRenderer();
rendererSetPreviewCallbacks(&renderer, NULL, NULL, _previewUpdate); rendererSetPreviewCallbacks(renderer, NULL, NULL, _previewUpdate);
for (outputcount = 0; outputcount < conf_nb_pictures; outputcount++) for (outputcount = 0; outputcount < conf_nb_pictures; outputcount++)
{ {
/*autoSetDaytimeFraction(conf_daytime_start);*/ /* TODO */ /*autoSetDaytimeFraction(conf_daytime_start);*/ /* TODO */
sprintf(outputpath, "output/pic%05d.png", outputcount); sprintf(outputpath, "output/pic%05d.png", outputcount);
startRender(&renderer, outputpath, conf_render_params); startRender(renderer, outputpath, conf_render_params);
conf_daytime_start += conf_daytime_step; conf_daytime_start += conf_daytime_step;
} }
printf("Cleaning up ...\n"); printf("Cleaning up ...\n");
rendererDelete(&renderer); rendererDelete(renderer);
paysagesQuit(); paysagesQuit();
printf("\rDone. \n"); printf("\rDone. \n");

View file

@ -27,14 +27,14 @@ public:
protected: protected:
Color getColor(double x, double y) Color getColor(double x, double y)
{ {
return atmosphereGetPreview(&_renderer, x, -y, M_PI_2); return atmosphereGetPreview(_renderer, x, -y, M_PI_2);
} }
void updateData() void updateData()
{ {
AtmosphereRendererClass.bind(_renderer.atmosphere, _definition); AtmosphereRendererClass.bind(_renderer, _definition);
} }
private: private:
Renderer _renderer; Renderer* _renderer;
}; };
class PreviewSkyWest:public BasePreview class PreviewSkyWest:public BasePreview
@ -51,14 +51,14 @@ public:
protected: protected:
Color getColor(double x, double y) Color getColor(double x, double y)
{ {
return atmosphereGetPreview(&_renderer, x, -y, -M_PI_2); return atmosphereGetPreview(_renderer, x, -y, -M_PI_2);
} }
void updateData() void updateData()
{ {
AtmosphereRendererClass.bind(_renderer.atmosphere, _definition); AtmosphereRendererClass.bind(_renderer, _definition);
} }
private: private:
Renderer _renderer; Renderer* _renderer;
}; };
/**************** Form ****************/ /**************** Form ****************/

View file

@ -14,7 +14,7 @@ public:
PreviewCloudsCoverage(QWidget* parent, CloudsLayerDefinition* layer):BasePreview(parent) PreviewCloudsCoverage(QWidget* parent, CloudsLayerDefinition* layer):BasePreview(parent)
{ {
_renderer = rendererCreate(); _renderer = rendererCreate();
_renderer.render_quality = 5; _renderer->render_quality = 5;
//_renderer.applyLightStatus = _applyLightStatus; //_renderer.applyLightStatus = _applyLightStatus;
_original_layer = layer; _original_layer = layer;
@ -25,6 +25,7 @@ public:
~PreviewCloudsCoverage() ~PreviewCloudsCoverage()
{ {
cloudsLayerDeleteDefinition(_preview_layer); cloudsLayerDeleteDefinition(_preview_layer);
rendererDelete(_renderer);
} }
protected: protected:
Color getColor(double x, double y) Color getColor(double x, double y)
@ -39,7 +40,7 @@ protected:
look.z = 1.0; look.z = 1.0;
look = v3Normalize(look); look = v3Normalize(look);
return cloudsApplyLayer(_preview_layer, COLOR_BLUE, &_renderer, eye, v3Add(eye, v3Scale(look, 1000.0))); return cloudsApplyLayer(_preview_layer, COLOR_BLUE, _renderer, eye, v3Add(eye, v3Scale(look, 1000.0)));
} }
void updateData() void updateData()
{ {
@ -51,7 +52,7 @@ protected:
} }
private: private:
Renderer _renderer; Renderer* _renderer;
CloudsLayerDefinition* _original_layer; CloudsLayerDefinition* _original_layer;
CloudsLayerDefinition* _preview_layer; CloudsLayerDefinition* _preview_layer;
}; };
@ -79,10 +80,10 @@ public:
lightingValidateDefinition(&_lighting);*/ lightingValidateDefinition(&_lighting);*/
_renderer = rendererCreate(); _renderer = rendererCreate();
_renderer.render_quality = 8; _renderer->render_quality = 8;
/*_renderer.alterLight = _alterLight; /*_renderer.alterLight = _alterLight;
_renderer.getLightStatus = _getLightStatus;*/ _renderer.getLightStatus = _getLightStatus;*/
_renderer.customData[0] = _preview_layer; _renderer->customData[0] = _preview_layer;
//_renderer.customData[1] = &_lighting; //_renderer.customData[1] = &_lighting;
configScaling(0.5, 2.0, 0.1, 2.0); configScaling(0.5, 2.0, 0.1, 2.0);
@ -100,7 +101,7 @@ protected:
end.y = -y * _preview_layer->thickness * 0.5; end.y = -y * _preview_layer->thickness * 0.5;
end.z = -_preview_layer->thickness * 0.5; end.z = -_preview_layer->thickness * 0.5;
return cloudsApplyLayer(_preview_layer, COLOR_BLUE, &_renderer, start, end); return cloudsApplyLayer(_preview_layer, COLOR_BLUE, _renderer, start, end);
} }
void updateData() void updateData()
{ {
@ -112,7 +113,7 @@ protected:
_preview_layer->_custom_coverage = _coverageFunc; _preview_layer->_custom_coverage = _coverageFunc;
} }
private: private:
Renderer _renderer; Renderer* _renderer;
CloudsLayerDefinition* _original_layer; CloudsLayerDefinition* _original_layer;
CloudsLayerDefinition* _preview_layer; CloudsLayerDefinition* _preview_layer;
//LightingDefinition _lighting; //LightingDefinition _lighting;

View file

@ -15,10 +15,10 @@ public:
PreviewRenderLandscape(QWidget* parent):BasePreview(parent) PreviewRenderLandscape(QWidget* parent):BasePreview(parent)
{ {
_renderer = rendererCreate(); _renderer = rendererCreate();
_renderer.applyTextures = _applyTextures; _renderer->applyTextures = _applyTextures;
_renderer.camera_location.x = 0.0; _renderer->camera_location.x = 0.0;
_renderer.camera_location.y = 50.0; _renderer->camera_location.y = 50.0;
_renderer.camera_location.z = 0.0; _renderer->camera_location.z = 0.0;
_textures = texturesCreateDefinition(); _textures = texturesCreateDefinition();
_water = waterCreateDefinition(); _water = waterCreateDefinition();
@ -26,12 +26,13 @@ public:
_atmosphere = (AtmosphereDefinition*)AtmosphereDefinitionClass.create(); _atmosphere = (AtmosphereDefinition*)AtmosphereDefinitionClass.create();
_terrain = (TerrainDefinition*)TerrainDefinitionClass.create(); _terrain = (TerrainDefinition*)TerrainDefinitionClass.create();
_renderer.customData[0] = &_terrain; _renderer->customData[0] = &_terrain;
_renderer.customData[1] = &_textures; _renderer->customData[1] = &_textures;
_renderer.customData[3] = &_water; _renderer->customData[3] = &_water;
addOsd(QString("geolocation")); addOsd(QString("geolocation"));
configHdrToneMapping(true);
configScaling(0.5, 200.0, 3.0, 50.0); configScaling(0.5, 200.0, 3.0, 50.0);
configScrolling(-1000.0, 1000.0, 0.0, -1000.0, 1000.0, 0.0); configScrolling(-1000.0, 1000.0, 0.0, -1000.0, 1000.0, 0.0);
} }
@ -40,21 +41,21 @@ protected:
{ {
Vector3 down = {0.0, -1.0, 0.0}; Vector3 down = {0.0, -1.0, 0.0};
Vector3 location; Vector3 location;
double height = _renderer.terrain->getHeight(&_renderer, x, y, 1); double height = _renderer->terrain->getHeight(_renderer, x, y, 1);
if (height < _water.height) if (height < _water.height)
{ {
location.x = x; location.x = x;
location.y = _water.height; location.y = _water.height;
location.z = y; location.z = y;
return waterGetColor(&_water, &_renderer, location, down); return waterGetColor(&_water, _renderer, location, down);
} }
else else
{ {
location.x = x; location.x = x;
location.y = height; location.y = height;
location.z = y; location.z = y;
return _renderer.terrain->getFinalColor(&_renderer, location, scaling); return _renderer->terrain->getFinalColor(_renderer, location, scaling);
} }
} }
void updateData() void updateData()
@ -63,14 +64,14 @@ protected:
sceneryGetWater(&_water); sceneryGetWater(&_water);
sceneryGetAtmosphere(_atmosphere); sceneryGetAtmosphere(_atmosphere);
AtmosphereRendererClass.bind(_renderer.atmosphere, _atmosphere); AtmosphereRendererClass.bind(_renderer, _atmosphere);
_renderer.atmosphere->applyAerialPerspective = _applyAerialPerspective; _renderer->atmosphere->applyAerialPerspective = _applyAerialPerspective;
sceneryGetTerrain(_terrain); sceneryGetTerrain(_terrain);
TerrainRendererClass.bind(_renderer.terrain, _terrain); TerrainRendererClass.bind(_renderer, _terrain);
} }
private: private:
Renderer _renderer; Renderer* _renderer;
TerrainDefinition* _terrain; TerrainDefinition* _terrain;
WaterDefinition _water; WaterDefinition _water;
TexturesDefinition _textures; TexturesDefinition _textures;
@ -126,7 +127,7 @@ FormRender::~FormRender()
{ {
if (_renderer_inited) if (_renderer_inited)
{ {
rendererDelete(&_renderer); rendererDelete(_renderer);
} }
} }
@ -174,12 +175,12 @@ void FormRender::startQuickRender()
{ {
if (_renderer_inited) if (_renderer_inited)
{ {
rendererDelete(&_renderer); rendererDelete(_renderer);
} }
_renderer = sceneryCreateStandardRenderer(); _renderer = sceneryCreateStandardRenderer();
_renderer_inited = true; _renderer_inited = true;
DialogRender* dialog = new DialogRender(this, &_renderer); DialogRender* dialog = new DialogRender(this, _renderer);
RenderParams params = {400, 300, 1, 3}; RenderParams params = {400, 300, 1, 3};
dialog->startRender(params); dialog->startRender(params);
@ -190,12 +191,12 @@ void FormRender::startRender()
{ {
if (_renderer_inited) if (_renderer_inited)
{ {
rendererDelete(&_renderer); rendererDelete(_renderer);
} }
_renderer = sceneryCreateStandardRenderer(); _renderer = sceneryCreateStandardRenderer();
_renderer_inited = true; _renderer_inited = true;
DialogRender* dialog = new DialogRender(this, &_renderer); DialogRender* dialog = new DialogRender(this, _renderer);
dialog->startRender(_params); dialog->startRender(_params);
delete dialog; delete dialog;
@ -205,7 +206,7 @@ void FormRender::showRender()
{ {
if (_renderer_inited) if (_renderer_inited)
{ {
DialogRender* dialog = new DialogRender(this, &_renderer); DialogRender* dialog = new DialogRender(this, _renderer);
dialog->loadLastRender(); dialog->loadLastRender();
delete dialog; delete dialog;
@ -225,7 +226,7 @@ void FormRender::saveRender()
{ {
filepath = filepath.append(".png"); filepath = filepath.append(".png");
} }
if (renderSaveToFile(_renderer.render_area, (char*)filepath.toStdString().c_str())) if (renderSaveToFile(_renderer->render_area, (char*)filepath.toStdString().c_str()))
{ {
QMessageBox::information(this, "Message", QString(tr("The picture %1 has been saved.")).arg(filepath)); QMessageBox::information(this, "Message", QString(tr("The picture %1 has been saved.")).arg(filepath));
} }

View file

@ -33,7 +33,7 @@ private slots:
private: private:
RenderParams _params; RenderParams _params;
CameraDefinition _camera; CameraDefinition _camera;
Renderer _renderer; Renderer* _renderer;
bool _renderer_inited; bool _renderer_inited;
BasePreview* _preview_landscape; BasePreview* _preview_landscape;
}; };

View file

@ -24,14 +24,14 @@ public:
protected: protected:
Color getColor(double x, double y) Color getColor(double x, double y)
{ {
return terrainGetPreviewColor(&_renderer, x, -y, scaling); return terrainGetPreviewColor(_renderer, x, -y, scaling);
} }
void updateData() void updateData()
{ {
TerrainRendererClass.bind(_renderer.terrain, _definition); TerrainRendererClass.bind(_renderer, _definition);
} }
private: private:
Renderer _renderer; Renderer* _renderer;
}; };
/**************** Form ****************/ /**************** Form ****************/

View file

@ -20,7 +20,7 @@ public:
_terrain = (TerrainDefinition*)TerrainDefinitionClass.create(); _terrain = (TerrainDefinition*)TerrainDefinitionClass.create();
_renderer = rendererCreate(); _renderer = rendererCreate();
_renderer.render_quality = 3; _renderer->render_quality = 3;
_original_layer = layer; _original_layer = layer;
_preview_layer = texturesLayerCreateDefinition(); _preview_layer = texturesLayerCreateDefinition();
@ -40,21 +40,21 @@ protected:
Vector3 location; Vector3 location;
Color result; Color result;
location.x = x; location.x = x;
location.y = _renderer.terrain->getHeight(&_renderer, x, y, 1); location.y = _renderer->terrain->getHeight(_renderer, x, y, 1);
location.z = y; location.z = y;
result.r = result.g = result.b = texturesGetLayerCoverage(_preview_layer, &_renderer, location, this->scaling); result.r = result.g = result.b = texturesGetLayerCoverage(_preview_layer, _renderer, location, this->scaling);
return result; return result;
} }
void updateData() void updateData()
{ {
sceneryGetTerrain(_terrain); sceneryGetTerrain(_terrain);
TerrainRendererClass.bind(_renderer.terrain, _terrain); TerrainRendererClass.bind(_renderer, _terrain);
texturesLayerCopyDefinition(_original_layer, _preview_layer); texturesLayerCopyDefinition(_original_layer, _preview_layer);
} }
private: private:
Renderer _renderer; Renderer* _renderer;
TextureLayerDefinition* _original_layer; TextureLayerDefinition* _original_layer;
TextureLayerDefinition* _preview_layer; TextureLayerDefinition* _preview_layer;
TerrainDefinition* _terrain; TerrainDefinition* _terrain;
@ -82,12 +82,12 @@ public:
lightingValidateDefinition(&_lighting);*/ lightingValidateDefinition(&_lighting);*/
_renderer = rendererCreate(); _renderer = rendererCreate();
_renderer.render_quality = 3; _renderer->render_quality = 3;
/*_renderer.getLightStatus = _getLightStatus; /*_renderer->getLightStatus = _getLightStatus;
_renderer.customData[0] = &_lighting;*/ _renderer->customData[0] = &_lighting;*/
_renderer.camera_location.x = 0.0; _renderer->camera_location.x = 0.0;
_renderer.camera_location.y = 20.0; _renderer->camera_location.y = 20.0;
_renderer.camera_location.z = 0.0; _renderer->camera_location.z = 0.0;
_zone = zoneCreate(); _zone = zoneCreate();
@ -105,7 +105,7 @@ protected:
location.x = x; location.x = x;
location.y = 0.0; location.y = 0.0;
location.z = y; location.z = y;
return texturesGetLayerColor(_preview_layer, &_renderer, location, this->scaling); return texturesGetLayerColor(_preview_layer, _renderer, location, this->scaling);
} }
void updateData() void updateData()
{ {
@ -114,7 +114,7 @@ protected:
} }
private: private:
Zone* _zone; Zone* _zone;
Renderer _renderer; Renderer* _renderer;
TextureLayerDefinition* _original_layer; TextureLayerDefinition* _original_layer;
TextureLayerDefinition* _preview_layer; TextureLayerDefinition* _preview_layer;
//LightingDefinition _lighting; //LightingDefinition _lighting;

View file

@ -35,10 +35,10 @@ protected:
{ {
double height; double height;
height = _renderer.terrain->getHeight(&_renderer, x, -y, 1); height = _renderer->terrain->getHeight(_renderer, x, -y, 1);
if (height > _definition.height) if (height > _definition.height)
{ {
return terrainGetPreviewColor(&_renderer, x, -y, scaling); return terrainGetPreviewColor(_renderer, x, -y, scaling);
} }
else else
{ {
@ -53,7 +53,7 @@ protected:
look.y = -1.0; look.y = -1.0;
look.z = 0.0; look.z = 0.0;
base = waterGetColor(&_water, &_renderer, location, look); base = waterGetColor(&_water, _renderer, location, look);
if (_highlight_enabled) if (_highlight_enabled)
{ {
@ -71,7 +71,7 @@ protected:
// TODO Do this only on full refresh // TODO Do this only on full refresh
TerrainDefinition* terrain = (TerrainDefinition*)TerrainDefinitionClass.create(); TerrainDefinition* terrain = (TerrainDefinition*)TerrainDefinitionClass.create();
sceneryGetTerrain(terrain); sceneryGetTerrain(terrain);
TerrainRendererClass.bind(_renderer.terrain, terrain); TerrainRendererClass.bind(_renderer, terrain);
TerrainDefinitionClass.destroy(terrain); TerrainDefinitionClass.destroy(terrain);
} }
void toggleChangeEvent(QString key, bool value) void toggleChangeEvent(QString key, bool value)
@ -83,7 +83,7 @@ protected:
} }
} }
private: private:
Renderer _renderer; Renderer* _renderer;
WaterDefinition _water; WaterDefinition _water;
bool _highlight_enabled; bool _highlight_enabled;
}; };
@ -112,12 +112,12 @@ public:
lightingValidateDefinition(&_lighting);*/ lightingValidateDefinition(&_lighting);*/
_renderer = rendererCreate(); _renderer = rendererCreate();
_renderer.rayWalking = _rayWalking; _renderer->rayWalking = _rayWalking;
/*_renderer.getLightStatus = _getLightStatus; /*_renderer->getLightStatus = _getLightStatus;
_renderer.applyLightStatus = _applyLightStatus;*/ _renderer->applyLightStatus = _applyLightStatus;*/
_renderer.customData[0] = &_water; _renderer->customData[0] = &_water;
//_renderer.customData[1] = &_lighting; //_renderer->customData[1] = &_lighting;
_renderer.customData[2] = this; _renderer->customData[2] = this;
configScaling(10.0, 1000.0, 10.0, 250.0); configScaling(10.0, 1000.0, 10.0, 250.0);
//configScrolling(-30.0, 30.0, 0.0, -20.0, 20.0, 0.0); //configScrolling(-30.0, 30.0, 0.0, -20.0, 20.0, 0.0);
@ -141,7 +141,7 @@ protected:
if (look.y > -0.0001) if (look.y > -0.0001)
{ {
return _rayWalking(&_renderer, eye, look, 0, 0, 0, 0).hit_color; return _rayWalking(_renderer, eye, look, 0, 0, 0, 0).hit_color;
} }
location.x = eye.x - look.x * eye.y / look.y; location.x = eye.x - look.x * eye.y / look.y;
@ -150,10 +150,10 @@ protected:
if (location.z > 0.0) if (location.z > 0.0)
{ {
return _rayWalking(&_renderer, eye, look, 0, 0, 0, 0).hit_color; return _rayWalking(_renderer, eye, look, 0, 0, 0, 0).hit_color;
} }
return waterGetColor(&_water, &_renderer, location, look); return waterGetColor(&_water, _renderer, location, look);
} }
void updateData() void updateData()
{ {
@ -178,7 +178,7 @@ protected:
} }
private: private:
Renderer _renderer; Renderer* _renderer;
WaterDefinition _water; WaterDefinition _water;
//LightingDefinition _lighting; //LightingDefinition _lighting;
bool _lighting_enabled; bool _lighting_enabled;

View file

@ -28,14 +28,14 @@ SmallMaterialPreview::SmallMaterialPreview(QWidget* parent, SurfaceMaterial* mat
_material = material; _material = material;
_renderer = rendererCreate(); _renderer = rendererCreate();
_renderer.camera_location.x = 0.0; _renderer->camera_location.x = 0.0;
_renderer.camera_location.x = 0.0; _renderer->camera_location.x = 0.0;
_renderer.camera_location.z = 10.0; _renderer->camera_location.z = 10.0;
} }
SmallMaterialPreview::~SmallMaterialPreview() SmallMaterialPreview::~SmallMaterialPreview()
{ {
rendererDelete(&_renderer); rendererDelete(_renderer);
} }
Color SmallMaterialPreview::getColor(double x, double y) Color SmallMaterialPreview::getColor(double x, double y)

View file

@ -20,7 +20,7 @@ protected:
private: private:
SurfaceMaterial* _material; SurfaceMaterial* _material;
LightStatus* _light; LightStatus* _light;
Renderer _renderer; Renderer* _renderer;
}; };
class PreviewMaterial:public BasePreview class PreviewMaterial:public BasePreview

View file

@ -68,10 +68,10 @@ WidgetExplorer::WidgetExplorer(QWidget *parent, CameraDefinition* camera):
sceneryGetTextures(&_textures); sceneryGetTextures(&_textures);
_renderer = sceneryCreateStandardRenderer(); _renderer = sceneryCreateStandardRenderer();
_renderer.render_quality = 3; _renderer->render_quality = 3;
_renderer.customData[1] = &_textures; _renderer->customData[1] = &_textures;
_renderer.customData[3] = &_water; _renderer->customData[3] = &_water;
_renderer.applyTextures = _applyTextures; _renderer->applyTextures = _applyTextures;
_updated = false; _updated = false;
@ -84,7 +84,7 @@ WidgetExplorer::WidgetExplorer(QWidget *parent, CameraDefinition* camera):
{ {
for (int j = 0; j < chunks; j++) for (int j = 0; j < chunks; j++)
{ {
ExplorerChunkTerrain* chunk = new ExplorerChunkTerrain(&_renderer, start + chunksize * (double)i, start + chunksize * (double)j, chunksize, chunks); ExplorerChunkTerrain* chunk = new ExplorerChunkTerrain(_renderer, start + chunksize * (double)i, start + chunksize * (double)j, chunksize, chunks);
_chunks.append(chunk); _chunks.append(chunk);
_updateQueue.append(chunk); _updateQueue.append(chunk);
} }
@ -93,7 +93,7 @@ WidgetExplorer::WidgetExplorer(QWidget *parent, CameraDefinition* camera):
// Add skybox // Add skybox
for (int orientation = 0; orientation < 5; orientation++) for (int orientation = 0; orientation < 5; orientation++)
{ {
ExplorerChunkSky* chunk = new ExplorerChunkSky(&_renderer, 500.0, (SkyboxOrientation)orientation); ExplorerChunkSky* chunk = new ExplorerChunkSky(_renderer, 500.0, (SkyboxOrientation)orientation);
_chunks.append(chunk); _chunks.append(chunk);
_updateQueue.append(chunk); _updateQueue.append(chunk);
} }
@ -116,6 +116,7 @@ WidgetExplorer::~WidgetExplorer()
delete _chunks[i]; delete _chunks[i];
} }
waterDeleteDefinition(&_water); waterDeleteDefinition(&_water);
rendererDelete(_renderer);
} }
void WidgetExplorer::startThreads() void WidgetExplorer::startThreads()
@ -390,7 +391,7 @@ void WidgetExplorer::paintGL()
double frame_time; double frame_time;
cameraValidateDefinition(&_current_camera, 1); cameraValidateDefinition(&_current_camera, 1);
_renderer.camera_location = _current_camera.location; _renderer->camera_location = _current_camera.location;
start_time = QTime::currentTime(); start_time = QTime::currentTime();

View file

@ -39,7 +39,7 @@ private:
CameraDefinition _current_camera; CameraDefinition _current_camera;
CameraDefinition* _base_camera; CameraDefinition* _base_camera;
Renderer _renderer; Renderer* _renderer;
bool _updated; bool _updated;
QVector<BaseExplorerChunk*> _chunks; QVector<BaseExplorerChunk*> _chunks;

View file

@ -19,7 +19,7 @@ WidgetHeightMap::WidgetHeightMap(QWidget *parent, TerrainDefinition* terrain):
_terrain = terrain; _terrain = terrain;
_renderer = rendererCreate(); _renderer = rendererCreate();
TerrainRendererClass.bind(_renderer.terrain, _terrain); TerrainRendererClass.bind(_renderer, _terrain);
_vertices = new _VertexInfo[HEIGHTMAP_RESOLUTION * HEIGHTMAP_RESOLUTION]; _vertices = new _VertexInfo[HEIGHTMAP_RESOLUTION * HEIGHTMAP_RESOLUTION];
_dirty = true; _dirty = true;
@ -49,7 +49,7 @@ WidgetHeightMap::WidgetHeightMap(QWidget *parent, TerrainDefinition* terrain):
WidgetHeightMap::~WidgetHeightMap() WidgetHeightMap::~WidgetHeightMap()
{ {
rendererDelete(&_renderer); rendererDelete(_renderer);
noiseDeleteGenerator(_brush_noise); noiseDeleteGenerator(_brush_noise);
delete[] _vertices; delete[] _vertices;
} }

View file

@ -52,7 +52,7 @@ private:
private: private:
TerrainDefinition* _terrain; TerrainDefinition* _terrain;
Renderer _renderer; Renderer* _renderer;
_VertexInfo* _vertices; _VertexInfo* _vertices;
bool _dirty; bool _dirty;

View file

@ -47,9 +47,11 @@ void basicGetLightingStatus(Renderer* renderer, LightStatus* status, Vector3 nor
light.direction = v3Scale(renderer->atmosphere->getSunDirection(renderer), -1.0); light.direction = v3Scale(renderer->atmosphere->getSunDirection(renderer), -1.0);
light.color = renderer->atmosphere->definition->sun_color; light.color = renderer->atmosphere->definition->sun_color;
/*light.color.r *= 100.0;
light.color.g *= 100.0;
light.color.b *= 100.0;*/
light.reflection = 1.0; light.reflection = 1.0;
light.filtered = 1; light.altered = 1;
light.masked = 1;
lightingPushLight(status, &light); lightingPushLight(status, &light);
/* TODO Sample other directions */ /* TODO Sample other directions */

View file

@ -19,6 +19,7 @@
/*********************** Constants ***********************/ /*********************** Constants ***********************/
#define WORLD_SCALING 0.05 #define WORLD_SCALING 0.05
#define GROUND_OFFSET 10.0
static const double Rg = 6360.0; static const double Rg = 6360.0;
static const double Rt = 6420.0; static const double Rt = 6420.0;
static const double RL = 6421.0; static const double RL = 6421.0;
@ -1189,7 +1190,7 @@ void brunetonInit()
Color brunetonGetSkyColor(AtmosphereDefinition* definition, Vector3 eye, Vector3 direction, Vector3 sun_position) Color brunetonGetSkyColor(AtmosphereDefinition* definition, Vector3 eye, Vector3 direction, Vector3 sun_position)
{ {
Vector3 x = {0.0, Rg + (eye.y + WORLD_SCALING) * 0.01, 0.0}; Vector3 x = {0.0, Rg + (max(eye.y, 0.0) + GROUND_OFFSET) * WORLD_SCALING, 0.0};
Vector3 v = v3Normalize(direction); Vector3 v = v3Normalize(direction);
Vector3 s = v3Normalize(v3Sub(sun_position, x)); Vector3 s = v3Normalize(v3Sub(sun_position, x));
@ -1214,7 +1215,7 @@ Color brunetonApplyAerialPerspective(Renderer* renderer, Vector3 location, Color
Vector3 direction = v3Scale(v3Sub(location, eye), WORLD_SCALING); Vector3 direction = v3Scale(v3Sub(location, eye), WORLD_SCALING);
Vector3 sun_position = v3Scale(renderer->atmosphere->getSunDirection(renderer), 149597870.0); Vector3 sun_position = v3Scale(renderer->atmosphere->getSunDirection(renderer), 149597870.0);
Vector3 x = {0.0, Rg + (eye.y + 10.0) * WORLD_SCALING, 0.0}; Vector3 x = {0.0, Rg + (max(eye.y, 0.0) + GROUND_OFFSET) * WORLD_SCALING, 0.0};
Vector3 v = v3Normalize(direction); Vector3 v = v3Normalize(direction);
Vector3 s = v3Normalize(v3Sub(sun_position, x)); Vector3 s = v3Normalize(v3Sub(sun_position, x));
@ -1237,3 +1238,36 @@ Color brunetonApplyAerialPerspective(Renderer* renderer, Vector3 location, Color
return groundColor; /* Eq (16) */ return groundColor; /* Eq (16) */
} }
void brunetonGetLightingStatus(Renderer* renderer, LightStatus* status, Vector3 normal, int opaque)
{
LightDefinition sun, irradiance;
double muS;
double r0 = Rg + (max(lightingGetStatusLocation(status).y, 0.0) + GROUND_OFFSET) * WORLD_SCALING;
Vector3 up = {0.0, 1.0, 0.0};
Vector3 sun_position = v3Scale(renderer->atmosphere->getSunDirection(renderer), 149597870.0);
Vector3 x = {0.0, r0, 0.0};
Vector3 s = v3Normalize(v3Sub(sun_position, x));
muS = v3Dot(up, s);
sun.color = _transmittanceWithShadow(r0, muS);
sun.color.r *= 100.0;
sun.color.g *= 100.0;
sun.color.b *= 100.0;
sun.direction = s;
sun.reflection = 1.0;
sun.altered = 1;
lightingPushLight(status, &sun);
irradiance.color = _irradiance(_irradianceTexture, r0, muS);
irradiance.color.r *= 100.0;
irradiance.color.g *= 100.0;
irradiance.color.b *= 100.0;
irradiance.direction = v3Scale(normal, -1.0);
irradiance.reflection = 0.0;
irradiance.altered = 0;
lightingPushLight(status, &irradiance);
}

View file

@ -331,22 +331,24 @@ static void _deleteRenderer(AtmosphereRenderer* renderer)
free(renderer); free(renderer);
} }
static void _bindRenderer(AtmosphereRenderer* renderer, AtmosphereDefinition* definition) static void _bindRenderer(Renderer* renderer, AtmosphereDefinition* definition)
{ {
AtmosphereRendererCache* cache = (AtmosphereRendererCache*)renderer->_internal_data; AtmosphereRendererCache* cache = (AtmosphereRendererCache*)renderer->atmosphere->_internal_data;
AtmosphereDefinitionClass.copy(definition, renderer->definition); AtmosphereDefinitionClass.copy(definition, renderer->atmosphere->definition);
renderer->getSkyColor = _getSkyColor; renderer->atmosphere->getSkyColor = _getSkyColor;
renderer->getLightingStatus = basicGetLightingStatus;
switch (definition->model) switch (definition->model)
{ {
case ATMOSPHERE_MODEL_BRUNETON: case ATMOSPHERE_MODEL_BRUNETON:
renderer->applyAerialPerspective = brunetonApplyAerialPerspective; renderer->atmosphere->applyAerialPerspective = brunetonApplyAerialPerspective;
/*renderer->atmosphere->getLightingStatus = brunetonGetLightingStatus;*/
renderer->atmosphere->getLightingStatus = basicGetLightingStatus;
break; break;
default: default:
renderer->applyAerialPerspective = basicApplyAerialPerspective; renderer->atmosphere->applyAerialPerspective = basicApplyAerialPerspective;
renderer->atmosphere->getLightingStatus = basicGetLightingStatus;
} }
mutexAcquire(cache->lock); mutexAcquire(cache->lock);

View file

@ -149,13 +149,13 @@ Color atmosphereGetPreview(Renderer* renderer, double x, double y, double headin
} }
} }
Renderer atmosphereCreatePreviewRenderer() Renderer* atmosphereCreatePreviewRenderer()
{ {
Renderer result = rendererCreate(); Renderer* result = rendererCreate();
result.camera_location.x = 0.0; result->camera_location.x = 0.0;
result.camera_location.y = 7.0; result->camera_location.y = 7.0;
result.camera_location.z = 0.0; result->camera_location.z = 0.0;
return result; return result;
} }

View file

@ -11,6 +11,7 @@ void basicGetLightingStatus(Renderer* renderer, LightStatus* status, Vector3 nor
void brunetonInit(); void brunetonInit();
Color brunetonGetSkyColor(AtmosphereDefinition* definition, Vector3 eye, Vector3 direction, Vector3 sun_position); Color brunetonGetSkyColor(AtmosphereDefinition* definition, Vector3 eye, Vector3 direction, Vector3 sun_position);
Color brunetonApplyAerialPerspective(Renderer* renderer, Vector3 location, Color base); Color brunetonApplyAerialPerspective(Renderer* renderer, Vector3 location, Color base);
void brunetonGetLightingStatus(Renderer* renderer, LightStatus* status, Vector3 normal, int opaque);
Color preethamGetSkyColor(AtmosphereDefinition* definition, Vector3 eye, Vector3 direction, Vector3 sun_position); Color preethamGetSkyColor(AtmosphereDefinition* definition, Vector3 eye, Vector3 direction, Vector3 sun_position);

View file

@ -62,7 +62,7 @@ extern StandardRenderer AtmosphereRendererClass;
void atmosphereAutoPreset(AtmosphereDefinition* definition, AtmospherePreset preset); void atmosphereAutoPreset(AtmosphereDefinition* definition, AtmospherePreset preset);
void atmosphereRenderSkydome(Renderer* renderer); void atmosphereRenderSkydome(Renderer* renderer);
Renderer atmosphereCreatePreviewRenderer(); Renderer* atmosphereCreatePreviewRenderer();
Color atmosphereGetPreview(Renderer* renderer, double x, double y, double heading); Color atmosphereGetPreview(Renderer* renderer, double x, double y, double heading);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -50,7 +50,7 @@ void autoGenRealisticLandscape(int seed)
texture->material.base.r = 0.6; texture->material.base.r = 0.6;
texture->material.base.g = 0.55; texture->material.base.g = 0.55;
texture->material.base.b = 0.57; texture->material.base.b = 0.57;
texture->material.reflection = 0.2; texture->material.reflection = 0.02;
texture->material.shininess = 3.0; texture->material.shininess = 3.0;
texture->thickness = 0.001; texture->thickness = 0.001;
texture->slope_range = 0.001; texture->slope_range = 0.001;
@ -68,7 +68,7 @@ void autoGenRealisticLandscape(int seed)
texture->material.base.r = 0.12; texture->material.base.r = 0.12;
texture->material.base.g = 0.19; texture->material.base.g = 0.19;
texture->material.base.b = 0.035; texture->material.base.b = 0.035;
texture->material.reflection = 0.03; texture->material.reflection = 0.003;
texture->material.shininess = 2.0; texture->material.shininess = 2.0;
texture->thickness = 0.02; texture->thickness = 0.02;
texture->slope_range = 0.03; texture->slope_range = 0.03;

View file

@ -61,7 +61,7 @@ void cameraCopyDefinition(CameraDefinition* source, CameraDefinition* destinatio
void cameraValidateDefinition(CameraDefinition* definition, int check_above) void cameraValidateDefinition(CameraDefinition* definition, int check_above)
{ {
WaterDefinition water; WaterDefinition water;
Renderer renderer; Renderer* renderer;
double water_height, terrain_height, diff; double water_height, terrain_height, diff;
Vector3 move; Vector3 move;
Matrix4 rotation; Matrix4 rotation;
@ -74,8 +74,8 @@ void cameraValidateDefinition(CameraDefinition* definition, int check_above)
waterDeleteDefinition(&water); waterDeleteDefinition(&water);
renderer = sceneryCreateStandardRenderer(); renderer = sceneryCreateStandardRenderer();
terrain_height = renderer.terrain->getHeight(&renderer, definition->location.x, definition->location.z, 1) + 0.5; terrain_height = renderer->terrain->getHeight(renderer, definition->location.x, definition->location.z, 1) + 0.5;
rendererDelete(&renderer); rendererDelete(renderer);
if (definition->location.y < water_height || definition->location.y < terrain_height) if (definition->location.y < water_height || definition->location.y < terrain_height)
{ {

View file

@ -74,39 +74,39 @@ static Color _applyClouds(Renderer* renderer, Color base, Vector3 start, Vector3
return base; return base;
} }
Renderer rendererCreate() Renderer* rendererCreate()
{ {
Renderer result; Renderer* result = malloc(sizeof(Renderer));
RenderParams params = {1, 1, 1, 5}; RenderParams params = {1, 1, 1, 5};
result.render_quality = 5; result->render_quality = 5;
result.render_width = 1; result->render_width = 1;
result.render_height = 1; result->render_height = 1;
result.render_interrupt = 0; result->render_interrupt = 0;
result.render_progress = 0.0; result->render_progress = 0.0;
result.is_rendering = 0; result->is_rendering = 0;
result.render_camera = cameraCreateDefinition(); result->render_camera = cameraCreateDefinition();
result.camera_location = result.render_camera.location; result->camera_location = result->render_camera.location;
result.render_area = renderCreateArea(); result->render_area = renderCreateArea();
renderSetParams(result.render_area, params); renderSetParams(result->render_area, params);
result.addRenderProgress = _addRenderProgress; result->addRenderProgress = _addRenderProgress;
result.getPrecision = _getPrecision; result->getPrecision = _getPrecision;
result.projectPoint = _projectPoint; result->projectPoint = _projectPoint;
result.unprojectPoint = _unprojectPoint; result->unprojectPoint = _unprojectPoint;
result.pushTriangle = _pushTriangle; result->pushTriangle = _pushTriangle;
result.pushQuad = _pushQuad; result->pushQuad = _pushQuad;
result.rayWalking = _rayWalking; result->rayWalking = _rayWalking;
result.getWaterHeightInfo = _getWaterHeightInfo; result->getWaterHeightInfo = _getWaterHeightInfo;
result.applyTextures = _applyTextures; result->applyTextures = _applyTextures;
result.applyClouds = _applyClouds; result->applyClouds = _applyClouds;
result.lighting = lightingManagerCreate(); result->lighting = lightingManagerCreate();
result.atmosphere = AtmosphereRendererClass.create(); result->atmosphere = AtmosphereRendererClass.create();
result.terrain = TerrainRendererClass.create(); result->terrain = TerrainRendererClass.create();
return result; return result;
} }
@ -119,6 +119,8 @@ void rendererDelete(Renderer* renderer)
TerrainRendererClass.destroy(renderer->terrain); TerrainRendererClass.destroy(renderer->terrain);
renderDeleteArea(renderer->render_area); renderDeleteArea(renderer->render_area);
free(renderer);
} }
void rendererSetPreviewCallbacks(Renderer* renderer, RenderCallbackStart start, RenderCallbackDraw draw, RenderCallbackUpdate update) void rendererSetPreviewCallbacks(Renderer* renderer, RenderCallbackStart start, RenderCallbackDraw draw, RenderCallbackUpdate update)

View file

@ -48,7 +48,7 @@ struct Renderer
void* customData[10]; void* customData[10];
}; };
Renderer rendererCreate(); Renderer* rendererCreate();
void rendererDelete(Renderer* renderer); void rendererDelete(Renderer* renderer);
void rendererSetPreviewCallbacks(Renderer* renderer, RenderCallbackStart start, RenderCallbackDraw draw, RenderCallbackUpdate update); void rendererSetPreviewCallbacks(Renderer* renderer, RenderCallbackStart start, RenderCallbackDraw draw, RenderCallbackUpdate update);
void rendererStart(Renderer* renderer, RenderParams params); void rendererStart(Renderer* renderer, RenderParams params);

View file

@ -224,25 +224,25 @@ static double _getPrecision(Renderer* renderer, Vector3 location)
return v3Norm(v3Sub(cameraUnproject(&renderer->render_camera, renderer, projected), location)); // / (double)render_quality; return v3Norm(v3Sub(cameraUnproject(&renderer->render_camera, renderer, projected), location)); // / (double)render_quality;
} }
Renderer sceneryCreateStandardRenderer() Renderer* sceneryCreateStandardRenderer()
{ {
Renderer result; Renderer* result;
result = rendererCreate(); result = rendererCreate();
cameraCopyDefinition(&_camera, &result.render_camera); cameraCopyDefinition(&_camera, &result->render_camera);
result.camera_location = _camera.location; result->camera_location = _camera.location;
result.rayWalking = _rayWalking; result->rayWalking = _rayWalking;
result.getWaterHeightInfo = _getWaterHeightInfo; result->getWaterHeightInfo = _getWaterHeightInfo;
result.applyTextures = _applyTextures; result->applyTextures = _applyTextures;
result.applyClouds = _applyClouds; result->applyClouds = _applyClouds;
result.projectPoint = _projectPoint; result->projectPoint = _projectPoint;
result.unprojectPoint = _unprojectPoint; result->unprojectPoint = _unprojectPoint;
result.getPrecision = _getPrecision; result->getPrecision = _getPrecision;
AtmosphereRendererClass.bind(result.atmosphere, _atmosphere); AtmosphereRendererClass.bind(result, _atmosphere);
TerrainRendererClass.bind(result.terrain, _terrain); TerrainRendererClass.bind(result, _terrain);
return result; return result;
} }

View file

@ -49,7 +49,7 @@ void sceneryGetTextures(TexturesDefinition* textures);
void scenerySetWater(WaterDefinition* water); void scenerySetWater(WaterDefinition* water);
void sceneryGetWater(WaterDefinition* water); void sceneryGetWater(WaterDefinition* water);
Renderer sceneryCreateStandardRenderer(); Renderer* sceneryCreateStandardRenderer();
void sceneryRenderFirstPass(Renderer* renderer); void sceneryRenderFirstPass(Renderer* renderer);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -193,36 +193,27 @@ static RayCastingResult _castRay(Renderer* renderer, Vector3 start, Vector3 dire
return result; return result;
} }
static LightDefinition _fakeAlterLight(Renderer* renderer, LightDefinition* light, Vector3 at) static int _alterLight(LightDefinition* light, Vector3 location, Renderer* renderer)
{
UNUSED(renderer);
UNUSED(at);
return *light;
}
static LightDefinition _alterLight(Renderer* renderer, LightDefinition* light, Vector3 location)
{ {
TerrainDefinition* definition = renderer->terrain->definition; TerrainDefinition* definition = renderer->terrain->definition;
LightDefinition result = *light;
Vector3 inc_vector, direction_to_light; Vector3 inc_vector, direction_to_light;
double inc_value, inc_base, inc_factor, height, diff, light_factor, smoothing, length; double inc_value, inc_base, inc_factor, height, diff, light_factor, smoothing, length;
direction_to_light = v3Scale(light->direction, -1.0); direction_to_light = v3Scale(light->direction, -1.0);
if ((fabs(direction_to_light.x) < 0.0001 && fabs(direction_to_light.z) < 0.0001) || definition->height < 0.001) if ((fabs(direction_to_light.x) < 0.0001 && fabs(direction_to_light.z) < 0.0001) || definition->height < 0.001)
{ {
return result; return 0;
} }
else if (direction_to_light.y < 0.05) else if (direction_to_light.y < 0.05)
{ {
result.color = COLOR_BLACK; light->color = COLOR_BLACK;
return result; return 1;
} }
else if (direction_to_light.y < 0.0000) else if (direction_to_light.y < 0.0000)
{ {
result.color.r *= (0.05 + direction_to_light.y) / 0.05; light->color.r *= (0.05 + direction_to_light.y) / 0.05;
result.color.g *= (0.05 + direction_to_light.y) / 0.05; light->color.g *= (0.05 + direction_to_light.y) / 0.05;
result.color.b *= (0.05 + direction_to_light.y) / 0.05; light->color.b *= (0.05 + direction_to_light.y) / 0.05;
} }
inc_factor = (double)renderer->render_quality; inc_factor = (double)renderer->render_quality;
@ -268,16 +259,16 @@ static LightDefinition _alterLight(Renderer* renderer, LightDefinition* light, V
if (light_factor <= 0.0) if (light_factor <= 0.0)
{ {
result.color = COLOR_BLACK; light->color = COLOR_BLACK;
return result; return 1;
} }
else else
{ {
result.color.r *= light_factor; light->color.r *= light_factor;
result.color.g *= light_factor; light->color.g *= light_factor;
result.color.b *= light_factor; light->color.b *= light_factor;
return result; return 1;
} }
} }
@ -315,13 +306,15 @@ static void _deleteRenderer(TerrainRenderer* renderer)
free(renderer); free(renderer);
} }
static void _bindRenderer(TerrainRenderer* renderer, TerrainDefinition* definition) static void _bindRenderer(Renderer* renderer, TerrainDefinition* definition)
{ {
TerrainDefinitionClass.copy(definition, renderer->definition); TerrainDefinitionClass.copy(definition, renderer->terrain->definition);
renderer->castRay = _castRay; renderer->terrain->castRay = _castRay;
renderer->getHeight = _getHeight; renderer->terrain->getHeight = _getHeight;
renderer->getFinalColor = _getFinalColor; renderer->terrain->getFinalColor = _getFinalColor;
lightingManagerRegisterFilter(renderer->lighting, (FuncLightingAlterLight)_alterLight, renderer);
} }
StandardRenderer TerrainRendererClass = { StandardRenderer TerrainRendererClass = {

View file

@ -18,15 +18,15 @@ static Color _applyTextures(Renderer* renderer, Vector3 location, double precisi
return texturesGetColor(&_textures, renderer, location.x, location.z, precision); return texturesGetColor(&_textures, renderer, location.x, location.z, precision);
} }
Renderer terrainCreatePreviewRenderer() Renderer* terrainCreatePreviewRenderer()
{ {
Renderer result = rendererCreate(); Renderer* result = rendererCreate();
result.render_quality = 3; result->render_quality = 3;
result.applyTextures = _applyTextures; result->applyTextures = _applyTextures;
result.camera_location.x = 0.0; result->camera_location.x = 0.0;
result.camera_location.y = 50.0; result->camera_location.y = 50.0;
result.camera_location.z = 0.0; result->camera_location.z = 0.0;
if (!_inited) if (!_inited)
{ {

View file

@ -52,7 +52,7 @@ void terrainAutoPreset(TerrainDefinition* definition, TerrainPreset preset);
void terrainRenderSurface(Renderer* renderer); void terrainRenderSurface(Renderer* renderer);
double terrainGetGridHeight(TerrainDefinition* definition, int x, int z, int with_painting); double terrainGetGridHeight(TerrainDefinition* definition, int x, int z, int with_painting);
Renderer terrainCreatePreviewRenderer(); Renderer* terrainCreatePreviewRenderer();
Color terrainGetPreviewColor(Renderer* renderer, double x, double z, double detail); Color terrainGetPreviewColor(Renderer* renderer, double x, double z, double detail);
typedef struct typedef struct

View file

@ -237,6 +237,14 @@ static double _uncharted2Tonemap(double x)
Color colorProfileApply(ColorProfile* profile, Color pixel) Color colorProfileApply(ColorProfile* profile, Color pixel)
{ {
/*pixel.r *= 0.4;
pixel.g *= 0.4;
pixel.b *= 0.4;
pixel.r = pixel.r < 1.413 ? pow(pixel.r * 0.38317, 1.0 / 2.2) : 1.0 - exp(-pixel.r);
pixel.g = pixel.g < 1.413 ? pow(pixel.g * 0.38317, 1.0 / 2.2) : 1.0 - exp(-pixel.g);
pixel.b = pixel.b < 1.413 ? pow(pixel.b * 0.38317, 1.0 / 2.2) : 1.0 - exp(-pixel.b);
return pixel;*/
double exposure_bias = 2.0; double exposure_bias = 2.0;
double W = 11.2; double W = 11.2;
double white_scale = 1.0 / _uncharted2Tonemap(W); double white_scale = 1.0 / _uncharted2Tonemap(W);

View file

@ -46,6 +46,17 @@ void lightingManagerDelete(LightingManager* filter)
void lightingManagerRegisterFilter(LightingManager* filter, FuncLightingAlterLight callback, void* data) void lightingManagerRegisterFilter(LightingManager* filter, FuncLightingAlterLight callback, void* data)
{ {
int i;
for (i = 0; i < filter->callbacks_count; i++)
{
if (filter->callbacks[i].filter == callback)
{
filter->callbacks[i].data = data;
return;
}
}
if (filter->callbacks_count < MAX_CALLBACK_COUNT) if (filter->callbacks_count < MAX_CALLBACK_COUNT)
{ {
filter->callbacks[filter->callbacks_count].filter = callback; filter->callbacks[filter->callbacks_count].filter = callback;
@ -80,6 +91,8 @@ void lightingPushLight(LightStatus* status, LightDefinition* light)
LightingManager* manager = status->manager; LightingManager* manager = status->manager;
LightDefinition final = *light; LightDefinition final = *light;
if (light->altered)
{
for (i = 0; i < manager->callbacks_count; i++) for (i = 0; i < manager->callbacks_count; i++)
{ {
LightFilterCallback callback = manager->callbacks[i]; LightFilterCallback callback = manager->callbacks[i];
@ -89,6 +102,7 @@ void lightingPushLight(LightStatus* status, LightDefinition* light)
final = temp; final = temp;
} }
} }
}
status->lights[status->light_count++] = final; status->lights[status->light_count++] = final;
} }
@ -110,14 +124,14 @@ Color lightingApplyStatus(LightStatus* status, Vector3 normal, SurfaceMaterial*
final.b += result.b; final.b += result.b;
} }
return result; return final;
} }
Color lightingApplyOneLight(LightDefinition* light, Vector3 eye, Vector3 location, Vector3 normal, SurfaceMaterial* material) Color lightingApplyOneLight(LightDefinition* light, Vector3 eye, Vector3 location, Vector3 normal, SurfaceMaterial* material)
{ {
Color result, light_color; Color result, light_color;
double diffuse, specular, normal_norm; double normal_norm;
Vector3 view, reflect, direction_inv; Vector3 direction_inv;
light_color = light->color; light_color = light->color;
direction_inv = v3Scale(light->direction, -1.0); direction_inv = v3Scale(light->direction, -1.0);
@ -129,46 +143,60 @@ Color lightingApplyOneLight(LightDefinition* light, Vector3 eye, Vector3 locatio
} }
normal = v3Normalize(normal); normal = v3Normalize(normal);
diffuse = v3Dot(direction_inv, normal); result = COLOR_BLACK;
/* diffused light */
double diffuse = v3Dot(direction_inv, normal) / M_PI;
if (diffuse > 0.0) if (diffuse > 0.0)
{ {
if (material->shininess > 0.0 && light->reflection > 0.0) result.r += diffuse * material->base.r * light_color.r;
{ result.g += diffuse * material->base.g * light_color.g;
view = v3Normalize(v3Sub(location, eye)); result.b += diffuse * material->base.b * light_color.b;
reflect = v3Sub(direction_inv, v3Scale(normal, 2.0 * v3Dot(direction_inv, normal))); }
specular = v3Dot(reflect, view); /* specular reflection */
if (material->reflection > 0.0 && light->reflection > 0.0)
{
Vector3 view = view = v3Normalize(v3Sub(location, eye));
Vector3 reflect = v3Sub(direction_inv, v3Scale(normal, 2.0 * v3Dot(direction_inv, normal)));
double specular = v3Dot(reflect, view);
if (specular > 0.0) if (specular > 0.0)
{ {
specular = pow(specular, material->shininess) * material->reflection; specular = pow(specular, material->shininess) * material->reflection;
result.r += specular * light_color.r;
result.g += specular * light_color.g;
result.b += specular * light_color.b;
} }
else
{
specular = 0.0;
}
}
else
{
specular = 0.0;
}
}
else
{
diffuse = 0.0;
specular = 0.0;
} }
specular *= normal_norm * light->reflection; /* specular reflection with fresnel effect */
diffuse = 1.0 - normal_norm + diffuse * normal_norm; /*if (material->reflection > 0.0 && light->reflection > 0.0)
{
result.r = material->base.r * diffuse * light_color.r + specular * light_color.r; Vector3 view = v3Normalize(v3Sub(location, eye));
result.g = material->base.g * diffuse * light_color.g + specular * light_color.g; Vector3 h = v3Normalize(v3Sub(direction_inv, view));
result.b = material->base.b * diffuse * light_color.b + specular * light_color.b; double fresnel = 0.02 + 0.98 * pow(1.0 - v3Dot(v3Scale(view, -1.0), h), 5.0);
result.a = material->base.a; double refl = v3Dot(h, normal);
if (refl > 0.0)
{
double waterBrdf = fresnel * pow(refl, material->shininess);
if (waterBrdf > 0.0)
{
refl = material->reflection * waterBrdf * light->reflection;
result.r += refl * light_color.r;
result.g += refl * light_color.g;
result.b += refl * light_color.b;
}
}
}*/
return result; return result;
} }
Vector3 lightingGetStatusLocation(LightStatus* status)
{
return status->location;
}
void materialSave(PackStream* stream, SurfaceMaterial* material) void materialSave(PackStream* stream, SurfaceMaterial* material)
{ {
colorSave(stream, &material->base); colorSave(stream, &material->base);

View file

@ -21,8 +21,7 @@ typedef struct
Vector3 direction; /* Global direction of the light */ Vector3 direction; /* Global direction of the light */
Color color; /* Main color of the light */ Color color; /* Main color of the light */
double reflection; /* Reflected factor of the light (for specular lighting) */ double reflection; /* Reflected factor of the light (for specular lighting) */
int filtered; /* Should the light be filtered (by atmosphere, water...) */ int altered; /* Should the light be filtered or masked (by atmosphere, water, ground, clouds...) */
int masked; /* Should the light be masked (cast shadows..) */
} LightDefinition; } LightDefinition;
typedef int (*FuncLightingAlterLight)(LightDefinition* light, Vector3 at, void* data); typedef int (*FuncLightingAlterLight)(LightDefinition* light, Vector3 at, void* data);
@ -38,6 +37,7 @@ LightStatus* lightingCreateStatus(LightingManager* manager, Vector3 location, Ve
void lightingDeleteStatus(LightStatus* status); void lightingDeleteStatus(LightStatus* status);
void lightingPushLight(LightStatus* status, LightDefinition* light); void lightingPushLight(LightStatus* status, LightDefinition* light);
Color lightingApplyStatus(LightStatus* status, Vector3 normal, SurfaceMaterial* material); Color lightingApplyStatus(LightStatus* status, Vector3 normal, SurfaceMaterial* material);
Vector3 lightingGetStatusLocation(LightStatus* status);
Color lightingApplyOneLight(LightDefinition* light, Vector3 eye, Vector3 location, Vector3 normal, SurfaceMaterial* material); Color lightingApplyOneLight(LightDefinition* light, Vector3 eye, Vector3 location, Vector3 normal, SurfaceMaterial* material);

View file

@ -108,7 +108,7 @@ void waterAutoPreset(WaterDefinition* definition, WaterPreset preset)
definition->depth_color.a = 1.0; definition->depth_color.a = 1.0;
definition->material.base.a = 1.0; definition->material.base.a = 1.0;
definition->material.reflection = 1.0; definition->material.reflection = 0.4;
definition->material.shininess = 16.0; definition->material.shininess = 16.0;
definition->foam_material.base.r = 0.8; definition->foam_material.base.r = 0.8;
definition->foam_material.base.g = 0.8; definition->foam_material.base.g = 0.8;