1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2006 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: hdavid, amaechler |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #include "skydome.h" |
---|
17 | |
---|
18 | #include "util/loading/load_param.h" |
---|
19 | #include "util/loading/factory.h" |
---|
20 | #include "static_model.h" |
---|
21 | #include "shader.h" |
---|
22 | |
---|
23 | #include "network_game_manager.h" |
---|
24 | #include "converter.h" |
---|
25 | #include "util/loading/resource_manager.h" |
---|
26 | |
---|
27 | #include "debug.h" |
---|
28 | |
---|
29 | #define DTOR (PI/180.0f) |
---|
30 | #define SQR(x) (x*x) |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | ObjectListDefinition(Skydome); |
---|
35 | |
---|
36 | /** |
---|
37 | * initializes a skybox from a XmlElement |
---|
38 | */ |
---|
39 | Skydome::Skydome() |
---|
40 | { |
---|
41 | this->init(); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | void Skydome::init() |
---|
46 | { |
---|
47 | PRINTF(0)("Skydome init\n"); |
---|
48 | |
---|
49 | this->registerObject(this, Skydome::_objectList); |
---|
50 | this->toList(OM_BACKGROUND); |
---|
51 | this->toReflectionList(); |
---|
52 | this->indices = NULL; |
---|
53 | this->vertices = NULL; |
---|
54 | this->planeVertices = NULL; |
---|
55 | this->shader = NULL; |
---|
56 | activateDome = false; |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | /** |
---|
62 | * default destructor |
---|
63 | */ |
---|
64 | Skydome::~Skydome() |
---|
65 | { |
---|
66 | PRINTF(0)("Deleting Skydome\n"); |
---|
67 | |
---|
68 | if (glIsTexture(texture)) |
---|
69 | glDeleteTextures(1, &texture); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | void Skydome::setShader(Shader* shader) |
---|
74 | { |
---|
75 | this->shader = shader; |
---|
76 | } |
---|
77 | |
---|
78 | void Skydome::setTexture(GLuint texture) |
---|
79 | { |
---|
80 | this->texture = texture; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | void Skydome::activate() |
---|
85 | { |
---|
86 | this->activateDome = true; |
---|
87 | } |
---|
88 | |
---|
89 | void Skydome::deactivate() |
---|
90 | { |
---|
91 | this->activateDome = false; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | void Skydome::draw() const |
---|
96 | { |
---|
97 | if(!activateDome) |
---|
98 | return; |
---|
99 | |
---|
100 | glPushAttrib(GL_ENABLE_BIT); |
---|
101 | |
---|
102 | glDisable(GL_LIGHTING); |
---|
103 | glDisable(GL_BLEND); |
---|
104 | glDisable(GL_FOG); |
---|
105 | |
---|
106 | glEnable(GL_TEXTURE_3D); |
---|
107 | glBindTexture(GL_TEXTURE_3D, texture); |
---|
108 | |
---|
109 | this->shader->activateShader(); |
---|
110 | |
---|
111 | glPushMatrix(); |
---|
112 | glTranslatef(0.0f,pRadius,0.0f); |
---|
113 | |
---|
114 | glBegin(GL_TRIANGLES); |
---|
115 | for (int i=0; i < numIndices; i++) |
---|
116 | { |
---|
117 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
118 | |
---|
119 | glTexCoord2f(planeVertices[indices[i]].u, planeVertices[indices[i]].v); |
---|
120 | glVertex3f(planeVertices[indices[i]].x, planeVertices[indices[i]].y, planeVertices[indices[i]].z); |
---|
121 | } |
---|
122 | glEnd(); |
---|
123 | |
---|
124 | WorldEntity::draw(); |
---|
125 | |
---|
126 | glPopMatrix(); |
---|
127 | |
---|
128 | this->shader->deactivateShader(); |
---|
129 | |
---|
130 | glPopAttrib(); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | void Skydome::generateSkyPlane(int divisions, float planetRadius, float atmosphereRadius, float hTile, float vTile) |
---|
135 | { |
---|
136 | PRINTF(0)("Generating a sky plane\n"); |
---|
137 | |
---|
138 | // Make sure our vertex array is clear |
---|
139 | if (planeVertices) |
---|
140 | { |
---|
141 | delete planeVertices; |
---|
142 | planeVertices = NULL; |
---|
143 | } |
---|
144 | |
---|
145 | // Make sure our index array is clear |
---|
146 | if (indices) |
---|
147 | { |
---|
148 | delete indices; |
---|
149 | indices = NULL; |
---|
150 | } |
---|
151 | |
---|
152 | // Set the number of divisions into a valid range |
---|
153 | int divs = divisions; |
---|
154 | if (divisions < 1) |
---|
155 | divs = 1; |
---|
156 | |
---|
157 | if (divisions > 256) |
---|
158 | divs = 256; |
---|
159 | |
---|
160 | pRadius = planetRadius; |
---|
161 | |
---|
162 | // Initialize the Vertex and indices arrays |
---|
163 | numPlaneVertices = (divs + 1) * (divs + 1); // 1 division would give 4 verts |
---|
164 | numIndices = divs * divs * 2 * 3; // 1 division would give 6 indices for 2 tris |
---|
165 | |
---|
166 | planeVertices = new VertexInfo[numPlaneVertices]; |
---|
167 | memset(planeVertices, 0, sizeof(VertexInfo)); |
---|
168 | |
---|
169 | indices = new int[numIndices]; |
---|
170 | memset(indices, 0, sizeof(int)*numIndices); |
---|
171 | |
---|
172 | // Calculate some values we will need |
---|
173 | float plane_size = 2.0f * (float)sqrt((SQR(atmosphereRadius)-SQR(planetRadius))); |
---|
174 | float delta = plane_size/(float)divs; |
---|
175 | float tex_delta = 2.0f/(float)divs; |
---|
176 | |
---|
177 | // Variables we'll use during the dome's generation |
---|
178 | float x_dist = 0.0f; |
---|
179 | float z_dist = 0.0f; |
---|
180 | float x_height = 0.0f; |
---|
181 | float z_height = 0.0f; |
---|
182 | float height = 0.0f; |
---|
183 | |
---|
184 | VertexInfo SV; // temporary vertex |
---|
185 | |
---|
186 | for (int i=0;i <= divs;i++) |
---|
187 | { |
---|
188 | for (int j=0; j <= divs; j++) |
---|
189 | { |
---|
190 | x_dist = (-0.5f * plane_size) + ((float)j*delta); |
---|
191 | z_dist = (-0.5f * plane_size) + ((float)i*delta); |
---|
192 | |
---|
193 | x_height = (x_dist*x_dist) / atmosphereRadius; |
---|
194 | z_height = (z_dist*z_dist) / atmosphereRadius; |
---|
195 | height = x_height + z_height; |
---|
196 | |
---|
197 | SV.x = x_dist; |
---|
198 | SV.y = 0.0f - height; |
---|
199 | SV.z = z_dist; |
---|
200 | |
---|
201 | // Calculate the texture coordinates |
---|
202 | SV.u = hTile*((float)j * tex_delta*0.5f); |
---|
203 | SV.v = vTile*(1.0f - (float)i * tex_delta*0.5f); |
---|
204 | |
---|
205 | planeVertices[i*(divs+1)+j] = SV; |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | // Calculate the indices |
---|
210 | int index = 0; |
---|
211 | for (int i=0; i < divs;i++) |
---|
212 | { |
---|
213 | for (int j=0; j < divs; j++) |
---|
214 | { |
---|
215 | int startvert = (i*(divs+1) + j); |
---|
216 | |
---|
217 | // tri 1 |
---|
218 | indices[index++] = startvert; |
---|
219 | indices[index++] = startvert+1; |
---|
220 | indices[index++] = startvert+divs+1; |
---|
221 | |
---|
222 | // tri 2 |
---|
223 | indices[index++] = startvert+1; |
---|
224 | indices[index++] = startvert+divs+2; |
---|
225 | indices[index++] = startvert+divs+1; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|