1 | |
---|
2 | /* |
---|
3 | orxonox - the future of 3D-vertical-scrollers |
---|
4 | |
---|
5 | Copyright (C) 2004 orx |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2, or (at your option) |
---|
10 | any later version. |
---|
11 | |
---|
12 | ### File Specific: |
---|
13 | main-programmer: Benjamin Grauer |
---|
14 | co-programmer: ... |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
18 | |
---|
19 | |
---|
20 | #include "skybox.h" |
---|
21 | |
---|
22 | #include "stdincl.h" |
---|
23 | #include "factory.h" |
---|
24 | |
---|
25 | #include "load_param.h" |
---|
26 | #include "material.h" |
---|
27 | #include "vector.h" |
---|
28 | #include "resource_manager.h" |
---|
29 | #include "model.h" |
---|
30 | //#include "world_entity.h" |
---|
31 | |
---|
32 | CREATE_FACTORY(SkyBox); |
---|
33 | |
---|
34 | using namespace std; |
---|
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 char* fileName) |
---|
41 | { |
---|
42 | this->preInit(); |
---|
43 | if (fileName) |
---|
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 | this->loadParams(root); |
---|
56 | |
---|
57 | this->postInit(); |
---|
58 | } |
---|
59 | |
---|
60 | void SkyBox::loadParams(const TiXmlElement* root) |
---|
61 | { |
---|
62 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
63 | |
---|
64 | LoadParam<SkyBox>(root, "Materialset", this, &SkyBox::setTexture) |
---|
65 | .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg"); |
---|
66 | |
---|
67 | LoadParam<SkyBox>(root, "Size", this, &SkyBox::setSize) |
---|
68 | .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance)."); |
---|
69 | } |
---|
70 | |
---|
71 | void SkyBox::preInit() |
---|
72 | { |
---|
73 | this->setClassID(CL_SKYBOX, "SkyBox"); |
---|
74 | |
---|
75 | this->size = 100.0; |
---|
76 | |
---|
77 | this->material = new Material*[6]; |
---|
78 | for (int i = 0; i < 6; i++) |
---|
79 | { |
---|
80 | this->material[i] = new Material(); |
---|
81 | this->material[i]->setIllum(3); |
---|
82 | this->material[i]->setDiffuse(0.0,0.0,0.0); |
---|
83 | this->material[i]->setSpecular(0.0,0.0,0.0); |
---|
84 | this->material[i]->setAmbient(2.0, 2.0, 2.0); |
---|
85 | } |
---|
86 | this->setParentMode(PNODE_MOVEMENT); |
---|
87 | } |
---|
88 | |
---|
89 | void SkyBox::postInit() |
---|
90 | { |
---|
91 | this->rebuild(); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | /** |
---|
96 | * default destructor |
---|
97 | */ |
---|
98 | SkyBox::~SkyBox() |
---|
99 | { |
---|
100 | PRINTF(5)("Deleting SkyBox\n"); |
---|
101 | for (int i = 0; i < 6; i++) |
---|
102 | delete this->material[i]; |
---|
103 | delete[] this->material; |
---|
104 | delete this->model; |
---|
105 | this->model = NULL; //< so that WorldEntity does not try to delete it again. |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * sets A set of textures when just giving a Name and an extension: |
---|
110 | |
---|
111 | usage: give this function an argument like |
---|
112 | setTexture("skybox", "jpg"); |
---|
113 | and it will convert this to |
---|
114 | setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg", |
---|
115 | "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg"); |
---|
116 | */ |
---|
117 | void SkyBox::setTextureAndType(const char* name, const char* extension) |
---|
118 | { |
---|
119 | char* top = new char[strlen(name)+strlen(extension)+ 10]; |
---|
120 | char* bottom = new char[strlen(name)+strlen(extension)+ 10]; |
---|
121 | char* left = new char[strlen(name)+strlen(extension)+ 10]; |
---|
122 | char* right = new char[strlen(name)+strlen(extension)+ 10]; |
---|
123 | char* front = new char[strlen(name)+strlen(extension)+ 10]; |
---|
124 | char* back = new char[strlen(name)+strlen(extension)+ 10]; |
---|
125 | |
---|
126 | sprintf(top, "%s_top.%s", name, extension); |
---|
127 | sprintf(bottom, "%s_bottom.%s", name, extension); |
---|
128 | sprintf(left, "%s_left.%s", name, extension); |
---|
129 | sprintf(right, "%s_right.%s", name, extension); |
---|
130 | sprintf(front, "%s_front.%s", name, extension); |
---|
131 | sprintf(back, "%s_back.%s", name, extension); |
---|
132 | |
---|
133 | this->setTextures(top, bottom, left, right, front, back); |
---|
134 | |
---|
135 | // deleted alocated memory of this function |
---|
136 | delete []top; |
---|
137 | delete []bottom; |
---|
138 | delete []left; |
---|
139 | delete []right; |
---|
140 | delete []front; |
---|
141 | delete []back; |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Defines which textures should be loaded onto the SkyBox. |
---|
146 | * @param top the top texture. |
---|
147 | * @param bottom the bottom texture. |
---|
148 | * @param left the left texture. |
---|
149 | * @param right the right texture. |
---|
150 | * @param front the front texture. |
---|
151 | * @param back the back texture. |
---|
152 | */ |
---|
153 | void SkyBox::setTextures(const char* top, const char* bottom, const char* left, 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 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * @param size The new size of the SkyBox |
---|
165 | |
---|
166 | * do not forget to rebuild the SkyBox after this. |
---|
167 | */ |
---|
168 | void SkyBox::setSize(float size) |
---|
169 | { |
---|
170 | this->size = size; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * rebuilds the SkyBox |
---|
175 | |
---|
176 | this must be done, when changing the Size of the Skybox (runtime-efficency) |
---|
177 | */ |
---|
178 | void SkyBox::rebuild() |
---|
179 | { |
---|
180 | if (this->model) |
---|
181 | delete this->model; |
---|
182 | model = new Model(); |
---|
183 | |
---|
184 | this->model->addVertex (-0.5*size, -0.5*size, 0.5*size); |
---|
185 | this->model->addVertex (0.5*size, -0.5*size, 0.5*size); |
---|
186 | this->model->addVertex (-0.5*size, 0.5*size, 0.5*size); |
---|
187 | this->model->addVertex (0.5*size, 0.5*size, 0.5*size); |
---|
188 | this->model->addVertex (-0.5*size, 0.5*size, -0.5*size); |
---|
189 | this->model->addVertex (0.5*size, 0.5*size, -0.5*size); |
---|
190 | this->model->addVertex (-0.5*size, -0.5*size, -0.5*size); |
---|
191 | this->model->addVertex (0.5*size, -0.5*size, -0.5*size); |
---|
192 | |
---|
193 | this->model->addVertexTexture (0.0, 1.0); |
---|
194 | this->model->addVertexTexture (1.0, 1.0); |
---|
195 | this->model->addVertexTexture (1.0, 0.0); |
---|
196 | this->model->addVertexTexture (0.0, 0.0); |
---|
197 | |
---|
198 | this->model->addVertexNormal (0.0, 0.0, 1.0); |
---|
199 | this->model->addVertexNormal (0.0, 1.0, 0.0); |
---|
200 | this->model->addVertexNormal (0.0, 0.0, -1.0); |
---|
201 | this->model->addVertexNormal (0.0, -1.0, 0.0); |
---|
202 | this->model->addVertexNormal (1.0, 0.0, 0.0); |
---|
203 | this->model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
204 | |
---|
205 | this->model->setMaterial(material[0]); |
---|
206 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top |
---|
207 | this->model->setMaterial(material[1]); |
---|
208 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom |
---|
209 | this->model->setMaterial(material[2]); |
---|
210 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left |
---|
211 | this->model->setMaterial(material[3]); |
---|
212 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right |
---|
213 | this->model->setMaterial(material[4]); |
---|
214 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front |
---|
215 | this->model->setMaterial(material[5]); |
---|
216 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back |
---|
217 | |
---|
218 | this->model->finalize(); |
---|
219 | } |
---|