2012-11-29 02:47:26 +01:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2012-11-29 02:47:26 +01:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2012-12-04 00:18:39 +01:00
|
|
|
enum {
|
|
|
|
MAX_LIGHT_INTENSITY = 8,
|
|
|
|
MAX_AMBIENT_LIGHT_INTENSITY = 255
|
|
|
|
};
|
|
|
|
|
2012-11-29 02:47:26 +01:00
|
|
|
LightView::LightView()
|
|
|
|
{
|
2012-11-29 03:45:26 +01:00
|
|
|
m_lightbuffer = g_framebuffers.createFrameBuffer();
|
2012-12-04 00:18:39 +01:00
|
|
|
m_lightTexture = generateLightBubble(0.1f);
|
2012-11-29 02:47:26 +01:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:18:39 +01:00
|
|
|
TexturePtr LightView::generateLightBubble(float centerFactor)
|
2012-11-29 02:47:26 +01:00
|
|
|
{
|
2012-12-04 00:18:39 +01:00
|
|
|
int bubbleRadius = 256;
|
|
|
|
int centerRadius = bubbleRadius * centerFactor;
|
|
|
|
int bubbleDiameter = bubbleRadius * 2;
|
|
|
|
ImagePtr lightImage = ImagePtr(new Image(Size(bubbleDiameter, bubbleDiameter)));
|
2012-11-29 02:47:26 +01:00
|
|
|
|
2012-12-04 00:18:39 +01:00
|
|
|
for(int x = 0; x < bubbleDiameter; x++) {
|
|
|
|
for(int y = 0; y < bubbleDiameter; y++) {
|
|
|
|
float radius = std::sqrt((bubbleRadius - x)*(bubbleRadius - x) + (bubbleRadius - y)*(bubbleRadius - y));
|
|
|
|
float intensity = std::max(std::min((bubbleRadius-radius)/(float)(bubbleRadius-centerRadius), 1.0f), 0.0f);
|
2012-11-29 02:47:26 +01:00
|
|
|
|
2012-12-04 00:18:39 +01:00
|
|
|
// light intensity varies inversely with the square of the distance
|
2012-11-29 22:38:39 +01:00
|
|
|
intensity = intensity * intensity;
|
2012-12-04 00:18:39 +01:00
|
|
|
uint8_t colorByte = intensity * 0xff;
|
2012-11-29 02:47:26 +01:00
|
|
|
|
2012-12-04 00:18:39 +01:00
|
|
|
uint8_t pixel[4] = {colorByte,colorByte,colorByte,0xff};
|
2012-11-29 22:38:39 +01:00
|
|
|
lightImage->setPixel(x, y, pixel);
|
2012-11-29 02:47:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:18:39 +01:00
|
|
|
TexturePtr tex = TexturePtr(new Texture(lightImage, true));
|
|
|
|
tex->setSmooth(true);
|
|
|
|
return tex;
|
2012-11-29 02:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-04 00:18:39 +01:00
|
|
|
int intensity = std::min<int>(light.intensity, MAX_LIGHT_INTENSITY);
|
|
|
|
int radius = intensity * Otc::TILE_PIXELS * scaleFactor;
|
2012-11-29 22:38:39 +01:00
|
|
|
|
|
|
|
Color color = Color::from8bit(light.color);
|
2012-12-04 00:18:39 +01:00
|
|
|
float brightness = 0.5f + (intensity/(float)MAX_LIGHT_INTENSITY)*0.5f;
|
2012-11-29 22:38:39 +01:00
|
|
|
|
|
|
|
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-12-04 00:18:39 +01:00
|
|
|
float brightness = light.intensity / (float)MAX_AMBIENT_LIGHT_INTENSITY;
|
2012-11-29 22:38:39 +01:00
|
|
|
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)
|
|
|
|
{
|
2012-12-04 00:18:39 +01:00
|
|
|
// debug draw
|
|
|
|
//radius /= 16;
|
|
|
|
|
2012-11-29 02:47:26 +01:00
|
|
|
Rect dest = Rect(center - Point(radius, radius), Size(radius*2,radius*2));
|
|
|
|
g_painter->setColor(color);
|
|
|
|
g_painter->drawTexturedRect(dest, m_lightTexture);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|