paysages : ColorGradation now uses the new Curve system + started DialogColorGradation rewrite with curve editor (WIP).

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@276 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2012-03-08 15:10:25 +00:00 committed by ThunderK
parent a02d199c7b
commit c9fd2cca95
21 changed files with 422 additions and 331 deletions

2
TODO
View file

@ -1,5 +1,5 @@
- Implement scaling and scrolling on previews. - Implement scaling and scrolling on previews.
- Replace FILE* by a custom type for Save and Load.
- Water and terrain LOD moves with the camera, fix it like in the wanderer. - Water and terrain LOD moves with the camera, fix it like in the wanderer.
- Restore render progress. - Restore render progress.
- Find a licence and apply it. - Find a licence and apply it.
- Translate UI ?

View file

@ -8,6 +8,7 @@
#include "baseinput.h" #include "baseinput.h"
#include "../lib_paysages/shared/types.h" #include "../lib_paysages/shared/types.h"
#include "../lib_paysages/noise.h" #include "../lib_paysages/noise.h"
#include "../lib_paysages/color.h"
class BaseForm:public QWidget class BaseForm:public QWidget
{ {

View file

@ -10,117 +10,20 @@
#include <QPushButton> #include <QPushButton>
#include "baseform.h" #include "baseform.h"
#include "tools.h" #include "tools.h"
#include "widgetcurveeditor.h"
/**************** Preview ****************/
class PreviewColorGradation:public BasePreview
{
public:
PreviewColorGradation(QWidget* parent, ColorGradation* gradation): BasePreview(parent)
{
_gradation_original = gradation;
_gradation_preview = *gradation;
setMinimumSize(300, 30);
setMaximumSize(300, 30);
resize(300, 30);
// TODO No scrolling/scaling
}
protected:
void updateData()
{
_gradation_preview = *_gradation_original;
}
QColor getColor(double x, double y)
{
return colorToQColor(colorGradationGet(&_gradation_preview, x / 600 + 0.5));
}
private:
ColorGradation* _gradation_original;
ColorGradation _gradation_preview;
};
/**************** Form ****************/
class FormColorGradation:public BaseForm
{
public:
FormColorGradation(QWidget* parent, ColorGradation* gradation):BaseForm(parent, true, true)
{
QPushButton* button;
_gradation = gradation;
addPreview(new PreviewColorGradation(this, _gradation), tr("Preview"));
addInputDouble(tr("Position"), &_layer.start, 0.0, 1.0, 0.01, 0.1);
addInputColor(tr("Color"), &_layer.col);
button = addButton(tr("Validate"));
QObject::connect(button, SIGNAL(clicked()), parent, SLOT(accept()));
button = addButton(tr("Revert"));
QObject::connect(button, SIGNAL(clicked()), parent, SLOT(revert()));
button = addButton(tr("Cancel"));
QObject::connect(button, SIGNAL(clicked()), parent, SLOT(reject()));
revertConfig();
}
void revertConfig()
{
setLayerCount(colorGradationGetPartCount(_gradation));
}
void configChangeEvent()
{
colorGradationSetPart(_gradation, currentLayer(), _layer);
BaseForm::configChangeEvent();
}
void layerAddedEvent()
{
if (colorGradationAddPart(_gradation) >= 0)
{
BaseForm::layerAddedEvent();
}
}
void layerDeletedEvent(int layer)
{
colorGradationDelPart(_gradation, layer);
BaseForm::layerDeletedEvent(layer);
}
void layerSelectedEvent(int layer)
{
_layer = colorGradationGetPart(_gradation, layer);
BaseForm::layerSelectedEvent(layer);
}
private:
ColorGradation* _gradation;
ColorGradationPart _layer;
};
/**************** Dialog ****************/ /**************** Dialog ****************/
DialogColorGradation::DialogColorGradation(QWidget *parent, ColorGradation* gradation): DialogColorGradation::DialogColorGradation(QWidget *parent, ColorGradation* gradation):
QDialog(parent) QDialog(parent)
{ {
QWidget* preview;
_base = gradation; _base = gradation;
_current = colorGradationCreate(); _current = colorGradationCreate();
_current = *_base; colorGradationCopy(_base, _current);
setLayout(new QHBoxLayout()); setLayout(new QHBoxLayout());
preview = new QWidget(this); _curve_editor = new WidgetCurveEditor(this);
layout()->addWidget(preview); layout()->addWidget(_curve_editor);
_form = new FormColorGradation(this, &_current);
layout()->addWidget(_form);
/*QObject::connect(button, SIGNAL(clicked()), this, SLOT(accept())); /*QObject::connect(button, SIGNAL(clicked()), this, SLOT(accept()));
QObject::connect(button, SIGNAL(clicked()), this, SLOT(revert())); QObject::connect(button, SIGNAL(clicked()), this, SLOT(revert()));
@ -133,6 +36,7 @@ DialogColorGradation::DialogColorGradation(QWidget *parent, ColorGradation* grad
DialogColorGradation::~DialogColorGradation() DialogColorGradation::~DialogColorGradation()
{ {
colorGradationDelete(_current);
} }
bool DialogColorGradation::getGradation(QWidget* parent, ColorGradation* gradation) bool DialogColorGradation::getGradation(QWidget* parent, ColorGradation* gradation)
@ -154,18 +58,17 @@ void DialogColorGradation::closeEvent(QCloseEvent* e)
void DialogColorGradation::accept() void DialogColorGradation::accept()
{ {
*_base = _current; colorGradationCopy(_current, _base);
QDialog::accept(); QDialog::accept();
} }
void DialogColorGradation::revert() void DialogColorGradation::revert()
{ {
_current = *_base; colorGradationCopy(_base, _current);
revertToCurrent(); revertToCurrent();
} }
void DialogColorGradation::revertToCurrent() void DialogColorGradation::revertToCurrent()
{ {
_form->revertConfig(); // TODO
} }

View file

@ -3,6 +3,7 @@
#include <QDialog> #include <QDialog>
#include "baseform.h" #include "baseform.h"
#include "widgetcurveeditor.h"
#include "../lib_paysages/color.h" #include "../lib_paysages/color.h"
@ -27,8 +28,8 @@ private:
private: private:
ColorGradation* _base; ColorGradation* _base;
ColorGradation _current; ColorGradation* _current;
BaseForm* _form; WidgetCurveEditor* _curve_editor;
}; };
#endif #endif

View file

@ -86,10 +86,10 @@ FormSky::FormSky(QWidget *parent):
addPreview(previewEast, QString(tr("East preview"))); addPreview(previewEast, QString(tr("East preview")));
addInputDouble(tr("Day time"), &_definition.daytime, 0.0, 1.0, 0.01, 0.1); addInputDouble(tr("Day time"), &_definition.daytime, 0.0, 1.0, 0.01, 0.1);
addInputColorGradation(tr("Sun color"), &_definition.sun_color); addInputColorGradation(tr("Sun color"), _definition.sun_color);
addInputDouble(tr("Sun radius"), &_definition.sun_radius, 0.0, 0.3, 0.01, 0.03); addInputDouble(tr("Sun radius"), &_definition.sun_radius, 0.0, 0.3, 0.01, 0.03);
addInputColorGradation(tr("Zenith color"), &_definition.zenith_color); addInputColorGradation(tr("Zenith color"), _definition.zenith_color);
addInputColorGradation(tr("Haze color"), &_definition.haze_color); addInputColorGradation(tr("Haze color"), _definition.haze_color);
addInputDouble(tr("Haze height"), &_definition.haze_height, 0.0, 1.0, 0.01, 0.1); addInputDouble(tr("Haze height"), &_definition.haze_height, 0.0, 1.0, 0.01, 0.1);
addInputDouble(tr("Haze smoothing"), &_definition.haze_smoothing, 0.0, 1.0, 0.01, 0.1); addInputDouble(tr("Haze smoothing"), &_definition.haze_smoothing, 0.0, 1.0, 0.01, 0.1);

View file

@ -10,10 +10,9 @@
class ColorGradationPreview:public QWidget class ColorGradationPreview:public QWidget
{ {
public: public:
ColorGradationPreview(QWidget* parent, ColorGradation* gradation): ColorGradationPreview(QWidget* parent, ColorGradation* gradation) : QWidget(parent)
QWidget(parent),
gradation(gradation)
{ {
this->gradation = gradation;
} }
void paintEvent(QPaintEvent* event) void paintEvent(QPaintEvent* event)
@ -32,10 +31,10 @@ public:
ColorGradation* gradation; ColorGradation* gradation;
}; };
InputColorGradation::InputColorGradation(QWidget* form, QString label, ColorGradation* value): InputColorGradation::InputColorGradation(QWidget* form, QString label, ColorGradation* value) : BaseInput(form, label)
BaseInput(form, label),
_value(value)
{ {
_value = value;
_preview = new ColorGradationPreview(form, value); _preview = new ColorGradationPreview(form, value);
_preview->setMinimumSize(200, 20); _preview->setMinimumSize(200, 20);
@ -63,12 +62,14 @@ void InputColorGradation::revert()
void InputColorGradation::editGradation() void InputColorGradation::editGradation()
{ {
ColorGradation gradation; ColorGradation* gradation;
gradation = *_value; gradation = colorGradationCreate();
if (DialogColorGradation::getGradation(_preview, &gradation)) colorGradationCopy(_value, gradation);
if (DialogColorGradation::getGradation(_preview, gradation))
{ {
*_value = gradation; colorGradationCopy(gradation, _value);
applyValue(); applyValue();
} }
colorGradationDelete(gradation);
} }

View file

@ -4,7 +4,7 @@
#include <QWidget> #include <QWidget>
#include "baseinput.h" #include "baseinput.h"
#include "../lib_paysages/shared/types.h" #include "../lib_paysages/color.h"
class InputColorGradation:public BaseInput class InputColorGradation:public BaseInput
{ {

View file

@ -94,9 +94,9 @@ MainWindow::MainWindow(QWidget *parent) :
tabs->addTab(form, tr("Clouds")); tabs->addTab(form, tr("Clouds"));
QObject::connect(form, SIGNAL(configApplied()), this, SLOT(refreshAll())); QObject::connect(form, SIGNAL(configApplied()), this, SLOT(refreshAll()));
form = new FormLighting(tabs); /*form = new FormLighting(tabs);
tabs->addTab(form, tr("Lighting")); tabs->addTab(form, tr("Lighting"));
QObject::connect(form, SIGNAL(configApplied()), this, SLOT(refreshAll())); QObject::connect(form, SIGNAL(configApplied()), this, SLOT(refreshAll()));*/
form = new FormRender(tabs); form = new FormRender(tabs);
tabs->addTab(form, tr("Render")); tabs->addTab(form, tr("Render"));

View file

@ -0,0 +1,32 @@
#include "widgetcurveeditor.h"
#include <QPainter>
WidgetCurveEditor::WidgetCurveEditor(QWidget *parent) : QWidget(parent)
{
_curve = curveCreate();
setMinimumSize(500, 500);
setMaximumSize(500, 500);
}
WidgetCurveEditor::~WidgetCurveEditor()
{
curveDelete(_curve);
}
void WidgetCurveEditor::paintEvent(QPaintEvent* event)
{
double position, value;
QPainter painter(this);
painter.fillRect(0, 0, 500, 500, QColor(255, 255, 255));
painter.setPen(QColor(255, 0, 0));
for (int x = 0; x < 500; x++)
{
position = ((double)x) / 499.0;
value = curveGetValue(_curve, position);
painter.drawPoint(x, 499 - (int)(value * 499.0));
}
}

View file

@ -0,0 +1,22 @@
#ifndef _PAYSAGES_QT_WIDGETCURVEEDITOR_H_
#define _PAYSAGES_QT_WIDGETCURVEEDITOR_H_
#include <QWidget>
#include "../lib_paysages/curve.h"
class WidgetCurveEditor : public QWidget
{
Q_OBJECT
public:
WidgetCurveEditor(QWidget* parent);
~WidgetCurveEditor();
protected:
void paintEvent(QPaintEvent* event);
private:
Curve* _curve;
};
#endif

View file

@ -4,40 +4,34 @@
<context> <context>
<name>BaseForm</name> <name>BaseForm</name>
<message> <message>
<location filename="../gui_qt/baseform.cpp" line="33"/>
<source>Layers : </source> <source>Layers : </source>
<translation>Niveaux :</translation> <translation type="obsolete">Niveaux :</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/baseform.cpp" line="39"/>
<source>Add layer</source> <source>Add layer</source>
<translation>Ajouter un niveau</translation> <translation type="obsolete">Ajouter un niveau</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/baseform.cpp" line="43"/>
<source>Delete layer</source> <source>Delete layer</source>
<translation>Supprimer un niveau</translation> <translation type="obsolete">Supprimer un niveau</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/baseform.cpp" line="73"/>
<source>Apply</source> <source>Apply</source>
<translation>Appliquer</translation> <translation type="obsolete">Appliquer</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/baseform.cpp" line="76"/>
<source>Revert</source> <source>Revert</source>
<translation>Annuler les modifications</translation> <translation type="obsolete">Annuler les modifications</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/baseform.cpp" line="239"/>
<source>Layer %1</source> <source>Layer %1</source>
<translation>Niveau %1</translation> <translation type="obsolete">Niveau %1</translation>
</message> </message>
</context> </context>
<context> <context>
<name>DialogColorGradation</name> <name>DialogColorGradation</name>
<message> <message>
<location filename="../gui_qt/dialogcolorgradation.cpp" line="129"/> <location filename="../gui_qt/dialogcolorgradation.cpp" line="34"/>
<source>Paysages 3D - Color gradation editor</source> <source>Paysages 3D - Color gradation editor</source>
<translation>Paysages 3D - Editeur de gradients de couleur</translation> <translation>Paysages 3D - Editeur de gradients de couleur</translation>
</message> </message>
@ -245,34 +239,28 @@
<context> <context>
<name>FormColorGradation</name> <name>FormColorGradation</name>
<message> <message>
<location filename="../gui_qt/dialogcolorgradation.cpp" line="51"/>
<source>Preview</source> <source>Preview</source>
<translation>Aperçu</translation> <translation type="obsolete">Aperçu</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/dialogcolorgradation.cpp" line="53"/>
<source>Position</source> <source>Position</source>
<translation>Position</translation> <translation type="obsolete">Position</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/dialogcolorgradation.cpp" line="54"/>
<source>Color</source> <source>Color</source>
<translation>Couleur</translation> <translation type="obsolete">Couleur</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/dialogcolorgradation.cpp" line="56"/>
<source>Validate</source> <source>Validate</source>
<translation>Valider</translation> <translation type="obsolete">Valider</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/dialogcolorgradation.cpp" line="59"/>
<source>Revert</source> <source>Revert</source>
<translation>Recommencer</translation> <translation type="obsolete">Recommencer</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/dialogcolorgradation.cpp" line="62"/>
<source>Cancel</source> <source>Cancel</source>
<translation>Annuler</translation> <translation type="obsolete">Annuler</translation>
</message> </message>
</context> </context>
<context> <context>
@ -504,7 +492,7 @@
<context> <context>
<name>InputColorGradation</name> <name>InputColorGradation</name>
<message> <message>
<location filename="../gui_qt/inputcolorgradation.cpp" line="42"/> <location filename="../gui_qt/inputcolorgradation.cpp" line="41"/>
<source>Edit</source> <source>Edit</source>
<translation>Editer</translation> <translation>Editer</translation>
</message> </message>
@ -550,9 +538,8 @@
<translation>Nuages</translation> <translation>Nuages</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/mainwindow.cpp" line="98"/>
<source>Lighting</source> <source>Lighting</source>
<translation>Eclairage</translation> <translation type="obsolete">Eclairage</translation>
</message> </message>
<message> <message>
<location filename="../gui_qt/mainwindow.cpp" line="102"/> <location filename="../gui_qt/mainwindow.cpp" line="102"/>

View file

@ -78,7 +78,7 @@ void atmosphereValidateDefinition(AtmosphereDefinition* definition)
{ {
sky = skyCreateDefinition(); sky = skyCreateDefinition();
sceneryGetSky(&sky); sceneryGetSky(&sky);
definition->color = colorGradationGet(&sky.haze_color, sky.daytime); definition->color = colorGradationGet(sky.haze_color, sky.daytime);
skyDeleteDefinition(&sky); skyDeleteDefinition(&sky);
} }
} }

View file

@ -108,23 +108,23 @@ void autoGenRealisticLandscape(int seed)
/* Sky */ /* Sky */
sky = skyCreateDefinition(); sky = skyCreateDefinition();
colorGradationQuickAddRgba(&sky.sun_color, 0.3, 1.0, 0.91, 0.8, 1.0); colorGradationQuickAddRgb(sky.sun_color, 0.3, 1.0, 0.91, 0.8);
colorGradationQuickAddRgba(&sky.sun_color, 0.5, 1.0, 0.95, 0.9, 1.0); colorGradationQuickAddRgb(sky.sun_color, 0.5, 1.0, 0.95, 0.9);
colorGradationQuickAddRgba(&sky.sun_color, 0.7, 1.0, 0.91, 0.8, 1.0); colorGradationQuickAddRgb(sky.sun_color, 0.7, 1.0, 0.91, 0.8);
colorGradationQuickAddRgba(&sky.zenith_color, 0.2, 0.03, 0.03, 0.05, 1.0); colorGradationQuickAddRgb(sky.zenith_color, 0.2, 0.03, 0.03, 0.05);
colorGradationQuickAddRgba(&sky.zenith_color, 0.25, 0.25, 0.33, 0.37, 1.0); colorGradationQuickAddRgb(sky.zenith_color, 0.25, 0.25, 0.33, 0.37);
colorGradationQuickAddRgba(&sky.zenith_color, 0.35, 0.52, 0.63, 0.8, 1.0); colorGradationQuickAddRgb(sky.zenith_color, 0.35, 0.52, 0.63, 0.8);
colorGradationQuickAddRgba(&sky.zenith_color, 0.65, 0.52, 0.63, 0.8, 1.0); colorGradationQuickAddRgb(sky.zenith_color, 0.65, 0.52, 0.63, 0.8);
colorGradationQuickAddRgba(&sky.zenith_color, 0.75, 0.25, 0.33, 0.37, 1.0); colorGradationQuickAddRgb(sky.zenith_color, 0.75, 0.25, 0.33, 0.37);
colorGradationQuickAddRgba(&sky.zenith_color, 0.8, 0.03, 0.03, 0.05, 1.0); colorGradationQuickAddRgb(sky.zenith_color, 0.8, 0.03, 0.03, 0.05);
colorGradationQuickAddRgba(&sky.haze_color, 0.2, 0.05, 0.05, 0.08, 1.0); colorGradationQuickAddRgb(sky.haze_color, 0.2, 0.05, 0.05, 0.08);
colorGradationQuickAddRgba(&sky.haze_color, 0.25, 0.55, 0.42, 0.42, 1.0); colorGradationQuickAddRgb(sky.haze_color, 0.25, 0.55, 0.42, 0.42);
colorGradationQuickAddRgba(&sky.haze_color, 0.3, 0.6, 0.6, 0.6, 1.0); colorGradationQuickAddRgb(sky.haze_color, 0.3, 0.6, 0.6, 0.6);
colorGradationQuickAddRgba(&sky.haze_color, 0.4, 0.92, 0.93, 1.0, 1.0); colorGradationQuickAddRgb(sky.haze_color, 0.4, 0.92, 0.93, 1.0);
colorGradationQuickAddRgba(&sky.haze_color, 0.6, 0.92, 0.93, 1.0, 1.0); colorGradationQuickAddRgb(sky.haze_color, 0.6, 0.92, 0.93, 1.0);
colorGradationQuickAddRgba(&sky.haze_color, 0.7, 0.6, 0.6, 0.8, 1.0); colorGradationQuickAddRgb(sky.haze_color, 0.7, 0.6, 0.6, 0.8);
colorGradationQuickAddRgba(&sky.haze_color, 0.75, 0.62, 0.50, 0.42, 1.0); colorGradationQuickAddRgb(sky.haze_color, 0.75, 0.62, 0.50, 0.42);
colorGradationQuickAddRgba(&sky.haze_color, 0.8, 0.05, 0.05, 0.08, 1.0); colorGradationQuickAddRgb(sky.haze_color, 0.8, 0.05, 0.05, 0.08);
sky.daytime = 0.0; sky.daytime = 0.0;
sky.haze_height = 0.75; sky.haze_height = 0.75;
sky.haze_smoothing = 0.3; sky.haze_smoothing = 0.3;

View file

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include "shared/constants.h" #include "shared/constants.h"
#include "tools.h" #include "tools.h"
#include "curve.h"
Color COLOR_TRANSPARENT = {0.0, 0.0, 0.0, 0.0}; Color COLOR_TRANSPARENT = {0.0, 0.0, 0.0, 0.0};
Color COLOR_BLACK = {0.0, 0.0, 0.0, 1.0}; Color COLOR_BLACK = {0.0, 0.0, 0.0, 1.0};
@ -14,13 +15,12 @@ Color COLOR_BLUE = {0.0, 0.0, 1.0, 1.0};
Color COLOR_WHITE = {1.0, 1.0, 1.0, 1.0}; Color COLOR_WHITE = {1.0, 1.0, 1.0, 1.0};
Color COLOR_GREY = {0.5, 0.5, 0.5, 1.0}; Color COLOR_GREY = {0.5, 0.5, 0.5, 1.0};
ColorGradationPart COLORGRADATIONPART_NULL; struct ColorGradation
void colorInit()
{ {
COLORGRADATIONPART_NULL.col = COLOR_TRANSPARENT; Curve* red;
COLORGRADATIONPART_NULL.start = 0.0; Curve* green;
} Curve* blue;
};
void colorSave(FILE* f, Color* col) void colorSave(FILE* f, Color* col)
{ {
@ -110,158 +110,106 @@ double colorGetValue(Color* col)
return max; return max;
} }
ColorGradation colorGradationCreate() ColorGradation* colorGradationCreate()
{ {
ColorGradation result; ColorGradation* result;
result.nbparts = 0; result = malloc(sizeof(ColorGradation));
result->red = curveCreate();
result->green = curveCreate();
result->blue = curveCreate();
return result; return result;
} }
int _part_compare(const void* part1, const void* part2) void colorGradationDelete(ColorGradation* gradation)
{ {
if (((ColorGradationPart*)part1)->start > ((ColorGradationPart*)part2)->start) curveDelete(gradation->red);
{ curveDelete(gradation->green);
return 1; curveDelete(gradation->blue);
} free(gradation);
else }
{
return -1; void colorGradationCopy(ColorGradation* source, ColorGradation* destination)
} {
curveCopy(source->red, destination->red);
curveCopy(source->green, destination->green);
curveCopy(source->blue, destination->blue);
} }
void colorGradationSave(FILE* f, ColorGradation* gradation) void colorGradationSave(FILE* f, ColorGradation* gradation)
{ {
int i; curveSave(f, gradation->red);
curveSave(f, gradation->green);
toolsSaveInt(f, &gradation->nbparts); curveSave(f, gradation->blue);
for (i = 0; i < gradation->nbparts; i++)
{
toolsSaveDouble(f, &gradation->parts[i].start);
colorSave(f, &gradation->parts[i].col);
}
} }
void colorGradationLoad(FILE* f, ColorGradation* gradation) void colorGradationLoad(FILE* f, ColorGradation* gradation)
{ {
int i; curveLoad(f, gradation->red);
curveLoad(f, gradation->green);
toolsLoadInt(f, &gradation->nbparts); curveLoad(f, gradation->blue);
for (i = 0; i < gradation->nbparts; i++)
{
toolsLoadDouble(f, &gradation->parts[i].start);
colorLoad(f, &gradation->parts[i].col);
}
} }
int colorGradationGetPartCount(ColorGradation* gradation) void colorGradationGetRedCurve(ColorGradation* gradation, Curve* curve)
{ {
return gradation->nbparts; curveCopy(gradation->red, curve);
} }
int colorGradationAddPart(ColorGradation* gradation) void colorGradationGetGreenCurve(ColorGradation* gradation, Curve* curve)
{ {
if (gradation->nbparts == MAX_COLORGRADATION_PARTS) curveCopy(gradation->green, curve);
{
return -1;
}
else
{
return gradation->nbparts++;
}
} }
void colorGradationDelPart(ColorGradation* gradation, int part) void colorGradationGetBlueCurve(ColorGradation* gradation, Curve* curve)
{ {
if (part >= 0 && part < gradation->nbparts) curveCopy(gradation->blue, curve);
{
memmove(gradation->parts + part, gradation->parts + part + 1, sizeof(ColorGradationPart) * (gradation->nbparts - part - 1));
gradation->nbparts--;
}
} }
ColorGradationPart colorGradationGetPart(ColorGradation* gradation, int part) void colorGradationSetRedCurve(ColorGradation* gradation, Curve* curve)
{ {
if (part >= 0 && part < gradation->nbparts) curveCopy(curve, gradation->red);
{ curveValidate(gradation->red);
return gradation->parts[part];
}
else
{
return COLORGRADATIONPART_NULL;
}
} }
void colorGradationSetPart(ColorGradation* gradation, int part, ColorGradationPart value) void colorGradationSetGreenCurve(ColorGradation* gradation, Curve* curve)
{ {
if (part >= 0 && part < gradation->nbparts) curveCopy(curve, gradation->green);
{ curveValidate(gradation->green);
gradation->parts[part] = value; }
}
void colorGradationSetBlueCurve(ColorGradation* gradation, Curve* curve)
{
curveCopy(curve, gradation->blue);
curveValidate(gradation->blue);
} }
void colorGradationQuickAdd(ColorGradation* gradation, double value, Color* col) void colorGradationQuickAdd(ColorGradation* gradation, double value, Color* col)
{ {
if (gradation->nbparts == MAX_COLORGRADATION_PARTS) colorGradationQuickAddRgb(gradation, value, col->r, col->g, col->b);
{
return;
}
else
{
gradation->parts[gradation->nbparts].start = value;
gradation->parts[gradation->nbparts].col = *col;
if (gradation->nbparts++ > 1)
{
qsort(gradation->parts, gradation->nbparts, sizeof(ColorGradationPart), _part_compare);
}
}
} }
void colorGradationQuickAddRgba(ColorGradation* gradation, double value, double r, double g, double b, double a) void colorGradationQuickAddRgb(ColorGradation* gradation, double value, double r, double g, double b)
{ {
Color col; curveQuickAddPoint(gradation->red, value, r);
col.r = r; curveValidate(gradation->red);
col.g = g;
col.b = b; curveQuickAddPoint(gradation->green, value, g);
col.a = a; curveValidate(gradation->green);
colorGradationQuickAdd(gradation, value, &col);
curveQuickAddPoint(gradation->blue, value, b);
curveValidate(gradation->blue);
} }
Color colorGradationGet(ColorGradation* gradation, double value) Color colorGradationGet(ColorGradation* gradation, double value)
{ {
Color result; Color result;
int i;
double fact;
if (gradation->nbparts == 0) result.r = curveGetValue(gradation->red, value);
{ result.g = curveGetValue(gradation->green, value);
return COLOR_TRANSPARENT; result.b = curveGetValue(gradation->blue, value);
} result.a = 1.0;
else if (gradation->nbparts == 1 || value <= gradation->parts[0].start)
{
return gradation->parts[0].col;
}
else if (value >= gradation->parts[gradation->nbparts - 1].start)
{
return gradation->parts[gradation->nbparts - 1].col;
}
else
{
for (i = 1; i < gradation->nbparts; i++)
{
if (value < gradation->parts[i].start)
{
fact = (value - gradation->parts[i - 1].start) / (gradation->parts[i].start - gradation->parts[i - 1].start);
result.r = gradation->parts[i - 1].col.r + (gradation->parts[i].col.r - gradation->parts[i - 1].col.r) * fact;
result.g = gradation->parts[i - 1].col.g + (gradation->parts[i].col.g - gradation->parts[i - 1].col.g) * fact;
result.b = gradation->parts[i - 1].col.b + (gradation->parts[i].col.b - gradation->parts[i - 1].col.b) * fact;
result.a = gradation->parts[i - 1].col.a + (gradation->parts[i].col.a - gradation->parts[i - 1].col.a) * fact;
return result; return result;
}
}
return gradation->parts[gradation->nbparts - 1].col;
}
} }

View file

@ -3,33 +3,46 @@
#include "shared/types.h" #include "shared/types.h"
#include "shared/constants.h" #include "shared/constants.h"
#include "curve.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void colorInit(); /* Color */
void colorSave(FILE* f, Color* col); void colorSave(FILE* f, Color* col);
void colorLoad(FILE* f, Color* col); void colorLoad(FILE* f, Color* col);
unsigned int colorTo32BitRGBA(Color* col); unsigned int colorTo32BitRGBA(Color* col);
unsigned int colorTo32BitBGRA(Color* col); unsigned int colorTo32BitBGRA(Color* col);
unsigned int colorTo32BitARGB(Color* col); unsigned int colorTo32BitARGB(Color* col);
unsigned int colorTo32BitABGR(Color* col); unsigned int colorTo32BitABGR(Color* col);
void colorMask(Color* base, Color* mask); void colorMask(Color* base, Color* mask);
double colorNormalize(Color* col); double colorNormalize(Color* col);
double colorGetValue(Color* col); double colorGetValue(Color* col);
ColorGradation colorGradationCreate(); /* ColorGradation */
typedef struct ColorGradation ColorGradation;
ColorGradation* colorGradationCreate();
void colorGradationDelete(ColorGradation* gradation);
void colorGradationCopy(ColorGradation* source, ColorGradation* destination);
void colorGradationSave(FILE* f, ColorGradation* gradation); void colorGradationSave(FILE* f, ColorGradation* gradation);
void colorGradationLoad(FILE* f, ColorGradation* gradation); void colorGradationLoad(FILE* f, ColorGradation* gradation);
int colorGradationGetPartCount(ColorGradation* gradation);
int colorGradationAddPart(ColorGradation* gradation); void colorGradationGetRedCurve(ColorGradation* gradation, Curve* curve);
void colorGradationDelPart(ColorGradation* gradation, int part); void colorGradationGetGreenCurve(ColorGradation* gradation, Curve* curve);
ColorGradationPart colorGradationGetPart(ColorGradation* gradation, int part); void colorGradationGetBlueCurve(ColorGradation* gradation, Curve* curve);
void colorGradationSetPart(ColorGradation* gradation, int part, ColorGradationPart value);
void colorGradationSetRedCurve(ColorGradation* gradation, Curve* curve);
void colorGradationSetGreenCurve(ColorGradation* gradation, Curve* curve);
void colorGradationSetBlueCurve(ColorGradation* gradation, Curve* curve);
void colorGradationQuickAdd(ColorGradation* gradation, double value, Color* col); void colorGradationQuickAdd(ColorGradation* gradation, double value, Color* col);
void colorGradationQuickAddRgba(ColorGradation* gradation, double value, double r, double g, double b, double a); void colorGradationQuickAddRgb(ColorGradation* gradation, double value, double r, double g, double b);
Color colorGradationGet(ColorGradation* gradation, double value); Color colorGradationGet(ColorGradation* gradation, double value);
#ifdef __cplusplus #ifdef __cplusplus

148
lib_paysages/curve.c Normal file
View file

@ -0,0 +1,148 @@
#include "curve.h"
#include <stdlib.h>
#include "tools.h"
#define MAX_NB_POINTS 40
struct Curve
{
int nbpoints;
CurvePoint points[MAX_NB_POINTS];
};
Curve* curveCreate()
{
Curve* result;
result = malloc(sizeof(Curve));
result->nbpoints = 0;
return result;
}
void curveDelete(Curve* curve)
{
free(curve);
}
void curveCopy(Curve* source, Curve* destination)
{
*destination = *source;
}
void curveSave(FILE* f, Curve* curve)
{
int i;
toolsSaveInt(f, &curve->nbpoints);
for (i = 0; i < curve->nbpoints; i++)
{
toolsSaveDouble(f, &curve->points[i].position);
toolsSaveDouble(f, &curve->points[i].value);
}
}
void curveLoad(FILE* f, Curve* curve)
{
int i;
toolsLoadInt(f, &curve->nbpoints);
for (i = 0; i < curve->nbpoints; i++)
{
toolsLoadDouble(f, &curve->points[i].position);
toolsLoadDouble(f, &curve->points[i].value);
}
}
void curveClear(Curve* curve)
{
curve->nbpoints = 0;
}
int curveAddPoint(Curve* curve, CurvePoint* point)
{
if (curve->nbpoints < MAX_NB_POINTS)
{
curve->points[curve->nbpoints] = *point;
return curve->nbpoints++;
}
else
{
return -1;
}
}
int curveQuickAddPoint(Curve* curve, double position, double value)
{
CurvePoint point;
point.position = position;
point.value = value;
return curveAddPoint(curve, &point);
}
int curveGetPointCount(Curve* curve)
{
return curve->nbpoints;
}
void curveGetPoint(Curve* curve, int number, CurvePoint* point)
{
if (number >= 0 && number < curve->nbpoints)
{
*point = curve->points[number];
}
}
int _point_compare(const void* part1, const void* part2)
{
if (((CurvePoint*)part1)->position > ((CurvePoint*)part2)->position)
{
return 1;
}
else
{
return -1;
}
}
void curveValidate(Curve* curve)
{
if (curve->nbpoints > 1)
{
qsort(curve->points, curve->nbpoints, sizeof(CurvePoint), _point_compare);
}
}
double curveGetValue(Curve* curve, double position)
{
int i;
double fact;
if (curve->nbpoints == 0)
{
return 0.0;
}
else if (curve->nbpoints == 1 || position <= curve->points[0].position)
{
return curve->points[0].value;
}
else if (position >= curve->points[curve->nbpoints - 1].position)
{
return curve->points[curve->nbpoints - 1].value;
}
else
{
for (i = 1; i < curve->nbpoints; i++)
{
if (position < curve->points[i].position)
{
fact = (position - curve->points[i - 1].position) / (curve->points[i].position - curve->points[i - 1].position);
return curve->points[i - 1].value + (curve->points[i].value - curve->points[i - 1].value) * fact;
}
}
return curve->points[curve->nbpoints - 1].value;
}
}

36
lib_paysages/curve.h Normal file
View file

@ -0,0 +1,36 @@
#ifndef _PAYSAGES_CURVE_H_
#define _PAYSAGES_CURVE_H_
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
double position;
double value;
} CurvePoint;
typedef struct Curve Curve;
Curve* curveCreate();
void curveDelete(Curve* curve);
void curveCopy(Curve* source, Curve* destination);
void curveSave(FILE* f, Curve* curve);
void curveLoad(FILE* f, Curve* curve);
void curveClear(Curve* curve);
int curveAddPoint(Curve* curve, CurvePoint* point);
int curveQuickAddPoint(Curve* curve, double position, double value);
int curveGetPointCount(Curve* curve);
void curveGetPoint(Curve* curve, int number, CurvePoint* point);
void curveValidate(Curve* curve);
double curveGetValue(Curve* curve, double position);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -22,7 +22,6 @@ void paysagesInit()
ilInit(); ilInit();
iluInit(); iluInit();
colorInit();
sceneryInit(); sceneryInit();
renderInit(); renderInit();

View file

@ -42,18 +42,6 @@ typedef struct
double a; double a;
} Color; } Color;
#define MAX_COLORGRADATION_PARTS 10
typedef struct
{
double start;
Color col;
} ColorGradationPart;
typedef struct
{
int nbparts;
ColorGradationPart parts[MAX_COLORGRADATION_PARTS];
} ColorGradation;
struct RenderFragment; struct RenderFragment;
struct Renderer; struct Renderer;

