OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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
 

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 ( )

Definition at line 88 of file C4FoWAmbient.cpp.

88  :
89 #ifndef USE_CONSOLE
90  Tex(0),
91 #endif
92  Resolution(0.), Radius(0.), FullCoverage(0.),
93  SizeX(0), LandscapeX(0), SizeY(0), LandscapeY(0),
94  Brightness(1.)
95 {
96 }
C4FoWAmbient::~C4FoWAmbient ( )

Definition at line 98 of file C4FoWAmbient.cpp.

References Clear().

99 {
100  Clear();
101 }

Here is the call graph for this function:

Member Function Documentation

void C4FoWAmbient::Clear ( )

Definition at line 103 of file C4FoWAmbient.cpp.

References Tex.

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

104 {
105 #ifndef USE_CONSOLE
106  if(Tex != 0) glDeleteTextures(1, &Tex);
107  Tex = 0;
108 #endif
109  Resolution = Radius = FullCoverage = 0.;
110  SizeX = SizeY = 0;
111  LandscapeX = LandscapeY = 0;
112  Brightness = 1.;
113 }

Here is the caller graph for this function:

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

Definition at line 115 of file C4FoWAmbient.cpp.

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

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

Here is the call graph for this function:

double C4FoWAmbient::GetBrightness ( ) const
inline

Definition at line 56 of file C4FoWAmbient.h.

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

56 { return Brightness; }

Here is the caller graph for this function:

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

Definition at line 207 of file C4FoWAmbient.cpp.

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().

208 {
209  C4FragTransform trans;
210  // Invert Y coordinate
211  trans.Scale(1, -1);
212  trans.Translate(0, outRect.Hgt);
213  // Clip offset
214  trans.Translate(-clipRect.x, -clipRect.y);
215  // Clip normalization (0,0 -> 1,1)
216  trans.Scale(1.0f / clipRect.Wdt, 1.0f / clipRect.Hgt);
217  // Viewport normalization
218  trans.Scale(vpRect.right - vpRect.left, vpRect.bottom - vpRect.top);
219  // Viewport offset
220  trans.Translate(vpRect.left, vpRect.top);
221  // Landscape normalization
222  trans.Scale(1.0f / LandscapeX, 1.0f / LandscapeY);
223 
224  // Extract matrix
225  trans.Get2x3(ambientTransform);
226 }
void Translate(float dx, float dy)
Definition: C4FoW.h:38
float bottom
Definition: C4Rect.h:27
float right
Definition: C4Rect.h:27
float left
Definition: C4Rect.h:27
int32_t Wdt
Definition: C4Rect.h:32
void Scale(float sx, float sy)
Definition: C4FoW.h:45
int32_t y
Definition: C4Rect.h:32
int32_t x
Definition: C4Rect.h:32
void Get2x3(float transform[6])
Definition: C4FoW.h:54
int32_t Hgt
Definition: C4Rect.h:32
float top
Definition: C4Rect.h:27

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int C4FoWAmbient::GetLandscapeHeight ( ) const
inline

Definition at line 73 of file C4FoWAmbient.h.

73 { return LandscapeY; }
unsigned int C4FoWAmbient::GetLandscapeWidth ( ) const
inline

Definition at line 72 of file C4FoWAmbient.h.

72 { return LandscapeX; }
void C4FoWAmbient::SetBrightness ( double  brightness)
inline

Definition at line 55 of file C4FoWAmbient.h.

55 { Brightness = brightness; }
void C4FoWAmbient::UpdateFromLandscape ( const C4Landscape landscape,
const C4Rect update 
)

Definition at line 150 of file C4FoWAmbient.cpp.

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

Referenced by CreateFromLandscape().

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

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

GLuint C4FoWAmbient::Tex

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