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