Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/inputdevice/src/world_entities/environments/skydome.cc @ 10635

Last change on this file since 10635 was 10618, checked in by bknecht, 18 years ago

merged cleanup into trunk (only improvements)

File size: 4.6 KB
Line 
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
26#include "debug.h"
27
28#define DTOR (PI/180.0f)
29#define SQR(x) (x*x)
30
31
32
33ObjectListDefinition(Skydome);
34
35/**
36 *  initializes a skybox from a XmlElement
37 */
38Skydome::Skydome()
39{
40  this->init();
41}
42
43
44void Skydome::init()
45{
46  PRINTF(4)("Skydome init\n");
47
48  this->registerObject(this, Skydome::_objectList);
49  this->toList(OM_BACKGROUND);
50  this->toReflectionList();
51  this->indices = NULL;
52  this->vertices = NULL;
53  this->planeVertices = NULL;
54  this->shader = NULL;
55  activateDome = false;
56
57}
58
59
60/**
61 *  default destructor
62 */
63Skydome::~Skydome()
64{
65  PRINTF(4)("Deleting Skydome\n");
66
67  if (glIsTexture(texture))
68    glDeleteTextures(1, &texture);
69}
70
71
72void Skydome::setShader(Shader* shader)
73{
74  this->shader = shader;
75}
76
77void Skydome::setTexture(GLuint texture)
78{
79  this->texture = texture;
80}
81
82
83void Skydome::activate()
84{
85  this->activateDome = true;
86}
87
88void Skydome::deactivate()
89{
90  this->activateDome = false;
91}
92
93
94void Skydome::draw() const
95{
96  if(!activateDome)
97    return;
98
99  glPushAttrib(GL_ENABLE_BIT);
100
101  glDisable(GL_LIGHTING);
102  glDisable(GL_BLEND);
103  glDisable(GL_FOG);
104
105  glEnable(GL_TEXTURE_3D);
106  glBindTexture(GL_TEXTURE_3D, texture);
107
108  if ( this->shader && Shader::isSupported() )
109  {
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();
130  }
131  glPopAttrib();
132}
133
134
135void Skydome::generateSkyPlane(int divisions, float planetRadius, float atmosphereRadius, float hTile, float vTile)
136{
137  PRINTF(0)("Generating a sky plane\n");
138
139  // Make sure our vertex array is clear
140  if (planeVertices)
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;
155  if (divisions < 1)
156    divs = 1;
157
158  if (divisions > 256)
159    divs = 256;
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;
177
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
218      // tri 1
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    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.