paysages: Qt GUI (WIP)

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@205 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2011-12-27 19:03:46 +00:00 committed by ThunderK
parent 199f306226
commit 73f15a44f7
6 changed files with 172 additions and 24 deletions

View file

@ -2,11 +2,16 @@
#include "formwater.h"
#include "ui_formwater.h"
#include <QColor>
#include <math.h>
#include "../lib_paysages/water.h"
#include "../lib_paysages/shared/functions.h"
#include "../lib_paysages/shared/constants.h"
class PreviewCoverage:private Preview
static WaterDefinition _definition;
/**************** Previews ****************/
class PreviewCoverage:public Preview
{
public:
PreviewCoverage(QWidget* parent):
@ -19,32 +24,159 @@ protected:
double height;
height = terrainGetHeight(x, y);
if (height > 0.0)
if (height > _definition.height)
{
height = terrainGetHeightNormalized(x, y);
return QColor((int)(255.0 * height), (int)(255.0 * height), (int)(255.0 * height));
}
else
{
return QColor(0, 0, 255);
return QColor(_definition.main_color.r * 255.0, _definition.main_color.g * 255.0, _definition.main_color.b * 255.0);
}
}
};
class PreviewColor:public Preview
{
public:
PreviewColor(QWidget* parent):
Preview(parent)
{
}
protected:
QColor getColor(double x, double y)
{
Vector3 eye, look, location;
WaterDefinition definition;
WaterEnvironment environment;
WaterQuality quality;
Color result;
eye.x = 0.0;
eye.y = scaling;
eye.z = -10.0 * scaling;
look.x = x * 0.01 / scaling;
look.y = -y * 0.01 / scaling - 0.3;
look.z = 1.0;
look = v3Normalize(look);
if (look.y > -0.0001)
{
result = this->rayCastFromWater(eye, look).hit_color;
return QColor(result.r * 255.0, result.g * 255.0, result.b * 255.0);
}
location.x = eye.x - look.x * eye.y / look.y;
location.y = 0.0;
location.z = eye.z - look.z * eye.y / look.y;
if (location.z > 0.0)
{
result = this->rayCastFromWater(eye, look).hit_color;
return QColor(result.r * 255.0, result.g * 255.0, result.b * 255.0);
}
definition = _definition;
definition.height = 0.0;
environment.reflection_function = (RayCastingFunction)(&this->rayCastFromWater);
environment.refraction_function = (RayCastingFunction)(&this->rayCastFromWater);
environment.toggle_fog = 0;
environment.toggle_shadows = 0;
quality.force_detail = 0.0001;
result = waterGetColorCustom(location, look, &definition, &quality, &environment).final;
return QColor(result.r * 255.0, result.g * 255.0, result.b * 255.0);
}
private:
static RayCastingResult rayCastFromWater(Vector3 start, Vector3 direction)
{
RayCastingResult result;
double x, y;
result.hit = 1;
if (direction.z < 0.0001)
{
result.hit_color = COLOR_WHITE;
}
else
{
x = start.x + direction.x * (0.0 - start.z) / direction.z;
y = start.y + direction.y * (0.0 - start.z) / direction.z;
if (((int)ceil(x * 0.2) % 2 == 0) ^ ((int)ceil(y * 0.2 - 0.5) % 2 == 0))
{
result.hit_color = COLOR_WHITE;
}
else
{
result.hit_color = COLOR_GREY;
}
}
/* TODO hit_location */
return result;
}
};
/**************** Form ****************/
FormWater::FormWater(QWidget *parent) :
QWidget(parent),
ui(new Ui::FormWater)
{
PreviewCoverage* previewCoverage;
ui->setupUi(this);
this->findChild<QToolBox*>("water_configs")->setCurrentIndex(0);
findChild<QToolBox*>("water_configs")->setCurrentIndex(0);
previewCoverage = new PreviewCoverage(this->findChild<QWidget*>("water_preview_coverage"));
_definition = waterCreateDefinition();
previewCoverage = new PreviewCoverage(findChild<QWidget*>("water_preview_coverage"));
previewColor = new PreviewColor(findChild<QWidget*>("water_preview_color"));
QObject::connect(findChild<QSlider*>("water_height"), SIGNAL(valueChanged(int)), this, SLOT(configChange()));
QObject::connect(findChild<QSlider*>("water_transparency"), SIGNAL(valueChanged(int)), this, SLOT(configChange()));
QObject::connect(findChild<QSlider*>("water_reflection"), SIGNAL(valueChanged(int)), this, SLOT(configChange()));
QObject::connect(findChild<QSlider*>("water_depth_limit"), SIGNAL(valueChanged(int)), this, SLOT(configChange()));
revertConfig();
}
FormWater::~FormWater()
{
delete ui;
}
void FormWater::dataUpdated()
{
revertConfig();
}
void FormWater::configChange()
{
_definition.height = (double)findChild<QSlider*>("water_height")->value() / 10.0;
_definition.transparency = (double)findChild<QSlider*>("water_transparency")->value() / 1000.0;
_definition.reflection = (double)findChild<QSlider*>("water_reflection")->value() / 1000.0;
_definition.transparency_depth = (double)findChild<QSlider*>("water_depth_limit")->value() / 10.0;
previewCoverage->redraw();
previewColor->redraw();
}
void FormWater::applyConfig()
{
waterSetDefinition(_definition);
//guiUpdate();
}
void FormWater::revertConfig()
{
waterCopyDefinition(waterGetDefinition(), &_definition);
findChild<QSlider*>("water_height")->setValue(_definition.height * 10.0);
findChild<QSlider*>("water_transparency")->setValue(_definition.transparency * 1000.0);
findChild<QSlider*>("water_reflection")->setValue(_definition.reflection * 1000.0);
findChild<QSlider*>("water_depth_limit")->setValue(_definition.transparency_depth * 10.0);
previewCoverage->redraw();
previewColor->redraw();
}