View file

@ -25,10 +25,10 @@ void skyQuit()
void skySave(FILE* f, SkyDefinition* definition) void skySave(FILE* f, SkyDefinition* definition)
{ {
toolsSaveDouble(f, &definition->daytime); toolsSaveDouble(f, &definition->daytime);
colorGradationSave(f, &definition->sun_color); colorGradationSave(f, definition->sun_color);
toolsSaveDouble(f, &definition->sun_radius); toolsSaveDouble(f, &definition->sun_radius);
colorGradationSave(f, &definition->zenith_color); colorGradationSave(f, definition->zenith_color);
colorGradationSave(f, &definition->haze_color); colorGradationSave(f, definition->haze_color);
toolsSaveDouble(f, &definition->haze_height); toolsSaveDouble(f, &definition->haze_height);
toolsSaveDouble(f, &definition->haze_smoothing); toolsSaveDouble(f, &definition->haze_smoothing);
} }
@ -36,10 +36,10 @@ void skySave(FILE* f, SkyDefinition* definition)
void skyLoad(FILE* f, SkyDefinition* definition) void skyLoad(FILE* f, SkyDefinition* definition)
{ {
toolsLoadDouble(f, &definition->daytime); toolsLoadDouble(f, &definition->daytime);
colorGradationLoad(f, &definition->sun_color); colorGradationLoad(f, definition->sun_color);
toolsLoadDouble(f, &definition->sun_radius); toolsLoadDouble(f, &definition->sun_radius);
colorGradationLoad(f, &definition->zenith_color); colorGradationLoad(f, definition->zenith_color);
colorGradationLoad(f, &definition->haze_color); colorGradationLoad(f, definition->haze_color);
toolsLoadDouble(f, &definition->haze_height); toolsLoadDouble(f, &definition->haze_height);
toolsLoadDouble(f, &definition->haze_smoothing); toolsLoadDouble(f, &definition->haze_smoothing);
@ -65,25 +65,36 @@ SkyDefinition skyCreateDefinition()
void skyDeleteDefinition(SkyDefinition* definition) void skyDeleteDefinition(SkyDefinition* definition)
{ {
colorGradationDelete(definition->sun_color);
colorGradationDelete(definition->zenith_color);
colorGradationDelete(definition->haze_color);
} }
void skyCopyDefinition(SkyDefinition* source, SkyDefinition* destination) void skyCopyDefinition(SkyDefinition* source, SkyDefinition* destination)
{ {
*destination = *source; destination->daytime = source->daytime;
destination->sun_radius = source->sun_radius;
destination->haze_height = source->haze_height;
destination->haze_smoothing = source->haze_smoothing;
colorGradationCopy(source->sun_color, destination->sun_color);
colorGradationCopy(source->zenith_color, destination->zenith_color);
colorGradationCopy(source->haze_color, destination->haze_color);
colorGradationCopy(source->_sky_gradation, destination->_sky_gradation);
} }
void skyValidateDefinition(SkyDefinition* definition) void skyValidateDefinition(SkyDefinition* definition)
{ {
Color zenith, haze; Color zenith, haze;
zenith = colorGradationGet(&definition->zenith_color, definition->daytime); zenith = colorGradationGet(definition->zenith_color, definition->daytime);
haze = colorGradationGet(&definition->haze_color, definition->daytime); haze = colorGradationGet(definition->haze_color, definition->daytime);
definition->_sky_gradation = colorGradationCreate(); definition->_sky_gradation = colorGradationCreate();
colorGradationQuickAdd(&definition->_sky_gradation, 0.0, &haze); colorGradationQuickAdd(definition->_sky_gradation, 0.0, &haze);
colorGradationQuickAdd(&definition->_sky_gradation, definition->haze_height - definition->haze_smoothing, &haze); colorGradationQuickAdd(definition->_sky_gradation, definition->haze_height - definition->haze_smoothing, &haze);
colorGradationQuickAdd(&definition->_sky_gradation, definition->haze_height, &zenith); colorGradationQuickAdd(definition->_sky_gradation, definition->haze_height, &zenith);
colorGradationQuickAdd(&definition->_sky_gradation, 1.0, &zenith); colorGradationQuickAdd(definition->_sky_gradation, 1.0, &zenith);
} }
int skyGetLights(SkyDefinition* sky, LightDefinition* lights, int max_lights) int skyGetLights(SkyDefinition* sky, LightDefinition* lights, int max_lights)
@ -103,7 +114,7 @@ int skyGetLights(SkyDefinition* sky, LightDefinition* lights, int max_lights)
{ {
/* Light from the sun */ /* Light from the sun */
lights[0].direction = v3Scale(sun_direction, -1.0); lights[0].direction = v3Scale(sun_direction, -1.0);
lights[0].color = colorGradationGet(&sky->sun_color, sky->daytime); lights[0].color = colorGradationGet(sky->sun_color, sky->daytime);
lights[0].reflection = 1.0; lights[0].reflection = 1.0;
lights[0].filtered = 1; lights[0].filtered = 1;
lights[0].masked = 1; lights[0].masked = 1;
@ -115,7 +126,7 @@ int skyGetLights(SkyDefinition* sky, LightDefinition* lights, int max_lights)
lights[1].direction.x = 0.0; lights[1].direction.x = 0.0;
lights[1].direction.y = -1.0; lights[1].direction.y = -1.0;
lights[1].direction.z = 0.0; lights[1].direction.z = 0.0;
lights[1].color = colorGradationGet(&sky->zenith_color, sky->daytime); lights[1].color = colorGradationGet(sky->zenith_color, sky->daytime);
lights[1].color.r *= 0.6; lights[1].color.r *= 0.6;
lights[1].color.g *= 0.6; lights[1].color.g *= 0.6;
lights[1].color.b *= 0.6; lights[1].color.b *= 0.6;
@ -144,11 +155,11 @@ Color skyGetColor(SkyDefinition* definition, Renderer* renderer, Vector3 eye, Ve
look = v3Normalize(look); look = v3Normalize(look);
dist = v3Norm(v3Sub(look, sun_position)); dist = v3Norm(v3Sub(look, sun_position));
sky_color = colorGradationGet(&definition->_sky_gradation, look.y * 0.5 + 0.5); sky_color = colorGradationGet(definition->_sky_gradation, look.y * 0.5 + 0.5);
if (dist < definition->sun_radius) if (dist < definition->sun_radius)
{ {
dist = dist / definition->sun_radius; dist = dist / definition->sun_radius;
sun_color = colorGradationGet(&definition->sun_color, definition->daytime); sun_color = colorGradationGet(definition->sun_color, definition->daytime);
if (dist < 0.9) if (dist < 0.9)
{ {
return sun_color; return sun_color;

View file

@ -2,6 +2,7 @@
#define _PAYSAGES_SKY_H_ #define _PAYSAGES_SKY_H_
#include "shared/types.h" #include "shared/types.h"
#include "color.h"
#include "lighting.h" #include "lighting.h"
#include <stdio.h> #include <stdio.h>
@ -12,13 +13,13 @@ extern "C" {
typedef struct typedef struct
{ {
double daytime; double daytime;
ColorGradation sun_color; ColorGradation* sun_color;
double sun_radius; double sun_radius;
ColorGradation zenith_color; ColorGradation* zenith_color;
ColorGradation haze_color; ColorGradation* haze_color;
double haze_height; double haze_height;
double haze_smoothing; double haze_smoothing;
ColorGradation _sky_gradation; ColorGradation* _sky_gradation;
} SkyDefinition; } SkyDefinition;
void skyInit(); void skyInit();