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 {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 ( )
default
C4FoWAmbient::~C4FoWAmbient ( )

Definition at line 90 of file C4FoWAmbient.cpp.

References Clear().

91 {
92  Clear();
93 }

Here is the call graph for this function:

Member Function Documentation

void C4FoWAmbient::Clear ( )

Definition at line 95 of file C4FoWAmbient.cpp.

References Tex.

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

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 }

Here is the caller graph for this function:

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

Definition at line 107 of file C4FoWAmbient.cpp.

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

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 }
int MaxTexSize
Definition: C4Draw.h:99
void UpdateFromLandscape(const C4Landscape &landscape, const C4Rect &update)
Definition: C4Rect.h:27
C4Landscape Landscape
C4Draw * pDraw
Definition: C4Draw.cpp:42
int32_t GetHeight() const
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
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 199 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().

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 }
void Translate(float dx, float dy)
Definition: C4FoW.h:38
float bottom
Definition: C4Rect.h:25
float right
Definition: C4Rect.h:25
float left
Definition: C4Rect.h:25
int32_t Wdt
Definition: C4Rect.h:30
void Scale(float sx, float sy)
Definition: C4FoW.h:45
int32_t y
Definition: C4Rect.h:30
int32_t x
Definition: C4Rect.h:30
void Get2x3(float transform[6])
Definition: C4FoW.h:54
int32_t Hgt
Definition: C4Rect.h:30
float top
Definition: C4Rect.h:25

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

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:184
int32_t Wdt
Definition: C4Rect.h:30
int32_t y
Definition: C4Rect.h:30
C4Landscape Landscape
int32_t x
Definition: C4Rect.h:30
int32_t GetHeight() const
int32_t Hgt
Definition: C4Rect.h:30
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 {0}

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