2012-01-06 16:09:03 +00:00
|
|
|
#include "formrender.h"
|
|
|
|
|
2012-01-18 16:20:14 +00:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2012-01-06 16:09:03 +00:00
|
|
|
#include "dialogrender.h"
|
2012-01-27 23:01:21 +00:00
|
|
|
#include "inputcamera.h"
|
2012-04-12 20:02:31 +00:00
|
|
|
#include "tools.h"
|
2012-01-24 13:16:20 +00:00
|
|
|
#include "../lib_paysages/render.h"
|
2012-01-27 23:01:21 +00:00
|
|
|
#include "../lib_paysages/scenery.h"
|
2012-01-06 16:09:03 +00:00
|
|
|
|
2012-04-12 20:02:31 +00:00
|
|
|
/**************** Previews ****************/
|
|
|
|
class PreviewRenderLandscape:public BasePreview
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PreviewRenderLandscape(QWidget* parent):BasePreview(parent)
|
|
|
|
{
|
|
|
|
_renderer = rendererCreate();
|
|
|
|
_renderer.applyTextures = _applyTextures;
|
|
|
|
_renderer.getTerrainHeight = _getTerrainHeight;
|
2012-06-05 20:22:12 +00:00
|
|
|
_renderer.alterLight = _alterLight;
|
|
|
|
_renderer.getLightStatus = _getLightStatus;
|
2012-06-24 16:04:01 +00:00
|
|
|
_renderer.getSkyDomeLights = _getSkyDomeLights;
|
2012-04-12 20:02:31 +00:00
|
|
|
_renderer.camera_location.x = 0.0;
|
|
|
|
_renderer.camera_location.y = 50.0;
|
|
|
|
_renderer.camera_location.z = 0.0;
|
|
|
|
|
|
|
|
_terrain = terrainCreateDefinition();
|
|
|
|
_textures = texturesCreateDefinition();
|
|
|
|
_lighting = lightingCreateDefinition();
|
|
|
|
_water = waterCreateDefinition();
|
2012-06-24 16:04:01 +00:00
|
|
|
_sky = skyCreateDefinition();
|
2012-04-12 20:02:31 +00:00
|
|
|
|
|
|
|
_renderer.customData[0] = &_terrain;
|
|
|
|
_renderer.customData[1] = &_textures;
|
|
|
|
_renderer.customData[2] = &_lighting;
|
|
|
|
_renderer.customData[3] = &_water;
|
2012-06-24 16:04:01 +00:00
|
|
|
_renderer.customData[4] = &_sky;
|
2012-04-12 20:02:31 +00:00
|
|
|
|
2012-06-26 19:36:50 +00:00
|
|
|
addOsd(QString("geolocation"));
|
|
|
|
|
2012-06-02 14:17:01 +00:00
|
|
|
configScaling(0.5, 200.0, 3.0, 50.0);
|
2012-04-15 20:08:01 +00:00
|
|
|
configScrolling(-1000.0, 1000.0, 0.0, -1000.0, 1000.0, 0.0);
|
2012-04-12 20:02:31 +00:00
|
|
|
}
|
|
|
|
protected:
|
2012-06-17 09:40:40 +00:00
|
|
|
QColor getColor(double x, double y)
|
2012-04-12 20:02:31 +00:00
|
|
|
{
|
|
|
|
Vector3 down = {0.0, -1.0, 0.0};
|
|
|
|
Vector3 location;
|
2012-06-17 09:40:40 +00:00
|
|
|
double height = terrainGetHeight(&_terrain, x, y);
|
2012-04-12 20:02:31 +00:00
|
|
|
|
|
|
|
if (height < _water.height)
|
|
|
|
{
|
|
|
|
location.x = x;
|
|
|
|
location.y = _water.height;
|
|
|
|
location.z = y;
|
|
|
|
return colorToQColor(waterGetColor(&_water, &_renderer, location, down));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return colorToQColor(terrainGetColor(&_terrain, &_renderer, x, y, scaling));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void updateData()
|
|
|
|
{
|
|
|
|
sceneryGetTerrain(&_terrain);
|
|
|
|
sceneryGetLighting(&_lighting);
|
|
|
|
sceneryGetTextures(&_textures);
|
|
|
|
sceneryGetWater(&_water);
|
2012-06-24 16:04:01 +00:00
|
|
|
sceneryGetSky(&_sky);
|
2012-04-12 20:02:31 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
Renderer _renderer;
|
|
|
|
TerrainDefinition _terrain;
|
|
|
|
WaterDefinition _water;
|
|
|
|
TexturesDefinition _textures;
|
|
|
|
LightingDefinition _lighting;
|
2012-06-24 16:04:01 +00:00
|
|
|
SkyDefinition _sky;
|
2012-04-12 20:02:31 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static double _getTerrainHeight(Renderer* renderer, double x, double z)
|
2012-04-12 20:02:31 +00:00
|
|
|
{
|
|
|
|
return terrainGetHeight((TerrainDefinition*)(renderer->customData[0]), x, z);
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
static Color _applyTextures(Renderer* renderer, Vector3 location, double precision)
|
2012-04-12 20:02:31 +00:00
|
|
|
{
|
2012-05-05 22:07:02 +00:00
|
|
|
return texturesGetColor((TexturesDefinition*)(renderer->customData[1]), renderer, location.x, location.z, precision);
|
2012-04-12 20:02:31 +00:00
|
|
|
}
|
2012-06-24 16:04:01 +00:00
|
|
|
|
|
|
|
static int _getSkyDomeLights(Renderer* renderer, LightDefinition* array, int max_lights)
|
|
|
|
{
|
|
|
|
return skyGetLights((SkyDefinition*)(renderer->customData[4]), renderer, array, max_lights);
|
|
|
|
}
|
2012-04-12 20:02:31 +00:00
|
|
|
|
2012-06-05 20:22:12 +00:00
|
|
|
static void _alterLight(Renderer* renderer, LightDefinition* light, Vector3 location)
|
2012-04-12 20:02:31 +00:00
|
|
|
{
|
2012-06-05 20:22:12 +00:00
|
|
|
light->color = terrainLightFilter((TerrainDefinition*)(renderer->customData[0]), renderer, light->color, location, v3Scale(light->direction, -1000.0), v3Scale(light->direction, -1.0));
|
2012-04-12 20:02:31 +00:00
|
|
|
}
|
2012-06-05 20:22:12 +00:00
|
|
|
|
|
|
|
static void _getLightStatus(Renderer* renderer, LightStatus* status, Vector3 location)
|
2012-04-12 20:02:31 +00:00
|
|
|
{
|
2012-06-05 20:22:12 +00:00
|
|
|
lightingGetStatus((LightingDefinition*)renderer->customData[2], renderer, location, status);
|
2012-04-12 20:02:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-06 16:09:03 +00:00
|
|
|
/**************** Form ****************/
|
|
|
|
FormRender::FormRender(QWidget *parent) :
|
2012-01-27 23:01:21 +00:00
|
|
|
BaseForm(parent, true)
|
2012-01-06 16:09:03 +00:00
|
|
|
{
|
|
|
|
QPushButton* button;
|
|
|
|
|
2012-06-13 15:38:11 +00:00
|
|
|
_params.quality = 5;
|
|
|
|
_params.width = 800;
|
|
|
|
_params.height = 600;
|
|
|
|
_params.antialias = 1;
|
2012-01-27 23:01:21 +00:00
|
|
|
_camera = cameraCreateDefinition();
|
2012-04-02 19:38:59 +00:00
|
|
|
|
|
|
|
_renderer_inited = false;
|
2012-01-27 23:01:21 +00:00
|
|
|
|
2012-04-12 20:02:31 +00:00
|
|
|
_preview_landscape = new PreviewRenderLandscape(this);
|
|
|
|
addPreview(_preview_landscape, QString(tr("Top-down preview")));
|
|
|
|
|
2012-02-28 13:45:11 +00:00
|
|
|
addInput(new InputCamera(this, tr("Camera"), &_camera));
|
2012-06-13 15:38:11 +00:00
|
|
|
addInputInt(tr("Quality"), &_params.quality, 1, 10, 1, 1);
|
|
|
|
addInputInt(tr("Image width"), &_params.width, 100, 2000, 10, 100);
|
2012-06-15 19:45:41 +00:00
|
|
|
addInputInt(tr("Image height"), &_params.height, 100, 1200, 10, 100);
|
2012-06-13 15:38:11 +00:00
|
|
|
addInputInt(tr("Anti aliasing"), &_params.antialias, 1, 4, 1, 1);
|
2012-01-06 16:09:03 +00:00
|
|
|
|
2012-02-28 13:45:11 +00:00
|
|
|
button = addButton(tr("Start new render"));
|
2012-01-06 16:09:03 +00:00
|
|
|
connect(button, SIGNAL(clicked()), this, SLOT(startRender()));
|
2012-02-28 13:45:11 +00:00
|
|
|
button = addButton(tr("Show last render"));
|
2012-01-06 16:09:03 +00:00
|
|
|
connect(button, SIGNAL(clicked()), this, SLOT(showRender()));
|
2012-02-28 13:45:11 +00:00
|
|
|
button = addButton(tr("Save last render"));
|
2012-01-06 16:09:03 +00:00
|
|
|
connect(button, SIGNAL(clicked()), this, SLOT(saveRender()));
|
|
|
|
|
|
|
|
revertConfig();
|
|
|
|
}
|
|
|
|
|
2012-04-02 19:38:59 +00:00
|
|
|
FormRender::~FormRender()
|
|
|
|
{
|
|
|
|
if (_renderer_inited)
|
|
|
|
{
|
|
|
|
rendererDelete(&_renderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-27 10:30:00 +00:00
|
|
|
void FormRender::savePack(PackStream* stream)
|
|
|
|
{
|
|
|
|
BaseForm::savePack(stream);
|
|
|
|
|
|
|
|
packWriteInt(stream, &_params.width);
|
|
|
|
packWriteInt(stream, &_params.height);
|
|
|
|
packWriteInt(stream, &_params.antialias);
|
|
|
|
packWriteInt(stream, &_params.quality);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormRender::loadPack(PackStream* stream)
|
|
|
|
{
|
|
|
|
BaseForm::loadPack(stream);
|
|
|
|
|
|
|
|
packReadInt(stream, &_params.width);
|
|
|
|
packReadInt(stream, &_params.height);
|
|
|
|
packReadInt(stream, &_params.antialias);
|
|
|
|
packReadInt(stream, &_params.quality);
|
|
|
|
|
|
|
|
revertConfig();
|
|
|
|
}
|
|
|
|
|
2012-01-27 23:01:21 +00:00
|
|
|
void FormRender::revertConfig()
|
|
|
|
{
|
|
|
|
sceneryGetCamera(&_camera);
|
|
|
|
BaseForm::revertConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormRender::applyConfig()
|
|
|
|
{
|
|
|
|
scenerySetCamera(&_camera);
|
|
|
|
BaseForm::applyConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormRender::configChangeEvent()
|
|
|
|
{
|
2012-01-29 11:34:49 +00:00
|
|
|
cameraValidateDefinition(&_camera, 1);
|
2012-01-27 23:01:21 +00:00
|
|
|
BaseForm::configChangeEvent();
|
|
|
|
}
|
|
|
|
|
2012-04-02 19:38:59 +00:00
|
|
|
void FormRender::startQuickRender()
|
|
|
|
{
|
|
|
|
if (_renderer_inited)
|
|
|
|
{
|
|
|
|
rendererDelete(&_renderer);
|
|
|
|
}
|
|
|
|
_renderer = sceneryCreateStandardRenderer();
|
|
|
|
_renderer_inited = true;
|
|
|
|
|
|
|
|
DialogRender* dialog = new DialogRender(this, &_renderer);
|
2012-06-13 15:38:11 +00:00
|
|
|
RenderParams params = {400, 300, 1, 3};
|
|
|
|
dialog->startRender(params);
|
2012-04-02 19:38:59 +00:00
|
|
|
|
|
|
|
delete dialog;
|
|
|
|
}
|
|
|
|
|
2012-01-06 16:09:03 +00:00
|
|
|
void FormRender::startRender()
|
|
|
|
{
|
2012-04-02 19:38:59 +00:00
|
|
|
if (_renderer_inited)
|
|
|
|
{
|
|
|
|
rendererDelete(&_renderer);
|
|
|
|
}
|
|
|
|
_renderer = sceneryCreateStandardRenderer();
|
|
|
|
_renderer_inited = true;
|
|
|
|
|
|
|
|
DialogRender* dialog = new DialogRender(this, &_renderer);
|
2012-06-13 15:38:11 +00:00
|
|
|
dialog->startRender(_params);
|
2012-01-24 13:16:20 +00:00
|
|
|
|
2012-01-06 16:09:03 +00:00
|
|
|
delete dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormRender::showRender()
|
|
|
|
{
|
2012-04-02 19:38:59 +00:00
|
|
|
if (_renderer_inited)
|
|
|
|
{
|
|
|
|
DialogRender* dialog = new DialogRender(this, &_renderer);
|
|
|
|
dialog->loadLastRender();
|
2012-01-24 13:16:20 +00:00
|
|
|
|
2012-04-02 19:38:59 +00:00
|
|
|
delete dialog;
|
|
|
|
}
|
2012-01-06 16:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FormRender::saveRender()
|
|
|
|
{
|
2012-04-02 19:38:59 +00:00
|
|
|
if (_renderer_inited)
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-04-02 19:38:59 +00:00
|
|
|
QString filepath;
|
|
|
|
|
2012-04-16 19:24:46 +00:00
|
|
|
filepath = QFileDialog::getSaveFileName(this, tr("Paysages 3D - Choose a filename to save the last render"), QString(), tr("Images (*.png *.jpg)"));
|
2012-04-02 19:38:59 +00:00
|
|
|
if (!filepath.isNull())
|
|
|
|
{
|
2012-04-16 19:24:46 +00:00
|
|
|
if (!filepath.toLower().endsWith(".jpg") && !filepath.toLower().endsWith(".jpeg") && !filepath.toLower().endsWith(".png"))
|
|
|
|
{
|
|
|
|
filepath = filepath.append(".png");
|
|
|
|
}
|
2012-05-06 10:13:34 +00:00
|
|
|
if (renderSaveToFile(_renderer.render_area, (char*)filepath.toStdString().c_str()))
|
|
|
|
{
|
|
|
|
QMessageBox::information(this, "Message", QString(tr("The picture %1 has been saved.")).arg(filepath));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMessageBox::critical(this, "Message", QString(tr("Can't write to file : %1")).arg(filepath));
|
|
|
|
}
|
2012-04-02 19:38:59 +00:00
|
|
|
}
|
2012-01-18 16:20:14 +00:00
|
|
|
}
|
2012-01-06 16:09:03 +00:00
|
|
|
}
|