paysages: Started heavy lighting refactoring (WIP - Broken).

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@230 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2012-01-22 18:39:42 +00:00 committed by ThunderK
parent 6b9b813753
commit e265be6105
20 changed files with 592 additions and 180 deletions

View file

@ -207,7 +207,7 @@ void DialogNoise::revertToCurrent()
n = noiseGetLevelCount(_current); n = noiseGetLevelCount(_current);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
levels->addItem("Level"); levels->addItem(QString("Component %1").arg(i + 1));
} }
previewLevel->redraw(); previewLevel->redraw();
@ -230,7 +230,9 @@ void DialogNoise::addLevel()
void DialogNoise::removeLevel() void DialogNoise::removeLevel()
{ {
// TODO noiseRemoveLevel(_current, _current_level);
revertToCurrent();
} }
void DialogNoise::levelChanged(int row) void DialogNoise::levelChanged(int row)

75
gui_qt/formterrain.cpp Normal file
View file

@ -0,0 +1,75 @@
#include "formterrain.h"
#include "tools.h"
#include <QColor>
#include <QSlider>
#include <math.h>
#include "../lib_paysages/terrain.h"
#include "../lib_paysages/shared/functions.h"
#include "../lib_paysages/shared/constants.h"
static TerrainDefinition _definition;
/**************** Previews ****************/
class PreviewTerrainHeight:public Preview
{
public:
PreviewTerrainHeight(QWidget* parent):
Preview(parent)
{
}
protected:
QColor getColor(double x, double y)
{
double height;
height = terrainGetHeightNormalized(x, y);
return QColor((int)(255.0 * height), (int)(255.0 * height), (int)(255.0 * height));
}
};
class PreviewTerrainColor:public Preview
{
public:
PreviewTerrainColor(QWidget* parent):
Preview(parent)
{
_environment.toggle_fog = 0;
}
protected:
QColor getColor(double x, double y)
{
return colorToQColor(terrainGetColorCustom(x, y, scaling, &_definition, NULL, &_environment));
}
private:
TerrainEnvironment _environment;
};
/**************** Form ****************/
FormTerrain::FormTerrain(QWidget *parent):
BaseForm(parent)
{
_definition = terrainCreateDefinition();
previewHeight = new PreviewTerrainHeight(this);
previewColor = new PreviewTerrainColor(this);
addPreview(previewHeight, QString("Height preview"));
addPreview(previewColor, QString("Color preview"));
addInputNoise("Height", _definition.height_noise);
revertConfig();
}
void FormTerrain::revertConfig()
{
terrainCopyDefinition(terrainGetDefinition(), &_definition);
BaseForm::revertConfig();
}
void FormTerrain::applyConfig()
{
terrainSetDefinition(_definition);
BaseForm::applyConfig();
}

24
gui_qt/formterrain.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef _PAYSAGES_QT_FORMTERRAIN_H_
#define _PAYSAGES_QT_FORMTERRAIN_H_
#include <QWidget>
#include "preview.h"
#include "baseform.h"
class FormTerrain : public BaseForm
{
Q_OBJECT
public:
explicit FormTerrain(QWidget *parent = 0);
public slots:
virtual void revertConfig();
virtual void applyConfig();
private:
Preview* previewHeight;
Preview* previewColor;
};
#endif

View file

