| 1 |
/* Copyright (C) 2025 Wildfire Games. |
| 2 |
* This file is part of 0 A.D. |
| 3 |
* |
| 4 |
* 0 A.D. is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* 0 A.D. is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "precompiled.h" |
| 19 |
|
| 20 |
#include "LightControl.h" |
| 21 |
|
| 22 |
#include "tools/atlas/AtlasUI/General/Observable.h" |
| 23 |
#include "tools/atlas/GameInterface/Messages.h" |
| 24 |
#include "tools/atlas/GameInterface/Shareable.h" |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <cmath> |
| 28 |
#include <list> |
| 29 |
#include <wx/bitmap.h> |
| 30 |
#include <wx/control.h> |
| 31 |
#include <wx/dcclient.h> |
| 32 |
#include <wx/defs.h> |
| 33 |
#include <wx/event.h> |
| 34 |
#include <wx/gdicmn.h> |
| 35 |
#include <wx/image.h> |
| 36 |
#include <wx/sizer.h> |
| 37 |
#include <wx/toolbar.h> |
| 38 |
|
| 39 |
class wxWindow; |
| 40 |
|
| 41 |
using AtlasMessage::Shareable; |
| 42 |
|
| 43 |
class LightSphere : public wxControl |
| 44 |
{ |
| 45 |
public: |
| 46 |
|
| 47 |
LightSphere(wxWindow* parent, const wxSize& size, LightControl* lightControl) |
| 48 |
: wxControl(parent, wxID_ANY, wxDefaultPosition, size), |
| 49 |
m_LightControl(lightControl) |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
void OnPaint(wxPaintEvent& WXUNUSED(event)) |
| 54 |
{ |
| 55 |
// Draw a lit 3D sphere: |
| 56 |
|
| 57 |
int w = GetClientSize().GetWidth(); |
| 58 |
int h = GetClientSize().GetHeight(); |
| 59 |
|
| 60 |
float lx = sin(-theta)*cos(phi); |
| 61 |
float ly = cos(-theta)*cos(phi); |
| 62 |
float lz = sin(phi); |
| 63 |
|
| 64 |
wxImage img (w, h); |
| 65 |
unsigned char* imgData = img.GetData(); |
| 66 |
|
| 67 |
for (int y = 0; y < h; ++y) |
| 68 |
{ |
| 69 |
for (int x = 0; x < w; ++x) |
| 70 |
{ |
| 71 |
// Calculate normal vector of sphere (radius 1) |
| 72 |
float nx = 2*x / (float)(w-1) - 1; // [-1, 1] |
| 73 |
float ny = 2*y / (float)(h-1) - 1; |
| 74 |
float nz2 = 1 - nx*nx - ny*ny; |
| 75 |
if (nz2 >= 0.f) |
| 76 |
{ |
| 77 |
float nz = sqrt(nz2); |
| 78 |
|
| 79 |
// Get reflection vector (r) of camera vector (c) in n |
| 80 |
float cx = 0; |
| 81 |
float cy = 0; |
| 82 |
float cz = 1; // (camera is infinitely far upwards) |
| 83 |
float ndotc = nz; |
| 84 |
float rx = -cx + nx*(2*ndotc); |
| 85 |
float ry = -cy + ny*(2*ndotc); |
| 86 |
float rz = -cz + nz*(2*ndotc); |
| 87 |
|
| 88 |
float ndotl = nx*lx + ny*ly + nz*lz; |
| 89 |
float rdotl = rx*lx + ry*ly + rz*lz; |
| 90 |
|
| 91 |
int diffuse = (int)std::max(0.f, ndotl*128.f); |
| 92 |
int specular = (int)std::min(255.f, 64.f*powf(std::max(0.f, rdotl), 16.f)); |
| 93 |
|
| 94 |
imgData[0] = std::min(64+diffuse+specular, 255); |
| 95 |
imgData[1] = std::min(48+diffuse+specular, 255); |
| 96 |
imgData[2] = std::min(48+diffuse+specular, 255); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
imgData[0] = 0; |
| 101 |
imgData[1] = 0; |
| 102 |
imgData[2] = 0; |
| 103 |
} |
| 104 |
imgData += 3; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
wxPaintDC dc(this); |
| 109 |
#ifdef __WXMSW__ |
| 110 |
dc.DrawBitmap(wxBitmap(img, dc), 0, 0); // TODO: is this any better than the version below? |
| 111 |
#else |
| 112 |
dc.DrawBitmap(wxBitmap(img), 0, 0); |
| 113 |
#endif |
| 114 |
} |
| 115 |
|
| 116 |
void OnMouse(wxMouseEvent& event) |
| 117 |
{ |
| 118 |
if (event.LeftIsDown()) |
| 119 |
{ |
| 120 |
int x = event.GetX(); |
| 121 |
int y = event.GetY(); |
| 122 |
int w = GetClientSize().GetWidth(); |
| 123 |
int h = GetClientSize().GetHeight(); |
| 124 |
|
| 125 |
float mx = 2*x / (float)(w-1) - 1; // [-1, 1] |
| 126 |
float my = 2*y / (float)(h-1) - 1; |
| 127 |
float mz2 = 1 - mx*mx - my*my; |
| 128 |
if (mz2 >= 0.f) |
| 129 |
{ |
| 130 |
//float mz = sqrt(mz2); |
| 131 |
//phi = asin(mz); |
| 132 |
|
| 133 |
// ^^ That's giving the height of the sphere at that point, so it's |
| 134 |
// matching the point the user clicked on - but that's quite inconvenient |
| 135 |
// when you want to move the sun near the horizon. So just make up |
| 136 |
// some formula that gives a slightly nicer-feeling result: |
| 137 |
phi = asin(mz2*mz2); |
| 138 |
|
| 139 |
theta = -atan2(mx, my); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
theta = -atan2(mx, my); |
| 144 |
phi = 0; |
| 145 |
} |
| 146 |
|
| 147 |
Refresh(false); |
| 148 |
m_LightControl->NotifyOtherObservers(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
float theta, phi; |
| 153 |
LightControl* m_LightControl; |
| 154 |
|
| 155 |
DECLARE_EVENT_TABLE(); |
| 156 |
}; |
| 157 |
|
| 158 |
BEGIN_EVENT_TABLE(LightSphere, wxControl) |
| 159 |
EVT_PAINT(LightSphere::OnPaint) |
| 160 |
EVT_MOTION(LightSphere::OnMouse) |
| 161 |
EVT_LEFT_DOWN(LightSphere::OnMouse) |
| 162 |
END_EVENT_TABLE() |
| 163 |
|
| 164 |
LightControl::LightControl(wxWindow* parent, const wxSize& size, Observable<AtlasMessage::sEnvironmentSettings>& environment) |
| 165 |
: wxPanel(parent), m_Environment(environment) |
| 166 |
{ |
| 167 |
wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); |
| 168 |
sizer->SetMinSize(size); |
| 169 |
m_Sphere = new LightSphere(this, size, this); |
| 170 |
m_Sphere->theta = environment.sunrotation; |
| 171 |
m_Sphere->phi = environment.sunelevation; |
| 172 |
sizer->Add(m_Sphere, wxSizerFlags().Expand().Proportion(1)); |
| 173 |
SetSizer(sizer); |
| 174 |
|
| 175 |
m_Conn = environment.RegisterObserver(0, &LightControl::OnSettingsChange, this); |
| 176 |
} |
| 177 |
|
| 178 |
void LightControl::OnSettingsChange(const AtlasMessage::sEnvironmentSettings& settings) |
| 179 |
{ |
| 180 |
m_Sphere->theta = settings.sunrotation; |
| 181 |
m_Sphere->phi = settings.sunelevation; |
| 182 |
m_Sphere->Refresh(false); |
| 183 |
} |
| 184 |
|
| 185 |
void LightControl::NotifyOtherObservers() |
| 186 |
{ |
| 187 |
m_Environment.sunrotation = m_Sphere->theta; |
| 188 |
m_Environment.sunelevation = m_Sphere->phi; |
| 189 |
m_Environment.NotifyObserversExcept(m_Conn); |
| 190 |
} |