OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4LandscapeRenderGL Class Referenceabstract

#include <C4LandscapeRender.h>

Inheritance diagram for C4LandscapeRenderGL:
[legend]
Collaboration diagram for C4LandscapeRenderGL:
[legend]

Public Member Functions

 C4LandscapeRenderGL ()
 
 ~C4LandscapeRenderGL ()
 
virtual bool ReInit (int32_t iWidth, int32_t iHeight)
 
virtual bool Init (int32_t iWidth, int32_t iHeight, C4TextureMap *pMap, C4GroupSet *pGraphics)
 
virtual void Clear ()
 
virtual C4Rect GetAffectedRect (C4Rect Rect)
 
virtual void Update (C4Rect Rect, C4Landscape *pSource)
 
virtual void Draw (const C4TargetFacet &cgo, const C4FoWRegion *Light, uint32_t clrMod)
 
virtual void Draw (const C4TargetFacet &cgo, const class C4FoWRegion *Light, uint32_t clrMod)=0
 

Protected Attributes

int32_t iWidth
 
int32_t iHeight
 
C4TextureMappTexs
 

Detailed Description

Definition at line 108 of file C4LandscapeRender.h.

Constructor & Destructor Documentation

C4LandscapeRenderGL::C4LandscapeRenderGL ( )

Definition at line 55 of file C4LandscapeRender.cpp.

References ZeroMem().

56 {
57  ZeroMem(Surfaces, sizeof(Surfaces));
58  hMaterialTexture = matMapTexture = 0;
59  hVBO = 0;
60  hVAOIDNoLight = 0;
61  hVAOIDLight = 0;
62 }
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
Definition: Standard.h:63

Here is the call graph for this function:

C4LandscapeRenderGL::~C4LandscapeRenderGL ( )

Definition at line 64 of file C4LandscapeRender.cpp.

References Clear().

65 {
66  Clear();
67 }

Here is the call graph for this function:

Member Function Documentation

void C4LandscapeRenderGL::Clear ( )
virtual

Implements C4LandscapeRender.

Definition at line 137 of file C4LandscapeRender.cpp.

References C4LR_SurfaceCount, CStdGL::FreeVAOID(), and pGL.

Referenced by Init(), and ~C4LandscapeRenderGL().

138 {
139  ClearShaders();
140 
141  // free textures
142  int i;
143  for (i = 0; i < C4LR_SurfaceCount; i++)
144  {
145  delete Surfaces[i];
146  Surfaces[i] = nullptr;
147  }
148  if (hMaterialTexture) glDeleteTextures(1, &hMaterialTexture);
149  hMaterialTexture = 0;
150  if (matMapTexture) glDeleteTextures(1, &matMapTexture);
151  matMapTexture = 0;
152 
153  if (hVBO != 0)
154  {
155  glDeleteBuffers(1, &hVBO);
156  hVBO = 0;
157  }
158 
159  if (hVAOIDLight != 0)
160  {
161  pGL->FreeVAOID(hVAOIDLight);
162  hVAOIDLight = 0;
163  }
164 
165  if (hVAOIDNoLight != 0)
166  {
167  pGL->FreeVAOID(hVAOIDNoLight);
168  hVAOIDNoLight = 0;
169  }
170 }
const int C4LR_SurfaceCount
void FreeVAOID(unsigned int vaoid)
Definition: C4DrawGL.cpp:976
CStdGL * pGL
Definition: C4DrawGL.cpp:914

Here is the call graph for this function:

Here is the caller graph for this function:

virtual void C4LandscapeRender::Draw ( const C4TargetFacet cgo,
const class C4FoWRegion Light,
uint32_t  clrMod 
)
pure virtualinherited
void C4LandscapeRenderGL::Draw ( const C4TargetFacet cgo,
const C4FoWRegion Light,
uint32_t  clrMod 
)
virtual

Definition at line 934 of file C4LandscapeRender.cpp.