@ -13,10 +13,10 @@
static WaterDefinition _definition; static WaterDefinition _definition;
/**************** Previews ****************/ /**************** Previews ****************/
class PreviewCoverage:public Preview class PreviewWaterCoverage:public Preview
{ {
public: public:
PreviewCoverage(QWidget* parent): PreviewWaterCoverage(QWidget* parent):
Preview(parent) Preview(parent)
{ {
} }
@ -38,10 +38,10 @@ protected:
} }
}; };
class PreviewColor:public Preview class PreviewWaterColor:public Preview
{ {
public: public:
PreviewColor(QWidget* parent): PreviewWaterColor(QWidget* parent):
Preview(parent) Preview(parent)
{ {
} }
@ -85,6 +85,7 @@ protected:
environment.toggle_fog = 0; environment.toggle_fog = 0;
environment.toggle_shadows = 0; environment.toggle_shadows = 0;
quality.force_detail = 0.0001; quality.force_detail = 0.0001;
quality.detail_boost = 1.0;
result = waterGetColorCustom(location, look, &definition, &quality, &environment).final; result = waterGetColorCustom(location, look, &definition, &quality, &environment).final;
return colorToQColor(result); return colorToQColor(result);
@ -100,6 +101,7 @@ private:
if (direction.z < 0.0001) if (direction.z < 0.0001)
{ {
result.hit_color = COLOR_WHITE; result.hit_color = COLOR_WHITE;
result.hit_location = start;
} }
else else
{ {
@ -114,8 +116,10 @@ private:
{ {
result.hit_color = COLOR_GREY; result.hit_color = COLOR_GREY;
} }
result.hit_location.x = x;
result.hit_location.y = y;
result.hit_location.z = 0.0;
} }
/* TODO hit_location */
return result; return result;
} }
@ -127,8 +131,8 @@ FormWater::FormWater(QWidget *parent):
{ {
_definition = waterCreateDefinition(); _definition = waterCreateDefinition();
previewCoverage = new PreviewCoverage(this); previewCoverage = new PreviewWaterCoverage(this);
previewColor = new PreviewColor(this); previewColor = new PreviewWaterColor(this);
addPreview(previewCoverage, QString("Coverage preview")); addPreview(previewCoverage, QString("Coverage preview"));
addPreview(previewColor, QString("Color preview")); addPreview(previewColor, QString("Color preview"));

View file

@ -4,6 +4,7 @@
#include <QMenuBar> #include <QMenuBar>
#include <QFileDialog> #include <QFileDialog>
#include "formterrain.h"
#include "formwater.h" #include "formwater.h"
#include "formsky.h" #include "formsky.h"
#include "formrender.h" #include "formrender.h"
@ -30,7 +31,7 @@ MainWindow::MainWindow(QWidget *parent) :
QMenu* menu; QMenu* menu;
tabs = new QTabWidget(this); tabs = new QTabWidget(this);
tabs->addTab(new BaseForm(tabs), "Temp"); tabs->addTab(new FormTerrain(tabs), "Terrain");
tabs->addTab(new FormWater(tabs), "Water"); tabs->addTab(new FormWater(tabs), "Water");
tabs->addTab(new FormSky(tabs), "Sky"); tabs->addTab(new FormSky(tabs), "Sky");
tabs->addTab(new FormRender(tabs), "Render"); tabs->addTab(new FormRender(tabs), "Render");

View file

@ -14,34 +14,36 @@ win32:LIBS += ../libpaysages.a -lDevIL -lILU -lILUT -lglib-2.0 -lgthread-2.0
# Input # Input
HEADERS += ../lib_paysages/shared/functions.h ../lib_paysages/shared/types.h \ HEADERS += ../lib_paysages/shared/functions.h ../lib_paysages/shared/types.h \
mainwindow.h \
formwater.h \
preview.h \
baseform.h \ baseform.h \
inputdouble.h \
baseinput.h \ baseinput.h \
inputcolor.h \
formrender.h \
inputint.h \
dialogrender.h \
dialognoise.h \ dialognoise.h \
inputcolorgradation.h \ dialogrender.h \
formrender.h \
formsky.h \ formsky.h \
formterrain.h \
formwater.h \
inputcolor.h \
inputcolorgradation.h \
inputdouble.h \
inputint.h \
inputnoise.h \ inputnoise.h \
mainwindow.h \
preview.h \
tools.h tools.h
FORMS += FORMS +=
SOURCES += \ SOURCES += \
mainwindow.cpp \
formwater.cpp \
preview.cpp \
baseform.cpp \ baseform.cpp \
inputdouble.cpp \
baseinput.cpp \ baseinput.cpp \
inputcolor.cpp \
formrender.cpp \
inputint.cpp \
dialogrender.cpp \
dialognoise.cpp \ dialognoise.cpp \
inputcolorgradation.cpp \ dialogrender.cpp \
formrender.cpp \
formsky.cpp \ formsky.cpp \
inputnoise.cpp formterrain.cpp \
formwater.cpp \
inputcolor.cpp \
inputcolorgradation.cpp \
inputdouble.cpp \
inputint.cpp \
inputnoise.cpp \
mainwindow.cpp \
preview.cpp

View file

@ -3,8 +3,6 @@
#include <QPainter> #include <QPainter>
#include <QTimer> #include <QTimer>
static QVector<Preview*> _previews;
class PreviewDrawer:public QThread class PreviewDrawer:public QThread
{ {
public: public:
@ -48,15 +46,18 @@ Preview::Preview(QWidget* parent) :
this->xoffset = 0.0; this->xoffset = 0.0;
this->yoffset = 0.0; this->yoffset = 0.0;
this->pixbuf = new QImage(this->size(), QImage::Format_ARGB32); this->pixbuf = new QImage(this->size(), QImage::Format_ARGB32);
this->pixbuf->fill(0x00000000);
this->alive = true; this->alive = true;
this->need_rerender = false; this->need_rerender = false;
this->need_render = false; this->need_render = true;
this->setMinimumSize(256, 256); this->setMinimumSize(256, 256);
this->setMaximumSize(256, 256); this->setMaximumSize(256, 256);
this->resize(256, 256); this->resize(256, 256);
QObject::connect(this, SIGNAL(contentChange()), this, SLOT(update()));
this->updater = new PreviewDrawer(this); this->updater = new PreviewDrawer(this);
this->updater->start(); this->updater->start();
} }
@ -85,7 +86,7 @@ void Preview::doRender()
} }
if (this->need_render) if (this->need_render)
{ {
this->need_render = 0; this->need_render = false;
this->renderPixbuf(); this->renderPixbuf();
} }
} }
@ -94,7 +95,7 @@ void Preview::doRender()
void Preview::redraw() void Preview::redraw()
{ {
//lock->lock(); //lock->lock();
need_rerender = 1; need_rerender = true;
//lock->unlock(); //lock->unlock();
} }
@ -110,12 +111,18 @@ void Preview::resizeEvent(QResizeEvent* event)
QImage* image; QImage* image;
this->lock->lock(); this->lock->lock();
image = this->pixbuf;
this->pixbuf = new QImage(this->size(), QImage::Format_ARGB32);
delete image;
this->lock->unlock();
this->forceRender(); image = this->pixbuf;
this->pixbuf = new QImage(this->size(), QImage::Format_ARGB32);
this->pixbuf->fill(0x00000000);
this->need_rerender = false;
this->need_render = true;
delete image;
this->lock->unlock();
} }
void Preview::paintEvent(QPaintEvent* event) void Preview::paintEvent(QPaintEvent* event)
@ -128,8 +135,8 @@ void Preview::forceRender()
{ {
this->lock->lock(); this->lock->lock();
this->pixbuf->fill(0x00000000); this->pixbuf->fill(0x00000000);
this->need_rerender = 0; this->need_rerender = false;
this->need_render = 1; this->need_render = true;
this->lock->unlock(); this->lock->unlock();
} }
@ -164,14 +171,12 @@ void Preview::renderPixbuf()
} }
if (done && (x == w - 1 || x % 10 == 0)) if (done && (x == w - 1 || x % 10 == 0))
{ {
this->update(); emit contentChange();
} }
this->lock->unlock(); this->lock->unlock();
} }
} }
//static void _scrollPixbuf(SmallPreview* preview, int dx, int dy) //static void _scrollPixbuf(SmallPreview* preview, int dx, int dy)
//{ //{
// int xstart, ystart, xsize, ysize, y; // int xstart, ystart, xsize, ysize, y;

