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