Lights minor fixes

master
Eduardo Bart 12 years ago
parent dc6d2bb078
commit f5b3bfda11

@ -82,10 +82,12 @@ void Creature::draw(const Point& dest, float scaleFactor, bool animate, LightVie
m_footStepDrawn = true;
if(lightView) {
Light light = m_light;
Light light = rawGetThingType()->getLight();
if(m_light.intensity > light.intensity)
light = m_light;
// local player always have a minimum light in complete darkness
if(isLocalPlayer() && (g_map.getLight().intensity < 40 || m_position.z > Otc::SEA_FLOOR)) {
if(isLocalPlayer() && (g_map.getLight().intensity < 64 || m_position.z > Otc::SEA_FLOOR)) {
light.intensity = std::max<uint8>(light.intensity, 2);
if(light.color == 0 || light.color > 215)
light.color = 215;

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/client>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -27,35 +27,42 @@
#include <framework/graphics/painter.h>
#include <framework/graphics/image.h>
enum {
MAX_LIGHT_INTENSITY = 8,
MAX_AMBIENT_LIGHT_INTENSITY = 255
};
LightView::LightView()
{
m_lightbuffer = g_framebuffers.createFrameBuffer();
generateLightBubble();
m_lightTexture = generateLightBubble(0.1f);
reset();
}
void LightView::generateLightBubble()
TexturePtr LightView::generateLightBubble(float centerFactor)
{
m_lightRadius = 128;
int circleDiameter = m_lightRadius * 2;
ImagePtr lightImage = ImagePtr(new Image(Size(circleDiameter, circleDiameter)));
int bubbleRadius = 256;
int centerRadius = bubbleRadius * centerFactor;
int bubbleDiameter = bubbleRadius * 2;
ImagePtr lightImage = ImagePtr(new Image(Size(bubbleDiameter, bubbleDiameter)));
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);
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);
// light intensity varies inversely with the square of the distance
// light intensity varies inversely with the square of the distance
intensity = intensity * intensity;
uint8_t colorByte = intensity * 255;
uint8_t colorByte = intensity * 0xff;
uint8_t pixel[4] = {colorByte,colorByte,colorByte,255};
uint8_t pixel[4] = {colorByte,colorByte,colorByte,0xff};
lightImage->setPixel(x, y, pixel);
}
}
m_lightTexture = TexturePtr(new Texture(lightImage, true));
m_lightTexture->setSmooth(true);
TexturePtr tex = TexturePtr(new Texture(lightImage, true));
tex->setSmooth(true);
return tex;
}
void LightView::reset()
@ -70,10 +77,11 @@ void LightView::setGlobalLight(const Light& light)
void LightView::addLightSource(const Point& center, float scaleFactor, const Light& light)
{
int radius = light.intensity * m_lightRadius * scaleFactor * 0.25f;
int intensity = std::min<int>(light.intensity, MAX_LIGHT_INTENSITY);
int radius = intensity * Otc::TILE_PIXELS * scaleFactor;
Color color = Color::from8bit(light.color);
float brightness = 0.8f + (light.intensity/8.0f)*0.2f;
float brightness = 0.5f + (intensity/(float)MAX_LIGHT_INTENSITY)*0.5f;
color.setRed(color.rF() * brightness);
color.setGreen(color.gF() * brightness);
@ -89,7 +97,7 @@ void LightView::addLightSource(const Point& center, float scaleFactor, const Lig
void LightView::drawGlobalLight(const Light& light)
{
Color color = Color::from8bit(light.color);
float brightness = light.intensity / 256.0f;
float brightness = light.intensity / (float)MAX_AMBIENT_LIGHT_INTENSITY;
color.setRed(color.rF() * brightness);
color.setGreen(color.gF() * brightness);
color.setBlue(color.bF() * brightness);
@ -99,18 +107,12 @@ void LightView::drawGlobalLight(const Light& light)
void LightView::drawLightSource(const Point& center, const Color& color, int radius)
{
// debug draw
//radius /= 16;
Rect dest = Rect(center - Point(radius, radius), Size(radius*2,radius*2));
g_painter->setColor(color);
g_painter->setCompositionMode(Painter::CompositionMode_Add);
g_painter->drawTexturedRect(dest, m_lightTexture);
// debug draw
/*
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);
*/
}
void LightView::resize(const Size& size)
@ -128,7 +130,6 @@ void LightView::draw(const Rect& dest, const Rect& src)
for(const LightSource& source : m_lightMap)
drawLightSource(source.center, source.color, source.radius);
m_lightbuffer->release();
g_painter->setCompositionMode(Painter::CompositionMode_Light);
m_lightbuffer->draw(dest, src);
g_painter->restoreSavedState();

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/client>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -47,14 +47,13 @@ public:
private:
void drawGlobalLight(const Light& light);
void drawLightSource(const Point& center, const Color& color, int radius);
void generateLightBubble();
TexturePtr generateLightBubble(float centerFactor);
TexturePtr m_lightTexture;
int m_lightRadius;
FrameBufferPtr m_lightbuffer;
MapView* m_mapView;
Light m_globalLight;
std::vector<LightSource> m_lightMap;
};
#endif // LIGHTVIEW_H
#endif

@ -375,16 +375,20 @@ bool Map::isCovered(const Position& pos, int firstFloor)
bool Map::isCompletelyCovered(const Position& pos, int firstFloor)
{
const TilePtr& checkTile = getTile(pos);
Position tilePos = pos;
while(tilePos.coveredUp() && tilePos.z >= firstFloor) {
bool covered = true;
bool done = false;
// check in 2x2 range tiles that has no transparent pixels
for(int x=0;x<2;++x) {
for(int y=0;y<2;++y) {
for(int x=0;x<2 && !done;++x) {
for(int y=0;y<2 && !done;++y) {
const TilePtr& tile = getTile(tilePos.translated(-x, -y));
if(!tile || !tile->isFullyOpaque()) {
covered = false;
break;
done = true;
} else if(x==0 && y==0 && (!checkTile || checkTile->isSingleDimension())) {
done = true;
}
}
}

@ -104,7 +104,7 @@ void MapView::draw(const Rect& rect)
m_lightView->resize(m_framebuffer->getSize());
Light ambientLight;
if(cameraPosition.z <= 7) {
if(cameraPosition.z <= Otc::SEA_FLOOR) {
ambientLight = g_map.getLight();
} else {
ambientLight.color = 215;

@ -1642,20 +1642,10 @@ CreaturePtr ProtocolGame::getCreature(const InputMessagePtr& msg, int type)
creature->setSkull(skull);
creature->setShield(shield);
creature->setPassable(!unpass);
creature->setLight(light);
if(emblem != -1)
creature->setEmblem(emblem);
ThingTypePtr thing = creature->getThingType();
if(thing) {
Light newLight = thing->getLight();
if(light.color > 0 && light.color != 215)
newLight.color = light.color;
if(light.intensity > 0)
newLight.intensity = light.intensity;
creature->setLight(newLight);
}
if(creature == m_localPlayer && !m_localPlayer->isKnown())
m_localPlayer->setKnown(true);
}

@ -508,6 +508,17 @@ bool Tile::isFullyOpaque()
return firstObject && firstObject->isFullGround();
}
bool Tile::isSingleDimension()
{
if(!m_walkingCreatures.empty())
return false;
for(const ThingPtr& thing : m_things) {
if(thing->getHeight() != 1 || thing->getWidth() != 1)
return false;
}
return true;
}
bool Tile::isLookPossible()
{
for(const ThingPtr& thing : m_things) {

@ -98,6 +98,7 @@ public:
bool changesFloor();
bool isFullGround();
bool isFullyOpaque();
bool isSingleDimension();
bool isLookPossible();
bool isClickable();
bool isEmpty();

Loading…
Cancel
Save