diff --git a/TODO b/TODO index 60b2312..1a0682c 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,5 @@ - 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. - Restore render progress. - Find a licence and apply it. -- Translate UI ? diff --git a/gui_qt/baseform.h b/gui_qt/baseform.h index 877e4f4..8a476a6 100644 --- a/gui_qt/baseform.h +++ b/gui_qt/baseform.h @@ -8,6 +8,7 @@ #include "baseinput.h" #include "../lib_paysages/shared/types.h" #include "../lib_paysages/noise.h" +#include "../lib_paysages/color.h" class BaseForm:public QWidget { diff --git a/gui_qt/dialogcolorgradation.cpp b/gui_qt/dialogcolorgradation.cpp index 624b5d0..8b2fd00 100644 --- a/gui_qt/dialogcolorgradation.cpp +++ b/gui_qt/dialogcolorgradation.cpp @@ -10,117 +10,20 @@ #include #include "baseform.h" #include "tools.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; -}; +#include "widgetcurveeditor.h" /**************** Dialog ****************/ DialogColorGradation::DialogColorGradation(QWidget *parent, ColorGradation* gradation): QDialog(parent) { - QWidget* preview; - _base = gradation; _current = colorGradationCreate(); - _current = *_base; + colorGradationCopy(_base, _current); setLayout(new QHBoxLayout()); - preview = new QWidget(this); - layout()->addWidget(preview); - - _form = new FormColorGradation(this, &_current); - layout()->addWidget(_form); + _curve_editor = new WidgetCurveEditor(this); + layout()->addWidget(_curve_editor); /*QObject::connect(button, SIGNAL(clicked()), this, SLOT(accept())); QObject::connect(button, SIGNAL(clicked()), this, SLOT(revert())); @@ -133,6 +36,7 @@ DialogColorGradation::DialogColorGradation(QWidget *parent, ColorGradation* grad DialogColorGradation::~DialogColorGradation() { + colorGradationDelete(_current); } bool DialogColorGradation::getGradation(QWidget* parent, ColorGradation* gradation) @@ -154,18 +58,17 @@ void DialogColorGradation::closeEvent(QCloseEvent* e) void DialogColorGradation::accept() { - *_base = _current; + colorGradationCopy(_current, _base); QDialog::accept(); } void DialogColorGradation::revert() { - _current = *_base; - + colorGradationCopy(_base, _current); revertToCurrent(); } void DialogColorGradation::revertToCurrent() { - _form->revertConfig(); + // TODO } diff --git a/gui_qt/dialogcolorgradation.h b/gui_qt/dialogcolorgradation.h index 6c908e7..0205120 100644 --- a/gui_qt/dialogcolorgradation.h +++ b/gui_qt/dialogcolorgradation.h @@ -3,6 +3,7 @@ #include #include "baseform.h" +#include "widgetcurveeditor.h" #include "../lib_paysages/color.h" @@ -27,8 +28,8 @@ private: private: ColorGradation* _base; - ColorGradation _current; - BaseForm* _form; + ColorGradation* _current; + WidgetCurveEditor* _curve_editor; }; #endif diff --git a/gui_qt/formsky.cpp b/gui_qt/formsky.cpp index 307ec86..c96facb 100644 --- a/gui_qt/formsky.cpp +++ b/gui_qt/formsky.cpp @@ -86,10 +86,10 @@ FormSky::FormSky(QWidget *parent): addPreview(previewEast, QString(tr("East preview"))); 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); - addInputColorGradation(tr("Zenith color"), &_definition.zenith_color); - addInputColorGradation(tr("Haze color"), &_definition.haze_color); + addInputColorGradation(tr("Zenith color"), _definition.zenith_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 smoothing"), &_definition.haze_smoothing, 0.0, 1.0, 0.01, 0.1); diff --git a/gui_qt/inputcolorgradation.cpp b/gui_qt/inputcolorgradation.cpp index 0741771..1a281b8 100644 --- a/gui_qt/inputcolorgradation.cpp +++ b/gui_qt/inputcolorgradation.cpp @@ -10,12 +10,11 @@ class ColorGradationPreview:public QWidget { public: - ColorGradationPreview(QWidget* parent, ColorGradation* gradation): - QWidget(parent), - gradation(gradation) + ColorGradationPreview(QWidget* parent, ColorGradation* gradation) : QWidget(parent) { + this->gradation = gradation; } - + void paintEvent(QPaintEvent* event) { QPainter painter(this); @@ -32,10 +31,10 @@ public: ColorGradation* gradation; }; -InputColorGradation::InputColorGradation(QWidget* form, QString label, ColorGradation* value): - BaseInput(form, label), - _value(value) +InputColorGradation::InputColorGradation(QWidget* form, QString label, ColorGradation* value) : BaseInput(form, label) { + _value = value; + _preview = new ColorGradationPreview(form, value); _preview->setMinimumSize(200, 20); @@ -63,12 +62,14 @@ void InputColorGradation::revert() void InputColorGradation::editGradation() { - ColorGradation gradation; + ColorGradation* gradation; - gradation = *_value; - if (DialogColorGradation::getGradation(_preview, &gradation)) + gradation = colorGradationCreate(); + colorGradationCopy(_value, gradation); + if (DialogColorGradation::getGradation(_preview, gradation)) { - *_value = gradation; + colorGradationCopy(gradation, _value); applyValue(); } + colorGradationDelete(gradation); } diff --git a/gui_qt/inputcolorgradation.h b/gui_qt/inputcolorgradation.h index fe6ff5b..42bdbd7 100644 --- a/gui_qt/inputcolorgradation.h +++ b/gui_qt/inputcolorgradation.h @@ -4,7 +4,7 @@ #include #include "baseinput.h" -#include "../lib_paysages/shared/types.h" +#include "../lib_paysages/color.h" class InputColorGradation:public BaseInput { diff --git a/gui_qt/mainwindow.cpp b/gui_qt/mainwindow.cpp index 8015379..df7a856 100644 --- a/gui_qt/mainwindow.cpp +++ b/gui_qt/mainwindow.cpp @@ -94,9 +94,9 @@ MainWindow::MainWindow(QWidget *parent) : tabs->addTab(form, tr("Clouds")); QObject::connect(form, SIGNAL(configApplied()), this, SLOT(refreshAll())); - form = new FormLighting(tabs); + /*form = new FormLighting(tabs); 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); tabs->addTab(form, tr("Render")); diff --git a/gui_qt/widgetcurveeditor.cpp b/gui_qt/widgetcurveeditor.cpp new file mode 100644 index 0000000..9132810 --- /dev/null +++ b/gui_qt/widgetcurveeditor.cpp @@ -0,0 +1,32 @@ +#include "widgetcurveeditor.h" + +#include + +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)); + } +} diff --git a/gui_qt/widgetcurveeditor.h b/gui_qt/widgetcurveeditor.h new file mode 100644 index 0000000..52139dd --- /dev/null +++ b/gui_qt/widgetcurveeditor.h @@ -0,0 +1,22 @@ +#ifndef _PAYSAGES_QT_WIDGETCURVEEDITOR_H_ +#define _PAYSAGES_QT_WIDGETCURVEEDITOR_H_ + +#include +#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 diff --git a/i18n/paysages_fr.ts b/i18n/paysages_fr.ts index 3ec95c3..38f4515 100644 --- a/i18n/paysages_fr.ts +++ b/i18n/paysages_fr.ts @@ -4,40 +4,34 @@ BaseForm - Layers : - Niveaux : + Niveaux : - Add layer - Ajouter un niveau + Ajouter un niveau - Delete layer - Supprimer un niveau + Supprimer un niveau - Apply - Appliquer + Appliquer - Revert - Annuler les modifications + Annuler les modifications - Layer %1 - Niveau %1 + Niveau %1 DialogColorGradation - + Paysages 3D - Color gradation editor Paysages 3D - Editeur de gradients de couleur @@ -245,34 +239,28 @@ FormColorGradation - Preview - Aperçu + Aperçu - Position - Position + Position - Color - Couleur + Couleur - Validate - Valider + Valider - Revert - Recommencer + Recommencer - Cancel - Annuler + Annuler @@ -504,7 +492,7 @@ InputColorGradation - + Edit Editer @@ -550,9 +538,8 @@ Nuages - Lighting - Eclairage + Eclairage diff --git a/lib_paysages/atmosphere.c b/lib_paysages/atmosphere.c index 6d9ee0b..7bd18d8 100644 --- a/lib_paysages/atmosphere.c +++ b/lib_paysages/atmosphere.c @@ -78,7 +78,7 @@ void atmosphereValidateDefinition(AtmosphereDefinition* definition) { sky = skyCreateDefinition(); sceneryGetSky(&sky); - definition->color = colorGradationGet(&sky.haze_color, sky.daytime); + definition->color = colorGradationGet(sky.haze_color, sky.daytime); skyDeleteDefinition(&sky); } } diff --git a/lib_paysages/auto.c b/lib_paysages/auto.c index 19450f9..8419906 100644 --- a/lib_paysages/auto.c +++ b/lib_paysages/auto.c @@ -108,23 +108,23 @@ void autoGenRealisticLandscape(int seed) /* Sky */ sky = skyCreateDefinition(); - colorGradationQuickAddRgba(&sky.sun_color, 0.3, 1.0, 0.91, 0.8, 1.0); - colorGradationQuickAddRgba(&sky.sun_color, 0.5, 1.0, 0.95, 0.9, 1.0); - colorGradationQuickAddRgba(&sky.sun_color, 0.7, 1.0, 0.91, 0.8, 1.0); - colorGradationQuickAddRgba(&sky.zenith_color, 0.2, 0.03, 0.03, 0.05, 1.0); - colorGradationQuickAddRgba(&sky.zenith_color, 0.25, 0.25, 0.33, 0.37, 1.0); - colorGradationQuickAddRgba(&sky.zenith_color, 0.35, 0.52, 0.63, 0.8, 1.0); - colorGradationQuickAddRgba(&sky.zenith_color, 0.65, 0.52, 0.63, 0.8, 1.0); - colorGradationQuickAddRgba(&sky.zenith_color, 0.75, 0.25, 0.33, 0.37, 1.0); - colorGradationQuickAddRgba(&sky.zenith_color, 0.8, 0.03, 0.03, 0.05, 1.0); - colorGradationQuickAddRgba(&sky.haze_color, 0.2, 0.05, 0.05, 0.08, 1.0); - colorGradationQuickAddRgba(&sky.haze_color, 0.25, 0.55, 0.42, 0.42, 1.0); - colorGradationQuickAddRgba(&sky.haze_color, 0.3, 0.6, 0.6, 0.6, 1.0); - colorGradationQuickAddRgba(&sky.haze_color, 0.4, 0.92, 0.93, 1.0, 1.0); - colorGradationQuickAddRgba(&sky.haze_color, 0.6, 0.92, 0.93, 1.0, 1.0); - colorGradationQuickAddRgba(&sky.haze_color, 0.7, 0.6, 0.6, 0.8, 1.0); - colorGradationQuickAddRgba(&sky.haze_color, 0.75, 0.62, 0.50, 0.42, 1.0); - colorGradationQuickAddRgba(&sky.haze_color, 0.8, 0.05, 0.05, 0.08, 1.0); + colorGradationQuickAddRgb(sky.sun_color, 0.3, 1.0, 0.91, 0.8); + colorGradationQuickAddRgb(sky.sun_color, 0.5, 1.0, 0.95, 0.9); + colorGradationQuickAddRgb(sky.sun_color, 0.7, 1.0, 0.91, 0.8); + colorGradationQuickAddRgb(sky.zenith_color, 0.2, 0.03, 0.03, 0.05); + colorGradationQuickAddRgb(sky.zenith_color, 0.25, 0.25, 0.33, 0.37); + colorGradationQuickAddRgb(sky.zenith_color, 0.35, 0.52, 0.63, 0.8); + colorGradationQuickAddRgb(sky.zenith_color, 0.65, 0.52, 0.63, 0.8); + colorGradationQuickAddRgb(sky.zenith_color, 0.75, 0.25, 0.33, 0.37); + colorGradationQuickAddRgb(sky.zenith_color, 0.8, 0.03, 0.03, 0.05); + colorGradationQuickAddRgb(sky.haze_color, 0.2, 0.05, 0.05, 0.08); + colorGradationQuickAddRgb(sky.haze_color, 0.25, 0.55, 0.42, 0.42); + colorGradationQuickAddRgb(sky.haze_color, 0.3, 0.6, 0.6, 0.6); + colorGradationQuickAddRgb(sky.haze_color, 0.4, 0.92, 0.93, 1.0); + colorGradationQuickAddRgb(sky.haze_color, 0.6, 0.92, 0.93, 1.0); + colorGradationQuickAddRgb(sky.haze_color, 0.7, 0.6, 0.6, 0.8); + colorGradationQuickAddRgb(sky.haze_color, 0.75, 0.62, 0.50, 0.42); + colorGradationQuickAddRgb(sky.haze_color, 0.8, 0.05, 0.05, 0.08); sky.daytime = 0.0; sky.haze_height = 0.75; sky.haze_smoothing = 0.3; diff --git a/lib_paysages/color.c b/lib_paysages/color.c index f8a7470..af3740a 100644 --- a/lib_paysages/color.c +++ b/lib_paysages/color.c @@ -5,6 +5,7 @@ #include #include "shared/constants.h" #include "tools.h" +#include "curve.h" Color COLOR_TRANSPARENT = {0.0, 0.0, 0.0, 0.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_GREY = {0.5, 0.5, 0.5, 1.0}; -ColorGradationPart COLORGRADATIONPART_NULL; - -void colorInit() +struct ColorGradation { - COLORGRADATIONPART_NULL.col = COLOR_TRANSPARENT; - COLORGRADATIONPART_NULL.start = 0.0; -} + Curve* red; + Curve* green; + Curve* blue; +}; void colorSave(FILE* f, Color* col) { @@ -110,158 +110,106 @@ double colorGetValue(Color* col) return max; } -ColorGradation colorGradationCreate() +ColorGradation* colorGradationCreate() { - ColorGradation result; - - result.nbparts = 0; + ColorGradation* result; + result = malloc(sizeof(ColorGradation)); + result->red = curveCreate(); + result->green = curveCreate(); + result->blue = curveCreate(); + return result; } -int _part_compare(const void* part1, const void* part2) +void colorGradationDelete(ColorGradation* gradation) { - if (((ColorGradationPart*)part1)->start > ((ColorGradationPart*)part2)->start) - { - return 1; - } - else - { - return -1; - } + curveDelete(gradation->red); + curveDelete(gradation->green); + curveDelete(gradation->blue); + free(gradation); +} + +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) { - int i; - - toolsSaveInt(f, &gradation->nbparts); - for (i = 0; i < gradation->nbparts; i++) - { - toolsSaveDouble(f, &gradation->parts[i].start); - colorSave(f, &gradation->parts[i].col); - } + curveSave(f, gradation->red); + curveSave(f, gradation->green); + curveSave(f, gradation->blue); } void colorGradationLoad(FILE* f, ColorGradation* gradation) { - int i; - - toolsLoadInt(f, &gradation->nbparts); - for (i = 0; i < gradation->nbparts; i++) - { - toolsLoadDouble(f, &gradation->parts[i].start); - colorLoad(f, &gradation->parts[i].col); - } + curveLoad(f, gradation->red); + curveLoad(f, gradation->green); + curveLoad(f, gradation->blue); } -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) - { - return -1; - } - else - { - return gradation->nbparts++; - } + curveCopy(gradation->green, curve); } -void colorGradationDelPart(ColorGradation* gradation, int part) +void colorGradationGetBlueCurve(ColorGradation* gradation, Curve* curve) { - if (part >= 0 && part < gradation->nbparts) - { - memmove(gradation->parts + part, gradation->parts + part + 1, sizeof(ColorGradationPart) * (gradation->nbparts - part - 1)); - gradation->nbparts--; - } + curveCopy(gradation->blue, curve); } -ColorGradationPart colorGradationGetPart(ColorGradation* gradation, int part) +void colorGradationSetRedCurve(ColorGradation* gradation, Curve* curve) { - if (part >= 0 && part < gradation->nbparts) - { - return gradation->parts[part]; - } - else - { - return COLORGRADATIONPART_NULL; - } + curveCopy(curve, gradation->red); + curveValidate(gradation->red); } -void colorGradationSetPart(ColorGradation* gradation, int part, ColorGradationPart value) +void colorGradationSetGreenCurve(ColorGradation* gradation, Curve* curve) { - if (part >= 0 && part < gradation->nbparts) - { - gradation->parts[part] = value; - } + curveCopy(curve, gradation->green); + curveValidate(gradation->green); +} + +void colorGradationSetBlueCurve(ColorGradation* gradation, Curve* curve) +{ + curveCopy(curve, gradation->blue); + curveValidate(gradation->blue); } void colorGradationQuickAdd(ColorGradation* gradation, double value, Color* col) { - if (gradation->nbparts == MAX_COLORGRADATION_PARTS) - { - 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); - } - } + colorGradationQuickAddRgb(gradation, value, col->r, col->g, col->b); } -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; - col.r = r; - col.g = g; - col.b = b; - col.a = a; - colorGradationQuickAdd(gradation, value, &col); + curveQuickAddPoint(gradation->red, value, r); + curveValidate(gradation->red); + + curveQuickAddPoint(gradation->green, value, g); + curveValidate(gradation->green); + + curveQuickAddPoint(gradation->blue, value, b); + curveValidate(gradation->blue); } Color colorGradationGet(ColorGradation* gradation, double value) { Color result; - int i; - double fact; - if (gradation->nbparts == 0) - { - return COLOR_TRANSPARENT; - } - 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 gradation->parts[gradation->nbparts - 1].col; - } + result.r = curveGetValue(gradation->red, value); + result.g = curveGetValue(gradation->green, value); + result.b = curveGetValue(gradation->blue, value); + result.a = 1.0; + + return result; } diff --git a/lib_paysages/color.h b/lib_paysages/color.h index b9a8527..416467a 100644 --- a/lib_paysages/color.h +++ b/lib_paysages/color.h @@ -3,33 +3,46 @@ #include "shared/types.h" #include "shared/constants.h" +#include "curve.h" #ifdef __cplusplus extern "C" { #endif -void colorInit(); - +/* Color */ void colorSave(FILE* f, Color* col); void colorLoad(FILE* f, Color* col); + unsigned int colorTo32BitRGBA(Color* col); unsigned int colorTo32BitBGRA(Color* col); unsigned int colorTo32BitARGB(Color* col); unsigned int colorTo32BitABGR(Color* col); + void colorMask(Color* base, Color* mask); double colorNormalize(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 colorGradationLoad(FILE* f, ColorGradation* gradation); -int colorGradationGetPartCount(ColorGradation* gradation); -int colorGradationAddPart(ColorGradation* gradation); -void colorGradationDelPart(ColorGradation* gradation, int part); -ColorGradationPart colorGradationGetPart(ColorGradation* gradation, int part); -void colorGradationSetPart(ColorGradation* gradation, int part, ColorGradationPart value); + +void colorGradationGetRedCurve(ColorGradation* gradation, Curve* curve); +void colorGradationGetGreenCurve(ColorGradation* gradation, Curve* curve); +void colorGradationGetBlueCurve(ColorGradation* gradation, Curve* curve); + +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 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); #ifdef __cplusplus diff --git a/lib_paysages/curve.c b/lib_paysages/curve.c new file mode 100644 index 0000000..e03f879 --- /dev/null +++ b/lib_paysages/curve.c @@ -0,0 +1,148 @@ +#include "curve.h" + +#include +#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; + } +} diff --git a/lib_paysages/curve.h b/lib_paysages/curve.h new file mode 100644 index 0000000..1f1642c --- /dev/null +++ b/lib_paysages/curve.h @@ -0,0 +1,36 @@ +#ifndef _PAYSAGES_CURVE_H_ +#define _PAYSAGES_CURVE_H_ + +#include + +#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 diff --git a/lib_paysages/main.c b/lib_paysages/main.c index a38bee4..b1b3cb2 100644 --- a/lib_paysages/main.c +++ b/lib_paysages/main.c @@ -22,7 +22,6 @@ void paysagesInit() ilInit(); iluInit(); - colorInit(); sceneryInit(); renderInit(); diff --git a/lib_paysages/shared/types.h b/lib_paysages/shared/types.h index 6b9aafb..0905215 100644 --- a/lib_paysages/shared/types.h +++ b/lib_paysages/shared/types.h @@ -42,18 +42,6 @@ typedef struct double a; } 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 Renderer; diff --git a/lib_paysages/sky.c b/lib_paysages/sky.c index cd9876e..4f8a17a 100644 --- a/lib_paysages/sky.c +++ b/lib_paysages/sky.c @@ -25,10 +25,10 @@ void skyQuit() void skySave(FILE* f, SkyDefinition* definition) { toolsSaveDouble(f, &definition->daytime); - colorGradationSave(f, &definition->sun_color); + colorGradationSave(f, definition->sun_color); toolsSaveDouble(f, &definition->sun_radius); - colorGradationSave(f, &definition->zenith_color); - colorGradationSave(f, &definition->haze_color); + colorGradationSave(f, definition->zenith_color); + colorGradationSave(f, definition->haze_color); toolsSaveDouble(f, &definition->haze_height); toolsSaveDouble(f, &definition->haze_smoothing); } @@ -36,10 +36,10 @@ void skySave(FILE* f, SkyDefinition* definition) void skyLoad(FILE* f, SkyDefinition* definition) { toolsLoadDouble(f, &definition->daytime); - colorGradationLoad(f, &definition->sun_color); + colorGradationLoad(f, definition->sun_color); toolsLoadDouble(f, &definition->sun_radius); - colorGradationLoad(f, &definition->zenith_color); - colorGradationLoad(f, &definition->haze_color); + colorGradationLoad(f, definition->zenith_color); + colorGradationLoad(f, definition->haze_color); toolsLoadDouble(f, &definition->haze_height); toolsLoadDouble(f, &definition->haze_smoothing); @@ -65,25 +65,36 @@ SkyDefinition skyCreateDefinition() void skyDeleteDefinition(SkyDefinition* definition) { + colorGradationDelete(definition->sun_color); + colorGradationDelete(definition->zenith_color); + colorGradationDelete(definition->haze_color); } 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) { Color zenith, haze; - zenith = colorGradationGet(&definition->zenith_color, definition->daytime); - haze = colorGradationGet(&definition->haze_color, definition->daytime); + zenith = colorGradationGet(definition->zenith_color, definition->daytime); + haze = colorGradationGet(definition->haze_color, definition->daytime); definition->_sky_gradation = colorGradationCreate(); - 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, &zenith); - colorGradationQuickAdd(&definition->_sky_gradation, 1.0, &zenith); + 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, &zenith); + colorGradationQuickAdd(definition->_sky_gradation, 1.0, &zenith); } 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 */ 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].filtered = 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.y = -1.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.g *= 0.6; lights[1].color.b *= 0.6; @@ -144,11 +155,11 @@ Color skyGetColor(SkyDefinition* definition, Renderer* renderer, Vector3 eye, Ve look = v3Normalize(look); 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) { 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) { return sun_color; diff --git a/lib_paysages/sky.h b/lib_paysages/sky.h index d88a2d5..86079f6 100644 --- a/lib_paysages/sky.h +++ b/lib_paysages/sky.h @@ -2,6 +2,7 @@ #define _PAYSAGES_SKY_H_ #include "shared/types.h" +#include "color.h" #include "lighting.h" #include @@ -12,13 +13,13 @@ extern "C" { typedef struct { double daytime; - ColorGradation sun_color; + ColorGradation* sun_color; double sun_radius; - ColorGradation zenith_color; - ColorGradation haze_color; + ColorGradation* zenith_color; + ColorGradation* haze_color; double haze_height; double haze_smoothing; - ColorGradation _sky_gradation; + ColorGradation* _sky_gradation; } SkyDefinition; void skyInit();