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