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 () override
 
bool ReInit (int32_t iWidth, int32_t iHeight) override
 
bool Init (int32_t iWidth, int32_t iHeight, C4TextureMap *pMap, C4GroupSet *pGraphics) override
 
void Clear () override
 
C4Rect GetAffectedRect (C4Rect Rect) override
 
void Update (C4Rect Rect, C4Landscape *pSource) override
 
void Draw (const C4TargetFacet &cgo, const C4FoWRegion *Light, uint32_t clrMod) override
 
virtual void Draw (const C4TargetFacet &cgo, const class C4FoWRegion *Light, uint32_t clrMod)=0
 

Protected Attributes

int32_t iWidth {0}
 
int32_t iHeight {0}
 
C4TextureMappTexs {nullptr}
 

Detailed Description

Definition at line 106 of file C4LandscapeRender.h.

Constructor & Destructor Documentation

C4LandscapeRenderGL::C4LandscapeRenderGL ( )

Definition at line 51 of file C4LandscapeRender.cpp.

References ZeroMem().

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

Here is the call graph for this function:

C4LandscapeRenderGL::~C4LandscapeRenderGL ( )
override

Definition at line 60 of file C4LandscapeRender.cpp.

References Clear().

61 {
62  Clear();
63 }

Here is the call graph for this function:

Member Function Documentation

void C4LandscapeRenderGL::Clear ( )
overridevirtual

Implements C4LandscapeRender.

Definition at line 133 of file C4LandscapeRender.cpp.

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

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

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

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

Definition at line 930 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(), 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, C4Facet::Wdt, C4Rect::x, C4Facet::X, C4Rect::y, C4Facet::Y, and C4Draw::Zoom.

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

Implements C4LandscapeRender.

Definition at line 347 of file C4LandscapeRender.cpp.

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

348 {
350  return Rect;
351 }
const int C4LR_BiasDistanceX
const int C4LR_BiasDistanceY
void Enlarge(int32_t iByX, int32_t iByY)
Definition: C4Rect.h:51

Here is the call graph for this function:

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

Implements C4LandscapeRender.

Definition at line 65 of file C4LandscapeRender.cpp.

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

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

Here is the call graph for this function:

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

Implements C4LandscapeRender.

Definition at line 111 of file C4LandscapeRender.cpp.

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

112 {
113  // Safe info
114  this->iWidth = iWidth;
115  this->iHeight = iHeight;
116 
117  // Clear old landscape textures
118  for (auto & Surface : Surfaces)
119  {
120  delete Surface;
121  Surface = nullptr;
122  }
123 
124  // Allocate new landscape textures
125  if (!InitLandscapeTexture())
126  {
127  LogFatal("[!] Could not initialize landscape texture!");
128  return false;
129  }
130  return true;
131 }
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:227

Here is the call graph for this function:

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

Implements C4LandscapeRender.

Definition at line 353 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(), C4Rect::Hgt, C4LandscapeRender::iHeight, C4Rect::Intersect(), C4LandscapeRender::iWidth, RGBA(), C4Rect::Wdt, C4Rect::x, and C4Rect::y.

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

Here is the call graph for this function:

Member Data Documentation

int32_t C4LandscapeRender::iHeight {0}
protectedinherited

Definition at line 86 of file C4LandscapeRender.h.

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

int32_t C4LandscapeRender::iWidth {0}
protectedinherited

Definition at line 86 of file C4LandscapeRender.h.

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

C4TextureMap* C4LandscapeRender::pTexs {nullptr}
protectedinherited

Definition at line 87 of file C4LandscapeRender.h.

Referenced by Init().


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