2012-11-29 02:47:26 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lightview.h"
|
|
|
|
#include "mapview.h"
|
|
|
|
#include <framework/graphics/framebuffer.h>
|
|
|
|
#include <framework/graphics/framebuffermanager.h>
|
|
|
|
#include <framework/graphics/painter.h>
|
|
|
|
#include <framework/graphics/image.h>
|
|
|
|
|
|
|
|
LightView::LightView()
|
|
|
|
{
|
2012-11-29 03:45:26 +01:00
|
|
|
m_lightbuffer = g_framebuffers.createFrameBuffer();
|
2012-12-03 01:17:41 +01:00
|
|
|
generateLightBubble();
|
2012-11-29 02:47:26 +01:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2012-12-03 01:17:41 +01:00
|
|
|
void LightView::generateLightBubble()
|
2012-11-29 02:47:26 +01:00
|
|
|
{
|
2012-11-29 22:38:39 +01:00
|
|
|
m_lightRadius = 128;
|
|
|
|
int circleDiameter = m_lightRadius * 2;
|
|
|
|
ImagePtr lightImage = ImagePtr(new Image(Size(circleDiameter, circleDiameter)));
|
2012-11-29 02:47:26 +01:00
|
|
|
|
2012-11-29 22:38:39 +01:00
|
|
|
for(int x = 0; x < circleDiameter; x++) {
|
|
|
|
for(int y = 0; y < circleDiameter; y++) {
|
|
|
|
float radius = std::sqrt((m_lightRadius - x)*(m_lightRadius - x) + (m_lightRadius - y)*(m_lightRadius - y));
|
|
|
|
float intensity = std::max((m_lightRadius-radius)/(float)m_lightRadius, 0.0f);
|
2012-11-29 02:47:26 +01:00
|
|
|
|
2012-11-29 22:38:39 +01:00
|
|
|
// light intensity varies inversely with the square of the distance
|
|
|
|
intensity = intensity * intensity;
|
|
|
|
uint8_t colorByte = intensity * 255;
|
2012-11-29 02:47:26 +01:00
|
|
|
|
2012-11-29 22:38:39 +01:00
|
|
|
uint8_t pixel[4] = {colorByte,colorByte,colorByte,255};
|
|
|
|
lightImage->setPixel(x, y, pixel);
|
2012-11-29 02:47:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-29 22:38:39 +01:00
|
|
|
m_lightTexture = TexturePtr(new Texture(lightImage, true));
|
2012-11-29 02:47:26 +01:00
|
|
|
m_lightTexture->setSmooth(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LightView::reset()
|
|
|
|
{
|
2012-11-29 03:45:26 +01:00
|
|
|
m_lightMap.clear();
|
2012-11-29 02:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightView::setGlobalLight(const Light& light)
|
|
|
|
{
|
|
|
|
m_globalLight = light;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LightView::addLightSource(const Point& center, float scaleFactor, const Light& light)
|
|
|
|
{
|
2012-11-29 22:38:39 +01:00
|
|
|
int radius = light.intensity * m_lightRadius * scaleFactor * 0.25f;
|
|
|
|
|
|
|
|
Color color = Color::from8bit(light.color);
|
|
|
|
float brightness = 0.8f + (light.intensity/8.0f)*0.2f;
|
|
|
|
|
|
|
|
color.setRed(color.rF() * brightness);
|
|
|
|
color.setGreen(color.gF() * brightness);
|
|
|
|
color.setBlue(color.bF() * brightness);
|
2012-11-29 02:47:26 +01:00
|
|
|
|
2012-11-29 03:45:26 +01:00
|
|
|
LightSource source;
|
|
|
|
source.center = center;
|
2012-11-29 22:38:39 +01:00
|
|
|
source.color = color;
|
2012-11-29 03:45:26 +01:00
|
|
|
source.radius = radius;
|
|
|
|
m_lightMap.push_back(source);
|
2012-11-29 02:47:26 +01:00
|
|
|
}
|
|
|
|
|
2012-11-29 22:38:39 +01:00
|
|
|
void LightView::drawGlobalLight(const Light& light)
|
2012-11-29 02:47:26 +01:00
|
|
|
{
|
|
|
|
Color color = Color::from8bit(light.color);
|
2012-11-29 22:38:39 +01:00
|
|
|
float brightness = light.intensity / 256.0f;
|
|
|
|
color.setRed(color.rF() * brightness);
|
|
|
|
color.setGreen(color.gF() * brightness);
|
|
|
|
color.setBlue(color.bF() * brightness);
|
2012-11-29 03:45:26 +01:00
|
|
|
g_painter->setColor(color);
|
2012-11-29 22:38:39 +01:00
|
|
|
g_painter->drawFilledRect(Rect(0,0,m_lightbuffer->getSize()));
|
2012-11-29 02:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightView::drawLightSource(const Point& center, const Color& color, int radius)
|
|
|
|
{
|
|
|
|
Rect dest = Rect(center - Point(radius, radius), Size(radius*2,radius*2));
|
|
|
|
g_painter->setColor(color);
|
2012-11-29 22:38:39 +01:00
|
|
|
g_painter->setCompositionMode(Painter::CompositionMode_Add);
|
2012-11-29 02:47:26 +01:00
|
|
|
g_painter->drawTexturedRect(dest, m_lightTexture);
|
|
|
|
|
|
|
|
// debug draw
|
2012-11-29 22:38:39 +01:00
|
|
|
/*
|
|
|
|
radius = 8;
|
|
|
|
g_painter->setColor(Color::black);
|
|
|
|
g_painter->setCompositionMode(Painter::CompositionMode_Replace);
|
|
|
|
g_painter->drawBoundingRect(Rect(center - Point(radius, radius), Size(radius*2,radius*2)), 4);
|
|
|
|
*/
|
2012-11-29 02:47:26 +01:00
|
|
|
}
|
|
|
|
|
2012-11-29 22:38:39 +01:00
|
|
|
void LightView::resize(const Size& size)
|
2012-11-29 02:47:26 +01:00
|
|
|
{
|
|
|
|
m_lightbuffer->resize(size);
|
2012-11-29 22:38:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightView::draw(const Rect& dest, const Rect& src)
|
|
|
|
{
|
|
|
|
g_painter->saveAndResetState();
|
2012-11-29 02:47:26 +01:00
|
|
|
m_lightbuffer->bind();
|
2012-11-29 22:38:39 +01:00
|
|
|
g_painter->setCompositionMode(Painter::CompositionMode_Replace);
|
|
|
|
drawGlobalLight(m_globalLight);
|
2012-11-29 02:47:26 +01:00
|
|
|
g_painter->setCompositionMode(Painter::CompositionMode_Add);
|
2012-11-29 03:45:26 +01:00
|
|
|
for(const LightSource& source : m_lightMap)
|
|
|
|
drawLightSource(source.center, source.color, source.radius);
|
2012-11-29 02:47:26 +01:00
|
|
|
m_lightbuffer->release();
|
|
|
|
|
|
|
|
g_painter->setCompositionMode(Painter::CompositionMode_Light);
|
2012-11-29 22:38:39 +01:00
|
|
|
m_lightbuffer->draw(dest, src);
|
|
|
|
g_painter->restoreSavedState();
|
2012-11-29 02:47:26 +01:00
|
|
|
}
|