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