References C4ShaderCall::AllocTexUnit(), C4FoW::Ambient, C4ScriptUniform::Apply(), C4Draw::ApplyZoom(), FLOAT_RECT::bottom, C4LR_SurfaceCount, C4LRA_LandscapeTexCoord, C4LRA_LightTexCoord, C4LRA_Position, C4LRU_AmbientBrightness, C4LRU_AmbientTex, C4LRU_AmbientTransform, C4LRU_Center, C4LRU_Gamma, C4LRU_LandscapeTex, C4LRU_LightTex, C4LRU_MaterialDepth, C4LRU_MaterialSize, C4LRU_MaterialTex, C4LRU_MatMapTex, C4LRU_Modulation, C4LRU_ProjectionMatrix, C4LRU_Resolution, C4LRU_ScalerTex, C4Game::C4S, C4ShaderCall::Finish(), Game, C4Draw::gammaOut, C4Shader::GetAttribute(), C4FoWAmbient::GetBrightness(), C4Draw::GetClipRect(), C4FoWRegion::getFoW(), C4FoWAmbient::GetFragTransform(), C4Draw::GetOutRect(), CStdGL::GetProjectionMatrix(), C4FoWRegion::getRegion(), C4FoWRegion::getSurfaceHeight(), C4FoWRegion::getSurfaceName(), C4FoWRegion::getSurfaceWidth(), CStdGL::GetVAO(), C4FoWRegion::getViewportRegion(), C4Shader::HaveUniform(), C4Surface::Hgt, C4Facet::Hgt, C4Shader::Initialised(), C4Scenario::Landscape, FLOAT_RECT::left, C4SLandscape::MaterialZoom, pDraw, pGL, CStdGL::PrepareRendering(), FLOAT_RECT::right, C4Draw::scriptUniform, C4ShaderCall::SetUniform1f(), C4ShaderCall::SetUniform1iv(), C4ShaderCall::SetUniform2f(), C4ShaderCall::SetUniform3fv(), C4ShaderCall::SetUniform4fv(), C4ShaderCall::SetUniformMatrix2x3fv(), C4ShaderCall::SetUniformMatrix4x4(), C4ShaderCall::Start(), C4Facet::Surface, C4TargetFacet::TargetX, C4TargetFacet::TargetY, C4FoWAmbient::Tex, C4Surface::texture, FLOAT_RECT::top, C4Surface::Wdt, C4Facet::Wdt, C4Rect::x, C4Facet::X, C4Rect::y, C4Facet::Y, and C4Draw::Zoom.

