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 "model.h" |
---|
23 | |
---|
24 | |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | CREATE_FACTORY(SkyBox, CL_SKYBOX); |
---|
28 | |
---|
29 | /** |
---|
30 | * Constructs a SkyBox and takes fileName as a map. |
---|
31 | * @param fileName the file to take as input for the SkyBox |
---|
32 | */ |
---|
33 | SkyBox::SkyBox(const char* fileName) |
---|
34 | { |
---|
35 | this->preInit(); |
---|
36 | if (fileName) |
---|
37 | this->setTextureAndType(fileName, ".jpg"); |
---|
38 | this->postInit(); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * initializes a skybox from a XmlElement |
---|
43 | */ |
---|
44 | SkyBox::SkyBox(const TiXmlElement* root) |
---|
45 | { |
---|
46 | this->preInit(); |
---|
47 | |
---|
48 | this->loadParams(root); |
---|
49 | |
---|
50 | this->postInit(); |
---|
51 | } |
---|
52 | |
---|
53 | void SkyBox::loadParams(const TiXmlElement* root) |
---|
54 | { |
---|
55 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
56 | |
---|
57 | LoadParam(root, "Materialset", this, SkyBox, setTexture) |
---|
58 | .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg"); |
---|
59 | |
---|
60 | LoadParam(root, "Size", this, SkyBox, setSize) |
---|
61 | .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance)."); |
---|
62 | } |
---|
63 | |
---|
64 | void SkyBox::preInit() |
---|
65 | { |
---|
66 | this->setClassID(CL_SKYBOX, "SkyBox"); |
---|
67 | |
---|
68 | this->size = 100.0; |
---|
69 | |
---|
70 | this->material = new Material*[6]; |
---|
71 | for (int i = 0; i < 6; i++) |
---|
72 | { |
---|
73 | this->material[i] = new Material(); |
---|
74 | this->material[i]->setIllum(3); |
---|
75 | this->material[i]->setDiffuse(0.0,0.0,0.0); |
---|
76 | this->material[i]->setSpecular(0.0,0.0,0.0); |
---|
77 | this->material[i]->setAmbient(2.0, 2.0, 2.0); |
---|
78 | } |
---|
79 | this->setParentMode(PNODE_MOVEMENT); |
---|
80 | } |
---|
81 | |
---|
82 | void SkyBox::postInit() |
---|
83 | { |
---|
84 | this->rebuild(); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | /** |
---|
89 | * default destructor |
---|
90 | */ |
---|
91 | SkyBox::~SkyBox() |
---|
92 | { |
---|
93 | PRINTF(5)("Deleting SkyBox\n"); |
---|
94 | this->setModel(NULL); //< so that WorldEntity does not try to delete it again. |
---|
95 | for (int i = 0; i < 6; i++) |
---|
96 | delete this->material[i]; |
---|
97 | delete[] this->material; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * sets A set of textures when just giving a Name and an extension: |
---|
102 | |
---|
103 | usage: give this function an argument like |
---|
104 | setTexture("skybox", "jpg"); |
---|
105 | and it will convert this to |
---|
106 | setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg", |
---|
107 | "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg"); |
---|
108 | */ |
---|
109 | void SkyBox::setTextureAndType(const char* name, const char* extension) |
---|
110 | { |
---|
111 | char* top = new char[strlen(name)+strlen(extension)+ 10]; |
---|
112 | char* bottom = new char[strlen(name)+strlen(extension)+ 10]; |
---|
113 | char* left = new char[strlen(name)+strlen(extension)+ 10]; |
---|
114 | char* right = new char[strlen(name)+strlen(extension)+ 10]; |
---|
115 | char* front = new char[strlen(name)+strlen(extension)+ 10]; |
---|
116 | char* back = new char[strlen(name)+strlen(extension)+ 10]; |
---|
117 | |
---|
118 | sprintf(top, "%s_top.%s", name, extension); |
---|
119 | sprintf(bottom, "%s_bottom.%s", name, extension); |
---|
120 | sprintf(left, "%s_left.%s", name, extension); |
---|
121 | sprintf(right, "%s_right.%s", name, extension); |
---|
122 | sprintf(front, "%s_front.%s", name, extension); |
---|
123 | sprintf(back, "%s_back.%s", name, extension); |
---|
124 | |
---|
125 | this->setTextures(top, bottom, left, right, front, back); |
---|
126 | |
---|
127 | // deleted alocated memory of this function |
---|
128 | delete []top; |
---|
129 | delete []bottom; |
---|
130 | delete []left; |
---|
131 | delete []right; |
---|
132 | delete []front; |
---|
133 | delete []back; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Defines which textures should be loaded onto the SkyBox. |
---|
138 | * @param top the top texture. |
---|
139 | * @param bottom the bottom texture. |
---|
140 | * @param left the left texture. |
---|
141 | * @param right the right texture. |
---|
142 | * @param front the front texture. |
---|
143 | * @param back the back texture. |
---|
144 | */ |
---|
145 | void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back) |
---|
146 | { |
---|
147 | this->material[0]->setDiffuseMap(top); |
---|
148 | this->material[1]->setDiffuseMap(bottom); |
---|
149 | this->material[2]->setDiffuseMap(left); |
---|
150 | this->material[3]->setDiffuseMap(right); |
---|
151 | this->material[4]->setDiffuseMap(front); |
---|
152 | this->material[5]->setDiffuseMap(back); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * @param size The new size of the SkyBox |
---|
157 | |
---|
158 | * do not forget to rebuild the SkyBox after this. |
---|
159 | */ |
---|
160 | void SkyBox::setSize(float size) |
---|
161 | { |
---|
162 | this->size = size; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * rebuilds the SkyBox |
---|
167 | |
---|
168 | this must be done, when changing the Size of the Skybox (runtime-efficency) |
---|
169 | */ |
---|
170 | void SkyBox::rebuild() |
---|
171 | { |
---|
172 | Model* model = new Model(); |
---|
173 | |
---|
174 | model->addVertex (-0.5*size, -0.5*size, 0.5*size); |
---|
175 | model->addVertex (0.5*size, -0.5*size, 0.5*size); |
---|
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 | |
---|
183 | model->addVertexTexture (0.0, 1.0); |
---|
184 | model->addVertexTexture (1.0, 1.0); |
---|
185 | model->addVertexTexture (1.0, 0.0); |
---|
186 | model->addVertexTexture (0.0, 0.0); |
---|
187 | |
---|
188 | model->addVertexNormal (0.0, 0.0, 1.0); |
---|
189 | model->addVertexNormal (0.0, 1.0, 0.0); |
---|
190 | model->addVertexNormal (0.0, 0.0, -1.0); |
---|
191 | model->addVertexNormal (0.0, -1.0, 0.0); |
---|
192 | model->addVertexNormal (1.0, 0.0, 0.0); |
---|
193 | model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
194 | |
---|
195 | model->setMaterial(material[0]); |
---|
196 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top |
---|
197 | model->setMaterial(material[1]); |
---|
198 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom |
---|
199 | model->setMaterial(material[2]); |
---|
200 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left |
---|
201 | model->setMaterial(material[3]); |
---|
202 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right |
---|
203 | model->setMaterial(material[4]); |
---|
204 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front |
---|
205 | model->setMaterial(material[5]); |
---|
206 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back |
---|
207 | |
---|
208 | model->finalize(); |
---|
209 | |
---|
210 | this->setModel(model); |
---|
211 | } |
---|