paysages3d/src/render/software/CloudsRenderer.cpp

159 lines
3.9 KiB
C++
Raw Normal View History

#include "CloudsRenderer.h"
#include "SoftwareRenderer.h"
#include "Scenery.h"
#include "CloudsDefinition.h"
2013-12-04 21:52:18 +00:00
#include "CloudLayerDefinition.h"
#include "BaseCloudLayerRenderer.h"
#include "CloudBasicLayerRenderer.h"
2013-12-04 21:52:18 +00:00
#include "clouds/BaseCloudsModel.h"
#include "clouds/CloudModelStratoCumulus.h"
CloudsRenderer::CloudsRenderer(SoftwareRenderer* parent):
parent(parent)
{
fake_renderer = new BaseCloudLayerRenderer(parent);
2013-12-04 21:52:18 +00:00
CloudLayerDefinition* fake_layer = new CloudLayerDefinition(NULL);
fake_model = new BaseCloudsModel(fake_layer);
}
CloudsRenderer::~CloudsRenderer()
{
for (auto renderer : layer_renderers)
{
delete renderer;
}
delete fake_renderer;
2013-12-04 21:52:18 +00:00
for (auto model : layer_models)
{
delete model;
}
delete fake_model->getLayer();
delete fake_model;
}
void CloudsRenderer::update()
{
for (auto renderer : layer_renderers)
{
delete renderer;
}
layer_renderers.clear();
2013-12-04 21:52:18 +00:00
for (auto model : layer_models)
{
delete model;
}
layer_models.clear();
CloudsDefinition* clouds = parent->getScenery()->getClouds();
int n = clouds->count();
for (int i = 0; i < n; i++)
{
layer_renderers.push_back(new CloudBasicLayerRenderer(parent));
2013-12-04 21:52:18 +00:00
CloudLayerDefinition* layer = clouds->getCloudLayer(i);
BaseCloudsModel* model;
switch (layer->type)
{
case CloudLayerDefinition::STRATUS:
case CloudLayerDefinition::NIMBOSTRATUS:
case CloudLayerDefinition::CUMULUS:
model = new BaseCloudsModel(layer);
break;
case CloudLayerDefinition::STRATOCUMULUS:
model = new CloudModelStratoCumulus(layer);
break;
case CloudLayerDefinition::ALTOCUMULUS:
case CloudLayerDefinition::ALTOSTRATUS:
case CloudLayerDefinition::CUMULONIMBUS:
case CloudLayerDefinition::CIRROCUMULUS:
case CloudLayerDefinition::CIRROSTRATUS:
case CloudLayerDefinition::CIRRUS:
model = new BaseCloudsModel(layer);
break;
}
layer_models.push_back(model);
model->update();
}
}
BaseCloudLayerRenderer* CloudsRenderer::getLayerRenderer(unsigned int layer)
{
if (layer < layer_renderers.size())
{
return layer_renderers[layer];
}
else
{
2013-12-17 22:01:36 +00:00
qWarning("Asked for unknown layer renderer %d", layer);
return fake_renderer;
}
}
2013-12-04 21:52:18 +00:00
BaseCloudsModel* CloudsRenderer::getLayerModel(unsigned int layer)
{
if (layer < layer_models.size())
{
return layer_models[layer];
}
else
{
2013-12-17 22:01:36 +00:00
qWarning("Asked for unknown layer renderer %d", layer);
2013-12-04 21:52:18 +00:00
return fake_model;
}
}
Color CloudsRenderer::getColor(const Vector3 &eye, const Vector3 &location, const Color &base)
{
CloudsDefinition* definition = parent->getScenery()->getClouds();
int n = definition->count();
if (n < 1)
{
return base;
}
/* TODO Iter layers in sorted order */
Color cumul = base;
for (int i = 0; i < n; i++)
{
BaseCloudLayerRenderer* layer_renderer = getLayerRenderer(i);
2013-12-04 21:52:18 +00:00
BaseCloudsModel* layer_model = getLayerModel(i);
2013-12-04 21:52:18 +00:00
Color layer_color = layer_renderer->getColor(layer_model, eye, location);
2013-12-11 09:24:35 +00:00
cumul.mask(layer_color);
}
return cumul;
}
bool CloudsRenderer::alterLight(LightComponent* light, const Vector3 &eye, const Vector3 &location)
{
CloudsDefinition* definition = parent->getScenery()->getClouds();
int n = definition->count();
if (n < 1)
{
return false;
}
/* TODO Iter layers in sorted order */
bool altered = false;
for (int i = 0; i < n; i++)
{
BaseCloudLayerRenderer* layer_renderer = getLayerRenderer(i);
BaseCloudsModel* layer_model = getLayerModel(i);
2013-12-17 22:01:36 +00:00
altered = layer_renderer->alterLight(layer_model, light, eye, location) || altered;
}
return altered;
}