1 /* Copyright (C) 2013 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
19 * Sky settings, texture management and rendering.
22 #include "precompiled.h"
26 #include "lib/timer.h"
27 #include "lib/tex/tex.h"
28 #include "lib/res/graphics/ogl_tex.h"
30 #include "maths/MathUtil.h"
33 #include "ps/CLogger.h"
35 #include "ps/Loader.h"
36 #include "ps/Filesystem.h"
39 #include "renderer/SkyManager.h"
40 #include "renderer/Renderer.h"
42 #include "graphics/LightEnv.h"
43 #include "graphics/ShaderManager.h"
44 #include "graphics/Terrain.h"
45 #include "graphics/TextureManager.h"
48 ///////////////////////////////////////////////////////////////////////////////////////////////
49 // SkyManager implementation
52 ///////////////////////////////////////////////////////////////////
53 // String names for each image, in order of the IMG_ constants
54 const wchar_t* SkyManager::s_imageNames
[numTextures
] = {
63 ///////////////////////////////////////////////////////////////////
64 // Construction/Destruction
65 SkyManager::SkyManager()
71 m_HorizonHeight
= -150.0f
;
77 ///////////////////////////////////////////////////////////////////
78 // Load all sky textures
79 void SkyManager::LoadSkyTextures()
81 /*for (size_t i = 0; i < ARRAY_SIZE(m_SkyTexture); ++i)
83 VfsPath path = VfsPath("art/textures/skies") / m_SkySet / (Path::String(s_imageNames[i])+L".dds");
85 CTextureProperties textureProps(path);
86 textureProps.SetWrap(GL_CLAMP_TO_EDGE);
87 CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
89 m_SkyTexture[i] = texture;
92 ///////////////////////////////////////////////////////////////////////////
93 // HACK: THE HORRIBLENESS HERE IS OVER 9000. The following code is a HUGE hack and will be removed completely
94 // as soon as all the hardcoded GL_TEXTURE_2D references are corrected in the TextureManager/OGL/tex libs.
96 glGenTextures(1, &m_SkyCubeMap
);
97 glBindTexture(GL_TEXTURE_CUBE_MAP
, m_SkyCubeMap
);
100 GL_TEXTURE_CUBE_MAP_POSITIVE_X
,
101 GL_TEXTURE_CUBE_MAP_NEGATIVE_X
,
102 GL_TEXTURE_CUBE_MAP_POSITIVE_Z
,
103 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
,
104 GL_TEXTURE_CUBE_MAP_POSITIVE_Y
,
105 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
108 const wchar_t* images
[numTextures
+1] = {
117 for (size_t i
= 0; i
< numTextures
+1; ++i
)
119 VfsPath path
= VfsPath("art/textures/skies") / m_SkySet
/ (Path::String(images
[i
])+L
".dds");
123 if (g_VFS
->LoadFile(path
, file
, fileSize
) < 0)
125 VfsPath path2
= VfsPath("art/textures/skies") / m_SkySet
/ (Path::String(images
[i
])+L
".dds.cached.dds");
126 if (g_VFS
->LoadFile(path2
, file
, fileSize
) < 0)
128 glDeleteTextures(1, &m_SkyCubeMap
);
129 LOGERROR("Error creating sky cubemap.");
135 tex
.decode(file
, fileSize
);
137 tex
.transform_to((tex
.m_Flags
| TEX_BOTTOM_UP
| TEX_ALPHA
) & ~(TEX_DXT
| TEX_MIPMAPS
));
139 u8
* data
= tex
.get_data();
141 if (types
[i
] == GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
|| types
[i
] == GL_TEXTURE_CUBE_MAP_POSITIVE_Y
)
143 std::vector
<u8
> rotated(tex
.m_DataSize
);
145 for (size_t y
= 0; y
< tex
.m_Height
; ++y
)
147 for (size_t x
= 0; x
< tex
.m_Width
; ++x
)
149 size_t invx
= y
, invy
= tex
.m_Width
-x
-1;
151 rotated
[(y
*tex
.m_Width
+ x
) * 4 + 0] = data
[(invy
*tex
.m_Width
+ invx
) * 4 + 0];
152 rotated
[(y
*tex
.m_Width
+ x
) * 4 + 1] = data
[(invy
*tex
.m_Width
+ invx
) * 4 + 1];
153 rotated
[(y
*tex
.m_Width
+ x
) * 4 + 2] = data
[(invy
*tex
.m_Width
+ invx
) * 4 + 2];
154 rotated
[(y
*tex
.m_Width
+ x
) * 4 + 3] = data
[(invy
*tex
.m_Width
+ invx
) * 4 + 3];
158 glTexImage2D(types
[i
], 0, GL_RGB
, tex
.m_Width
, tex
.m_Height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, &rotated
[0]);
162 glTexImage2D(types
[i
], 0, GL_RGB
, tex
.m_Width
, tex
.m_Height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
166 glTexParameteri(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
167 glTexParameteri(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
168 glTexParameteri(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
169 glTexParameteri(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
171 #warning TODO: fix SkyManager::LoadSkyTextures for GLES
173 glTexParameteri(GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
175 glBindTexture(GL_TEXTURE_2D
, 0);
176 ///////////////////////////////////////////////////////////////////////////
180 ///////////////////////////////////////////////////////////////////
181 // Switch to a different sky set (while the game is running)
182 void SkyManager::SetSkySet( const CStrW
& newSet
)
184 if(newSet
== m_SkySet
)
189 glDeleteTextures(1, &m_SkyCubeMap
);
198 ///////////////////////////////////////////////////////////////////
199 // Generate list of available skies
200 std::vector
<CStrW
> SkyManager::GetSkySets() const
202 std::vector
<CStrW
> skies
;
204 // Find all subdirectories in art/textures/skies
206 const VfsPath
path(L
"art/textures/skies/");
207 DirectoryNames subdirectories
;
208 if(g_VFS
->GetDirectoryEntries(path
, 0, &subdirectories
) < 0)
210 LOGERROR("Error opening directory '%s'", path
.string8());
211 return std::vector
<CStrW
>(1, GetSkySet()); // just return what we currently have
214 for(size_t i
= 0; i
< subdirectories
.size(); i
++)
215 skies
.push_back(subdirectories
[i
].string());
216 sort(skies
.begin(), skies
.end());
221 ///////////////////////////////////////////////////////////////////
223 void SkyManager::RenderSky()
226 #warning TODO: implement SkyManager::RenderSky for GLES
229 // Draw the sky as a small box around the map, with depth write enabled.
230 // This will be done before anything else is drawn so we'll be overlapped by everything else.
232 // Do nothing unless SetSkySet was called
233 if (m_SkySet
.empty())
236 glDepthMask( GL_FALSE
);
238 pglActiveTextureARB(GL_TEXTURE0_ARB
);
239 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
241 glMatrixMode(GL_MODELVIEW
);
244 // Translate so we are at the center of the map.
245 ssize_t mapSize
= g_Game
->GetWorld()->GetTerrain()->GetVerticesPerSide();
246 glTranslatef(mapSize
*(TERRAIN_TILE_SIZE
/2.0f
), m_HorizonHeight
, mapSize
*(TERRAIN_TILE_SIZE
/2.0f
) );
248 // Rotate so that the "left" face, which contains the brightest part of each
249 // skymap, is in the direction of the sun from our light environment
250 glRotatef( 180.0f
/*+ 45.0f*/ + RADTODEG(g_Renderer
.GetLightEnv().GetRotation()), 0.0f
, 1.0f
, 0.0f
);
252 // Distance to draw the faces at
253 const float D
= 1500.0f
; // distance from map center
254 const float H
= 500.0f
; // height of the ceiling
255 const float FH
= -100.0f
; // height of the "floor"
257 CShaderProgramPtr shader
;
258 CShaderTechniquePtr skytech
;
260 if (g_Renderer
.GetRenderPath() == CRenderer::RP_SHADER
)
262 skytech
= g_Renderer
.GetShaderManager().LoadEffect(str_sky_simple
);
263 skytech
->BeginPass();
264 shader
= skytech
->GetShader();
265 shader
->BindTexture(str_baseTex
, m_SkyCubeMap
);
269 glDisable(GL_TEXTURE_2D
);
270 glEnable(GL_TEXTURE_CUBE_MAP
);
271 glBindTexture(GL_TEXTURE_CUBE_MAP
, m_SkyCubeMap
);
276 // GL_TEXTURE_CUBE_MAP_NEGATIVE_X
277 glTexCoord3f( +1, +1, +1 ); glVertex3f( -D
, FH
, -D
);
278 glTexCoord3f( +1, +1, -1 ); glVertex3f( -D
, FH
, +D
);
279 glTexCoord3f( +1, -1, -1 ); glVertex3f( -D
, +H
, +D
);
280 glTexCoord3f( +1, -1, +1 ); glVertex3f( -D
, +H
, -D
);
282 // GL_TEXTURE_CUBE_MAP_POSITIVE_X
283 glTexCoord3f( -1, +1, -1 ); glVertex3f( +D
, FH
, +D
);
284 glTexCoord3f( -1, +1, +1 ); glVertex3f( +D
, FH
, -D
);
285 glTexCoord3f( -1, -1, +1 ); glVertex3f( +D
, +H
, -D
);
286 glTexCoord3f( -1, -1, -1 ); glVertex3f( +D
, +H
, +D
);
288 // GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
289 glTexCoord3f( -1, +1, +1 ); glVertex3f( +D
, FH
, -D
);
290 glTexCoord3f( -1, +1, -1 ); glVertex3f( +D
, FH
, +D
);
291 glTexCoord3f( +1, +1, -1 ); glVertex3f( -D
, FH
, +D
);
292 glTexCoord3f( +1, +1, +1 ); glVertex3f( -D
, FH
, -D
);
294 // GL_TEXTURE_CUBE_MAP_POSITIVE_Y
295 glTexCoord3f( +1, -1, +1 ); glVertex3f( -D
, +H
, -D
);
296 glTexCoord3f( +1, -1, -1 ); glVertex3f( -D
, +H
, +D
);
297 glTexCoord3f( -1, -1, -1 ); glVertex3f( +D
, +H
, +D
);
298 glTexCoord3f( -1, -1, +1 ); glVertex3f( +D
, +H
, -D
);
300 // GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
301 glTexCoord3f( -1, +1, +1 ); glVertex3f( +D
, FH
, -D
);
302 glTexCoord3f( +1, +1, +1 ); glVertex3f( -D
, FH
, -D
);
303 glTexCoord3f( +1, -1, +1 ); glVertex3f( -D
, +H
, -D
);
304 glTexCoord3f( -1, -1, +1 ); glVertex3f( +D
, +H
, -D
);
306 // GL_TEXTURE_CUBE_MAP_POSITIVE_Z
307 glTexCoord3f( +1, +1, -1 ); glVertex3f( -D
, FH
, +D
);
308 glTexCoord3f( -1, +1, -1 ); glVertex3f( +D
, FH
, +D
);
309 glTexCoord3f( -1, -1, -1 ); glVertex3f( +D
, +H
, +D
);
310 glTexCoord3f( +1, -1, -1 ); glVertex3f( -D
, +H
, +D
);
313 if (g_Renderer
.GetRenderPath() == CRenderer::RP_SHADER
)
319 glBindTexture(GL_TEXTURE_CUBE_MAP
, 0);
320 glDisable(GL_TEXTURE_CUBE_MAP
);
321 glEnable(GL_TEXTURE_2D
);
326 glDepthMask( GL_TRUE
);