2012-06-02 14:17:01 +00:00
|
|
|
#include "previewmaterial.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <QPainter>
|
|
|
|
#include "tools.h"
|
|
|
|
|
2013-01-19 22:42:50 +00:00
|
|
|
#include "../lib_paysages/tools/lighting.h"
|
|
|
|
#include "../lib_paysages/tools/color.h"
|
2012-06-02 14:17:01 +00:00
|
|
|
|
|
|
|
/***** Small static preview *****/
|
|
|
|
|
|
|
|
SmallMaterialPreview::SmallMaterialPreview(QWidget* parent, SurfaceMaterial* material) : QWidget(parent)
|
|
|
|
{
|
|
|
|
LightDefinition light;
|
|
|
|
|
2013-01-19 22:42:50 +00:00
|
|
|
/*_lighting = lightingCreateDefinition();
|
2012-06-02 14:17:01 +00:00
|
|
|
light.color = COLOR_WHITE;
|
|
|
|
light.direction.x = -0.5;
|
|
|
|
light.direction.y = -0.5;
|
|
|
|
light.direction.z = -0.5;
|
|
|
|
light.direction = v3Normalize(light.direction);
|
|
|
|
light.filtered = 0;
|
|
|
|
light.masked = 0;
|
|
|
|
light.reflection = 1.0;
|
|
|
|
lightingAddLight(&_lighting, light);
|
2013-01-19 22:42:50 +00:00
|
|
|
lightingValidateDefinition(&_lighting);*/
|
2012-06-02 14:17:01 +00:00
|
|
|
|
|
|
|
_material = material;
|
|
|
|
|
|
|
|
_renderer = rendererCreate();
|
2013-01-20 15:07:45 +00:00
|
|
|
_renderer->camera_location.x = 0.0;
|
|
|
|
_renderer->camera_location.x = 0.0;
|
|
|
|
_renderer->camera_location.z = 10.0;
|
2012-06-02 14:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallMaterialPreview::~SmallMaterialPreview()
|
|
|
|
{
|
2013-01-20 15:07:45 +00:00
|
|
|
rendererDelete(_renderer);
|
2012-06-02 14:17:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-16 14:26:46 +00:00
|
|
|
Color SmallMaterialPreview::getColor(double x, double y)
|
2012-06-02 14:17:01 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double dist = sqrt(x * x + y * y);
|
2012-06-02 14:17:01 +00:00
|
|
|
Vector3 point;
|
2012-06-02 21:01:35 +00:00
|
|
|
Color color;
|
2012-06-02 14:17:01 +00:00
|
|
|
|
2012-06-02 21:01:35 +00:00
|
|
|
if (dist >= 1.0)
|
2012-06-02 14:17:01 +00:00
|
|
|
{
|
2013-01-16 14:26:46 +00:00
|
|
|
return COLOR_TRANSPARENT;
|
2012-06-02 14:17:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
point.x = x;
|
|
|
|
point.y = -y;
|
|
|
|
if (dist == 0)
|
|
|
|
{
|
|
|
|
point.z = 1.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
point.z = fabs(acos(dist));
|
|
|
|
}
|
|
|
|
|
|
|
|
point = v3Normalize(point);
|
2013-01-19 22:42:50 +00:00
|
|
|
//color = lightingApplyToSurface(&_lighting, &_renderer, point, point, *_material);
|
|
|
|
color = COLOR_RED;
|
2012-06-02 21:01:35 +00:00
|
|
|
if (dist > 0.95)
|
|
|
|
{
|
|
|
|
color.a = (1.0 - dist) / 0.05;
|
|
|
|
}
|
2013-01-16 14:26:46 +00:00
|
|
|
return color;
|
2012-06-02 14:17:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 14:26:46 +00:00
|
|
|
void SmallMaterialPreview::paintEvent(QPaintEvent*)
|
2012-06-02 14:17:01 +00:00
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
int width = this->width();
|
|
|
|
int height = this->height();
|
2012-06-17 09:40:40 +00:00
|
|
|
double factor, dx, dy;
|
2013-01-16 14:26:46 +00:00
|
|
|
|
2012-06-02 14:17:01 +00:00
|
|
|
if (width > height)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
factor = 2.0 / (double)height;
|
2012-06-02 14:17:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
factor = 2.0 / (double)width;
|
2012-06-02 14:17:01 +00:00
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
dx = factor * (double)width / 2.0;
|
|
|
|
dy = factor * (double)height / 2.0;
|
2012-06-02 14:17:01 +00:00
|
|
|
|
|
|
|
for (int x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
2013-01-16 14:26:46 +00:00
|
|
|
painter.setPen(colorToQColor(getColor((double)x * factor - dx, (double)y * factor - dy)));
|
2012-06-02 14:17:01 +00:00
|
|
|
painter.drawPoint(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** Large dynamic preview *****/
|
|
|
|
|
|
|
|
PreviewMaterial::PreviewMaterial(QWidget* parent, SurfaceMaterial* material) : BasePreview(parent)
|
|
|
|
{
|
|
|
|
_small = new SmallMaterialPreview(this, material);
|
|
|
|
_small->hide();
|
2013-01-16 14:26:46 +00:00
|
|
|
|
2012-06-02 14:17:01 +00:00
|
|
|
configScaling(0.05, 2.0, 0.05, 2.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PreviewMaterial::~PreviewMaterial()
|
|
|
|
{
|
|
|
|
delete _small;
|
|
|
|
}
|
|
|
|
|
2013-01-16 14:26:46 +00:00
|
|
|
Color PreviewMaterial::getColor(double x, double y)
|
2012-06-02 14:17:01 +00:00
|
|
|
{
|
|
|
|
return _small->getColor(x, y);
|
|
|
|
}
|