935 {
936  // Must have GL and be initialized
937  if(!pGL && !Shader.Initialised() && !ShaderLight.Initialised()) return;
938 
939  // prepare rendering to surface
940  C4Surface *sfcTarget = cgo.Surface;
941  if (!pGL->PrepareRendering(sfcTarget)) return;
942 
943  // Choose the right shader depending on whether we have dynamic lighting or not
944  const C4Shader* shader = &Shader;
945  if (Light) shader = &ShaderLight;
946  if (!shader->Initialised()) return;
947 
948  // Activate shader
949  C4ShaderCall ShaderCall(shader);
950  ShaderCall.Start();
951 
952  // Bind data
953  ShaderCall.SetUniformMatrix4x4(C4LRU_ProjectionMatrix, pGL->GetProjectionMatrix());
954  ShaderCall.SetUniform3fv(C4LRU_Gamma, 1, pGL->gammaOut);
955  ShaderCall.SetUniform2f(C4LRU_Resolution, Surfaces[0]->Wdt, Surfaces[0]->Hgt);
956  float centerX = float(cgo.TargetX)+float(cgo.Wdt)/2,
957  centerY = float(cgo.TargetY)+float(cgo.Hgt)/2;
958  ShaderCall.SetUniform2f(C4LRU_Center,
959  centerX / float(Surfaces[0]->Wdt),
960  centerY / float(Surfaces[0]->Hgt));
961  ShaderCall.SetUniform1f(C4LRU_MaterialDepth, float(iMaterialTextureDepth));
962  ShaderCall.SetUniform2f(C4LRU_MaterialSize,
963  float(iMaterialWidth) / ::Game.C4S.Landscape.MaterialZoom,
964  float(iMaterialHeight) / ::Game.C4S.Landscape.MaterialZoom);
965  const float fMod[4] = {
966  ((clrMod >> 16) & 0xff) / 255.0f,
967  ((clrMod >> 8) & 0xff) / 255.0f,
968  ((clrMod ) & 0xff) / 255.0f,
969  ((clrMod >> 24) & 0xff) / 255.0f
970  };
971  ShaderCall.SetUniform4fv(C4LRU_Modulation, 1, fMod);
972 
973  if (Light)
974  {
975  const FLOAT_RECT ViewportRect = Light->getViewportRegion();
976  const C4Rect ClipRect = pDraw->GetClipRect();
977  const C4Rect OutRect = pDraw->GetOutRect();
978  float ambientTransform[6];
979  Light->getFoW()->Ambient.GetFragTransform(ViewportRect, ClipRect, OutRect, ambientTransform);
980  ShaderCall.SetUniformMatrix2x3fv(C4LRU_AmbientTransform, 1, ambientTransform);
981  ShaderCall.SetUniform1f(C4LRU_AmbientBrightness, Light->getFoW()->Ambient.GetBrightness());
982  }
983 
984  pDraw->scriptUniform.Apply(ShaderCall);
985 
986  // Start binding textures
987  if(shader->HaveUniform(C4LRU_LandscapeTex))
988  {
989  GLint iLandscapeUnits[C4LR_SurfaceCount];
990  for(int i = 0; i < C4LR_SurfaceCount; i++)
991  {
992  iLandscapeUnits[i] = ShaderCall.AllocTexUnit(-1) - GL_TEXTURE0;
993  glBindTexture(GL_TEXTURE_2D, Surfaces[i]->texture->texName);
994  if (pGL->Zoom != 1.0)
995  {
996  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
997  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
998  }
999  else
1000  {
1001  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1002  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1003  }
1004  }
1005  ShaderCall.SetUniform1iv(C4LRU_LandscapeTex, C4LR_SurfaceCount, iLandscapeUnits);
1006  }
1007  if(Light && ShaderCall.AllocTexUnit(C4LRU_LightTex))
1008  {
1009  glBindTexture(GL_TEXTURE_2D, Light->getSurfaceName());
1010  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1011  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1012  }
1013  if(Light && ShaderCall.AllocTexUnit(C4LRU_AmbientTex))
1014  {
1015  glBindTexture(GL_TEXTURE_2D, Light->getFoW()->Ambient.Tex);
1016  }
1017  if(ShaderCall.AllocTexUnit(C4LRU_ScalerTex))
1018  {
1019  glBindTexture(GL_TEXTURE_2D, fctScaler.Surface->texture->texName);
1020  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1021  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1022  }
1023  if(ShaderCall.AllocTexUnit(C4LRU_MaterialTex))
1024  {
1025  glBindTexture(GL_TEXTURE_2D_ARRAY, hMaterialTexture);
1026  }
1027  if(ShaderCall.AllocTexUnit(C4LRU_MatMapTex))
1028  {
1029  uint32_t MatMap[2*256];
1030  BuildMatMap(MatMap);
1031  glBindTexture(GL_TEXTURE_1D, matMapTexture);
1032  glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 2*256, 0, GL_RGBA, GL_UNSIGNED_BYTE, MatMap);
1033  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1034  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1035  }
1036 
1037  // Calculate coordinates into landscape texture
1038  FLOAT_RECT fTexBlt;
1039  float fx = float(cgo.TargetX), fy = float(cgo.TargetY);
1040  fTexBlt.left = fx / Surfaces[0]->Wdt;
1041  fTexBlt.top = fy / Surfaces[0]->Hgt;
1042  fTexBlt.right = (fx + float(cgo.Wdt)) / Surfaces[0]->Wdt;
1043  fTexBlt.bottom= (fy + float(cgo.Hgt)) / Surfaces[0]->Hgt;
1044 
1045  // Calculate coordinates on screen (zoomed!)
1046  FLOAT_RECT tTexBlt;
1047  float tx = float(cgo.X), ty = float(cgo.Y);
1048  pGL->ApplyZoom(tx, ty);
1049  tTexBlt.left = tx;
1050  tTexBlt.top = ty;
1051  tTexBlt.right = tx + float(cgo.Wdt) * pGL->Zoom;
1052  tTexBlt.bottom= ty + float(cgo.Hgt) * pGL->Zoom;
1053 
1054  // Blend it
1055  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1056 
1057  // Prepare vertex data
1058  float vtxData[24];
1059  float* pos = &vtxData[0];
1060  float* tex = &vtxData[8];
1061  float* lightTex = &vtxData[16];
1062 
1063  pos[0] = tTexBlt.left;
1064  pos[1] = tTexBlt.top;
1065  pos[2] = tTexBlt.right;
1066  pos[3] = tTexBlt.top;
1067  pos[4] = tTexBlt.left;
1068  pos[5] = tTexBlt.bottom;
1069  pos[6] = tTexBlt.right;
1070  pos[7] = tTexBlt.bottom;
1071 
1072  tex[0] = fTexBlt.left;
1073  tex[1] = fTexBlt.top;
1074  tex[2] = fTexBlt.right;
1075  tex[3] = fTexBlt.top;
1076  tex[4] = fTexBlt.left;
1077  tex[5] = fTexBlt.bottom;
1078  tex[6] = fTexBlt.right;
1079  tex[7] = fTexBlt.bottom;
1080 
1081  unsigned int nFloats = 16;
1082  if (Light)
1083  {
1084  FLOAT_RECT lTexBlt;
1085  const C4Rect LightRect = Light->getRegion();
1086  int32_t iLightWdt = Light->getSurfaceWidth(),
1087  iLightHgt = Light->getSurfaceHeight();
1088  lTexBlt.left = (fx - LightRect.x) / iLightWdt;
1089  lTexBlt.top = 1.0 - (fy - LightRect.y) / iLightHgt;
1090  lTexBlt.right = (fx + cgo.Wdt - LightRect.x) / iLightWdt;
1091  lTexBlt.bottom = 1.0 - (fy + cgo.Hgt - LightRect.y) / iLightHgt;
1092 
1093  lightTex[0] = lTexBlt.left;
1094  lightTex[1] = lTexBlt.top;
1095  lightTex[2] = lTexBlt.right;
1096  lightTex[3] = lTexBlt.top;
1097  lightTex[4] = lTexBlt.left;
1098  lightTex[5] = lTexBlt.bottom;
1099  lightTex[6] = lTexBlt.right;
1100  lightTex[7] = lTexBlt.bottom;
1101  nFloats = 24;
1102  }
1103 
1104  // Upload vertex data
1105  glBindBuffer(GL_ARRAY_BUFFER, hVBO);
1106  glBufferSubData(GL_ARRAY_BUFFER, 0, nFloats * sizeof(float), vtxData);
1107 
1108  // Bind VAO
1109  unsigned int vaoid = Light ? hVAOIDLight : hVAOIDNoLight;
1110  GLuint vao;
1111  const bool has_vao = pGL->GetVAO(vaoid, vao);
1112  glBindVertexArray(vao);
1113  if (!has_vao)
1114  {
1115  // Setup state
1116  glEnableVertexAttribArray(shader->GetAttribute(C4LRA_Position));
1117  glEnableVertexAttribArray(shader->GetAttribute(C4LRA_LandscapeTexCoord));
1118  if (Light)
1119  glEnableVertexAttribArray(shader->GetAttribute(C4LRA_LightTexCoord));
1120 
1121  glVertexAttribPointer(shader->GetAttribute(C4LRA_Position), 2, GL_FLOAT, GL_FALSE, 0, 0);
1122  glVertexAttribPointer(shader->GetAttribute(C4LRA_LandscapeTexCoord), 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const uint8_t*>(8 * sizeof(float)));
1123  if (Light)
1124  glVertexAttribPointer(shader->GetAttribute(C4LRA_LightTexCoord), 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const uint8_t*>(16 * sizeof(float)));
1125  }
1126 
1127  // Do the blit
1128  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1129 
1130  // Reset state
1131  glBindVertexArray(0);
1132  glBindBuffer(GL_ARRAY_BUFFER, 0);
1133 
1134  ShaderCall.Finish();
1135 }
float Y
Definition: C4Facet.h:120
void ApplyZoom(float &X, float &Y)
Definition: C4Draw.cpp:787
const int C4LR_SurfaceCount
int Wdt
Definition: C4Surface.h:67
int32_t MaterialZoom
Definition: C4Scenario.h:185
C4Game Game
Definition: C4Globals.cpp:52
C4ScriptUniform scriptUniform
Definition: C4Draw.h:100
float bottom
Definition: C4Rect.h:27
C4Scenario C4S
Definition: C4Game.h:76
const FLOAT_RECT & getViewportRegion() const
Definition: C4FoWRegion.h:52
float right
Definition: C4Rect.h:27
float left
Definition: C4Rect.h:27
C4SLandscape Landscape
Definition: C4Scenario.h:234
float gammaOut[3]
Definition: C4Draw.h:98
double GetBrightness() const
Definition: C4FoWAmbient.h:56
void Apply(C4ShaderCall &call)
Definition: C4Shader.cpp:830
Definition: C4Rect.h:29
const C4FoW * getFoW() const
Definition: C4FoWRegion.h:50
int Hgt
Definition: C4Surface.h:67
int32_t y
Definition: C4Rect.h:32
int32_t getSurfaceHeight() const
GLuint getSurfaceName() const
C4Draw * pDraw
Definition: C4Draw.cpp:45
bool GetVAO(unsigned int vaoid, GLuint &vao)
Definition: C4DrawGL.cpp:1023
int32_t getSurfaceWidth() const
C4FoWAmbient Ambient
Definition: C4FoW.h:115
bool HaveUniform(int iUniform) const
Definition: C4Shader.h:123
GLint GetAttribute(int iAttribute) const
Definition: C4Shader.h:128
int32_t x
Definition: C4Rect.h:32
float Zoom
Definition: C4Draw.h:116
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:80
bool Initialised() const
Definition: C4Shader.h:107
float TargetX
Definition: C4Facet.h:167
float Hgt
Definition: C4Facet.h:120
virtual bool PrepareRendering(C4Surface *sfcToSurface) override
Definition: C4DrawGL.cpp:178
C4Rect GetOutRect() const
Definition: C4Draw.cpp:742
const C4Rect & getRegion() const
Definition: C4FoWRegion.h:51
CStdGL * pGL
Definition: C4DrawGL.cpp:914
C4Rect GetClipRect() const
Definition: C4Draw.cpp:733
const StdProjectionMatrix & GetProjectionMatrix() const
Definition: C4DrawGL.h:251
C4Surface * Surface
Definition: C4Facet.h:119
float TargetY
Definition: C4Facet.h:167
float Wdt
Definition: C4Facet.h:120
float X
Definition: C4Facet.h:120
float top
Definition: C4Rect.h:27
void GetFragTransform(const struct FLOAT_RECT &vpRect, const C4Rect &clipRect, const C4Rect &outRect, float ambientTransform[6]) const

