OpenClonk
C4FoWAmbient Class Reference

#include <C4FoWAmbient.h>

Public Member Functions

 C4FoWAmbient ()
 
 ~C4FoWAmbient ()
 
void Clear ()
 
void SetBrightness (double brightness)
 
double GetBrightness () const
 
void CreateFromLandscape (const C4Landscape &landscape, double resolution, double radius, double full_coverage)
 
void UpdateFromLandscape (const C4Landscape &landscape, const C4Rect &update)
 
void GetFragTransform (const struct FLOAT_RECT &vpRect, const C4Rect &clipRect, const C4Rect &outRect, float ambientTransform[6]) const
 
unsigned int GetLandscapeWidth () const
 
unsigned int GetLandscapeHeight () const
 

Public Attributes

GLuint Tex {0}
 

Detailed Description

This class manages a texture that holds the ambient light intensity

Definition at line 29 of file C4FoWAmbient.h.

Constructor & Destructor Documentation

◆ C4FoWAmbient()

C4FoWAmbient::C4FoWAmbient ( )
default

◆ ~C4FoWAmbient()

C4FoWAmbient::~C4FoWAmbient ( )

Definition at line 90 of file C4FoWAmbient.cpp.

91 {
92  Clear();
93 }

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ Clear()

void C4FoWAmbient::Clear ( )

Definition at line 95 of file C4FoWAmbient.cpp.

96 {
97 #ifndef USE_CONSOLE
98  if(Tex != 0) glDeleteTextures(1, &Tex);
99  Tex = 0;
100 #endif
101  Resolution = Radius = FullCoverage = 0.;
102  SizeX = SizeY = 0;
103  LandscapeX = LandscapeY = 0;
104  Brightness = 1.;
105 }

References Tex.

Referenced by CreateFromLandscape(), and ~C4FoWAmbient().

Here is the caller graph for this function:

◆ CreateFromLandscape()

void C4FoWAmbient::CreateFromLandscape ( const C4Landscape landscape,
double  resolution,
double  radius,
double  full_coverage 
)

Definition at line 107 of file C4FoWAmbient.cpp.

108 {
109  assert(resolution >= 1.);
110  assert(radius >= 1.);
111  assert(full_coverage > 0 && full_coverage <= 1.);
112 
113  // Clear old map
114  Clear();
115 
116  Resolution = resolution;
117  Radius = radius;
118  FullCoverage = full_coverage;
119 
120  // Number of zoomed pixels
121  LandscapeX = Landscape.GetWidth();
122  LandscapeY = Landscape.GetHeight();
123  SizeX = std::min<unsigned int>(static_cast<unsigned int>(ceil(LandscapeX / resolution)), pDraw->MaxTexSize);
124  SizeY = std::min<unsigned int>(static_cast<unsigned int>(ceil(LandscapeY / resolution)), pDraw->MaxTexSize);
125 
126 #ifndef USE_CONSOLE
127  glGenTextures(1, &Tex);
128  glBindTexture(GL_TEXTURE_2D, Tex);
129  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
130  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
131  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
132  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
133  glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, SizeX, SizeY, 0, GL_RED, GL_FLOAT, nullptr);
134 
137  uint32_t dt = C4TimeMilliseconds::Now() - begin;
138  LogF("Created %ux%u ambient map in %g secs", SizeX, SizeY, dt / 1000.);
139 #endif
140 }
C4Draw * pDraw
Definition: C4Draw.cpp:42
C4Landscape Landscape
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:262
int MaxTexSize
Definition: C4Draw.h:99
void UpdateFromLandscape(const C4Landscape &landscape, const C4Rect &update)
int32_t GetWidth() const
int32_t GetHeight() const
Definition: C4Rect.h:28
static C4TimeMilliseconds Now()

References Clear(), C4Landscape::GetHeight(), C4Landscape::GetWidth(), Landscape, LogF(), C4Draw::MaxTexSize, C4TimeMilliseconds::Now(), pDraw, Tex, and UpdateFromLandscape().

Here is the call graph for this function:

◆ GetBrightness()

double C4FoWAmbient::GetBrightness ( ) const
inline

Definition at line 56 of file C4FoWAmbient.h.

56 { return Brightness; }

Referenced by C4LandscapeRenderGL::Draw(), and CStdGL::SetupMultiBlt().

Here is the caller graph for this function:

◆ GetFragTransform()

void C4FoWAmbient::GetFragTransform ( const struct FLOAT_RECT vpRect,
const C4Rect clipRect,
const C4Rect outRect,
float  ambientTransform[6] 
) const

Definition at line 199 of file C4FoWAmbient.cpp.

200 {
201  C4FragTransform trans;
202  // Invert Y coordinate
203  trans.Scale(1, -1);
204  trans.Translate(0, outRect.Hgt);
205  // Clip offset
206  trans.Translate(-clipRect.x, -clipRect.y);
207  // Clip normalization (0,0 -> 1,1)
208  trans.Scale(1.0f / clipRect.Wdt, 1.0f / clipRect.Hgt);
209  // Viewport normalization
210  trans.Scale(vpRect.right - vpRect.left, vpRect.bottom - vpRect.top);
211  // Viewport offset
212  trans.Translate(vpRect.left, vpRect.top);
213  // Landscape normalization
214  trans.Scale(1.0f / LandscapeX, 1.0f / LandscapeY);
215 
216  // Extract matrix
217  trans.Get2x3(ambientTransform);
218 }
float bottom
Definition: C4Rect.h:25
float top
Definition: C4Rect.h:25
float right
Definition: C4Rect.h:25
float left
Definition: C4Rect.h:25
void Scale(float sx, float sy)
Definition: C4FoW.h:45
void Get2x3(float transform[6])
Definition: C4FoW.h:54
void Translate(float dx, float dy)
Definition: C4FoW.h:38
int32_t y
Definition: C4Rect.h:30
int32_t Hgt
Definition: C4Rect.h:30
int32_t Wdt
Definition: C4Rect.h:30
int32_t x
Definition: C4Rect.h:30

