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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
//------------------------------------------------------
//
// FOG By Johan 'Mace' GLysing - mace-x@usa.net
//
// Converted to ClanLib by Kenneth Gangstoe
//
//------------------------------------------------------
#include <stdio.h>
#include <ClanLib/gl.h>
#include "glfog.h"
using namespace std;
//------------------------------------------------------
// Todo note: These data should be put into class
// BASIC APPLICATION STUFF
static GLint i, j; // For loops
static GLint winW = 640, winH = 480; // Window width and height
// AUTO SWITCH
static GLboolean useTimer = GL_TRUE; // Auto change mode every 25th second
static GLint timer = 0; // The timer
// LIGHTS
static GLint fogMode = 0; // Different patterns
static GLint lights = 10; // Number of lights
static GLfloat lightX[10], lightY[10], lightZ[10], lightT[10];
// LIGHT REFLECTIVE COLORS
static const GLubyte lightR[] = { 255, 1, 1, 255, 255, 1, 150, 1, 255, 255 };
static const GLubyte lightG[] = { 1, 255, 1, 1, 255, 255, 1, 150, 150, 1 };
static const GLubyte lightB[] = { 1, 1, 255, 255, 1, 255, 255, 255, 1, 100 };
// LIGHT COLORS
static const GLubyte lightR2[] = { 255, 100, 100, 255, 255, 100, 150, 50, 255, 255 };
static const GLubyte lightG2[] = { 100, 255, 100, 100, 255, 255, 100, 150, 150, 50 };
static const GLubyte lightB2[] = { 100, 100, 255, 255, 100, 255, 255, 255, 50, 100 };
// FOG
static GLint fogW = 5, fogH = 8, fogNum; // Width/Height of fog cloud and w*h
static GLfloat fogX[100], fogY[100], fogZ[100], fogANG[100]; // Fog coords and angle
static GLubyte fogC[100][3]; // Fog colors
static GLint fogCLR = 0; // Colors of fog cloud
static const GLint fogR[] = { 50, 50, 10, 10, 50, 50, 10 };
static const GLint fogG[] = { 50, 10, 50, 10, 10, 50, 50 };
static const GLint fogB[] = { 50, 10, 10, 50, 50, 10, 50 };
// DISPLAY LISTS
static GLuint Background, fogPuff, lightSphere; // Display lists
// TIMER
static GLint startTime = 0, endTime = 0;
//------------------------------------------------------
FogApplication app;
//------------------------------------------------------
int FogApplication::main(int argc, char** argv)
{
// Create a console window for text-output if not available
CL_ConsoleWindow console("Console");
console.redirect_stdio();
try
{
CL_SetupCore setup_core;
CL_SetupGL setup_gl;
CL_SetupDisplay setup_display;
CL_DisplayWindow window("GLFog", 640, 480);
glstate = new CL_OpenGLState(window.get_gc());
// Connect the Window close event
CL_Slot slot_quit = window.sig_window_close().connect(this, &FogApplication::on_window_close);
init();
run();
delete manager;
delete texture[0];
delete texture[1];
delete texture[2];
}
catch (CL_Error err)
{
cout << "Exception caught: " << err.message.c_str() << endl;
// Display console close message and wait for a key
console.display_close_message();
}
return 0;
}
void FogApplication::init()
{
init_gl();
init_textures();
init_display_lists();
init_fog();
init_lightpos();
}
void FogApplication::init_gl()
{
glstate->set_active();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 100.0);
glMatrixMode (GL_MODELVIEW);
glShadeModel(GL_FLAT);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
gluLookAt(0, 5, 10, 0, 0, 0, 0, 1, 0);
}
void FogApplication::init_textures()
{
manager = new CL_ResourceManager("glfog.xml", false);
texture[0] = new CL_OpenGLSurface("floor", manager);
texture[1] = new CL_OpenGLSurface("fog", manager);
texture[2] = new CL_OpenGLSurface("sphere", manager);
}
void FogApplication::init_fog()
{
GLint i, w, h, num;
GLfloat ft = 0, stepsX, stepsZ, split;
// INIT FOG
i = 0;
stepsX = 3.0f;
stepsZ = 2.0f;
split = stepsX*(fogW/2);
for(h = 0; h < fogH; h++)
{
for(w = 0; w < fogW; w++)
{
num = (h*fogW)+w;
fogX[num] = ((stepsX+ft)*w)-split;
fogY[num] = 0;
fogZ[num] = (stepsZ*h)-split;
i += 41;
if(i > 359) i -= 359;
if(i < 50) ft = -0.25f;
else if(i < 100) ft = -0.2f;
else if(i < 150) ft = -0.12f;
else if(i < 200) ft = 0.0f;
else if(i < 250) ft = 0.12f;
else if(i < 300) ft = 0.2f;
else ft = 0.25f;
fogANG[num] = i;
fogC[num][0] = 255;
fogC[num][1] = 255;
fogC[num][2] = 255;
}
}
fogNum = fogH*fogW;
}
void FogApplication::init_lightpos()
{
GLint i;
GLfloat ft = 0;
ft = (6.28 / lights);
for(i = 0; i < lights; i++)
{
lightX[i] = 5 * cos( 1.57 + (ft*i) );
lightY[i] = 0;
lightZ[i] = 5 * sin( 1.57 + (ft*i) );
lightT[i] = 1.57+(ft*i);
}
}
void FogApplication::init_display_lists()
{
glstate->set_active();
// RENDERS A BLACK BG QUAD IN 2D COORD SYSTEM
Background = glGenLists(1);
glNewList(Background, GL_COMPILE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, winW, 0, winH);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(0, 0, 0);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(winW, 0);
glVertex2f(winW, winH);
glVertex2f(0, winH);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
texture[0]->bind();
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-8, 0, -8);
glTexCoord2f(0, 1); glVertex3f(-8, 0, 8);
glTexCoord2f(1, 1); glVertex3f( 8, 0, 8);
glTexCoord2f(1, 0); glVertex3f( 8, 0, -8);
glEnd();
glEndList();
// FOG
fogPuff = glGenLists(1);
glNewList(fogPuff, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-3, 0, -3);
glTexCoord2f(0, 1); glVertex3f(-3, 0, 3);
glTexCoord2f(1, 1); glVertex3f( 3, 0, 3);
glTexCoord2f(1, 0); glVertex3f( 3, 0, -3);
glEnd();
glEndList();
// LIGHTSPHERES
lightSphere = glGenLists(1);
glNewList(lightSphere, GL_COMPILE);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-0.5, 0, -0.5);
glTexCoord2f(0, 1); glVertex3f(-0.5, 0, 0.5);
glTexCoord2f(1, 1); glVertex3f( 0.5, 0, 0.5);
glTexCoord2f(1, 0); glVertex3f( 0.5, 0, -0.5);
glEnd();
glEndList();
}
void FogApplication::draw()
{
glstate->set_active();
// RENDER THE BG
glClear(GL_DEPTH_BUFFER_BIT);
glCallList(Background);
// RENDER THE FOG CLOUD
texture[1]->bind();
for(i = 0; i < fogNum; i++)
{
glColor3ub(fogC[i][0], fogC[i][1], fogC[i][2]);
glPushMatrix();
glTranslatef(fogX[i], fogY[i]+1, fogZ[i]);
glRotatef(40, 1, 0, 0);
glRotatef(fogANG[i], 0, 1, 0);
glCallList(fogPuff);
glPopMatrix();
fogANG[i]++;
if(fogANG[i] > 359) fogANG[i] = 0;
}
// LIGHT SPHERES
texture[2]->bind();
for(i = 0; i < lights; i++)
{
glColor3ub(lightR2[i], lightG2[i], lightB2[i]);
glPushMatrix();
glTranslatef(lightX[i], lightY[i]+1, lightZ[i]);
glRotatef(60, 1, 0, 0);
glCallList(lightSphere);
if(fogMode == 1)
{
glTranslatef(0, -4, 0);
glCallList(lightSphere);
glTranslatef(0, -4, 0);
glCallList(lightSphere);
glTranslatef(0, -4, 0);
glCallList(lightSphere);
}
glPopMatrix();
}
}
void FogApplication::set_lights()
{
GLint i, j;
GLfloat vX, vY, vZ, L;
GLint r[10], g[10], b[10], R, G, B;
// CALCULATE THE FOG COLORS FROM THE LIGHT POSITIONS
for(j = 0; j < fogNum; j++)
{
for(i = 0; i < lights; i++)
{
vX = lightX[i] - fogX[j];
vY = lightY[i] - fogY[j];
vZ = lightZ[i] - fogZ[j];
L = sqrt(vX*vX + vY*vY + vZ*vZ);
r[i] = (GLint) (lightR[i]/L);
g[i] = (GLint) (lightG[i]/L);
b[i] = (GLint) (lightB[i]/L);
}
R = 0;
G = 0;
B = 0;
for(i = 0; i < lights; i++)
{
R += r[i];
G += g[i];
B += b[i];
}
R /= lights;
G /= lights;
B /= lights;
if(R > 200) R = 200;
if(G > 200) G = 200;
if(B > 200) B = 200;
fogC[j][0] = fogR[fogCLR]+(R-255);
fogC[j][1] = fogG[fogCLR]+(G-255);
fogC[j][2] = fogB[fogCLR]+(B-255);
}
// MOVE THE LIGHTS
for(i = 0; i < lights; i++)
{
if(fogMode == 0)
{
lightX[i] = 5 * cos(lightT[i]);
lightY[i] = 0.5 * cos((lightT[i]*2));
lightZ[i] = 5 * sin(lightT[i]);
}
else if(fogMode == 1)
{
lightX[i] = 5 * cos(lightT[i]);
lightY[i] = 1 * cos((lightT[i]*4));
lightZ[i] = 5 * sin(lightT[i]);
}
else if(fogMode == 2)
{
lightX[i] = 3 * cos((lightT[i]*200));
lightY[i] = 0;
lightZ[i] = 3 * sin((lightT[i]*200));
}
else if(fogMode == 3)
{
lightX[i] = lightT[i] * cos(lightT[i]*4);
lightY[i] = lightT[i];
lightZ[i] = lightT[i] * sin(lightT[i]*4);
}
else if(fogMode == 4)
{
lightX[i] = 4 * cos((lightT[i]*2));
lightY[i] = 2 * cos(lightT[i]);
lightZ[i] = 3 * sin(lightT[i]);
}
lightT[i] += 0.034f;
if(lightT[i] > 6.28) lightT[i] -= 6.28f;
}
}
void FogApplication::check_timers()
{
if(startTime == 0)
{
startTime = CL_System::get_time();
set_lights();
}
else {
endTime = CL_System::get_time();
// UPDATE
if(endTime - startTime > 0)
{
startTime = 0;
if(useTimer)
{
timer++;
if(timer > 450)
{
timer = 0;
i = fogMode;
j = fogCLR;
fogMode = (rand() % 5);
fogCLR = (rand() % 7);
if(i == fogMode)
{
fogMode++;
if(fogMode > 4) fogMode = 0;
}
if(j == fogCLR)
{
fogCLR++;
if(fogCLR > 6) fogCLR = 0;
}
}
}
}
}
}
void FogApplication::run()
{
bool key_LEFT, key_RIGHT, key_UP, key_DOWN;
bool key_F1, key_F3;
key_LEFT = key_RIGHT = key_UP = key_DOWN = key_F1 = key_F3 = false;
do {
check_timers();
draw();
CL_Display::flip();
CL_System::keep_alive(1);
if(CL_Keyboard::get_keycode(CL_KEY_UP) && !key_UP)
{
fogMode++;
if(fogMode > 4) fogMode = 0;
}
key_UP = CL_Keyboard::get_keycode(CL_KEY_UP);
if(CL_Keyboard::get_keycode(CL_KEY_DOWN) && !key_DOWN)
{
fogMode--;
if(fogMode < 0) fogMode = 4;
}
key_DOWN = CL_Keyboard::get_keycode(CL_KEY_DOWN);
if(CL_Keyboard::get_keycode(CL_KEY_RIGHT) && !key_RIGHT)
{
fogCLR++;
if(fogCLR > 6) fogCLR = 0;
}
key_RIGHT = CL_Keyboard::get_keycode(CL_KEY_RIGHT);
if(CL_Keyboard::get_keycode(CL_KEY_LEFT) && !key_LEFT)
{
fogCLR--;
if(fogCLR < 0) fogCLR = 6;
}
key_LEFT = CL_Keyboard::get_keycode(CL_KEY_LEFT);
if(CL_Keyboard::get_keycode(CL_KEY_F1) && !key_F1)
{
srand((GLint)CL_System::get_time());
useTimer = !useTimer;
timer = 450;
}
key_F1 = CL_Keyboard::get_keycode(CL_KEY_F1);
if(CL_Keyboard::get_keycode(CL_KEY_F3) && !key_F3)
{
if(lights == 3) lights = 6;
else if(lights == 6) lights = 10;
else if(lights == 10) lights = 3;
init_lightpos();
}
key_F3 = CL_Keyboard::get_keycode(CL_KEY_F3);
if(CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
quit = true;
} while(quit == false);
}
void FogApplication::on_window_close()
{
quit = true;
}
|