Here is the call graph for this function:

C4Rect C4LandscapeRenderGL::GetAffectedRect ( C4Rect  Rect)
virtual

Implements C4LandscapeRender.

Definition at line 351 of file C4LandscapeRender.cpp.

References C4LR_BiasDistanceX, C4LR_BiasDistanceY, and C4Rect::Enlarge().

352 {
354  return Rect;
355 }
const int C4LR_BiasDistanceX
const int C4LR_BiasDistanceY
void Enlarge(int32_t iByX, int32_t iByY)
Definition: C4Rect.h:53

Here is the call graph for this function:

bool C4LandscapeRenderGL::Init ( int32_t  iWidth,
int32_t  iHeight,
C4TextureMap pMap,
C4GroupSet pGraphics 
)
virtual

Implements C4LandscapeRender.

Definition at line 69 of file C4LandscapeRender.cpp.

References Clear(), C4LandscapeRender::iHeight, C4LandscapeRender::iWidth, LogFatal(), and C4LandscapeRender::pTexs.

70 {
71  Clear();
72 
73  // Safe info
74  this->iWidth = iWidth;
75  this->iHeight = iHeight;
76  this->pTexs = pTexs;
77 
78  // Allocate landscape textures
79  if (!InitLandscapeTexture())
80  {
81  LogFatal("[!] Could not initialize landscape texture!");
82  return false;
83  }
84 
85  // Build texture, er, texture
86  if (!InitMaterialTexture(pTexs))
87  {
88  LogFatal("[!] Could not initialize landscape textures for rendering!");
89  return false;
90  }
91 
92  // Load sclaer
93  if (!LoadScaler(pGraphics))
94  {
95  LogFatal("[!] Could not load scaler!");
96  return false;
97  }
98 
99  // Load shader
100  if (!LoadShaders(pGraphics))
101  {
102  LogFatal("[!] Could not initialize landscape shader!");
103  return false;
104  }
105 
106  if (!InitVBO())
107  {
108  LogFatal("[!] Could not initialize landscape VBO!");
109  return false;
110  }
111 
112  return true;
113 }
C4TextureMap * pTexs
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:230

