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