1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2001 - 2004 Simon Goodall, University of Southampton
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sage/sage.h>
#include <sage/GL.h>
#include <math.h>
#include <iostream>
#include "src/Render.h"
#include "src/System.h"
#include "src/Graphics.h"
#include "src/Calendar.h"
#include "renderers/RenderSystem.h"
#define SQR(X) ((X))
#include "SkyDome.h"
#ifdef USE_MMGR
#include "common/mmgr.h"
#endif
#define VERTEX_COORDS { \
-1.0f, 1.0f, 1.0f, \
-1.0f, -1.0f, 1.0f, \
1.0f, -1.0f, 1.0f, \
1.0f, 1.0f, 1.0f, \
\
-1.0f, 1.0f, -1.0f, \
-1.0f, 1.0f, 1.0f, \
1.0f, 1.0f, 1.0f, \
1.0f, 1.0f, -1.0f, \
\
1.0f, -1.0f, -1.0f, \
1.0f, -1.0f, 1.0f, \
-1.0f, -1.0f, 1.0f, \
-1.0f, -1.0f, -1.0f, \
\
1.0f, 1.0f, -1.0f, \
1.0f, 1.0f, 1.0f, \
1.0f, -1.0f, 1.0f, \
1.0f, -1.0f, -1.0f, \
\
-1.0f ,-1.0f, -1.0f, \
-1.0f, -1.0f, 1.0f, \
-1.0f, 1.0f, 1.0f, \
-1.0f, 1.0f, -1.0f, \
\
-1.0f, -1.0f, -1.0f, \
-1.0f, 1.0f, -1.0f, \
1.0f, 1.0f, -1.0f, \
1.0f, -1.0f, -1.0f }
#define DIST 100.0f
#define QUAD_COORDS { \
-DIST, DIST, 1.0f, \
DIST, DIST, 1.0f, \
DIST, -DIST, 1.0f, \
-DIST, -DIST, 1.0f}
#define QUAD_TEX { \
0, 0, \
5, 0, \
5, 5, \
0, 5}
namespace Sear {
float SkyDome::m_box[] = VERTEX_COORDS;
float SkyDome::m_quad_v[] = QUAD_COORDS;
float SkyDome::m_quad_t[] = QUAD_TEX;
SkyDome::SkyDome(float radius, int levels, int segments) :
m_verts(NULL), m_texCoords(NULL),
m_vb_verts(0), m_vb_texCoords(0)
{
m_size = segments * (levels + 1); // extra level for the skirt
// disable for now
sage_ext[GL_ARB_VERTEX_BUFFER_OBJECT] = false;
domeInit(radius, levels, segments);
}
SkyDome::~SkyDome()
{
delete [] m_verts;
delete [] m_texCoords;
}
float* SkyDome::genLevelVerts(float a2, float a22, float radius, int segments, float* verts)
{
float segmentInc = (2.0f * M_PI) / segments;
for (int j = 0; j < segments; ++j) {
float a1 = ((float)j * segmentInc);
float a11 = ((float)(j + 1) * segmentInc);
float x,y,z;
x = sin(a1) * sin(a2) * SQR(radius);
y = cos(a1) * sin(a2) * SQR(radius);
z = cos(a2) * radius;
(*verts++) = x;
(*verts++) = y;
(*verts++) = z;
x = sin(a11) * sin(a2) * SQR(radius);
y = cos(a11) * sin(a2) * SQR(radius);
z = cos(a2) * radius;
(*verts++) = x;
(*verts++) = y;
(*verts++) = z;
x = sin(a11) * sin(a22) * SQR(radius);
y = cos(a11) * sin(a22) * SQR(radius);
z = cos(a22) * radius;
(*verts++) = x;
(*verts++) = y;
(*verts++) = z;
x = sin(a1) * sin(a22) * SQR(radius);
y = cos(a1) * sin(a22) * SQR(radius);
z = cos(a22) * radius;
(*verts++) = x;
(*verts++) = y;
(*verts++) = z;
}
return verts;
}
float *SkyDome::genVerts(float radius, int levels, int segments)
{
float *verts = new float[m_size * 3 * 4];
float levelInc = M_PI / (float)levels / 2.0f;
float* vptr = genLevelVerts((M_PI * 3) / 4, M_PI / 2, radius, segments, verts);
for (int i = 0; i < levels; ++i) {
float a2 = ((float)i * levelInc);
float a22 = ((float)(i + 1) * levelInc);
vptr = genLevelVerts(a2, a22, radius, segments, vptr);
}
return verts;
}
float *SkyDome::genTexCoords(float radius, int levels, int segments)
{
float *tex = new float[m_size * 2 * 4];
int tex_counter = -1;
// tex coords for the skirt
for (int k = 0; k < (segments * 8); ++k) {
tex[++tex_counter] = 0.0f;
}
for (int i = 0; i < levels; ++i) {
float v1 = (float)(levels - i)/(float)(levels);
float v2 = (float)(levels - (i + 1))/(float)(levels);
for (int j = 0; j < segments; ++j) {
tex[++tex_counter] = 0.0f;
tex[++tex_counter] = v1;
tex[++tex_counter] = 0.0f;
tex[++tex_counter] = v1;
tex[++tex_counter] = 0.0f;
tex[++tex_counter] = v2;
tex[++tex_counter] = 0.0f;
tex[++tex_counter] = v2;
}
}
return tex;
}
void SkyDome::domeInit(float radius, int levels, int segments) {
// Get texture handles
m_textures[0] = RenderSystem::getInstance().requestTexture("atmosphere");
m_textures[1] = RenderSystem::getInstance().requestTexture("cloud_layer_1");
m_textures[2] = RenderSystem::getInstance().requestTexture("cloud_layer_2");
m_verts = genVerts(radius, levels, segments);
m_texCoords = genTexCoords(radius, levels, segments);
if (sage_ext[GL_ARB_VERTEX_BUFFER_OBJECT]) {
glGenBuffersARB(1, &m_vb_verts);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_vb_verts);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, m_size * 3 * 4 * sizeof(float), m_verts, GL_STATIC_DRAW_ARB);
delete [] m_verts;
m_verts = NULL;
glGenBuffersARB(1, &m_vb_texCoords);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_vb_texCoords);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, m_size * 2 * 4 * sizeof(float), m_texCoords, GL_STATIC_DRAW_ARB);
delete [] m_texCoords;
m_texCoords = NULL;
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
}
}
void SkyDome::getHorizonColors()
{
m_horizonColors.clear();
RenderSystem::getInstance().switchTexture(m_textures[0]); // atmosphere tex
GLint width, height;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
GLubyte* skyTexels = new GLubyte[width * height * 4];
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, skyTexels);
int glerr = glGetError();
if (glerr != GL_NO_ERROR) {
std::cerr << "got error while trying to read atmosphere texels" << std::endl;
return;
}
GLubyte* texel = skyTexels;
for (int S=0; S < width; ++S) {
Color_4 c;
c.r = *texel++;
c.g = *texel++;
c.b = *texel++;
c.a = *texel++;
m_horizonColors.push_back(c);
}
delete[] skyTexels;
}
void SkyDome::updateFogColor(float t)
{
if (m_horizonColors.empty()) getHorizonColors();
if (t == 1.0f) t = 0.0f; // ensure t is in the range [0.0 .. 1.0)
t *= m_horizonColors.size(); // t is now in [0.0 ... num_colors)
// offset so integral values map to half way between two pixels
t -= 0.5f;
if (t < 0.0) t+= m_horizonColors.size();
// compute I and J indices into horizon colors, then interpolate
// note this code uses lots of local variables, for clarity, and we only
// run this once per frame
unsigned int I = static_cast<unsigned int>(t); // truncation is desired here
unsigned int J = (I == (m_horizonColors.size() - 1)? 0 : I + 1);
Color_4 lower = m_horizonColors[I],
upper = m_horizonColors[J];
const float interp = t - I, // get the fractional part of t
invInterp = 1.0f - interp;
float color[4];
color[0] = ((lower.r * invInterp) + (upper.r * interp)) / 255.0f;
color[1] = ((lower.g * invInterp) + (upper.g * interp)) / 255.0f;
color[2] = ((lower.b * invInterp) + (upper.b * interp)) / 255.0f;
color[3] = 1.0f;
glFogfv(GL_FOG_COLOR, color);
}
void SkyDome::render()
{
glColor3f(1.0f, 1.0f, 1.0f);
// ++counter;
// #define INCR 24000
// counter = counter % INCR;
// float val = (float)(counter) / (float)INCR;
Calendar *cal = System::instance()->getCalendar();
float val = cal->getHours();
val *= (float)cal->getMinutesPerHour();
val += (float)cal->getMinutes();
val *= (float)cal->getSecondsPerMinute();
val += (float)cal->getSeconds();
val /= (float)(cal->getSecondsPerMinute() * cal->getMinutesPerHour() * cal->getHoursPerDay());
updateFogColor(val);
glEnableClientState(GL_VERTEX_ARRAY);
// Select atmosphere texture
RenderSystem::getInstance().switchTexture(m_textures[0]);
// Translate texture for time of day
// and simple cloud animation
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glTranslatef(val, 0.0f,0.0f);
glMatrixMode(GL_MODELVIEW);
// Set the dome vetices
if (sage_ext[GL_ARB_VERTEX_BUFFER_OBJECT]) {
glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_vb_verts);
}
glVertexPointer(3, GL_FLOAT, 0, m_verts);
glEnable(GL_BLEND);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Setup dome texture coords
if (sage_ext[GL_ARB_VERTEX_BUFFER_OBJECT]) {
glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_vb_texCoords);
}
glTexCoordPointer(2, GL_FLOAT, 0, m_texCoords);
// Renderdome
glDrawArrays(GL_QUADS, 0, m_size * 4);
glDisable(GL_BLEND);
// Render Cloud layer one
glEnable(GL_BLEND);
RenderSystem::getInstance().switchTexture(m_textures[1]);
if (sage_ext[GL_ARB_VERTEX_BUFFER_OBJECT]) {
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
}
// Render large polygon for cloud layer
// TODO split into smaller polys
// turn down edges so its not so flat looking
if (sage_ext[GL_ARB_VERTEX_BUFFER_OBJECT]) {
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
}
glVertexPointer(3, GL_FLOAT, 0, &m_quad_v[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &m_quad_t[0]);
glDrawArrays(GL_QUADS, 0, 4);
// Translate further so cloud layers move
// at different speeds
glMatrixMode(GL_TEXTURE);
glTranslatef(val, 0.0f,0.0f);
glMatrixMode(GL_MODELVIEW);
// Render cloud layer two
RenderSystem::getInstance().switchTexture(m_textures[2]);
glVertexPointer(3, GL_FLOAT, 0, &m_quad_v[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &m_quad_t[0]);
glDrawArrays(GL_QUADS, 0, 4);
// reset texture matrix
glMatrixMode(GL_TEXTURE);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
// Reset states
glDisable(GL_BLEND);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
// Disable vertex buffer objects
if (sage_ext[GL_ARB_VERTEX_BUFFER_OBJECT]) {
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
}
}
} /* namespace Sear */
|