View file

@ -50,8 +50,12 @@ protected:
bool alive; bool alive;
bool need_rerender; bool need_rerender;
bool need_render; bool need_render;
private: private:
QThread* updater; QThread* updater;
signals:
void contentChange();
}; };
#endif #endif

View file

@ -15,6 +15,7 @@
#include "modifiers.h" #include "modifiers.h"
#include "terrain.h" #include "terrain.h"
#include "textures.h" #include "textures.h"
#include "lighting.h"
#ifdef WIN32 #ifdef WIN32
#include <windows.h> #include <windows.h>
@ -47,47 +48,6 @@ void autoInit()
#ifdef _SC_NPROCESSORS_ONLN #ifdef _SC_NPROCESSORS_ONLN
_cpu_count = (int)sysconf(_SC_NPROCESSORS_ONLN); _cpu_count = (int)sysconf(_SC_NPROCESSORS_ONLN);
#endif #endif
renderSetBackgroundColor(&COLOR_BLACK);
terrainInit();
waterInit();
renderInit();
}
void autoSave(char* filepath)
{
FILE* f = fopen(filepath, "wb");
texturesSave(f);
cameraSave(f);
cloudsSave(f);
fogSave(f);
lightingSave(f);
renderSave(f);
skySave(f);
terrainSave(f);
waterSave(f);
fclose(f);
}
void autoLoad(char* filepath)
{
FILE* f = fopen(filepath, "rb");
texturesLoad(f);
cameraLoad(f);
cloudsLoad(f);
fogLoad(f);
lightingLoad(f);
renderLoad(f);
skyLoad(f);
terrainLoad(f);
waterLoad(f);
fclose(f);
} }
void autoSetDaytime(int hour, int minute) void autoSetDaytime(int hour, int minute)
@ -98,8 +58,8 @@ void autoSetDaytime(int hour, int minute)
void autoSetDaytimeFraction(double daytime) void autoSetDaytimeFraction(double daytime)
{ {
SkyDefinition sky; SkyDefinition sky;
ColorGradation grad_sun; /*ColorGradation grad_sun;
Color sun; Color sun;*/
daytime = fmod(daytime, 1.0); daytime = fmod(daytime, 1.0);
if (daytime < 0.0) if (daytime < 0.0)
@ -107,7 +67,7 @@ void autoSetDaytimeFraction(double daytime)
daytime += 1.0; daytime += 1.0;
} }
lightingSetSunAngle(0.0, (daytime + 0.25) * M_PI * 2.0); /*lightingSetSunAngle(0.0, (daytime + 0.25) * M_PI * 2.0);
grad_sun = colorGradationCreate(); grad_sun = colorGradationCreate();
colorGradationAddRgba(&grad_sun, 0.2, 0.1, 0.1, 0.1, 1.0); colorGradationAddRgba(&grad_sun, 0.2, 0.1, 0.1, 0.1, 1.0);
@ -118,7 +78,7 @@ void autoSetDaytimeFraction(double daytime)
colorGradationAddRgba(&grad_sun, 0.75, 0.7, 0.6, 0.5, 1.0); colorGradationAddRgba(&grad_sun, 0.75, 0.7, 0.6, 0.5, 1.0);
colorGradationAddRgba(&grad_sun, 0.8, 0.1, 0.1, 0.1, 1.0); colorGradationAddRgba(&grad_sun, 0.8, 0.1, 0.1, 0.1, 1.0);
sun = colorGradationGet(&grad_sun, daytime); sun = colorGradationGet(&grad_sun, daytime);
lightingSetSunColor(sun); lightingSetSunColor(sun);*/
sky = skyGetDefinition(); sky = skyGetDefinition();
sky.daytime = daytime; sky.daytime = daytime;
@ -163,6 +123,7 @@ void autoGenRealisticLandscape(int seed)
CloudsDefinition cloud; CloudsDefinition cloud;
SkyDefinition sky; SkyDefinition sky;
TextureDefinition texture; TextureDefinition texture;
LightingDefinition lighting;
int layer; int layer;
HeightModifier* mod; HeightModifier* mod;
Zone* zone; Zone* zone;
@ -249,6 +210,12 @@ void autoGenRealisticLandscape(int seed)
sky.sun_radius = 0.02; sky.sun_radius = 0.02;
skySetDefinition(sky); skySetDefinition(sky);
/* Lighting */
lighting = lightingCreateDefinition();
lighting.autosetfromsky = 1;
lightingSetDefinition(lighting);
/* Terrain */
terrain = terrainCreateDefinition(); terrain = terrainCreateDefinition();
noiseGenerateBaseNoise(terrain.height_noise, 1048576); noiseGenerateBaseNoise(terrain.height_noise, 1048576);
noiseAddLevelsSimple(terrain.height_noise, 10, 10.0, 1.0); noiseAddLevelsSimple(terrain.height_noise, 10, 10.0, 1.0);
@ -275,6 +242,7 @@ void autoGenRealisticLandscape(int seed)
terrainSetDefinition(terrain); terrainSetDefinition(terrain);
terrainDeleteDefinition(terrain); terrainDeleteDefinition(terrain);
/* Textures */
layer = texturesAddLayer(); layer = texturesAddLayer();
texture = texturesCreateDefinition(); texture = texturesCreateDefinition();
noiseGenerateBaseNoise(texture.bump_noise, 102400); noiseGenerateBaseNoise(texture.bump_noise, 102400);
@ -308,6 +276,7 @@ void autoGenRealisticLandscape(int seed)
texturesSetDefinition(layer, texture); texturesSetDefinition(layer, texture);
texturesDeleteDefinition(texture);*/ texturesDeleteDefinition(texture);*/
/* Fog */
fogSetDistance(20.0, 100.0); fogSetDistance(20.0, 100.0);
} }