View file

@ -2,6 +2,7 @@
#define FORMWATER_H
#include <QWidget>
#include "preview.h"
namespace Ui {
class FormWater;
@ -15,8 +16,17 @@ public:
explicit FormWater(QWidget *parent = 0);
~FormWater();
void dataUpdated();
public slots:
void configChange();
void applyConfig();
void revertConfig();
private:
Ui::FormWater *ui;
Preview* previewCoverage;
Preview* previewColor;
};
#endif // FORMWATER_H

View file

@ -156,8 +156,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>98</width>
<height>28</height>
<width>451</width>
<height>438</height>
</rect>
</property>
<attribute name="label">
@ -185,6 +185,12 @@
</item>
<item row="0" column="1">
<widget class="QSlider" name="water_height">
<property name="minimum">
<number>-300</number>
</property>
<property name="maximum">
<number>300</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
@ -198,8 +204,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>98</width>
<height>28</height>
<width>451</width>
<height>438</height>
</rect>
</property>
<attribute name="label">
@ -315,7 +321,7 @@
<item row="4" column="1">
<widget class="QSlider" name="water_depth_limit">
<property name="maximum">
<number>50</number>
<number>500</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -324,7 +330,7 @@
<enum>QSlider::TicksBelow</enum>
</property>
<property name="tickInterval">
<number>1</number>
<number>10</number>
</property>
</widget>
</item>

View file

@ -6,11 +6,11 @@
int main(int argc, char** argv)
{
paysagesInit();
QApplication app(argc, argv);
MainWindow window;
paysagesInit();
window.show();
Preview::startUpdater();

View file

@ -15,9 +15,9 @@ public:
protected:
void run()
{
QVectorIterator<Preview*> iter(_previews);
while (true)
{
QVectorIterator<Preview*> iter(_previews);
while (iter.hasNext())
{
iter.next()->doRender();
@ -68,6 +68,13 @@ void Preview::doRender()
}
}
void Preview::redraw()
{
lock->lock();
need_rerender = 1;
lock->unlock();
}
void Preview::resizeEvent(QResizeEvent* event)
{
QImage* image;
@ -307,13 +314,6 @@ void Preview::renderPixbuf()
// return 1;
//}
//void guiPreviewRedraw(SmallPreview* preview)
//{
// mutexAcquire(preview->lock);
// preview->need_rerender = 1;
// mutexRelease(preview->lock);
//}
//void guiPreviewRedrawAll()
//{
// int i;

View file

@ -11,13 +11,13 @@ public:
Preview(QWidget* parent);
static void startUpdater();
void doRender();
void redraw();
protected:
void resizeEvent(QResizeEvent* event);
void paintEvent(QPaintEvent* event);
virtual QColor getColor(double x, double y) = 0;
private:
void renderPixbuf();
void forceRender();