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