Fix issue #145, #29 and #39

This commit is contained in:
Eduardo Bart 2012-11-29 00:45:26 -02:00
parent 81068f820d
commit d7a45311ed
3 changed files with 32 additions and 36 deletions

View File

@ -29,7 +29,7 @@
LightView::LightView()
{
m_lightbuffer = g_framebuffers.getTemporaryFrameBuffer();
m_lightbuffer = g_framebuffers.createFrameBuffer();
generateLightBuble();
reset();
}
@ -64,7 +64,7 @@ void LightView::generateLightBuble()
void LightView::reset()
{
m_numLights = 0;
m_lightMap.clear();
}
void LightView::setGlobalLight(const Light& light)
@ -74,26 +74,25 @@ void LightView::setGlobalLight(const Light& light)
void LightView::addLightSource(const Point& center, float scaleFactor, const Light& light)
{
if(m_numLights > MAX_LIGHTS)
return;
int radius = light.intensity * 64 * scaleFactor;
m_lightMap[m_numLights].center = center;
m_lightMap[m_numLights].color = Color::from8bit(light.color);
m_lightMap[m_numLights].radius = radius;
m_numLights++;
LightSource source;
source.center = center;
source.color = Color::from8bit(light.color);
source.radius = radius;
m_lightMap.push_back(source);
}
void LightView::drawGlobalLight(const Light& light)
void LightView::drawGlobalLight(const Light& light, const Size& size)
{
Color color = Color::from8bit(light.color);
float factor = 0;//std::max<int>(256 - light.intensity, 0) / 256.0f;
float factor = light.intensity / 256.0f;
color.setRed(color.rF() * factor);
color.setGreen(color.gF() * factor);
color.setBlue(color.bF() * factor);
color.setAlpha(1.0f);
g_painter->clear(color);
g_painter->setColor(color);
g_painter->drawFilledRect(Rect(0,0,size));
}
void LightView::drawLightSource(const Point& center, const Color& color, int radius)
@ -110,10 +109,10 @@ void LightView::draw(Size size)
{
m_lightbuffer->resize(size);
m_lightbuffer->bind();
drawGlobalLight(m_globalLight);
drawGlobalLight(m_globalLight, size);
g_painter->setCompositionMode(Painter::CompositionMode_Add);
for(int i=0;i<m_numLights;++i)
drawLightSource(m_lightMap[i].center, m_lightMap[i].color, m_lightMap[i].radius);
for(const LightSource& source : m_lightMap)
drawLightSource(source.center, source.color, source.radius);
m_lightbuffer->release();
g_painter->setCompositionMode(Painter::CompositionMode_Light);

View File

@ -35,10 +35,6 @@ struct LightSource {
class LightView : public LuaObject
{
enum {
MAX_LIGHTS = 1024
};
public:
LightView();
@ -48,16 +44,15 @@ public:
void draw(Size size);
private:
void drawGlobalLight(const Light& light);
void drawGlobalLight(const Light& light, const Size& size);
void drawLightSource(const Point& center, const Color& color, int radius);
void generateLightBuble();
TexturePtr m_lightTexture;
FrameBufferPtr m_lightbuffer;
MapView* m_mapView;
int m_numLights;
Light m_globalLight;
std::array<LightSource, MAX_LIGHTS> m_lightMap;
std::vector<LightSource> m_lightMap;
};
#endif // LIGHTVIEW_H

View File

@ -90,14 +90,6 @@ void MapView::draw(const Rect& rect)
Size tileSize = Size(1,1) * m_tileSize;
if(m_drawLights) {
m_lightView->reset();
Light globalLight = g_map.getLight();
if(cameraPosition.z <= 7)
globalLight.intensity = std::min<uint8>(200, globalLight.intensity);
m_lightView->setGlobalLight(globalLight);
}
if(m_mustDrawVisibleTilesCache || (drawFlags & Otc::DrawAnimations)) {
m_framebuffer->bind();
@ -105,6 +97,19 @@ void MapView::draw(const Rect& rect)
Rect clearRect = Rect(0, 0, m_drawDimension * m_tileSize);
g_painter->setColor(Color::black);
g_painter->drawFilledRect(clearRect);
if(m_drawLights) {
m_lightView->reset();
if(cameraPosition.z <= 7)
m_lightView->setGlobalLight(g_map.getLight());
else {
Light undergroundLight;
undergroundLight.color = 0;
undergroundLight.intensity = 0;
m_lightView->setGlobalLight(undergroundLight);
}
}
}
g_painter->setColor(Color::white);
@ -139,6 +144,9 @@ void MapView::draw(const Rect& rect)
}
}
if(m_drawLights && m_updateTilesPos == 0)
m_lightView->draw(m_framebuffer->getSize());
m_framebuffer->release();
// generating mipmaps each frame can be slow in older cards
@ -147,12 +155,6 @@ void MapView::draw(const Rect& rect)
m_mustDrawVisibleTilesCache = false;
}
if(m_drawLights) {
m_framebuffer->bind();
m_lightView->draw(m_framebuffer->getSize());
m_framebuffer->release();
}
Point drawOffset = ((m_drawDimension - m_visibleDimension - Size(1,1)).toPoint()/2) * m_tileSize;
if(isFollowingCreature())
drawOffset += m_followingCreature->getWalkOffset() * scaleFactor;