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