1lightview.cpp

main 95 lines · 4.4 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "lightview.h"
24#include "spritemanager.h"
25#include <framework/graphics/painter.h>
26
27void LightView::addLight(const Point& pos, uint8_t color, uint8_t intensity)
28{
29 if (!m_lights.empty()) {
30 Light& prevLight = m_lights.back();
31 if (prevLight.pos == pos && prevLight.color == color) {
32 prevLight.intensity = std::max(prevLight.intensity, intensity);
33 return;
34 }
35 }
36 m_lights.push_back(Light{ pos, color, intensity });
37}
38
39void LightView::setFieldBrightness(const Point& pos, size_t start, uint8_t color)
40{
41 size_t index = (pos.y / g_sprites.spriteSize()) * m_mapSize.width() + (pos.x / g_sprites.spriteSize());
42 if (index >= m_tiles.size()) return;
43 m_tiles[index].start = start;
44 m_tiles[index].color = color;
45}
46
47void LightView::draw() // render thread
48{
49 // TODO: optimize in the future for big areas
50 static std::vector<uint8_t> buffer;
51 if (buffer.size() < 4u * m_mapSize.area())
52 buffer.resize(m_mapSize.area() * 4);
53
54 for (int x = 0; x < m_mapSize.width(); ++x) {
55 for (int y = 0; y < m_mapSize.height(); ++y) {
56 Point pos(x * g_sprites.spriteSize() + g_sprites.spriteSize() / 2, y * g_sprites.spriteSize() + g_sprites.spriteSize() / 2);
57 int index = (y * m_mapSize.width() + x);
58 int colorIndex = index * 4;
59 buffer[colorIndex] = m_globalLight.r();
60 buffer[colorIndex + 1] = m_globalLight.g();
61 buffer[colorIndex + 2] = m_globalLight.b();
62 buffer[colorIndex + 3] = 255; // alpha channel
63 for (size_t i = m_tiles[index].start; i < m_lights.size(); ++i) {
64 Light& light = m_lights[i];
65 float distance = std::sqrt((pos.x - light.pos.x) * (pos.x - light.pos.x) +
66 (pos.y - light.pos.y) * (pos.y - light.pos.y));
67 distance /= g_sprites.spriteSize();
68 float intensity = (-distance + light.intensity) * 0.2f;
69 if (intensity < 0.01f) continue;
70 if (intensity > 1.0f) intensity = 1.0f;
71 Color lightColor = Color::from8bit(light.color) * intensity;
72 buffer[colorIndex] = std::max<int>(buffer[colorIndex], lightColor.r());
73 buffer[colorIndex + 1] = std::max<int>(buffer[colorIndex + 1], lightColor.g());
74 buffer[colorIndex + 2] = std::max<int>(buffer[colorIndex + 2], lightColor.b());
75 }
76 }
77 }
78
79 m_lightTexture->update();
80 glBindTexture(GL_TEXTURE_2D, m_lightTexture->getId());
81 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_mapSize.width(), m_mapSize.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data());
82
83 Point offset = m_src.topLeft();
84 Size size = m_src.size();
85 CoordsBuffer coords;
86 coords.addRect(RectF(m_dest.left(), m_dest.top(), m_dest.width(), m_dest.height()),
87 RectF((float)offset.x / g_sprites.spriteSize(), (float)offset.y / g_sprites.spriteSize(),
88 (float)size.width() / g_sprites.spriteSize(), (float)size.height() / g_sprites.spriteSize()));
89
90 g_painter->resetColor();
91 g_painter->setCompositionMode(Painter::CompositionMode_Multiply);
92 g_painter->drawTextureCoords(coords, m_lightTexture);
93 g_painter->resetCompositionMode();
94}
95
Top