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