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