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 "load_param.h" |
---|
21 | #include "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 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | CREATE_FACTORY(SkyBox, CL_SKYBOX); |
---|
33 | |
---|
34 | /** |
---|
35 | * Constructs a SkyBox and takes fileName as a map. |
---|
36 | * @param fileName the file to take as input for the SkyBox |
---|
37 | */ |
---|
38 | SkyBox::SkyBox(const char* fileName) |
---|
39 | { |
---|
40 | this->preInit(); |
---|
41 | if (fileName) |
---|
42 | this->setTextureAndType(fileName, ".jpg"); |
---|
43 | this->postInit(); |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * initializes a skybox from a XmlElement |
---|
48 | */ |
---|
49 | SkyBox::SkyBox(const TiXmlElement* root) |
---|
50 | { |
---|
51 | this->preInit(); |
---|
52 | |
---|
53 | this->loadParams(root); |
---|
54 | |
---|
55 | this->postInit(); |
---|
56 | } |
---|
57 | |
---|
58 | void SkyBox::loadParams(const TiXmlElement* root) |
---|
59 | { |
---|
60 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
61 | |
---|
62 | LoadParam(root, "Materialset", this, SkyBox, setTexture) |
---|
63 | .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg"); |
---|
64 | |
---|
65 | LoadParam(root, "Size", this, SkyBox, setSize) |
---|
66 | .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance)."); |
---|
67 | } |
---|
68 | |
---|
69 | void SkyBox::preInit() |
---|
70 | { |
---|
71 | this->setClassID(CL_SKYBOX, "SkyBox"); |
---|
72 | this->toList(OM_ENVIRON_NOTICK); |
---|
73 | this->size = 100.0; |
---|
74 | |
---|
75 | for (int i = 0; i < 6; i++) |
---|
76 | { |
---|
77 | this->material[i] = new Material(); |
---|
78 | this->material[i]->setIllum(3); |
---|
79 | this->material[i]->setDiffuse(0.0,0.0,0.0); |
---|
80 | this->material[i]->setSpecular(0.0,0.0,0.0); |
---|
81 | this->material[i]->setAmbient(2.0, 2.0, 2.0); |
---|
82 | |
---|
83 | this->texture[i] = NULL; |
---|
84 | } |
---|
85 | this->setParentMode(PNODE_MOVEMENT); |
---|
86 | |
---|
87 | this->textureName = NULL; |
---|
88 | } |
---|
89 | |
---|
90 | void SkyBox::postInit() |
---|
91 | { |
---|
92 | this->rebuild(); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * default destructor |
---|
98 | */ |
---|
99 | SkyBox::~SkyBox() |
---|
100 | { |
---|
101 | PRINTF(5)("Deleting SkyBox\n"); |
---|
102 | this->setModel(NULL); //< so that WorldEntity does not try to delete it again. |
---|
103 | for (int i = 0; i < 6; i++) |
---|
104 | delete this->material[i]; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * sets A set of textures when just giving a Name and an extension: |
---|
109 | |
---|
110 | usage: give this function an argument like |
---|
111 | setTexture("skybox", "jpg"); |
---|
112 | and it will convert this to |
---|
113 | setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg", |
---|
114 | "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg"); |
---|
115 | */ |
---|
116 | void SkyBox::setTextureAndType(const char* name, const char* extension) |
---|
117 | { |
---|
118 | char* top = new char[strlen(name)+strlen(extension)+ 10]; |
---|
119 | char* bottom = new char[strlen(name)+strlen(extension)+ 10]; |
---|
120 | char* left = new char[strlen(name)+strlen(extension)+ 10]; |
---|
121 | char* right = new char[strlen(name)+strlen(extension)+ 10]; |
---|
122 | char* front = new char[strlen(name)+strlen(extension)+ 10]; |
---|
123 | char* back = new char[strlen(name)+strlen(extension)+ 10]; |
---|
124 | |
---|
125 | sprintf(top, "%s_top.%s", name, extension); |
---|
126 | sprintf(bottom, "%s_bottom.%s", name, extension); |
---|
127 | sprintf(left, "%s_left.%s", name, extension); |
---|
128 | sprintf(right, "%s_right.%s", name, extension); |
---|
129 | sprintf(front, "%s_front.%s", name, extension); |
---|
130 | sprintf(back, "%s_back.%s", name, extension); |
---|
131 | |
---|
132 | this->setTextures(top, bottom, left, right, front, back); |
---|
133 | |
---|
134 | // deleted alocated memory of this function |
---|
135 | delete []top; |
---|
136 | delete []bottom; |
---|
137 | delete []left; |
---|
138 | delete []right; |
---|
139 | delete []front; |
---|
140 | delete []back; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Defines which textures should be loaded onto the SkyBox. |
---|
145 | * @param top the top texture. |
---|
146 | * @param bottom the bottom texture. |
---|
147 | * @param left the left texture. |
---|
148 | * @param right the right texture. |
---|
149 | * @param front the front texture. |
---|
150 | * @param back the back texture. |
---|
151 | */ |
---|
152 | void SkyBox::setTextures(const char* top, const char* bottom, const char* left, |
---|
153 | const char* right, const char* front, const char* back) |
---|
154 | { |
---|
155 | this->material[0]->setDiffuseMap(top); |
---|
156 | this->material[1]->setDiffuseMap(bottom); |
---|
157 | this->material[2]->setDiffuseMap(left); |
---|
158 | this->material[3]->setDiffuseMap(right); |
---|
159 | this->material[4]->setDiffuseMap(front); |
---|
160 | this->material[5]->setDiffuseMap(back); |
---|
161 | this->loadCubeMapTextures(top, bottom, left, right, front, back); |
---|
162 | //this->enableCubeMap(); |
---|
163 | } |
---|
164 | |
---|
165 | void SkyBox::loadCubeMapTextures(const char* top, const char* bottom, const char* left, |
---|
166 | const char* right, const char* front, const char* back) |
---|
167 | { |
---|
168 | this->texture[0] = new Texture (top, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB); |
---|
169 | this->texture[1] = new Texture (bottom, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB); |
---|
170 | this->texture[2] = new Texture (left, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB); |
---|
171 | this->texture[3] = new Texture (right, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB); |
---|
172 | this->texture[4] = new Texture (front, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB); |
---|
173 | this->texture[5] = new Texture (back, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB); |
---|
174 | } |
---|
175 | |
---|
176 | void SkyBox::enableCubeMap() |
---|
177 | { |
---|
178 | glEnable(GL_TEXTURE_CUBE_MAP_ARB); |
---|
179 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); |
---|
180 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); |
---|
181 | glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); |
---|
182 | glEnable(GL_TEXTURE_GEN_S); |
---|
183 | glEnable(GL_TEXTURE_GEN_T); |
---|
184 | glEnable(GL_TEXTURE_GEN_R); |
---|
185 | } |
---|
186 | |
---|
187 | void SkyBox::disableCubeMap() |
---|
188 | { |
---|
189 | glDisable(GL_TEXTURE_CUBE_MAP_ARB); |
---|
190 | glDisable(GL_TEXTURE_GEN_S); |
---|
191 | glDisable(GL_TEXTURE_GEN_T); |
---|
192 | glDisable(GL_TEXTURE_GEN_R); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | /** |
---|
198 | * @param size The new size of the SkyBox |
---|
199 | |
---|
200 | * do not forget to rebuild the SkyBox after this. |
---|
201 | */ |
---|
202 | void SkyBox::setSize(float size) |
---|
203 | { |
---|
204 | this->size = size; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * rebuilds the SkyBox |
---|
209 | |
---|
210 | this must be done, when changing the Size of the Skybox (runtime-efficency) |
---|
211 | */ |
---|
212 | void SkyBox::rebuild() |
---|
213 | { |
---|
214 | StaticModel* model = new StaticModel(); |
---|
215 | |
---|
216 | model->addVertex (-0.5*size, -0.5*size, 0.5*size); |
---|
217 | model->addVertex (0.5*size, -0.5*size, 0.5*size); |
---|
218 | model->addVertex (-0.5*size, 0.5*size, 0.5*size); |
---|
219 | model->addVertex (0.5*size, 0.5*size, 0.5*size); |
---|
220 | model->addVertex (-0.5*size, 0.5*size, -0.5*size); |
---|
221 | model->addVertex (0.5*size, 0.5*size, -0.5*size); |
---|
222 | model->addVertex (-0.5*size, -0.5*size, -0.5*size); |
---|
223 | model->addVertex (0.5*size, -0.5*size, -0.5*size); |
---|
224 | |
---|
225 | model->addVertexTexture (0.0, 1.0); |
---|
226 | model->addVertexTexture (1.0, 1.0); |
---|
227 | model->addVertexTexture (1.0, 0.0); |
---|
228 | model->addVertexTexture (0.0, 0.0); |
---|
229 | |
---|
230 | model->addVertexNormal (0.0, 0.0, 1.0); |
---|
231 | model->addVertexNormal (0.0, 1.0, 0.0); |
---|
232 | model->addVertexNormal (0.0, 0.0, -1.0); |
---|
233 | model->addVertexNormal (0.0, -1.0, 0.0); |
---|
234 | model->addVertexNormal (1.0, 0.0, 0.0); |
---|
235 | model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
236 | |
---|
237 | model->setMaterial(material[0]); |
---|
238 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top |
---|
239 | model->setMaterial(material[1]); |
---|
240 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom |
---|
241 | model->setMaterial(material[2]); |
---|
242 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left |
---|
243 | model->setMaterial(material[3]); |
---|
244 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right |
---|
245 | model->setMaterial(material[4]); |
---|
246 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front |
---|
247 | model->setMaterial(material[5]); |
---|
248 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back |
---|
249 | |
---|
250 | model->finalize(); |
---|
251 | |
---|
252 | this->setModel(model); |
---|
253 | } |
---|
254 | |
---|
255 | int SkyBox::writeBytes( const byte * data, int length, int sender ) |
---|
256 | { |
---|
257 | setRequestedSync( false ); |
---|
258 | setIsOutOfSync( false ); |
---|
259 | |
---|
260 | SYNCHELP_READ_BEGIN(); |
---|
261 | |
---|
262 | SYNCHELP_READ_FKT( WorldEntity::writeState ); |
---|
263 | |
---|
264 | SYNCHELP_READ_FLOAT( size ); |
---|
265 | if ( textureName ) |
---|
266 | { |
---|
267 | delete[] textureName; |
---|
268 | textureName = NULL; |
---|
269 | } |
---|
270 | SYNCHELP_READ_STRINGM( textureName ); |
---|
271 | |
---|
272 | this->setSize( size ); |
---|
273 | this->setTextureAndType( textureName, "jpg" ); |
---|
274 | this->rebuild(); |
---|
275 | |
---|
276 | return SYNCHELP_READ_N; |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | int SkyBox::readBytes( byte * data, int maxLength, int * reciever ) |
---|
282 | { |
---|
283 | if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) |
---|
284 | { |
---|
285 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
286 | setRequestedSync( true ); |
---|
287 | } |
---|
288 | |
---|
289 | int rec = this->getRequestSync(); |
---|
290 | if ( rec > 0 ) |
---|
291 | { |
---|
292 | *reciever = rec; |
---|
293 | |
---|
294 | SYNCHELP_WRITE_BEGIN(); |
---|
295 | |
---|
296 | SYNCHELP_WRITE_FKT( WorldEntity::readState ); |
---|
297 | |
---|
298 | SYNCHELP_WRITE_FLOAT(this->size); |
---|
299 | SYNCHELP_WRITE_STRING(this->textureName); |
---|
300 | |
---|
301 | return SYNCHELP_WRITE_N; |
---|
302 | } |
---|
303 | |
---|
304 | *reciever = 0; |
---|
305 | return 0; |
---|
306 | } |
---|
307 | |
---|
308 | void SkyBox::writeDebug( ) const |
---|
309 | { |
---|
310 | } |
---|
311 | |
---|
312 | void SkyBox::readDebug( ) const |
---|
313 | { |
---|
314 | } |
---|