Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/skydome.cc @ 9715

Last change on this file since 9715 was 9715, checked in by bensch, 18 years ago

renamed newclassid to classid and newobjectlist to objectlist

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