OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4LandscapeRender.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2011-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include "C4Include.h"
19 
20 #include "c4group/C4Components.h"
21 #include "c4group/C4GroupSet.h"
22 #include "graphics/C4DrawGL.h"
23 #include "landscape/C4Landscape.h"
24 #include "landscape/C4Texture.h"
26 #include "lib/StdColors.h"
27 
28 #ifndef USE_CONSOLE
29 
30 // Automatically reload shaders when changed at runtime?
31 #define AUTO_RELOAD_SHADERS
32 
33 #ifdef _DEBUG
34 
35 // Generate seperator textures into 3D texture so we can make sure that
36 // we are addressing textures using the right coordinates
37 #define DEBUG_SEPERATOR_TEXTURES
38 
39 // Replace all textures by solid colors
40 //#define DEBUG_SOLID_COLOR_TEXTURES
41 
42 #endif
43 
44 // How much to look into each direction for bias
45 const int C4LR_BiasDistanceX = 7;
46 const int C4LR_BiasDistanceY = 7;
47 
48 // Name used for the seperator texture
49 const char *const SEPERATOR_TEXTURE = "--SEP--";
50 
52 {
53  ZeroMem(Surfaces, sizeof(Surfaces));
54  hMaterialTexture = matMapTexture = 0;
55  hVBO = 0;
56  hVAOIDNoLight = 0;
57  hVAOIDLight = 0;
58 }
59 
61 {
62  Clear();
63 }
64 
65 bool C4LandscapeRenderGL::Init(int32_t iWidth, int32_t iHeight, C4TextureMap *pTexs, C4GroupSet *pGraphics)
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 }
110 
111 bool C4LandscapeRenderGL::ReInit(int32_t iWidth, int32_t iHeight)
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 }
132 
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 }
167 
168 bool C4LandscapeRenderGL::InitLandscapeTexture()
169 {
170  // Don't round up to the nearest power-of-two here. Modern
171  // hardware should handle NPOT-textures just fine, and they
172  // are already used in other places in Clonk. This avoids
173  // accessing one row or column of pixels below the actually
174  // used surface in the shader, which can lead to strange
175  // one-pixel edges at the bottom or right of the landscape
176  // (see e.g. http://bugs.openclonk.org/view.php?id=771).
177  int iSfcWdt = iWidth;
178  int iSfcHgt = iHeight;
179 
180  // Create our surfaces
181  for(auto & Surface : Surfaces)
182  {
183  Surface = new C4Surface();
184  if(!Surface->Create(iSfcWdt, iSfcHgt))
185  return false;
186  }
187 
188  return true;
189 }
190 
191 bool C4LandscapeRenderGL::InitMaterialTexture(C4TextureMap *pTexs)
192 {
193 
194  // Populate our map with all needed textures
195  MaterialTextureMap.emplace_back("");
196  AddTexturesFromMap(pTexs);
197 
198  // Determine depth to use
199  iMaterialTextureDepth = 2*MaterialTextureMap.size();
200  int32_t iNormalDepth = iMaterialTextureDepth / 2;
201 
202  // Find the largest texture
203  C4Texture *pTex; C4Surface *pRefSfc = nullptr;
204  for(int iTexIx = 0; (pTex = pTexs->GetTexture(pTexs->GetTexture(iTexIx))); iTexIx++)
205  if(C4Surface *pSfc = pTex->Surface32)
206  if (!pRefSfc || pRefSfc->Wdt < pSfc->Wdt || pRefSfc->Hgt < pSfc->Hgt)
207  pRefSfc = pSfc;
208  if(!pRefSfc)
209  return false;
210 
211  // Get size for our textures. We might be limited by hardware
212  int iTexWdt = pRefSfc->Wdt, iTexHgt = pRefSfc->Hgt;
213  GLint iMaxTexSize, iMaxTexLayers;
214  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &iMaxTexSize);
215  glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &iMaxTexLayers);
216  if (iTexWdt > iMaxTexSize || iTexHgt > iMaxTexSize)
217  {
218  iTexWdt = std::min(iTexWdt, iMaxTexSize);
219  iTexHgt = std::min(iTexHgt, iMaxTexSize);
220  LogF(" gl: Material textures too large, GPU only supports %dx%d! Cropping might occur!", iMaxTexSize, iMaxTexSize);
221  }
222  if(iMaterialTextureDepth >= iMaxTexLayers)
223  {
224  LogF(" gl: Too many material textures! GPU only supports 3D texture depth of %d!", iMaxTexSize);
225  return false;
226  }
227  iMaterialWidth = iTexWdt;
228  iMaterialHeight = iTexHgt;
229 
230  // Compose together data of all textures
231  const int iTexSize = iTexWdt * iTexHgt * C4Draw::COLOR_DEPTH_BYTES;
232  const int iSize = iTexSize * iMaterialTextureDepth;
233  BYTE *pData = new BYTE [iSize];
234  for(int i = 0; i < iMaterialTextureDepth; i++)
235  {
236  BYTE *p = pData + i * iTexSize;
237  // Get texture at position
238  StdStrBuf Texture;
239  bool fNormal = i >= iNormalDepth;
240  if(i < int32_t(MaterialTextureMap.size()))
241  Texture.Ref(MaterialTextureMap[i]);
242  else if(fNormal && i < iNormalDepth + int32_t(MaterialTextureMap.size()))
243  Texture.Format("%s_NRM", MaterialTextureMap[i-iNormalDepth].getData());
244  // Try to find the texture
245  C4Texture *pTex; C4Surface *pSurface;
246  if((pTex = pTexs->GetTexture(Texture.getData())) && (pSurface = pTex->Surface32))
247  {
248 #ifdef DEBUG_SOLID_COLOR_TEXTURES
249  // Just write a solid color that depends on the texture index
250  DWORD *texdata = reinterpret_cast<DWORD *>(p);
251  for (int y = 0; y < iTexHgt; ++y)
252  for (int x = 0; x < iTexWdt; ++x)
253  *texdata++ = RGBA((iTex & 48), (iTex & 3) * 16, (i & 12) * 4, 255);
254  continue;
255 #else
256  // Size recheck. It's fine if this texture's size is a divisor
257  // of the maximum texture size, because then we can just tile
258  // the smaller texture.
259  if(pSurface->Wdt != iTexWdt || pSurface->Hgt != iTexHgt)
260  if (iTexWdt % pSurface->Wdt != 0 || iTexHgt % pSurface->Hgt != 0)
261  LogF(" gl: texture %s size mismatch (%dx%d vs %dx%d)!", Texture.getData(), pSurface->Wdt, pSurface->Hgt, iTexWdt, iTexHgt);
262 
263  // Copy bytes
264  DWORD *texdata = reinterpret_cast<DWORD *>(p);
265  pSurface->Lock();
266  for (int y = 0; y < iTexHgt; ++y)
267  for (int x = 0; x < iTexWdt; ++x)
268  *texdata++ = pSurface->GetPixDw(x % pSurface->Wdt, y % pSurface->Hgt, false);
269  pSurface->Unlock();
270  continue;
271 #endif
272  }
273  // Seperator texture?
274  if(SEqual(Texture.getData(), SEPERATOR_TEXTURE))
275  {
276  // Make some ugly stripes
277  DWORD *texdata = reinterpret_cast<DWORD *>(p);
278  for (int y = 0; y < iTexHgt; ++y)
279  for (int x = 0; x < iTexWdt; ++x)
280  *texdata++ = ((x + y) % 32 < 16 ? RGBA(255, 0, 0, 255) : RGBA(0, 255, 255, 255));
281  continue;
282  }
283  // If we didn't "continue" yet, we haven't written the texture yet.
284  // Make color texture transparent, and normal texture flat.
285  if (fNormal)
286  {
287  DWORD *texdata = reinterpret_cast<DWORD *>(p);
288  for (int y = 0; y < iTexHgt; ++y)
289  for (int x = 0; x < iTexWdt; ++x)
290  *texdata++ = RGBA(127, 127, 255, 255);
291  }
292  else
293  memset(p, 0, iTexSize);
294  }
295 
296  // Clear error error(s?)
297  while(glGetError()) {}
298 
299  // Alloc 1D matmap texture
300  glGenTextures(1, &matMapTexture);
301 
302  // Alloc 2D texture array
303  glGenTextures(1, &hMaterialTexture);
304 
305  // Generate textures
306  int iSizeSum = 0;
307 
308  // Select texture
309  glBindTexture(GL_TEXTURE_2D_ARRAY, hMaterialTexture);
310  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
311 
312  // We fully expect to tile these
313  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
314  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
315  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
316  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
317 
318  // Make it happen!
319  glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, iTexWdt, iTexHgt, iMaterialTextureDepth, 0, GL_BGRA,
320  GL_UNSIGNED_INT_8_8_8_8_REV,
321  pData);
322 
323  glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
324 
325  // Statistics
326  iSizeSum += iTexWdt * iTexHgt * iMaterialTextureDepth * C4Draw::COLOR_DEPTH_BYTES;
327 
328  // Dispose of data
329  delete [] pData;
330 
331  // Check whether we were successful
332  if(int err = glGetError())
333  {
334  LogF(" gl: Could not load textures (error %d)", err);
335  return false;
336  }
337 
338  // Announce the good news
339  LogF(" gl: Texturing uses %d slots at %dx%d (%d MB total)",
340  static_cast<int>(MaterialTextureMap.size()),
341  iMaterialWidth, iMaterialHeight,
342  iSizeSum / 1000000);
343 
344  return true;
345 }
346 
348 {
350  return Rect;
351 }
352 
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 }
511 
519 int C4LandscapeRenderGL::CalculateScalerBitmask(int x, int y, C4Rect To, C4Landscape *pSource)
520 {
521  int pixel = pSource->_GetPix(To.x+x, To.y+y);
522  int placement = pSource->_GetPlacement(To.x+x, To.y+y);
523 
524  int neighbours[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
525  if(To.y+y > 0)
526  {
527  if(To.x+x > 0)
528  neighbours[0] = pSource->_GetPix(To.x+x-1, To.y+y-1);
529  neighbours[1] = pSource->_GetPix(To.x+x, To.y+y-1);
530  if(To.x+x < iWidth-1)
531  neighbours[2] = pSource->_GetPix(To.x+x+1, To.y+y-1);
532  }
533  if(To.x+x > 0)
534  neighbours[3] = pSource->_GetPix(To.x+x-1, To.y+y);
535  if(To.x+x < iWidth-1)
536  neighbours[4] = pSource->_GetPix(To.x+x+1, To.y+y);
537  if(To.y+y < iHeight-1)
538  {
539  if(To.x+x > 0)
540  neighbours[5] = pSource->_GetPix(To.x+x-1, To.y+y+1);
541  neighbours[6] = pSource->_GetPix(To.x+x, To.y+y+1);
542  if(To.x+x < iWidth-1)
543  neighbours[7] = pSource->_GetPix(To.x+x+1, To.y+y+1);
544  }
545 
546  // Look for highest-placement material in our surroundings
547  int maxPixel = pixel, maxPlacement = placement;
548  for(int neighbour : neighbours)
549  {
550  int tempPlacement = MatPlacement(PixCol2Mat(neighbour));
551  if(tempPlacement > maxPlacement || (tempPlacement == maxPlacement && neighbour > maxPixel) )
552  {
553  maxPixel = neighbour;
554  maxPlacement = tempPlacement;
555  }
556  }
557 
558  // Scaler calculation depends on whether this is the highest-placement material around
559  int scaler = 0;
560  if(maxPixel == pixel)
561  {
562  // If yes, we consider all other materials as "other"
563  for(int i = 0; i < 8; i++)
564  if(neighbours[i] == pixel)
565  scaler += (1<<i);
566 
567  } else {
568 
569  // Otherwise, we *only* consider the highest-placement material as "other"
570  for(int i = 0; i < 8; i++)
571  if(neighbours[i] != maxPixel)
572  scaler += (1<<i);
573  }
574  return scaler;
575 }
576 
577 const char *C4LandscapeRenderGL::UniformNames[C4LRU_Count+1];
578 
579 bool C4LandscapeRenderGL::LoadShader(C4GroupSet *pGroups, C4Shader& shader, const char* name, int ssc)
580 {
581  // Setup #defines
582  shader.AddDefine("OPENCLONK");
583  shader.AddDefine("OC_LANDSCAPE");
584  if(ssc & C4SSC_LIGHT) shader.AddDefine("OC_DYNAMIC_LIGHT"); // sample light from light texture
585 
586  // Create vertex shader
587  shader.LoadVertexSlices(pGroups, "LandscapeVertexShader.glsl");
588 
589  // Then load slices for fragment shader
590  shader.LoadFragmentSlices(pGroups, "CommonShader.glsl");
591  shader.LoadFragmentSlices(pGroups, "LandscapeShader.glsl");
592 
593  // Categories for script shaders.
594  shader.SetScriptCategories({"Common", "Landscape"});
595 
596  // Make attribute name map
597  const char* AttributeNames[C4LRA_Count + 1];
598  AttributeNames[C4LRA_Position] = "oc_Position";
599  AttributeNames[C4LRA_LandscapeTexCoord] = "oc_LandscapeTexCoord";
600  AttributeNames[C4LRA_LightTexCoord] = "oc_LightTexCoord"; // unused if no dynamic light
601  AttributeNames[C4LRA_Count] = nullptr;
602 
603  // Initialise!
604  if (!shader.Init(name, UniformNames, AttributeNames)) {
605  shader.ClearSlices();
606  return false;
607  }
608 
609  return true;
610 }
611 
612 bool C4LandscapeRenderGL::LoadShaders(C4GroupSet *pGroups)
613 {
614  // First, clear out all existing shaders
615  ClearShaders();
616 
617  // Make uniform name map
618  ZeroMem(UniformNames, sizeof(UniformNames));
619  UniformNames[C4LRU_ProjectionMatrix] = "projectionMatrix";
620  UniformNames[C4LRU_LandscapeTex] = "landscapeTex";
621  UniformNames[C4LRU_ScalerTex] = "scalerTex";
622  UniformNames[C4LRU_MaterialTex] = "materialTex";
623  UniformNames[C4LRU_LightTex] = "lightTex";
624  UniformNames[C4LRU_AmbientTex] = "ambientTex";
625  UniformNames[C4LRU_Gamma] = "gamma";
626  UniformNames[C4LRU_Resolution] = "resolution";
627  UniformNames[C4LRU_Center] = "center";
628  UniformNames[C4LRU_MatMapTex] = "matMapTex";
629  UniformNames[C4LRU_MaterialDepth] = "materialDepth";
630  UniformNames[C4LRU_MaterialSize] = "materialSize";
631  UniformNames[C4LRU_AmbientBrightness] = "ambientBrightness";
632  UniformNames[C4LRU_AmbientTransform] = "ambientTransform";
633  UniformNames[C4LRU_Modulation] = "clrMod";
634 
635  if(!LoadShader(pGroups, Shader, "landscape", 0))
636  return false;
637  if(!LoadShader(pGroups, ShaderLight, "landscapeLight", C4SSC_LIGHT))
638  return false;
639 
640  return true;
641 }
642 
643 bool C4LandscapeRenderGL::InitVBO()
644 {
645  // Our VBO needs to hold 4 vertices with 6 floats each.
646  assert(hVBO == 0);
647  glGenBuffers(1, &hVBO);
648  glBindBuffer(GL_ARRAY_BUFFER, hVBO);
649  glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), nullptr, GL_STREAM_DRAW);
650  glBindBuffer(GL_ARRAY_BUFFER, 0);
651  // Also allocate the VAO IDs
652  assert(hVAOIDLight == 0);
653  assert(hVAOIDNoLight == 0);
654  hVAOIDLight = pGL->GenVAOID();
655  hVAOIDNoLight = pGL->GenVAOID();
656  return true;
657 }
658 
659 void C4LandscapeRenderGL::ClearShaders()
660 {
661  if (Shader.Initialised())
662  {
663  Shader.Clear();
664  Shader.ClearSlices();
665  }
666 
667  if (ShaderLight.Initialised())
668  {
669  ShaderLight.Clear();
670  ShaderLight.ClearSlices();
671  }
672 }
673 
674 bool C4LandscapeRenderGL::LoadScaler(C4GroupSet *pGroups)
675 {
676  // Search for scaler
677  C4Group *pGroup = pGroups->FindEntry(C4CFN_LandscapeScaler);
678  if(!pGroup) return false;
679  // Load scaler from group
680  if(!fctScaler.Load(*pGroup, C4CFN_LandscapeScaler, C4FCT_Full, C4FCT_Full, false, 0))
681  return false;
682  // Check size
683  const int iOrigWdt = 8 * 3, iOrigHgt = 4 * 8 * 3;
684  const int iFactor = fctScaler.Wdt / iOrigWdt;
685  if(fctScaler.Wdt != iFactor * iOrigWdt || fctScaler.Hgt != iFactor * iOrigHgt)
686  {
687  LogF(" gl: Unexpected scaler size - should be multiple of %dx%d!", iOrigWdt, iOrigHgt);
688  return false;
689  }
690  // Walk through all lookups we have in the texture and decide where
691  // to look for the "other" pixel. This might not be unique later on,
692  // so it is a good idea to have a proper priority order here.
693  fctScaler.Surface->Lock();
694  int i;
695  for (i = 0; i < 8 * 4 * 8; i++) {
696  // Decode from ID what pixels are expected to be set in this case
697  enum Px { NW, N, NE, W, E, SW, S, SE, X };
698  int p_x[9] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
699  int p_y[9] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
700  bool pxAt[X];
701  for(int j = 0; j < X; j++)
702  pxAt[j] = !!(i & (1 << j));
703  // Oc = octant borders. Set up arrays to get righthand border
704  // of an octant, in a way that we can easily rotate further.
705  enum Oc { NWW, NEE, SWW, SEE, NNW, NNE, SSW, SSE };
706  int p2a[8] = { 5, 6, 7, 4, 0, 3, 2, 1 };
707  Oc a2o[8] = { SEE, SSE, SSW, SWW, NWW, NNW, NNE, NEE };
708  // Decide in which octant we want to interpolate towards
709  // which pixel. Pick the nearest unset pixel using a special
710  // priority order.
711  Px opx[8] = { X,X,X,X,X,X,X,X };
712  #define INTERPOLATE(x,da) do { \
713  int y = a2o[(8+p2a[x]+(da)) % 8];\
714  if (!pxAt[x] && opx[y] == X) opx[y] = x; \
715  } while(false)
716  for(int j = 0; j < 4; j++) {
717  // vertical
718  INTERPOLATE(N, j); INTERPOLATE(N, -j-1);
719  INTERPOLATE(S, j); INTERPOLATE(S, -j-1);
720  // horizontal
721  INTERPOLATE(W, j); INTERPOLATE(W, -j-1);
722  INTERPOLATE(E, j); INTERPOLATE(E, -j-1);
723  // diagonals
724  INTERPOLATE(NW, j); INTERPOLATE(NW, -j-1);
725  INTERPOLATE(SW, j); INTERPOLATE(SW, -j-1);
726  INTERPOLATE(NE, j); INTERPOLATE(NE, -j-1);
727  INTERPOLATE(SE, j); INTERPOLATE(SE, -j-1);
728  }
729  // Decide in which octants we will not interpolate normals.
730  // It doesn't make sense when there's another material in that
731  // general direction, as then the bias of that will factor into
732  // the interpolation, giving bright borders on dark shading,
733  // and vice-versa.
734  bool noNormals[8];
735  noNormals[NNW] = noNormals[NWW] = !pxAt[W] || !pxAt[NW] || !pxAt[N];
736  noNormals[NNE] = noNormals[NEE] = !pxAt[E] || !pxAt[NE] || !pxAt[N];
737  noNormals[SSW] = noNormals[SWW] = !pxAt[W] || !pxAt[SW] || !pxAt[S];
738  noNormals[SSE] = noNormals[SEE] = !pxAt[E] || !pxAt[SE] || !pxAt[S];
739  // Set blue and green components to relative coordinates of
740  // "other" pixel, and alpha to mix param for normals
741  const int x0 = (i % 8) * 3 * iFactor;
742  const int y0 = (i / 8) * 3 * iFactor;
743  const int iPxs = 3 * iFactor;
744  int y, x;
745 
746  for(y = 0; y < iPxs; y++)
747  {
748  for(x = 0; x < iPxs; x++)
749  {
750  // Find out in which octagon we are
751  int oct = 0;
752  if(2 * x >= iPxs) oct+=1;
753  if(2 * y >= iPxs) oct+=2;
754  if((x >= y) != (x >= iPxs - y)) oct+=4;
755  // Get pixel, do processing
756  DWORD pix = fctScaler.Surface->GetPixDw(x0+x, y0+y, false);
757  BYTE val = GetGreenValue(pix);
758  if(val >= 250) val = 255;
759  BYTE bx = 64 * (p_x[opx[oct]] + 1);
760  BYTE by = 64 * (p_y[opx[oct]] + 1);
761  BYTE bn = (noNormals[oct] ? 255 : 1);
762  fctScaler.Surface->SetPixDw(x0+x, y0+y, RGBA(val, bx, by, bn));
763  }
764  }
765  }
766  return fctScaler.Surface->Unlock();
767 }
768 
769 int32_t C4LandscapeRenderGL::LookupTextureTransition(const char *szFrom, const char *szTo)
770 {
771  // Is this actually a transition? Otherwise we're looking for a single texture
772  bool fTransit = !SEqual(szFrom, szTo);
773  // Look for a position in the map where the textures appear in sequence
774  uint32_t i;
775  for(i = 1; i < MaterialTextureMap.size(); i++)
776  {
777  if(SEqual(szFrom, MaterialTextureMap[i].getData()))
778  {
779  // Single texture: We're done
780  if(!fTransit) return i;
781  // Check next texture as well
782  if(i + 1 >= MaterialTextureMap.size())
783  return -1;
784  if(SEqual(szTo, MaterialTextureMap[i+1].getData()))
785  return i;
786  }
787  }
788  return -1;
789 }
790 
791 void C4LandscapeRenderGL::AddTextureTransition(const char *szFrom, const char *szTo)
792 {
793  // Empty?
794  if (!szFrom || !szTo) return;
795  // First try the lookup (both directions)
796  if (LookupTextureTransition(szFrom, szTo) >= 0) return;
797  if (LookupTextureTransition(szTo, szFrom) >= 0) return;
798  // Single texture? Add it as single
799  if (SEqual(szTo, szFrom))
800  MaterialTextureMap.emplace_back(szFrom);
801  // Have one of the textures at the end of the list?
802  else if(SEqual(MaterialTextureMap.back().getData(), szFrom))
803  MaterialTextureMap.emplace_back(szTo);
804  else if(SEqual(MaterialTextureMap.back().getData(), szTo))
805  MaterialTextureMap.emplace_back(szFrom);
806  else
807  {
808  // Otherwise add both
809  MaterialTextureMap.emplace_back(szFrom);
810  MaterialTextureMap.emplace_back(szTo);
811  }
812 }
813 
814 void C4LandscapeRenderGL::AddTextureAnim(const char *szTextureAnim)
815 {
816  if(!szTextureAnim) return;
817 #ifdef DEBUG_SEPERATOR_TEXTURES
818  // Save back count of textures at start
819  uint32_t iStartTexCount = MaterialTextureMap.size();
820 #endif
821  // Add all individual transitions
822  const char *pFrom = szTextureAnim;
823  for(;;)
824  {
825  // Get next phase
826  const char *pTo = strchr(pFrom, '-');
827  if(!pTo) pTo = szTextureAnim; else pTo++;
828  // Add transition
829  StdStrBuf From, To;
830  From.CopyUntil(pFrom, '-');
831  To.CopyUntil(pTo, '-');
832  AddTextureTransition(From.getData(), To.getData());
833  // Advance
834  if(pTo == szTextureAnim) break;
835  pFrom = pTo;
836  }
837 #ifdef DEBUG_SEPERATOR_TEXTURES
838  // Add a seperator texture, if we added any new ones
839  if(MaterialTextureMap.size() > iStartTexCount)
840  MaterialTextureMap.push_back(StdCopyStrBuf(SEPERATOR_TEXTURE));
841 #endif
842 }
843 
844 void C4LandscapeRenderGL::AddTexturesFromMap(C4TextureMap *pMap)
845 {
846  // Go through used texture (animations) and add all phases to our map
847 
848  // Note: We can be smarter here, for example add longer animations
849  // first in order to make better reuse of 3D texture slots.
850  // We could even make a full-blown optimization problem out of it.
851  // Future work...
852 
853  const C4TexMapEntry *pEntry;
854  for(int32_t i = 0; (pEntry = pMap->GetEntry(i)); i++)
855  // ToDo: Properly handle jumping back
856  AddTextureAnim(pEntry->GetTextureName());
857 
858 }
859 
860 void C4LandscapeRenderGL::BuildMatMap(uint32_t *pTex)
861 {
862  // TODO: Still merely an inefficient placeholder for things to come...
863 
864  // Build material-texture map (depth parameter where to find appropriate texture)
865  for(int pix = 0; pix < 256; pix++)
866  {
867  // Look up indexed entry
868  const C4TexMapEntry *pEntry = pTexs->GetEntry(PixCol2Tex(BYTE(pix)));
869  if(!pEntry->GetTextureName())
870  {
871  // Undefined textures transparent
872  pTex[2*pix] = 0;
873  pTex[2*pix+1] = RGBA(0,0,0,255);
874  continue;
875  }
876 
877  // Got animation?
878  int iPhases = 1; const char *p = pEntry->GetTextureName();
879  while((p = strchr(p, '-'))) { p++; iPhases++; }
880  // Hard-coded hack. Fix me!
881  C4Material *pMaterial = pEntry->GetMaterial();
882  const int iPhaseLength = pMaterial->AnimationSpeed;
883  float phase = 0;
884  if (iPhases > 1) {
885  phase = C4TimeMilliseconds::Now().AsInt() % (iPhases * iPhaseLength);
886  phase /= iPhaseLength;
887  }
888 
889  // Find our transition
890  const char *pFrom = pEntry->GetTextureName();
891  float gTexCoo = 0;
892  for(int iP = 0;; iP++)
893  {
894  // Get next phase
895  const char *pTo = strchr(pFrom, '-');
896  if(!pTo) pTo = pEntry->GetTextureName(); else pTo++;
897  // Add transition
898  if(iP == int(phase))
899  {
900  StdStrBuf From, To;
901  From.CopyUntil(pFrom, '-');
902  To.CopyUntil(pTo, '-');
903  // Find transition
904  int iTrans;
905  if ((iTrans = LookupTextureTransition(From.getData(), To.getData())) >= 0)
906  gTexCoo = float(iTrans) + fmod(phase, 1.0f);
907  else if ((iTrans = LookupTextureTransition(To.getData(), From.getData())) >= 0)
908  gTexCoo = float(iTrans) + 1.0 - fmod(phase, 1.0f);
909  break;
910  }
911  // Advance
912  pFrom = pTo;
913  }
914 
915  // Assign texture
916  int iTexCoo = int((gTexCoo * 256.0 / iMaterialTextureDepth) + 0.5);
917  pTex[2*pix] = RGBA(
918  Clamp(pMaterial->LightEmit[0], 0, 255),
919  Clamp(pMaterial->LightEmit[1], 0, 255),
920  Clamp(pMaterial->LightEmit[2], 0, 255),
921  iTexCoo);
922  pTex[2*pix+1] = RGBA(
923  Clamp(pMaterial->LightSpot[0], 0, 255),
924  Clamp(pMaterial->LightSpot[1], 0, 255),
925  Clamp(pMaterial->LightSpot[2], 0, 255),
926  Clamp(pMaterial->LightAngle, 0, 255));
927  }
928 }
929 
930 void C4LandscapeRenderGL::Draw(const C4TargetFacet &cgo, const C4FoWRegion *Light, uint32_t clrMod)
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
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);
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 }
1132 
1133 #endif // #ifndef USE_CONSOLE
const char * getData() const
Definition: StdBuf.h:442
BYTE _GetPix(int32_t x, int32_t y) const
static constexpr int COLOR_DEPTH_BYTES
Definition: C4Draw.h:90
float Y
Definition: C4Facet.h:118
void ApplyZoom(float &X, float &Y)
Definition: C4Draw.cpp:784
const int C4LR_SurfaceCount
void SetUniform2f(int iUniform, float gX, float gY) const
Definition: C4Shader.h:217
void SetScriptCategories(const std::vector< std::string > &categories)
Definition: C4Shader.cpp:100
int Wdt
Definition: C4Surface.h:65
int32_t MaterialZoom
Definition: C4Scenario.h:185
C4Game Game
Definition: C4Globals.cpp:52
C4Material * GetMaterial() const
Definition: C4Texture.h:63
C4ScriptUniform scriptUniform
Definition: C4Draw.h:100
#define GetGreenValue(rgb)
Definition: StdColors.h:28
float bottom
Definition: C4Rect.h:25
C4Scenario C4S
Definition: C4Game.h:74
const FLOAT_RECT & getViewportRegion() const
Definition: C4FoWRegion.h:52
bool Load(C4Group &hGroup, const char *szName, int iWdt, int iHgt, bool fNoErrIfNotFound, int iFlags)
Definition: C4FacetEx.cpp:78
const int C4LR_BiasDistanceX
const char *const SEPERATOR_TEXTURE
float right
Definition: C4Rect.h:25
bool Lock()
Definition: C4Surface.cpp:453
float left
Definition: C4Rect.h:25
void SetUniform1iv(int iUniform, int iLength, const int *pVals) const
Definition: C4Shader.h:221
C4SLandscape Landscape
Definition: C4Scenario.h:234
const int C4LR_BiasDistanceY
float gammaOut[3]
Definition: C4Draw.h:98
C4Rect GetAffectedRect(C4Rect Rect) override
double GetBrightness() const
Definition: C4FoWAmbient.h:56
bool LoadFragmentSlices(C4GroupSet *pGroupSet, const char *szFile)
Definition: C4Shader.cpp:90
void FreeVAOID(unsigned int vaoid)
Definition: C4DrawGL.cpp:967
bool Unlock()
Definition: C4Surface.cpp:464
void Apply(C4ShaderCall &call)
Definition: C4Shader.cpp:826
C4Surface * Surface32
Definition: C4Texture.h:35
int32_t AnimationSpeed
Definition: C4Material.h:133
void AddDefine(const char *name)
Definition: C4Shader.cpp:63
Definition: C4Rect.h:27
uint8_t BYTE
const C4FoW * getFoW() const
Definition: C4FoWRegion.h:50
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
int32_t PixCol2Mat(BYTE pixc)
void ClearSlices()
Definition: C4Shader.cpp:322
const C4TexMapEntry * GetEntry(int32_t iIndex) const
Definition: C4Texture.h:85
Definition: C4Texture.h:48
int Hgt
Definition: C4Surface.h:65
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
void Intersect(const C4Rect &r2)
Definition: C4Rect.cpp:100
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:44
int32_t LightAngle
Definition: C4Material.h:134
int32_t Wdt
Definition: C4Rect.h:30
void Draw(const C4TargetFacet &cgo, const C4FoWRegion *Light, uint32_t clrMod) override
int32_t y
Definition: C4Rect.h:30
int32_t getSurfaceHeight() const
bool SetPixDw(int iX, int iY, DWORD dwCol)
Definition: C4Surface.cpp:576
const char * GetTextureName() const
Definition: C4Texture.h:61
void Clear()
Definition: C4Shader.cpp:333
#define C4CFN_LandscapeScaler
Definition: C4Components.h:132
void LoadShader(StdMeshMaterialParserCtx &ctx, StdMeshMaterialShaderType type)
C4Group * FindEntry(const char *szWildcard, int32_t *pPriority=nullptr, int32_t *pID=nullptr)
Definition: C4GroupSet.cpp:175
void Finish()
Definition: C4Shader.cpp:703
#define INTERPOLATE(x, da)
GLuint getSurfaceName() const
C4TextureMap * pTexs
bool LoadVertexSlices(C4GroupSet *pGroupSet, const char *szFile)
Definition: C4Shader.cpp:95
C4Draw * pDraw
Definition: C4Draw.cpp:42
bool GetVAO(unsigned int vaoid, GLuint &vao)
Definition: C4DrawGL.cpp:1013
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:227
int32_t getSurfaceWidth() const
int32_t _GetPlacement(int32_t x, int32_t y) const
void Update(C4Rect Rect, C4Landscape *pSource) override
void SetUniformMatrix2x3fv(int iUniform, int iLength, const float *pVals) const
Definition: C4Shader.h:243
C4FoWAmbient Ambient
Definition: C4FoW.h:115
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
Definition: C4Surface.cpp:491
bool HaveUniform(int iUniform) const
Definition: C4Shader.h:122
bool ReInit(int32_t iWidth, int32_t iHeight) override
void CopyUntil(const char *szString, char cUntil)
Definition: StdBuf.h:613
bool Init(int32_t iWidth, int32_t iHeight, C4TextureMap *pMap, C4GroupSet *pGraphics) override
int32_t MatPlacement(int32_t mat)
Definition: C4Material.h:246
GLint GetAttribute(int iAttribute) const
Definition: C4Shader.h:127
int32_t x
Definition: C4Rect.h:30
void Ref(const char *pnData)
Definition: StdBuf.h:455
float Zoom
Definition: C4Draw.h:116
std::unique_ptr< C4TexRef > texture
Definition: C4Surface.h:78
bool Initialised() const
Definition: C4Shader.h:106
void SetUniform3fv(int iUniform, int iLength, const float *pVals) const
Definition: C4Shader.h:233
float TargetX
Definition: C4Facet.h:165
const char * GetTexture(int32_t iIndex)
Definition: C4Texture.cpp:494
float Hgt
Definition: C4Facet.h:118
bool Init(const char *szWhat, const char **szUniforms, const char **szAttributes)
Definition: C4Shader.cpp:346
void SetUniform1f(int iUniform, float gX) const
Definition: C4Shader.h:213
unsigned int GenVAOID()
Definition: C4DrawGL.cpp:925
void Enlarge(int32_t iByX, int32_t iByY)
Definition: C4Rect.h:51
bool PrepareRendering(C4Surface *sfcToSurface) override
Definition: C4DrawGL.cpp:169
GLint AllocTexUnit(int iUniform)
Definition: C4Shader.cpp:666
C4Rect GetOutRect() const
Definition: C4Draw.cpp:739
const C4Rect & getRegion() const
Definition: C4FoWRegion.h:51
void SetUniformMatrix4x4(int iUniform, const StdMeshMatrix &matrix)
Definition: C4Shader.h:287
int32_t LightEmit[3]
Definition: C4Material.h:135
#define X(sdl, oc)
int32_t LightSpot[3]
Definition: C4Material.h:136
CStdGL * pGL
Definition: C4DrawGL.cpp:905
int32_t Hgt
Definition: C4Rect.h:30
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
Definition: Standard.h:60
C4Rect GetClipRect() const
Definition: C4Draw.cpp:730
const StdProjectionMatrix & GetProjectionMatrix() const
Definition: C4DrawGL.h:251
uint32_t RGBA(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
Definition: StdColors.h:22
C4Surface * Surface
Definition: C4Facet.h:117
void Start()
Definition: C4Shader.cpp:689
float TargetY
Definition: C4Facet.h:165
uint32_t AsInt() const
uint32_t DWORD
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:250
const int C4FCT_Full
Definition: C4FacetEx.h:26
float Wdt
Definition: C4Facet.h:118
float X
Definition: C4Facet.h:118
float top
Definition: C4Rect.h:25
static C4TimeMilliseconds Now()
void GetFragTransform(const struct FLOAT_RECT &vpRect, const C4Rect &clipRect, const C4Rect &outRect, float ambientTransform[6]) const
int32_t PixCol2Tex(BYTE pixc)
Definition: C4Landscape.h:213
void SetUniform4fv(int iUniform, int iLength, const float *pVals) const
Definition: C4Shader.h:237
int iSize
Definition: TstC4NetIO.cpp:32