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