View file

@ -1,11 +1,13 @@
#include "clouds.h"
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include "lighting.h"
#include "shared/types.h" #include "shared/types.h"
#include "shared/functions.h" #include "shared/functions.h"
#include "shared/constants.h" #include "shared/constants.h"
#include "shared/globals.h" #include "shared/globals.h"
#include "clouds.h"
#define MAX_LAYERS 10 #define MAX_LAYERS 10
@ -378,22 +380,26 @@ static int _findSegments(CloudsDefinition* definition, CloudsQuality* quality, V
return segment_count; return segment_count;
} }
static Color _applyLayerLighting(CloudsDefinition* definition, CloudsQuality* quality, Vector3 position, Color base, double detail) typedef struct
{
CloudsDefinition* definition;
CloudsQuality* quality;
Color base;
double detail;
} LightFilterData;
static Color _lightFilter(Color light, Vector3 location, Vector3 light_location, Vector3 direction_to_light, void* custom_data)
{ {
Vector3 direction, normal;
double inside_depth, total_depth; double inside_depth, total_depth;
CloudSegment segments[20]; CloudSegment segments[20];
Color result; Color result;
LightFilterData data;
normal = _getNormal(definition, position, 0.5); data = *((LightFilterData*)custom_data);
normal = v3Add(normal, _getNormal(definition, position, 0.2)); data.detail = (data.detail < 0.1) ? 0.1 : data.detail;
normal = v3Add(normal, _getNormal(definition, position, 0.1));
result = lightingApply(position, normal, 0.0, base, 0.3, 0.1);
direction = sun_direction_inv;
detail = (detail < 0.1) ? 0.1 : detail;
/* FIXME Dont hard-code max_total_length */ /* FIXME Dont hard-code max_total_length */
_findSegments(definition, quality, position, direction, detail, 20, 50.0, 300.0, &inside_depth, &total_depth, segments); _findSegments(data.definition, data.quality, location, direction_to_light, data.detail, 20, 50.0, 300.0, &inside_depth, &total_depth, segments);
inside_depth *= 0.02; inside_depth *= 0.02;
if (inside_depth > 1.0) if (inside_depth > 1.0)
@ -401,13 +407,40 @@ static Color _applyLayerLighting(CloudsDefinition* definition, CloudsQuality* qu
inside_depth = 1.0; inside_depth = 1.0;
} }
result.r = base.r * sun_color_lum * (0.9 - 0.2 * inside_depth) + result.r * (0.1 + 0.1 * inside_depth) + (0.1 - inside_depth * 0.1) * sun_color_lum; result.r = data.base.r * sun_color_lum * (0.9 - 0.2 * inside_depth) + result.r * (0.1 + 0.1 * inside_depth) + (0.1 - inside_depth * 0.1) * sun_color_lum;
result.g = base.g * sun_color_lum * (0.9 - 0.2 * inside_depth) + result.g * (0.1 + 0.1 * inside_depth) + (0.1 - inside_depth * 0.1) * sun_color_lum; result.g = data.base.g * sun_color_lum * (0.9 - 0.2 * inside_depth) + result.g * (0.1 + 0.1 * inside_depth) + (0.1 - inside_depth * 0.1) * sun_color_lum;
result.b = base.b * sun_color_lum * (0.9 - 0.2 * inside_depth) + result.b * (0.1 + 0.1 * inside_depth) + (0.1 - inside_depth * 0.1) * sun_color_lum; result.b = data.base.b * sun_color_lum * (0.9 - 0.2 * inside_depth) + result.b * (0.1 + 0.1 * inside_depth) + (0.1 - inside_depth * 0.1) * sun_color_lum;
return result; return result;
} }
static Color _applyLayerLighting(CloudsDefinition* definition, CloudsQuality* quality, Vector3 position, Color base, double detail)
{
Vector3 normal;
ReceiverMaterial material;
LightingEnvironment lighting_environment;
LightFilterData data;
normal = _getNormal(definition, position, 0.5);
normal = v3Add(normal, _getNormal(definition, position, 0.2));
normal = v3Add(normal, _getNormal(definition, position, 0.1));
//normal = v3Normalize(normal);
data.definition = definition;
data.quality = quality;
data.detail = detail;
data.base = base;
lighting_environment.filter = _lightFilter;
lighting_environment.custom_data = &data;
material.base = base;
material.reflection = 0.3;
material.shininess = 0.1;
return lightingApplyCustom(position, normal, material, NULL, NULL, &lighting_environment);
}
Color cloudsGetColorCustom(Vector3 start, Vector3 end, CloudsDefinition* definition, CloudsQuality* quality, CloudsEnvironment* environment) Color cloudsGetColorCustom(Vector3 start, Vector3 end, CloudsDefinition* definition, CloudsQuality* quality, CloudsEnvironment* environment)
{ {
int i, segment_count; int i, segment_count;

View file

@ -1,25 +1,173 @@
#include "lighting.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <unistd.h>
#include <string.h>
#include "shared/types.h" #include "shared/types.h"
#include "shared/functions.h" #include "shared/functions.h"
#include "shared/constants.h" #include "shared/constants.h"
#include "shared/globals.h" #include "shared/globals.h"
static Color sun_color; static LightingDefinition _definition;
double sun_color_lum; static LightingQuality _quality;
Vector3 sun_direction; static LightingEnvironment _environment;
Vector3 sun_direction_inv;
static LightDefinition _LIGHT_NULL;
static Color _standardFilter(Color light, Vector3 location, Vector3 light_location, Vector3 direction_to_light, void* custom_data)
{
// TODO Find shadows
return light;
}
void lightingInit()
{
_definition = lightingCreateDefinition();
_environment.filter = _standardFilter;
_environment.custom_data = NULL;
_LIGHT_NULL.color = COLOR_BLACK;
_LIGHT_NULL.direction.x = 0.0;
_LIGHT_NULL.direction.y = 1.0;
_LIGHT_NULL.direction.z = 0.0;
}
void lightingSave(FILE* f) void lightingSave(FILE* f)
{ {
// TODO
} }
void lightingLoad(FILE* f) void lightingLoad(FILE* f)
{ {
// TODO
} }
void lightingSetSunDirection(double x, double y, double z) LightingDefinition lightingCreateDefinition()
{
LightingDefinition definition;
definition.autosetfromsky = 0;
definition.nblights = 0;
definition._nbautolights = 0;
return definition;
}
void lightingDeleteDefinition(LightingDefinition definition)
{
}
void lightingCopyDefinition(LightingDefinition source, LightingDefinition* destination)
{
*destination = source;
}
void lightingSetDefinition(LightingDefinition definition)
{
lightingCopyDefinition(definition, &_definition);
}
LightingDefinition lightingGetDefinition()
{
return _definition;
}
void lightingValidateDefinition(LightingDefinition* definition)
{
if (!definition)
{
lightingValidateDefinition(&_definition);
}
if (definition->autosetfromsky)
{
// TODO Get lights from sky
}
else
{
definition->_nbautolights = 0;
}
}
int lightingGetLightCount(LightingDefinition* definition)
{
return definition->nblights;
}
LightDefinition lightingGetLight(LightingDefinition* definition, int light)
{
if (light >= 0 && light < definition->nblights)
{
return definition->lights[light];
}
else
{
return _LIGHT_NULL;
}
}
int lightingAddLight(LightingDefinition* definition, LightDefinition light)
{
if (definition->nblights < MAX_LIGHTS)
{
definition->lights[definition->nblights] = light;
return definition->nblights++;
}
else
{
return -1;
}
}
void lightingDeleteLight(LightingDefinition* definition, int light)
{
if (light >= 0 && light < definition->nblights)
{
if (definition->nblights > 1 && light < definition->nblights - 1)
{
memmove(definition->lights + light, definition->lights + light + 1, sizeof(LightDefinition) * definition->nblights - light - 1);
}
definition->nblights--;
}
}
void lightingSetQuality(LightingQuality quality)
{
_quality = quality;
}
LightingQuality lightingGetQuality()
{
return _quality;
}
Color lightingApplyCustom(Vector3 location, Vector3 normal, ReceiverMaterial material, LightingDefinition* definition, LightingQuality* quality, LightingEnvironment* environment)
{
if (!definition)
{
definition = &_definition;
}
if (!quality)
{
quality = &_quality;
}
if (!environment)
{
environment = &_environment;
}
return COLOR_RED;
}
Color lightingApply(Vector3 location, Vector3 normal, ReceiverMaterial material)
{
return lightingApplyCustom(location, normal, material, &_definition, &_quality, &_environment);
}
/*void lightingSetSunDirection(double x, double y, double z)
{ {
sun_direction.x = x; sun_direction.x = x;
sun_direction.y = y; sun_direction.y = y;
@ -79,4 +227,4 @@ Color lightingApply(Vector3 location, Vector3 normal, double shadowing, Color ba
result.a = base.a; result.a = base.a;
return result; return result;
} }*/

75
lib_paysages/lighting.h Normal file
View file

@ -0,0 +1,75 @@
#ifndef _PAYSAGES_LIGHTING_H_
#define _PAYSAGES_LIGHTING_H_
#include "shared/types.h"
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_LIGHTS 10
typedef struct
{
Vector3 direction;
Color color;
double maxshadow;
} LightDefinition;
typedef struct
{
int autosetfromsky;
int nblights;
LightDefinition lights[MAX_LIGHTS];
int _nbautolights;
LightDefinition _autolights[MAX_LIGHTS];
} LightingDefinition;
typedef struct
{
int unused;
} LightingQuality;
typedef Color (*LightFilter)(Color light, Vector3 location, Vector3 light_location, Vector3 direction_to_light, void* custom_data);
typedef struct
{
LightFilter filter;
void* custom_data;
} LightingEnvironment;
typedef struct
{
Color base;
double reflection;
double shininess;
} ReceiverMaterial;
void lightingInit();
void lightingSave(FILE* f);
void lightingLoad(FILE* f);
LightingDefinition lightingCreateDefinition();
void lightingDeleteDefinition(LightingDefinition definition);
void lightingCopyDefinition(LightingDefinition source, LightingDefinition* destination);
void lightingSetDefinition(LightingDefinition definition);
LightingDefinition lightingGetDefinition();
void lightingValidateDefinition(LightingDefinition* definition);
int lightingGetLightCount(LightingDefinition* definition);
LightDefinition lightingGetLight(LightingDefinition* definition, int light);
int lightingAddLight(LightingDefinition* definition, LightDefinition light);
void lightingDeleteLight(LightingDefinition* definition, int light);
void lightingSetQuality(LightingQuality quality);
LightingQuality lightingGetQuality();
Color lightingApplyCustom(Vector3 location, Vector3 normal, ReceiverMaterial material, LightingDefinition* definition, LightingQuality* quality, LightingEnvironment* environment);
Color lightingApply(Vector3 location, Vector3 normal, ReceiverMaterial material);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,8 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include "IL/il.h" #include "IL/il.h"
#include "IL/ilu.h" #include "IL/ilu.h"
@ -12,7 +9,13 @@
#include "shared/functions.h" #include "shared/functions.h"
#include "shared/globals.h" #include "shared/globals.h"
#include "shared/system.h" #include "shared/system.h"
#include "terrain.h" #include "terrain.h"
#include "water.h"
#include "lighting.h"
#include "textures.h"
#include "sky.h"
#include "clouds.h"
void paysagesInit() void paysagesInit()
{ {
@ -24,6 +27,12 @@ void paysagesInit()
cameraSetTarget(0.0, 5.0, 0.0); cameraSetTarget(0.0, 5.0, 0.0);
autoInit(); autoInit();
skyInit();
terrainInit();
texturesInit();
waterInit();
lightingInit();
renderInit();
autoSetRenderQuality(5); autoSetRenderQuality(5);
autoGenRealisticLandscape(0); autoGenRealisticLandscape(0);
@ -42,3 +51,39 @@ void paysagesInit()
cameraSetLocation(x - 2.0, height, 0.0); cameraSetLocation(x - 2.0, height, 0.0);
cameraSetTarget(x - 1.0, height, 0.0);*/ cameraSetTarget(x - 1.0, height, 0.0);*/
} }
void paysagesSave(char* filepath)
{
FILE* f = fopen(filepath, "wb");
cameraSave(f);
cloudsSave(f);
fogSave(f);
renderSave(f);
skySave(f);
terrainSave(f);
texturesSave(f);
waterSave(f);
lightingSave(f);
fclose(f);
}
void paysagesLoad(char* filepath)
{
FILE* f = fopen(filepath, "rb");
cameraLoad(f);
cloudsLoad(f);
fogLoad(f);
renderLoad(f);
skyLoad(f);
terrainLoad(f);
texturesLoad(f);
waterLoad(f);
lightingLoad(f);
fclose(f);
}

View file

@ -217,6 +217,18 @@ void noiseAddLevelsSimple(NoiseGenerator* generator, int level_count, double sca
noiseAddLevels(generator, level_count, level, 0.5, 0.5, 1); noiseAddLevels(generator, level_count, level, 0.5, 0.5, 1);
} }
void noiseRemoveLevel(NoiseGenerator* generator, int level)
{
if (level >= 0 && level < generator->level_count)
{
if (generator->level_count > 1 && level < generator->level_count - 1)
{
memmove(generator->levels + level, generator->levels + level + 1, sizeof(NoiseLevel) * (generator->level_count - level - 1));
}
generator->level_count--;
}
}
int noiseGetLevel(NoiseGenerator* generator, int level, NoiseLevel* params) int noiseGetLevel(NoiseGenerator* generator, int level, NoiseLevel* params)
{ {
if (level >= 0 && level < generator->level_count) if (level >= 0 && level < generator->level_count)

View file

@ -56,6 +56,7 @@ void renderLoad(FILE* f)
void renderInit() void renderInit()
{ {
_lock = mutexCreate(); _lock = mutexCreate();
renderSetBackgroundColor(&COLOR_BLACK);
} }
void renderSetSize(int width, int height) void renderSetSize(int width, int height)

View file

@ -9,6 +9,8 @@ extern "C" {
#include <stdio.h> #include <stdio.h>
void paysagesInit(); void paysagesInit();
void paysagesSave(char* filepath);
void paysagesLoad(char* filepath);
/* array.c */ /* array.c */
void arrayCreate(Array* array, int item_size); void arrayCreate(Array* array, int item_size);
@ -21,8 +23,6 @@ void arrayClear(Array* array);
/* auto.c */ /* auto.c */
void autoInit(); void autoInit();
void autoSave(char* filepath);
void autoLoad(char* filepath);
void autoSetDaytime(int hour, int minute); void autoSetDaytime(int hour, int minute);
void autoSetDaytimeFraction(double daytime); void autoSetDaytimeFraction(double daytime);
void autoSetRenderQuality(int quality); void autoSetRenderQuality(int quality);
@ -97,14 +97,6 @@ void fogSetColor(Color col);
void fogSetDistance(double near, double far); void fogSetDistance(double near, double far);
Color fogApplyToLocation(Vector3 location, Color base); Color fogApplyToLocation(Vector3 location, Color base);
/* lighting.c */
void lightingSave(FILE* f);
void lightingLoad(FILE* f);
void lightingSetSunDirection(double x, double y, double z);
void lightingSetSunAngle(double hor, double ver);
void lightingSetSunColor(Color col);
Color lightingApply(Vector3 location, Vector3 normal, double shadowing, Color base, double reflection, double shininess);
/* noise.c */ /* noise.c */
NoiseGenerator* noiseCreateGenerator(); NoiseGenerator* noiseCreateGenerator();
void noiseDeleteGenerator(NoiseGenerator* generator); void noiseDeleteGenerator(NoiseGenerator* generator);
@ -120,6 +112,7 @@ void noiseAddLevel(NoiseGenerator* generator, NoiseLevel level);
void noiseAddLevelSimple(NoiseGenerator* generator, double scaling, double height); void noiseAddLevelSimple(NoiseGenerator* generator, double scaling, double height);
void noiseAddLevels(NoiseGenerator* generator, int level_count, NoiseLevel start_level, double scaling_factor, double height_factor, int randomize_offset); void noiseAddLevels(NoiseGenerator* generator, int level_count, NoiseLevel start_level, double scaling_factor, double height_factor, int randomize_offset);
void noiseAddLevelsSimple(NoiseGenerator* generator, int level_count, double scaling, double height); void noiseAddLevelsSimple(NoiseGenerator* generator, int level_count, double scaling, double height);
void noiseRemoveLevel(NoiseGenerator* generator, int level);
int noiseGetLevel(NoiseGenerator* generator, int level, NoiseLevel* params); int noiseGetLevel(NoiseGenerator* generator, int level, NoiseLevel* params);
void noiseSetLevel(NoiseGenerator* generator, int level, NoiseLevel params); void noiseSetLevel(NoiseGenerator* generator, int level, NoiseLevel params);
void noiseSetLevelSimple(NoiseGenerator* generator, int level, double scaling, double height); void noiseSetLevelSimple(NoiseGenerator* generator, int level, double scaling, double height);

View file

@ -20,6 +20,8 @@ void terrainInit()
{ {
_definition = terrainCreateDefinition(); _definition = terrainCreateDefinition();
_max_height = noiseGetMaxValue(_definition.height_noise); _max_height = noiseGetMaxValue(_definition.height_noise);
_environment.toggle_fog = 1;
} }
void terrainSave(FILE* f) void terrainSave(FILE* f)
@ -214,13 +216,15 @@ double terrainGetShadow(Vector3 start, Vector3 direction)
} }
} }
static Color _getColor(TerrainDefinition* definition, Vector3 point, double precision) static Color _getColor(TerrainDefinition* definition, TerrainEnvironment* environment, Vector3 point, double precision)
{ {
Color color; Color color;
/* FIXME Environment for textures should be customized */
color = texturesGetColor(point); color = texturesGetColor(point);
if (environment->toggle_fog)
{
color = fogApplyToLocation(point, color); color = fogApplyToLocation(point, color);
}
//color = cloudsApplySegmentResult(color, camera_location, point); //color = cloudsApplySegmentResult(color, camera_location, point);
return color; return color;
@ -248,7 +252,7 @@ int terrainProjectRay(Vector3 start, Vector3 direction, Vector3* hit_point, Colo
{ {
start.y = height; start.y = height;
*hit_point = start; *hit_point = start;
*hit_color = _getColor(&_definition, start, inc_value); *hit_color = _getColor(&_definition, &_environment, start, inc_value);
return 1; return 1;
} }
@ -279,7 +283,7 @@ static int _postProcessFragment(RenderFragment* fragment)
point = _getPoint(&_definition, point.x, point.z, precision); point = _getPoint(&_definition, point.x, point.z, precision);
fragment->vertex.color = _getColor(&_definition, point, precision); fragment->vertex.color = _getColor(&_definition, &_environment, point, precision);
return 1; return 1;
} }
@ -324,7 +328,20 @@ double terrainGetHeightNormalized(double x, double z)
Color terrainGetColorCustom(double x, double z, double detail, TerrainDefinition* definition, TerrainQuality* quality, TerrainEnvironment* environment) Color terrainGetColorCustom(double x, double z, double detail, TerrainDefinition* definition, TerrainQuality* quality, TerrainEnvironment* environment)
{ {
return _getColor(definition, _getPoint(definition, x, z, detail), detail); if (!definition)
{
definition = &_definition;
}
if (!quality)
{
quality = &_quality;
}
if (!environment)
{
environment = &_environment;
}
return _getColor(definition, environment, _getPoint(definition, x, z, detail), detail);
} }
Color terrainGetColor(double x, double z, double detail) Color terrainGetColor(double x, double z, double detail)

View file

@ -26,7 +26,7 @@ typedef struct
typedef struct typedef struct
{ {
int unused; int toggle_fog;
} TerrainEnvironment; } TerrainEnvironment;
void terrainInit(); void terrainInit();

View file

@ -5,6 +5,7 @@
#include "water.h" #include "water.h"
#include "terrain.h" #include "terrain.h"
#include "lighting.h"
#include <math.h> #include <math.h>
@ -80,6 +81,9 @@ WaterDefinition waterCreateDefinition()
result.main_color = COLOR_BLACK; result.main_color = COLOR_BLACK;
result.depth_color = COLOR_BLACK; result.depth_color = COLOR_BLACK;
result.height = -1000.0; result.height = -1000.0;
result.reflection = 0.0;
result.transparency = 0.0;
result.transparency_depth = 0.0;
result.waves_noise = noiseCreateGenerator(); result.waves_noise = noiseCreateGenerator();
result.waves_noise_height = 0.02; result.waves_noise_height = 0.02;
result.waves_noise_scale = 0.2; result.waves_noise_scale = 0.2;
@ -181,7 +185,8 @@ WaterResult waterGetColorCustom(Vector3 location, Vector3 look, WaterDefinition*
RayCastingResult refracted; RayCastingResult refracted;
Vector3 normal; Vector3 normal;
Color color; Color color;
double shadowed, detail, depth; ReceiverMaterial material;
double detail, depth;
if (definition == NULL) if (definition == NULL)
{ {
@ -231,15 +236,10 @@ WaterResult waterGetColorCustom(Vector3 location, Vector3 look, WaterDefinition*
color.b = definition->main_color.b * (1.0 - definition->transparency) + result.reflected.b * definition->reflection + result.refracted.b * definition->transparency; color.b = definition->main_color.b * (1.0 - definition->transparency) + result.reflected.b * definition->reflection + result.refracted.b * definition->transparency;
color.a = 1.0; color.a = 1.0;
if (environment->toggle_shadows) material.base = color;
{ material.reflection = 0.8;
shadowed = terrainGetShadow(location, sun_direction_inv); material.shininess = 0.6;
} color = lightingApplyCustom(location, normal, material, environment->lighting_definition, NULL, environment->lighting_environment);
else
{
shadowed = 0.0;
}
color = lightingApply(location, normal, shadowed, color, 0.8, 0.6);
if (environment->toggle_fog) if (environment->toggle_fog)
{ {
color = fogApplyToLocation(location, color); color = fogApplyToLocation(location, color);

View file

@ -2,6 +2,7 @@
#define _PAYSAGES_WATER_H_ #define _PAYSAGES_WATER_H_
#include "shared/types.h" #include "shared/types.h"
#include "lighting.h"
#include <stdio.h> #include <stdio.h>
#ifdef __cplusplus #ifdef __cplusplus
@ -32,7 +33,8 @@ typedef struct
RayCastingFunction reflection_function; RayCastingFunction reflection_function;
RayCastingFunction refraction_function; RayCastingFunction refraction_function;
int toggle_fog; int toggle_fog;
int toggle_shadows; LightingDefinition* lighting_definition;
LightingEnvironment* lighting_environment;
} WaterEnvironment; } WaterEnvironment;
typedef struct typedef struct