| [7504] | 1 | /* | 
|---|
| [7652] | 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
| [7504] | 3 |  | 
|---|
| [7652] | 4 | Copyright (C) 2004 orx | 
|---|
| [7504] | 5 |  | 
|---|
| [7652] | 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. | 
|---|
| [7504] | 10 |  | 
|---|
|  | 11 | ### File Specific: | 
|---|
| [7652] | 12 | main-programmer: hdavid, amaechler | 
|---|
| [7504] | 13 | */ | 
|---|
|  | 14 |  | 
|---|
|  | 15 | #include "volfog_effect.h" | 
|---|
|  | 16 |  | 
|---|
|  | 17 | #include "util/loading/load_param.h" | 
|---|
|  | 18 | #include "util/loading/factory.h" | 
|---|
|  | 19 |  | 
|---|
| [7760] | 20 | #include "p_node.h" | 
|---|
|  | 21 | #include "state.h" | 
|---|
|  | 22 |  | 
|---|
| [7504] | 23 |  | 
|---|
| [7759] | 24 | #define GLX_GLXEXT_PROTOTYPES | 
|---|
|  | 25 | #include <GL/glx.h> | 
|---|
| [7795] | 26 | // #include <GL/glut.h> | 
|---|
| [7759] | 27 |  | 
|---|
|  | 28 | //#include <GL/glext.h> //OpenGL Extensions | 
|---|
| [8316] | 29 | //#include <GL/glxext.h> // GLX Extensions | 
|---|
| [7759] | 30 |  | 
|---|
|  | 31 | #ifndef GL_EXT_fog_coord | 
|---|
|  | 32 | #define GL_EXT_fog_coord 1 | 
|---|
|  | 33 | typedef void (APIENTRY * PFNGLFOGCOORDFEXTPROC) (GLfloat coord); | 
|---|
|  | 34 | typedef void (APIENTRY * PFNGLFOGCOORDFVEXTPROC) (const GLfloat *coord); | 
|---|
|  | 35 | typedef void (APIENTRY * PFNGLFOGCOORDDEXTPROC) (GLdouble coord); | 
|---|
|  | 36 | typedef void (APIENTRY * PFNGLFOGCOORDDVEXTPROC) (const GLdouble *coord); | 
|---|
|  | 37 | typedef void (APIENTRY * PFNGLFOGCOORDPOINTEREXTPROC) (GLenum type, GLsizei stride, const GLvoid *pointer); | 
|---|
|  | 38 | #endif | 
|---|
|  | 39 |  | 
|---|
|  | 40 | PFNGLFOGCOORDFEXTPROC glFogCoordfEXT = 0; | 
|---|
|  | 41 | PFNGLFOGCOORDFVEXTPROC glFogCoordfvEXT = 0; | 
|---|
|  | 42 | PFNGLFOGCOORDDEXTPROC glFogCoorddEXT = 0; | 
|---|
|  | 43 | PFNGLFOGCOORDDVEXTPROC glFogCoorddvEXT = 0; | 
|---|
|  | 44 | PFNGLFOGCOORDPOINTEREXTPROC glFogCoordPointerEXT = 0; | 
|---|
|  | 45 |  | 
|---|
|  | 46 |  | 
|---|
| [7504] | 47 | using namespace std; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | CREATE_FACTORY(VolFogEffect, CL_VOLFOG_EFFECT); | 
|---|
|  | 50 |  | 
|---|
|  | 51 | VolFogEffect::VolFogEffect(const TiXmlElement* root) | 
|---|
|  | 52 | { | 
|---|
| [7652] | 53 | this->setClassID(CL_VOLFOG_EFFECT, "VolFogEffect"); | 
|---|
| [7504] | 54 |  | 
|---|
| [7652] | 55 | if (root != NULL) | 
|---|
|  | 56 | this->loadParams(root); | 
|---|
| [7532] | 57 |  | 
|---|
| [7652] | 58 | // Initialize Effect | 
|---|
|  | 59 | this->init(); | 
|---|
| [7546] | 60 |  | 
|---|
| [7652] | 61 | // Activate Effect | 
|---|
|  | 62 | this->activate(); | 
|---|
| [7504] | 63 | } | 
|---|
|  | 64 |  | 
|---|
|  | 65 |  | 
|---|
| [7516] | 66 | VolFogEffect::~VolFogEffect() | 
|---|
| [7504] | 67 | { | 
|---|
| [7652] | 68 | this->deactivate(); | 
|---|
| [7504] | 69 | } | 
|---|
|  | 70 |  | 
|---|
|  | 71 | void VolFogEffect::loadParams(const TiXmlElement* root) | 
|---|
|  | 72 | { | 
|---|
| [7652] | 73 | WeatherEffect::loadParams(root); | 
|---|
| [7504] | 74 | } | 
|---|
|  | 75 |  | 
|---|
|  | 76 | bool VolFogEffect::init() | 
|---|
| [7520] | 77 | { | 
|---|
| [7652] | 78 | PRINTF(0)("Initalize VolFogEffect\n"); | 
|---|
| [7504] | 79 |  | 
|---|
| [7759] | 80 | // set fog mode | 
|---|
| [7760] | 81 | GLenum fogMode  = GL_EXP; | 
|---|
| [7759] | 82 |  | 
|---|
|  | 83 | // set fog density | 
|---|
| [7760] | 84 | float  fogDensity  = 0.001f; | 
|---|
| [7759] | 85 |  | 
|---|
|  | 86 | // set fog near & far distance | 
|---|
| [7760] | 87 | float fogStart = 0.0f; | 
|---|
|  | 88 | float fogEnd   = 1000.0f; | 
|---|
| [7759] | 89 |  | 
|---|
| [7652] | 90 | // Set fog color | 
|---|
| [7760] | 91 | float fogColor[4] = {0.6f,0.58f,0.79f,0.0f}; | 
|---|
| [7504] | 92 |  | 
|---|
| [8316] | 93 |  | 
|---|
| [7759] | 94 | glFogCoordfEXT       = (PFNGLFOGCOORDFEXTPROC)glXGetProcAddressARB((const GLubyte*)"glFogCoordfEXT"); | 
|---|
|  | 95 | glFogCoordfvEXT      = (PFNGLFOGCOORDFVEXTPROC)glXGetProcAddressARB((const GLubyte*)"glFogCoordfvEXT"); | 
|---|
|  | 96 | glFogCoorddEXT       = (PFNGLFOGCOORDDEXTPROC)glXGetProcAddressARB((const GLubyte*)"glFogCoorddEXT"); | 
|---|
|  | 97 | glFogCoorddvEXT      = (PFNGLFOGCOORDDVEXTPROC)glXGetProcAddressARB((const GLubyte*)"glFogCoorddvEXT"); | 
|---|
|  | 98 | glFogCoordPointerEXT = (PFNGLFOGCOORDPOINTEREXTPROC)glXGetProcAddressARB((const GLubyte*)"glFogCoordPointerEXT"); | 
|---|
| [7557] | 99 |  | 
|---|
| [7759] | 100 | // set the fog attributes | 
|---|
|  | 101 | glFogf (GL_FOG_START,  fogStart); | 
|---|
|  | 102 | glFogf (GL_FOG_END,    fogEnd); | 
|---|
| [7760] | 103 | glFogfv(GL_FOG_COLOR,  fogColor); | 
|---|
| [7759] | 104 | glFogi (GL_FOG_MODE,   fogMode); | 
|---|
|  | 105 | glFogf (GL_FOG_DENSITY,fogDensity); | 
|---|
|  | 106 | glFogi (GL_FOG_COORDINATE_SOURCE_EXT,GL_FOG_COORDINATE_EXT); | 
|---|
| [7561] | 107 |  | 
|---|
| [7759] | 108 | // enable the fog | 
|---|
|  | 109 | glEnable(GL_FOG); | 
|---|
| [7557] | 110 |  | 
|---|
| [7759] | 111 | if (glewInit() == GLEW_OK) | 
|---|
|  | 112 | PRINTF(0)("glewInit OK\n"); | 
|---|
|  | 113 | else | 
|---|
|  | 114 | PRINTF(0)("glewInit failed\n"); | 
|---|
|  | 115 |  | 
|---|
| [7652] | 116 | if (glewGetExtension("GL_EXT_fog_coord")) | 
|---|
|  | 117 | { | 
|---|
|  | 118 | PRINTF(0)("GL_EXT_fog_coord extension found\n"); | 
|---|
|  | 119 | return true; | 
|---|
|  | 120 | } | 
|---|
|  | 121 | else | 
|---|
|  | 122 | { | 
|---|
|  | 123 | PRINTF(0)("GL_EXT_fog_coord extension NOT found\n"); | 
|---|
|  | 124 | return false; | 
|---|
|  | 125 | } | 
|---|
| [7546] | 126 | } | 
|---|
|  | 127 |  | 
|---|
|  | 128 |  | 
|---|
| [7504] | 129 | bool VolFogEffect::activate() | 
|---|
|  | 130 | { | 
|---|
| [7652] | 131 | PRINTF(0)("Activating VolFogEffect\n"); | 
|---|
| [8316] | 132 |  | 
|---|
|  | 133 | return true; | 
|---|
| [7504] | 134 | } | 
|---|
|  | 135 |  | 
|---|
|  | 136 | bool VolFogEffect::deactivate() | 
|---|
|  | 137 | { | 
|---|
| [7652] | 138 | PRINTF(0)("Deactivating VolFogEffect\n"); | 
|---|
| [7760] | 139 |  | 
|---|
|  | 140 | glDisable(GL_FOG); | 
|---|
| [8316] | 141 |  | 
|---|
|  | 142 | return true; | 
|---|
| [7504] | 143 | } | 
|---|
|  | 144 |  | 
|---|
| [7520] | 145 |  | 
|---|
| [7516] | 146 | /** | 
|---|
| [7652] | 147 | * draws the effect, if needed | 
|---|
|  | 148 | */ | 
|---|
| [7516] | 149 | void VolFogEffect::draw() const | 
|---|
| [7519] | 150 | { | 
|---|
| [7760] | 151 | glPushAttrib(GL_ENABLE_BIT); | 
|---|
|  | 152 | glClearColor(fogColor[0],fogColor[1],fogColor[2],0.0f); | 
|---|
|  | 153 | glShadeModel(GL_SMOOTH); | 
|---|
|  | 154 | glEnable(GL_DEPTH_TEST); | 
|---|
|  | 155 | glEnable(GL_CULL_FACE); | 
|---|
|  | 156 | glCullFace(GL_BACK); | 
|---|
|  | 157 |  | 
|---|
| [7557] | 158 | /* clear all pixels in colour buffer */ | 
|---|
|  | 159 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 
|---|
| [7530] | 160 |  | 
|---|
| [7577] | 161 | /* Enable blending */ | 
|---|
| [7568] | 162 | //glEnable(GL_BLEND); | 
|---|
|  | 163 | //glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA); | 
|---|
| [8316] | 164 |  | 
|---|
| [7760] | 165 | int i; | 
|---|
| [7768] | 166 | glLoadIdentity(); | 
|---|
| [7558] | 167 |  | 
|---|
| [8316] | 168 |  | 
|---|
| [7760] | 169 | glBegin( GL_LINES ); | 
|---|
|  | 170 | glNormal3f(0,1,0); | 
|---|
|  | 171 | for(i=-20;i<=20;i+=2) | 
|---|
|  | 172 | { | 
|---|
|  | 173 | float fog_c; | 
|---|
|  | 174 | float diff[3]; | 
|---|
| [7520] | 175 |  | 
|---|
| [7760] | 176 | diff[0] = State::getCameraNode()->getAbsCoor().x - i; | 
|---|
|  | 177 | diff[2] = State::getCameraNode()->getAbsCoor().z + 20; | 
|---|
|  | 178 | diff[1] = State::getCameraNode()->getAbsCoor().y; | 
|---|
|  | 179 | fog_c = 1.3f*sqrt(diff[0]*diff[0]+diff[1]*diff[1]+diff[2]*diff[2]); | 
|---|
|  | 180 | glFogCoordfEXT(fog_c*2); | 
|---|
|  | 181 | glVertex3f(i,0,-20); | 
|---|
| [7520] | 182 |  | 
|---|
| [7760] | 183 | diff[0] = State::getCameraNode()->getAbsCoor().x - i; | 
|---|
|  | 184 | diff[2] = State::getCameraNode()->getAbsCoor().z + 20; | 
|---|
|  | 185 | diff[1] = State::getCameraNode()->getAbsCoor().y; | 
|---|
|  | 186 | fog_c = 1.3f*sqrt(diff[0]*diff[0]+diff[1]*diff[1]+diff[2]*diff[2]); | 
|---|
|  | 187 | glFogCoordfEXT(fog_c*2); | 
|---|
|  | 188 | glVertex3f(i,0,20); | 
|---|
| [7520] | 189 |  | 
|---|
| [7760] | 190 | diff[0] = State::getCameraNode()->getAbsCoor().x - i; | 
|---|
|  | 191 | diff[2] = State::getCameraNode()->getAbsCoor().z + 20; | 
|---|
|  | 192 | diff[1] = State::getCameraNode()->getAbsCoor().y; | 
|---|
|  | 193 | fog_c = 1.3f*sqrt(diff[0]*diff[0]+diff[1]*diff[1]+diff[2]*diff[2]); | 
|---|
|  | 194 | glFogCoordfEXT(fog_c*2); | 
|---|
|  | 195 | glVertex3f(-20,0,i); | 
|---|
| [7546] | 196 |  | 
|---|
| [7760] | 197 | diff[0] = State::getCameraNode()->getAbsCoor().x - i; | 
|---|
|  | 198 | diff[2] = State::getCameraNode()->getAbsCoor().z + 20; | 
|---|
|  | 199 | diff[1] = State::getCameraNode()->getAbsCoor().y; | 
|---|
|  | 200 | fog_c = 1.3f*sqrt(diff[0]*diff[0]+diff[1]*diff[1]+diff[2]*diff[2]); | 
|---|
|  | 201 | glFogCoordfEXT(fog_c*2); | 
|---|
|  | 202 | glVertex3f(20,0,i); | 
|---|
|  | 203 | } | 
|---|
| [7557] | 204 | glEnd(); | 
|---|
| [7546] | 205 |  | 
|---|
| [7760] | 206 | glPopAttrib(); | 
|---|
| [7546] | 207 |  | 
|---|
| [7557] | 208 | } | 
|---|
| [7546] | 209 |  | 
|---|
|  | 210 |  | 
|---|
| [7516] | 211 | /** | 
|---|
| [7652] | 212 | * ticks the effect if there is any time dependancy | 
|---|
|  | 213 | */ | 
|---|
| [8316] | 214 | void VolFogEffect::tick(float dt) | 
|---|
| [7519] | 215 | { | 
|---|
|  | 216 | } | 
|---|