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