1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
17 | |
---|
18 | #include "skybox.h" |
---|
19 | |
---|
20 | #include "util/loading/load_param.h" |
---|
21 | #include "util/loading/factory.h" |
---|
22 | #include "static_model.h" |
---|
23 | |
---|
24 | #include "material.h" |
---|
25 | #include "texture.h" |
---|
26 | |
---|
27 | #include "network_game_manager.h" |
---|
28 | #include "converter.h" |
---|
29 | #include "util/loading/resource_manager.h" |
---|
30 | |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | CREATE_FACTORY(SkyBox, CL_SKYBOX); |
---|
35 | |
---|
36 | /** |
---|
37 | * Constructs a SkyBox and takes fileName as a map. |
---|
38 | * @param fileName the file to take as input for the SkyBox |
---|
39 | */ |
---|
40 | SkyBox::SkyBox(const std::string& fileName) |
---|
41 | { |
---|
42 | this->preInit(); |
---|
43 | if (!fileName.empty()) |
---|
44 | this->setTextureAndType(fileName, ".jpg"); |
---|
45 | this->postInit(); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * initializes a skybox from a XmlElement |
---|
50 | */ |
---|
51 | SkyBox::SkyBox(const TiXmlElement* root) |
---|
52 | { |
---|
53 | this->preInit(); |
---|
54 | |
---|
55 | if( root != NULL) |
---|
56 | this->loadParams(root); |
---|
57 | |
---|
58 | this->postInit(); |
---|
59 | } |
---|
60 | |
---|
61 | void SkyBox::loadParams(const TiXmlElement* root) |
---|
62 | { |
---|
63 | WorldEntity::loadParams(root); |
---|
64 | |
---|
65 | LoadParam(root, "Materialset", this, SkyBox, setTexture) |
---|
66 | .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg"); |
---|
67 | |
---|
68 | LoadParam(root, "Size", this, SkyBox, setSize) |
---|
69 | .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance)."); |
---|
70 | } |
---|
71 | |
---|
72 | void SkyBox::preInit() |
---|
73 | { |
---|
74 | this->setClassID(CL_SKYBOX, "SkyBox"); |
---|
75 | this->toList(OM_BACKGROUND); |
---|
76 | //this->size = 100.0; |
---|
77 | this->textureSize = 1024.0f; |
---|
78 | |
---|
79 | for (int i = 0; i < 6; i++) |
---|
80 | { |
---|
81 | this->material[i] = new Material(); |
---|
82 | this->material[i]->setIllum(3); |
---|
83 | this->material[i]->setDiffuse(0.0,0.0,0.0); |
---|
84 | this->material[i]->setSpecular(0.0,0.0,0.0); |
---|
85 | this->material[i]->setAmbient(2.0, 2.0, 2.0); |
---|
86 | |
---|
87 | this->cubeTexture[i] = NULL; |
---|
88 | } |
---|
89 | this->setParentMode(PNODE_MOVEMENT); |
---|
90 | |
---|
91 | this->textureName = ""; |
---|
92 | } |
---|
93 | |
---|
94 | void SkyBox::postInit() |
---|
95 | { |
---|
96 | this->rebuild(); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | * default destructor |
---|
102 | */ |
---|
103 | SkyBox::~SkyBox() |
---|
104 | { |
---|
105 | PRINTF(5)("Deleting SkyBox\n"); |
---|
106 | for (int i = 0; i < 6; i++) |
---|
107 | { |
---|
108 | if (this->material[i]) |
---|
109 | delete this->material[i]; |
---|
110 | if (this->cubeTexture[i]) |
---|
111 | ResourceManager::getInstance()->unload(this->cubeTexture[i]); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | void SkyBox::setTexture(const std::string& name) |
---|
116 | { |
---|
117 | this->textureName = name; |
---|
118 | this->setTextureAndType (name, "jpg"); |
---|
119 | }; |
---|
120 | |
---|
121 | |
---|
122 | /** |
---|
123 | * @brief sets A set of textures when just giving a Name and an extension: |
---|
124 | * @param name the prefix of the Name |
---|
125 | * @param extension the file extension (jpg by default) |
---|
126 | * usage: give this function an argument like |
---|
127 | * setTexture("skybox", "jpg"); |
---|
128 | * and it will convert this to |
---|
129 | * setTextures("skybox_negx.jpg", "skybox_posx.jpg", "skybox_negy.jpg", |
---|
130 | * "skybox_posy.jpg", "skybox_negz.jpg", "skybox_posz.jpg"); |
---|
131 | */ |
---|
132 | void SkyBox::setTextureAndType(const std::string& name, const std::string& extension) |
---|
133 | { |
---|
134 | std::string negX = name + "_negx." + extension; |
---|
135 | std::string posX = name + "_posx." + extension; |
---|
136 | |
---|
137 | std::string negY = name + "_negy." + extension; |
---|
138 | std::string posY = name + "_posy." + extension; |
---|
139 | |
---|
140 | std::string negZ = name + "_negz." + extension; |
---|
141 | std::string posZ = name + "_posz." + extension; |
---|
142 | |
---|
143 | this->setTextures(negX, posX, negY, posY, negZ, posZ); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * @brief Defines which textures should be loaded onto the SkyBox. |
---|
148 | * @param negX the top texture. |
---|
149 | * @param posX the bottom texture. |
---|
150 | * @param negY the left texture. |
---|
151 | * @param posY the right texture. |
---|
152 | * @param negZ the front texture. |
---|
153 | * @param posZ the back texture. |
---|
154 | */ |
---|
155 | void SkyBox::setTextures(const std::string& negX, const std::string& posX, |
---|
156 | const std::string& negY, const std::string& posY, |
---|
157 | const std::string& negZ, const std::string& posZ) |
---|
158 | { |
---|
159 | this->material[0]->setDiffuseMap(negX); |
---|
160 | this->material[1]->setDiffuseMap(posX); |
---|
161 | this->material[2]->setDiffuseMap(negY); |
---|
162 | this->material[3]->setDiffuseMap(posY); |
---|
163 | this->material[4]->setDiffuseMap(negZ); |
---|
164 | this->material[5]->setDiffuseMap(posZ); |
---|
165 | if (GLEW_EXT_texture_cube_map) |
---|
166 | this->loadCubeMapTextures(negX, posX, negY, posY, negZ, posZ); |
---|
167 | } |
---|
168 | |
---|
169 | void SkyBox::loadCubeMapTextures(const std::string& posY, const std::string& negY, const std::string& negZ, |
---|
170 | const std::string& posZ, const std::string& posX, const std::string& negX) |
---|
171 | { |
---|
172 | this->cubeTexture[0] = (Texture*)ResourceManager::getInstance()->load(negX, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT); |
---|
173 | this->cubeTexture[1] = (Texture*)ResourceManager::getInstance()->load(posX, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT); |
---|
174 | |
---|
175 | this->cubeTexture[2] = (Texture*)ResourceManager::getInstance()->load(negY, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT); |
---|
176 | this->cubeTexture[3] = (Texture*)ResourceManager::getInstance()->load(posY, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT); |
---|
177 | |
---|
178 | this->cubeTexture[4] = (Texture*)ResourceManager::getInstance()->load(negZ, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT); |
---|
179 | this->cubeTexture[5] = (Texture*)ResourceManager::getInstance()->load(posZ, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT); |
---|
180 | } |
---|
181 | |
---|
182 | void SkyBox::enableCubeMap() |
---|
183 | { |
---|
184 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); |
---|
185 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); |
---|
186 | glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); |
---|
187 | |
---|
188 | glEnable(GL_TEXTURE_CUBE_MAP_EXT); |
---|
189 | |
---|
190 | glEnable(GL_TEXTURE_GEN_S); |
---|
191 | glEnable(GL_TEXTURE_GEN_T); |
---|
192 | glEnable(GL_TEXTURE_GEN_R); |
---|
193 | |
---|
194 | } |
---|
195 | |
---|
196 | void SkyBox::disableCubeMap() |
---|
197 | { |
---|
198 | glDisable(GL_TEXTURE_CUBE_MAP); |
---|
199 | glDisable(GL_TEXTURE_2D); |
---|
200 | glDisable(GL_TEXTURE_GEN_S); |
---|
201 | glDisable(GL_TEXTURE_GEN_T); |
---|
202 | glDisable(GL_TEXTURE_GEN_R); |
---|
203 | |
---|
204 | glDisable(GL_TEXTURE_GEN_S); |
---|
205 | glDisable(GL_TEXTURE_GEN_T); |
---|
206 | glDisable(GL_TEXTURE_GEN_R); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | /** |
---|
212 | * @param size The new size of the SkyBox |
---|
213 | |
---|
214 | * do not forget to rebuild the SkyBox after this. |
---|
215 | */ |
---|
216 | void SkyBox::setSize(float size) |
---|
217 | { |
---|
218 | this->size = size; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | |
---|
223 | void SkyBox::draw() |
---|
224 | { |
---|
225 | glPushAttrib(GL_ENABLE_BIT); |
---|
226 | // glPushAttrib(GL_LIGHTING_BIT); |
---|
227 | glDisable(GL_LIGHTING); |
---|
228 | |
---|
229 | glDisable(GL_FOG); |
---|
230 | |
---|
231 | WorldEntity::draw(); |
---|
232 | |
---|
233 | glPopAttrib(); |
---|
234 | |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | /** |
---|
239 | * rebuilds the SkyBox |
---|
240 | |
---|
241 | this must be done, when changing the Size of the Skybox (runtime-efficency) |
---|
242 | */ |
---|
243 | void SkyBox::rebuild() |
---|
244 | { |
---|
245 | StaticModel* model = new StaticModel(); |
---|
246 | |
---|
247 | model->addVertex (-0.5*size, -0.5*size, 0.5*size); |
---|
248 | model->addVertex (0.5*size, -0.5*size, 0.5*size); |
---|
249 | model->addVertex (-0.5*size, 0.5*size, 0.5*size); |
---|
250 | model->addVertex (0.5*size, 0.5*size, 0.5*size); |
---|
251 | model->addVertex (-0.5*size, 0.5*size, -0.5*size); |
---|
252 | model->addVertex (0.5*size, 0.5*size, -0.5*size); |
---|
253 | model->addVertex (-0.5*size, -0.5*size, -0.5*size); |
---|
254 | model->addVertex (0.5*size, -0.5*size, -0.5*size); |
---|
255 | |
---|
256 | // model->addVertexTexture (0.0, 1.0); |
---|
257 | // model->addVertexTexture (1.0, 1.0); |
---|
258 | // model->addVertexTexture (1.0, 0.0); |
---|
259 | // model->addVertexTexture (0.0, 0.0); |
---|
260 | |
---|
261 | model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize); |
---|
262 | model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize); |
---|
263 | model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize); |
---|
264 | model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize); |
---|
265 | |
---|
266 | |
---|
267 | model->addVertexNormal (0.0, 0.0, 1.0); |
---|
268 | model->addVertexNormal (0.0, 1.0, 0.0); |
---|
269 | model->addVertexNormal (0.0, 0.0, -1.0); |
---|
270 | model->addVertexNormal (0.0, -1.0, 0.0); |
---|
271 | model->addVertexNormal (1.0, 0.0, 0.0); |
---|
272 | model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
273 | |
---|
274 | model->setMaterial(material[0]); |
---|
275 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back |
---|
276 | model->setMaterial(material[1]); |
---|
277 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front |
---|
278 | model->setMaterial(material[2]); |
---|
279 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom |
---|
280 | model->setMaterial(material[3]); |
---|
281 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top |
---|
282 | model->setMaterial(material[4]); |
---|
283 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left |
---|
284 | model->setMaterial(material[5]); |
---|
285 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right |
---|
286 | |
---|
287 | model->finalize(); |
---|
288 | |
---|
289 | this->setModel(model); |
---|
290 | } |
---|
291 | |
---|
292 | int SkyBox::writeBytes( const byte * data, int length, int sender ) |
---|
293 | { |
---|
294 | setRequestedSync( false ); |
---|
295 | setIsOutOfSync( false ); |
---|
296 | |
---|
297 | SYNCHELP_READ_BEGIN(); |
---|
298 | |
---|
299 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SB_WE_STATE ); |
---|
300 | |
---|
301 | SYNCHELP_READ_FLOAT( size, NWT_SB_SIZE ); |
---|
302 | if ( !this->textureName.empty() ) |
---|
303 | { |
---|
304 | textureName = ""; |
---|
305 | } |
---|
306 | std::string texName; |
---|
307 | SYNCHELP_READ_STRING( texName, NWT_SB_TEXTURENAME ); |
---|
308 | |
---|
309 | this->setSize( size ); |
---|
310 | this->setTextureAndType( texName, "jpg" ); |
---|
311 | this->rebuild(); |
---|
312 | |
---|
313 | return SYNCHELP_READ_N; |
---|
314 | } |
---|
315 | |
---|
316 | |
---|
317 | |
---|
318 | int SkyBox::readBytes( byte * data, int maxLength, int * reciever ) |
---|
319 | { |
---|
320 | if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) |
---|
321 | { |
---|
322 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
323 | setRequestedSync( true ); |
---|
324 | } |
---|
325 | |
---|
326 | int rec = this->getRequestSync(); |
---|
327 | if ( rec > 0 ) |
---|
328 | { |
---|
329 | *reciever = rec; |
---|
330 | |
---|
331 | SYNCHELP_WRITE_BEGIN(); |
---|
332 | |
---|
333 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SB_WE_STATE ); |
---|
334 | |
---|
335 | SYNCHELP_WRITE_FLOAT(this->size, NWT_SB_SIZE); |
---|
336 | SYNCHELP_WRITE_STRING(this->textureName, NWT_SB_TEXTURENAME); |
---|
337 | |
---|
338 | return SYNCHELP_WRITE_N; |
---|
339 | } |
---|
340 | |
---|
341 | *reciever = 0; |
---|
342 | return 0; |
---|
343 | } |
---|
344 | |
---|
345 | void SkyBox::writeDebug( ) const |
---|
346 | { |
---|
347 | } |
---|
348 | |
---|
349 | void SkyBox::readDebug( ) const |
---|
350 | { |
---|
351 | } |
---|