References FLOAT_RECT::bottom, C4FragTransform::Get2x3(), C4Rect::Hgt, FLOAT_RECT::left, FLOAT_RECT::right, C4FragTransform::Scale(), FLOAT_RECT::top, C4FragTransform::Translate(), C4Rect::Wdt, C4Rect::x, and C4Rect::y.

Referenced by C4LandscapeRenderGL::Draw(), and CStdGL::SetupMultiBlt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLandscapeHeight()

unsigned int C4FoWAmbient::GetLandscapeHeight ( ) const
inline

Definition at line 73 of file C4FoWAmbient.h.

73 { return LandscapeY; }

◆ GetLandscapeWidth()

unsigned int C4FoWAmbient::GetLandscapeWidth ( ) const
inline

Definition at line 72 of file C4FoWAmbient.h.

72 { return LandscapeX; }

◆ SetBrightness()

void C4FoWAmbient::SetBrightness ( double  brightness)
inline

Definition at line 55 of file C4FoWAmbient.h.

55 { Brightness = brightness; }

◆ UpdateFromLandscape()

void C4FoWAmbient::UpdateFromLandscape ( const C4Landscape landscape,
const C4Rect update 
)

Definition at line 142 of file C4FoWAmbient.cpp.

143 {
144 #ifndef USE_CONSOLE
145  // Nothing to do?
146  if(update.Wdt == 0 || update.Hgt == 0) return;
147 
148  assert(Tex != 0);
149 
150  // Factor to go from zoomed to landscape coordinates
151  const double zoom_x = static_cast<double>(Landscape.GetWidth()) / SizeX;
152  const double zoom_y = static_cast<double>(Landscape.GetHeight()) / SizeY;
153  // Update region in zoomed coordinates
154  const unsigned int left = std::max(static_cast<int>( (update.x - Radius) / zoom_x), 0);
155  const unsigned int right = std::min(static_cast<unsigned int>( (update.x + update.Wdt + Radius) / zoom_x), SizeX - 1) + 1;
156  const unsigned int top = std::max(static_cast<int>( (update.y - Radius) / zoom_y), 0);
157  const unsigned int bottom = std::min(static_cast<unsigned int>( (update.y + update.Hgt + Radius) / zoom_y), SizeY - 1) + 1;
158  assert(right > left);
159  assert(bottom > top);
160  // Zoomed radius
161  const double R = Radius / sqrt(zoom_x * zoom_y);
162  // Normalization factor with the full circle
163  // The analytic result is 2*R*M_PI, and this number is typically close to it
164  const double norm = AmbientForPix(0, 0, R, LightMapFull()) * FullCoverage;
165  // Create the ambient map
166  LightMapZoom light_mapZoom(landscape, zoom_x, zoom_y);
167  float* ambient = new float[(right - left) * (bottom - top)];
168  for(unsigned int y = top; y < bottom; ++y)
169  {
170  for(unsigned int x = left; x < right; ++x)
171  {
172  ambient[(y - top) * (right - left) + (x - left)] = std::min(AmbientForPix(x, y, R, light_mapZoom) / norm, 1.0);
173  }
174  }
175 
176 #if 0
177  CSurface8 debug(SizeX, SizeY);
178  for(unsigned int y = 0; y < SizeY; ++y)
179  {
180  for(unsigned int x = 0; x < SizeX; ++x)
181  {
182  debug.SetPix(x, y, int(ambient[y * SizeX + x] * 255. + 0.5));
183  }
184  }
185 
186  CStdPalette pal;
187  for(int i = 0; i < 256; ++i)
188  pal.Colors[i] = i + (i << 8) + (i << 16);
189  debug.Save("Ambient.bmp", &pal);
190 #endif
191 
192  // Update the texture
193  glBindTexture(GL_TEXTURE_2D, Tex);
194  glTexSubImage2D(GL_TEXTURE_2D, 0, left, top, (right - left), (bottom - top), GL_RED, GL_FLOAT, ambient);
195  delete[] ambient;
196 #endif
197 }
DWORD Colors[256]
Definition: StdColors.h:186

References C4ScriptGuiWindowPropertyName::bottom, CStdPalette::Colors, C4Landscape::GetHeight(), C4Landscape::GetWidth(), C4Rect::Hgt, Landscape, C4ScriptGuiWindowPropertyName::left, C4ScriptGuiWindowPropertyName::right, CSurface8::Save(), CSurface8::SetPix(), Tex, C4ScriptGuiWindowPropertyName::top, C4Rect::Wdt, C4Rect::x, and C4Rect::y.

Referenced by CreateFromLandscape().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ Tex

GLuint C4FoWAmbient::Tex {0}

The documentation for this class was generated from the following files: