[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 | #include "util/loading/resource_manager.h" |
---|
| 26 | |
---|
[9727] | 27 | #include "debug.h" |
---|
| 28 | |
---|
[8732] | 29 | #define DTOR (PI/180.0f) |
---|
| 30 | #define SQR(x) (x*x) |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | |
---|
[9715] | 34 | ObjectListDefinition(Skydome); |
---|
[9406] | 35 | |
---|
[8732] | 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"); |
---|
[9006] | 48 | |
---|
[9709] | 49 | this->registerObject(this, Skydome::_objectList); |
---|
[8732] | 50 | this->toList(OM_BACKGROUND); |
---|
[9006] | 51 | this->toReflectionList(); |
---|
[9235] | 52 | this->indices = NULL; |
---|
| 53 | this->vertices = NULL; |
---|
| 54 | this->planeVertices = NULL; |
---|
| 55 | this->shader = NULL; |
---|
[9006] | 56 | activateDome = false; |
---|
| 57 | |
---|
[8732] | 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * default destructor |
---|
| 63 | */ |
---|
| 64 | Skydome::~Skydome() |
---|
| 65 | { |
---|
| 66 | PRINTF(0)("Deleting Skydome\n"); |
---|
[9006] | 67 | |
---|
[8732] | 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 | |
---|
[9006] | 84 | void Skydome::activate() |
---|
| 85 | { |
---|
| 86 | this->activateDome = true; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void Skydome::deactivate() |
---|
| 90 | { |
---|
| 91 | this->activateDome = false; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
[8732] | 95 | void Skydome::draw() const |
---|
| 96 | { |
---|
[9006] | 97 | if(!activateDome) |
---|
| 98 | return; |
---|
[9235] | 99 | |
---|
[8732] | 100 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 101 | |
---|
| 102 | glDisable(GL_LIGHTING); |
---|
| 103 | glDisable(GL_BLEND); |
---|
[9235] | 104 | glDisable(GL_FOG); |
---|
[8732] | 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 | |
---|
[9709] | 124 | WorldEntity::draw(); |
---|
[9235] | 125 | |
---|
[8732] | 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 | { |
---|
[9235] | 136 | PRINTF(0)("Generating a sky plane\n"); |
---|
[9006] | 137 | |
---|
[8732] | 138 | // Make sure our vertex array is clear |
---|
[9006] | 139 | if (planeVertices) |
---|
[8732] | 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; |
---|
[9006] | 154 | if (divisions < 1) |
---|
[8732] | 155 | divs = 1; |
---|
| 156 | |
---|
[9006] | 157 | if (divisions > 256) |
---|
| 158 | divs = 256; |
---|
[8732] | 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; |
---|
[9006] | 176 | |
---|
[8732] | 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 | |
---|
[9006] | 217 | // tri 1 |
---|
[8732] | 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 | } |
---|
[9006] | 227 | } |
---|
[8732] | 228 | } |
---|