2026-06-11 08:53:41 -07:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-06-20 09:13:29 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-06-20 09:13:29 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-06-20 09:13:29 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-20 09:13:29 -07:00
|
|
|
*/
|
|
|
|
|
|
2007-06-05 11:35:05 -07:00
|
|
|
#include "precompiled.h"
|
2006-07-12 07:49:10 -07:00
|
|
|
|
2025-08-09 03:01:01 -07:00
|
|
|
#include "LightControl.h"
|
|
|
|
|
|
|
|
|
|
#include "tools/atlas/AtlasUI/General/Observable.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/Messages.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/Shareable.h"
|
|
|
|
|
|
2006-11-11 20:02:36 -08:00
|
|
|
#include <algorithm>
|
2025-08-09 03:01:01 -07:00
|
|
|
#include <cmath>
|
|
|
|
|
#include <wx/bitmap.h>
|
|
|
|
|
#include <wx/control.h>
|
|
|
|
|
#include <wx/dcclient.h>
|
|
|
|
|
#include <wx/event.h>
|
|
|
|
|
#include <wx/gdicmn.h>
|
|
|
|
|
#include <wx/image.h>
|
|
|
|
|
#include <wx/sizer.h>
|
|
|
|
|
#include <wx/toolbar.h>
|
2006-11-11 20:02:36 -08:00
|
|
|
|
2025-08-09 03:01:01 -07:00
|
|
|
class wxWindow;
|
2006-07-12 07:49:10 -07:00
|
|
|
|
|
|
|
|
using AtlasMessage::Shareable;
|
|
|
|
|
|
|
|
|
|
class LightSphere : public wxControl
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 10:36:42 -07:00
|
|
|
LightSphere(wxWindow* parent, const wxSize& size, LightControl* lightControl)
|
|
|
|
|
: wxControl(parent, wxID_ANY, wxDefaultPosition, size),
|
2006-07-12 07:49:10 -07:00
|
|
|
m_LightControl(lightControl)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 10:36:42 -07:00
|
|
|
void OnPaint(wxPaintEvent& WXUNUSED(event))
|
2006-07-12 07:49:10 -07:00
|
|
|
{
|
|
|
|
|
// Draw a lit 3D sphere:
|
|
|
|
|
|
|
|
|
|
int w = GetClientSize().GetWidth();
|
|
|
|
|
int h = GetClientSize().GetHeight();
|
|
|
|
|
|
|
|
|
|
float lx = sin(-theta)*cos(phi);
|
|
|
|
|
float ly = cos(-theta)*cos(phi);
|
|
|
|
|
float lz = sin(phi);
|
|
|
|
|
|
|
|
|
|
wxImage img (w, h);
|
|
|
|
|
unsigned char* imgData = img.GetData();
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < w; ++x)
|
|
|
|
|
{
|
|
|
|
|
// Calculate normal vector of sphere (radius 1)
|
|
|
|
|
float nx = 2*x / (float)(w-1) - 1; // [-1, 1]
|
|
|
|
|
float ny = 2*y / (float)(h-1) - 1;
|
|
|
|
|
float nz2 = 1 - nx*nx - ny*ny;
|
|
|
|
|
if (nz2 >= 0.f)
|
|
|
|
|
{
|
|
|
|
|
float nz = sqrt(nz2);
|
|
|
|
|
|
|
|
|
|
// Get reflection vector (r) of camera vector (c) in n
|
|
|
|
|
float cx = 0;
|
|
|
|
|
float cy = 0;
|
|
|
|
|
float cz = 1; // (camera is infinitely far upwards)
|
|
|
|
|
float ndotc = nz;
|
|
|
|
|
float rx = -cx + nx*(2*ndotc);
|
|
|
|
|
float ry = -cy + ny*(2*ndotc);
|
|
|
|
|
float rz = -cz + nz*(2*ndotc);
|
|
|
|
|
|
|
|
|
|
float ndotl = nx*lx + ny*ly + nz*lz;
|
|
|
|
|
float rdotl = rx*lx + ry*ly + rz*lz;
|
|
|
|
|
|
|
|
|
|
int diffuse = (int)std::max(0.f, ndotl*128.f);
|
2006-11-29 15:37:10 -08:00
|
|
|
int specular = (int)std::min(255.f, 64.f*powf(std::max(0.f, rdotl), 16.f));
|
2006-07-12 07:49:10 -07:00
|
|
|
|
|
|
|
|
imgData[0] = std::min(64+diffuse+specular, 255);
|
|
|
|
|
imgData[1] = std::min(48+diffuse+specular, 255);
|
|
|
|
|
imgData[2] = std::min(48+diffuse+specular, 255);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
imgData[0] = 0;
|
|
|
|
|
imgData[1] = 0;
|
|
|
|
|
imgData[2] = 0;
|
|
|
|
|
}
|
|
|
|
|
imgData += 3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wxPaintDC dc(this);
|
2006-12-15 17:01:15 -08:00
|
|
|
#ifdef __WXMSW__
|
|
|
|
|
dc.DrawBitmap(wxBitmap(img, dc), 0, 0); // TODO: is this any better than the version below?
|
2006-11-11 20:02:36 -08:00
|
|
|
#else
|
|
|
|
|
dc.DrawBitmap(wxBitmap(img), 0, 0);
|
|
|
|
|
#endif
|
2006-07-12 07:49:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnMouse(wxMouseEvent& event)
|
|
|
|
|
{
|
|
|
|
|
if (event.LeftIsDown())
|
|
|
|
|
{
|
|
|
|
|
int x = event.GetX();
|
|
|
|
|
int y = event.GetY();
|
|
|
|
|
int w = GetClientSize().GetWidth();
|
|
|
|
|
int h = GetClientSize().GetHeight();
|
|
|
|
|
|
|
|
|
|
float mx = 2*x / (float)(w-1) - 1; // [-1, 1]
|
|
|
|
|
float my = 2*y / (float)(h-1) - 1;
|
|
|
|
|
float mz2 = 1 - mx*mx - my*my;
|
|
|
|
|
if (mz2 >= 0.f)
|
|
|
|
|
{
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 10:36:42 -07:00
|
|
|
//float mz = sqrt(mz2);
|
|
|
|
|
//phi = asin(mz);
|
|
|
|
|
|
|
|
|
|
// ^^ That's giving the height of the sphere at that point, so it's
|
|
|
|
|
// matching the point the user clicked on - but that's quite inconvenient
|
|
|
|
|
// when you want to move the sun near the horizon. So just make up
|
|
|
|
|
// some formula that gives a slightly nicer-feeling result:
|
|
|
|
|
phi = asin(mz2*mz2);
|
|
|
|
|
|
2006-07-12 07:49:10 -07:00
|
|
|
theta = -atan2(mx, my);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
theta = -atan2(mx, my);
|
|
|
|
|
phi = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Refresh(false);
|
|
|
|
|
m_LightControl->NotifyOtherObservers();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float theta, phi;
|
|
|
|
|
LightControl* m_LightControl;
|
|
|
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(LightSphere, wxControl)
|
|
|
|
|
EVT_PAINT(LightSphere::OnPaint)
|
|
|
|
|
EVT_MOTION(LightSphere::OnMouse)
|
|
|
|
|
EVT_LEFT_DOWN(LightSphere::OnMouse)
|
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 10:36:42 -07:00
|
|
|
LightControl::LightControl(wxWindow* parent, const wxSize& size, Observable<AtlasMessage::sEnvironmentSettings>& environment)
|
2006-07-12 07:49:10 -07:00
|
|
|
: wxPanel(parent), m_Environment(environment)
|
|
|
|
|
{
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 10:36:42 -07:00
|
|
|
wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
|
sizer->SetMinSize(size);
|
|
|
|
|
m_Sphere = new LightSphere(this, size, this);
|
2006-07-12 07:49:10 -07:00
|
|
|
m_Sphere->theta = environment.sunrotation;
|
|
|
|
|
m_Sphere->phi = environment.sunelevation;
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 10:36:42 -07:00
|
|
|
sizer->Add(m_Sphere, wxSizerFlags().Expand().Proportion(1));
|
|
|
|
|
SetSizer(sizer);
|
2006-07-12 07:49:10 -07:00
|
|
|
|
|
|
|
|
m_Conn = environment.RegisterObserver(0, &LightControl::OnSettingsChange, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LightControl::OnSettingsChange(const AtlasMessage::sEnvironmentSettings& settings)
|
|
|
|
|
{
|
|
|
|
|
m_Sphere->theta = settings.sunrotation;
|
|
|
|
|
m_Sphere->phi = settings.sunelevation;
|
|
|
|
|
m_Sphere->Refresh(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LightControl::NotifyOtherObservers()
|
|
|
|
|
{
|
|
|
|
|
m_Environment.sunrotation = m_Sphere->theta;
|
|
|
|
|
m_Environment.sunelevation = m_Sphere->phi;
|
|
|
|
|
m_Environment.NotifyObserversExcept(m_Conn);
|
|
|
|
|
}
|