Here is the call graph for this function:

bool C4LandscapeRenderGL::ReInit ( int32_t  iWidth,
int32_t  iHeight 
)
virtual

Implements C4LandscapeRender.

Definition at line 115 of file C4LandscapeRender.cpp.

References C4LR_SurfaceCount, C4LandscapeRender::iHeight, C4LandscapeRender::iWidth, and LogFatal().

116 {
117  // Safe info
118  this->iWidth = iWidth;
119  this->iHeight = iHeight;
120 
121  // Clear old landscape textures
122  for (int i = 0; i < C4LR_SurfaceCount; i++)
123  {
124  delete Surfaces[i];
125  Surfaces[i] = nullptr;
126  }
127 
128  // Allocate new landscape textures
129  if (!InitLandscapeTexture())
130  {
131  LogFatal("[!] Could not initialize landscape texture!");
132  return false;
133  }
134  return true;
135 }
const int C4LR_SurfaceCount
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:230

Here is the call graph for this function:

void C4LandscapeRenderGL::Update ( C4Rect  Rect,
C4Landscape pSource 
)
virtual

Implements C4LandscapeRender.

Definition at line 357 of file C4LandscapeRender.cpp.

References C4Landscape::_GetPix(), C4Landscape::_GetPlacement(), C4LR_BiasDistanceX, C4LR_BiasDistanceY, C4LR_BiasX, C4LR_BiasY, C4LR_Material, C4LR_Place, C4LR_Scaler, C4LR_SurfaceCount, Clamp(), C4Surface::ClearBoxDw(), C4Rect::Hgt, C4LandscapeRender::iHeight, C4Rect::Intersect(), C4LandscapeRender::iWidth, RGBA(), C4Rect::Wdt, C4Rect::x, and C4Rect::y.

358 {
359  // clip to landscape size
360  To.Intersect(C4Rect(0,0,iWidth,iHeight));
361  // everything clipped?
362  if (To.Wdt<=0 || To.Hgt<=0) return;
363 
364  // Lock surfaces
365  // We clear the affected region here because ClearBoxDw allocates the
366  // main memory buffer for the box, so that only that box needs to be
367  // sent to the gpu, and not the whole texture, or every pixel
368  // separately. It's an important optimization.
369  for (int i = 0; i < C4LR_SurfaceCount; i++)
370  {
371  if (!Surfaces[i]->Lock()) return;
372  Surfaces[i]->ClearBoxDw(To.x, To.y, To.Wdt, To.Hgt);
373  }
374 
375  // Initialize up & down placement arrays. These arrays are always updated
376  // so that they contain the placement sums of C4LR_BiasDistanceY pixels
377  // above and below the current row.
378  int x, y;
379  int placementSumsWidth = C4LR_BiasDistanceX * 2 + To.Wdt;
380  int *placementSumsUp = new int [placementSumsWidth * 2];
381  int *placementSumsDown = placementSumsUp + placementSumsWidth;
382  for(x = 0; x < placementSumsWidth; x++)
383  {
384  placementSumsUp[x] = 0;
385  placementSumsDown[x] = 0;
386  if (To.x + x - C4LR_BiasDistanceX < 0 || To.x + x - C4LR_BiasDistanceX >= iWidth) continue;
387  for(y = 1; y <= std::min(C4LR_BiasDistanceY, To.y); y++)
388  placementSumsUp[x] += pSource->_GetPlacement(To.x+x-C4LR_BiasDistanceX, To.y-y);
389  for(y = 1; y <= std::min(C4LR_BiasDistanceY, iHeight - 1 - To.y); y++)
390  placementSumsDown[x] += pSource->_GetPlacement(To.x+x-C4LR_BiasDistanceX, To.y+y);
391  }
392 
393  // Get tex refs (shortcut, we will use them quite heavily)
394  C4TexRef *texture[C4LR_SurfaceCount];
395  x = y = 0;
396  for(int i = 0; i < C4LR_SurfaceCount; i++)
397  texture[i] = Surfaces[i]->texture.get();
398 
399  // Go through it from top to bottom
400  for(y = 0; y < To.Hgt; y++)
401  {
402  // Initialize left & right placement sums. These are meant to contain
403  // the placement sum of a (C4LR_BiasDistanceX, 2*C4LR_BiasDistanceY+1)
404  // rectangle left/right of the current pixel. So we initialise it to
405  // be correct at x=0. Note that the placementSum arrays don't contain
406  // information about the current row, therefore we need a special case
407  // for those pixels.
408  int sumLeft = 0, sumRight = 0;
409  for(x = 1; x <= std::min(C4LR_BiasDistanceX, To.x); x++)
410  sumLeft += pSource->_GetPlacement(To.x-x,To.y+y);
411  for(x = 1; x <= std::min(C4LR_BiasDistanceX, iWidth - 1 - To.x ); x++)
412  sumRight += pSource->_GetPlacement(To.x+x,To.y+y);
413  for (int i = 1; i <= C4LR_BiasDistanceX; i++) {
414  sumLeft += placementSumsUp[C4LR_BiasDistanceX - i];
415  sumLeft += placementSumsDown[C4LR_BiasDistanceX - i];
416  sumRight += placementSumsUp[C4LR_BiasDistanceX + i];
417  sumRight += placementSumsDown[C4LR_BiasDistanceX + i];
418  }
419 
420  // Initialise up & down sums. Same principle as above, but slightly
421  // easier as we do not miss pixels if we just use the placement sums.
422  int sumUp = 0, sumDown = 0;
423  for (int i = -C4LR_BiasDistanceX; i <= C4LR_BiasDistanceX; i++) {
424  sumUp += placementSumsUp[C4LR_BiasDistanceX + i];
425  sumDown += placementSumsDown[C4LR_BiasDistanceX + i];
426  }
427 
428  for(x = 0; x < To.Wdt; x++)
429  {
430  int pixel = pSource->_GetPix(To.x+x, To.y+y);
431  int placement = pSource->_GetPlacement(To.x+x, To.y+y);
432 
433  // Calculate bias. The scale here is the size of the rectangle (see above)
434  const int horizontalFactor = C4LR_BiasDistanceX * (2 * C4LR_BiasDistanceY + 1);
435  int horizontalBias = std::max(0, placement * horizontalFactor - sumRight) -
436  std::max(0, placement * horizontalFactor - sumLeft);
437  const int verticalFactor = C4LR_BiasDistanceY * (2 * C4LR_BiasDistanceX + 1);
438  int verticalBias = std::max(0, placement * verticalFactor - sumDown) -
439  std::max(0, placement * verticalFactor - sumUp);
440 
441  // Maximum placement differences that make a difference in the result, after which we are at the limits of
442  // what can be packed into a byte
443  const int maximumPlacementDifference = 40;
444  int horizontalBiasScaled = Clamp(horizontalBias * 127 / maximumPlacementDifference / horizontalFactor + 128, 0, 255);
445  int verticalBiasScaled = Clamp(verticalBias * 127 / maximumPlacementDifference / verticalFactor + 128, 0, 255);
446 
447  // Collect data to save per pixel
448  unsigned char data[C4LR_SurfaceCount * 4];
449  memset(data, 0, sizeof(data));
450 
451  data[C4LR_Material] = pixel;
452  data[C4LR_BiasX] = horizontalBiasScaled;
453  data[C4LR_BiasY] = verticalBiasScaled;
454  data[C4LR_Scaler] = CalculateScalerBitmask(x, y, To, pSource);
455  data[C4LR_Place] = placement;
456 
457  for(int i = 0; i < C4LR_SurfaceCount; i++)
458  texture[i]->SetPix(To.x+x, To.y+y,
459  RGBA(data[i*4+0], data[i*4+1], data[i*4+2], data[i*4+3]));
460 
461  // Update sums (last column would be out-of-bounds, and not
462  // necessary as we will re-initialise it for the next row)
463  if (x < To.Wdt - 1) {
464  sumLeft -= placementSumsUp[x] + placementSumsDown[x];
465  sumLeft += placementSumsUp[x + C4LR_BiasDistanceX] + placementSumsDown[x + C4LR_BiasDistanceX];
466  sumRight -= placementSumsUp[x + C4LR_BiasDistanceX + 1] + placementSumsDown[x + C4LR_BiasDistanceX + 1];
467  sumUp -= placementSumsUp[x];
468  sumDown -= placementSumsDown[x];
469  sumRight += placementSumsUp[x + 2 * C4LR_BiasDistanceX + 1] + placementSumsDown[x + 2 * C4LR_BiasDistanceX + 1];
470  sumUp += placementSumsUp[x + 2 * C4LR_BiasDistanceX + 1];
471  sumDown += placementSumsDown[x + 2 * C4LR_BiasDistanceX + 1];
472  }
473 
474  // Update left & right for next pixel in line
475  if(x + To.x + 1 < iWidth)
476  sumRight -= pSource->_GetPlacement(To.x+x + 1, To.y+y);
477  if(To.x+x + C4LR_BiasDistanceX + 1 < iWidth)
478  sumRight += pSource->_GetPlacement(To.x+x + C4LR_BiasDistanceX + 1, To.y+y);
479  sumLeft += placement;
480  if(To.x+x - C4LR_BiasDistanceX >= 0)
481  sumLeft -= pSource->_GetPlacement(To.x+x - C4LR_BiasDistanceX, To.y+y);
482 
483  // Update up & down arrays (for next line already)
484  if (To.x + x >= C4LR_BiasDistanceX) {
485  if (To.y + y + 1 < iHeight)
486  placementSumsDown[x] -= pSource->_GetPlacement(To.x + x - C4LR_BiasDistanceX, To.y + y + 1);
487  if (To.y + y + C4LR_BiasDistanceY + 1 < iHeight)
488  placementSumsDown[x] += pSource->_GetPlacement(To.x + x - C4LR_BiasDistanceX, To.y + y + C4LR_BiasDistanceY + 1);
489  if (To.y + y - C4LR_BiasDistanceY >= 0)
490  placementSumsUp[x] -= pSource->_GetPlacement(To.x + x - C4LR_BiasDistanceX, To.y + y - C4LR_BiasDistanceY);
491  placementSumsUp[x] += pSource->_GetPlacement(To.x + x - C4LR_BiasDistanceX, To.y + y);
492  }
493  }
494 
495  // Finish updating up & down arrays for the next line
496  if (To.x + x >= C4LR_BiasDistanceX)
497  {
498  for (; x < std::min(placementSumsWidth, iWidth - To.x + C4LR_BiasDistanceX); x++) {
499  if (To.y + y + 1 < iHeight)
500  placementSumsDown[x] -= pSource->_GetPlacement(To.x + x - C4LR_BiasDistanceX, To.y + y + 1);
501  if (To.y + y + C4LR_BiasDistanceY + 1 < iHeight)
502  placementSumsDown[x] += pSource->_GetPlacement(To.x + x - C4LR_BiasDistanceX, To.y + y + C4LR_BiasDistanceY + 1);
503  if (To.y + y - C4LR_BiasDistanceY >= 0)
504  placementSumsUp[x] -= pSource->_GetPlacement(To.x + x - C4LR_BiasDistanceX, To.y + y - C4LR_BiasDistanceY);
505  placementSumsUp[x] += pSource->_GetPlacement(To.x + x - C4LR_BiasDistanceX, To.y + y);
506  }
507  }
508  }
509 
510  // done
511  delete[] placementSumsUp;
512  for (int i = 0; i < C4LR_SurfaceCount; i++)
513  Surfaces[i]->Unlock();
514 }
BYTE _GetPix(int32_t x, int32_t y) const
const int C4LR_SurfaceCount
const int C4LR_BiasDistanceX
const int C4LR_BiasDistanceY
Definition: C4Rect.h:29
void ClearBoxDw(int iX, int iY, int iWdt, int iHgt)
Definition: C4Surface.cpp:626
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:46
int32_t _GetPlacement(int32_t x, int32_t y) const
uint32_t RGBA(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
Definition: StdColors.h:24

Here is the call graph for this function:

Member Data Documentation

int32_t C4LandscapeRender::iHeight
protectedinherited

Definition at line 88 of file C4LandscapeRender.h.

Referenced by Init(), ReInit(), and Update().

int32_t C4LandscapeRender::iWidth
protectedinherited

Definition at line 88 of file C4LandscapeRender.h.

Referenced by Init(), ReInit(), and Update().

C4TextureMap* C4LandscapeRender::pTexs
protectedinherited

Definition at line 89 of file C4LandscapeRender.h.

Referenced by Init().


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