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 | |
---|
26 | #define GLX_GLXEXT_PROTOTYPES |
---|
27 | #include <GL/glx.h> |
---|
28 | // #include <GL/glut.h> |
---|
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 | |
---|
49 | using namespace std; |
---|
50 | |
---|
51 | CREATE_FACTORY(VolFogEffect, CL_VOLFOG_EFFECT); |
---|
52 | |
---|
53 | VolFogEffect::VolFogEffect(const TiXmlElement* root) |
---|
54 | { |
---|
55 | this->setClassID(CL_VOLFOG_EFFECT, "VolFogEffect"); |
---|
56 | |
---|
57 | if (root != NULL) |
---|
58 | this->loadParams(root); |
---|
59 | |
---|
60 | // Initialize Effect |
---|
61 | this->init(); |
---|
62 | |
---|
63 | // Activate Effect |
---|
64 | this->activate(); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | VolFogEffect::~VolFogEffect() |
---|
69 | { |
---|
70 | this->deactivate(); |
---|
71 | } |
---|
72 | |
---|
73 | void VolFogEffect::loadParams(const TiXmlElement* root) |
---|
74 | { |
---|
75 | WeatherEffect::loadParams(root); |
---|
76 | } |
---|
77 | |
---|
78 | bool VolFogEffect::init() |
---|
79 | { |
---|
80 | PRINTF(0)("Initalize VolFogEffect\n"); |
---|
81 | |
---|
82 | // set fog mode |
---|
83 | GLenum fogMode = GL_EXP; |
---|
84 | |
---|
85 | // set fog density |
---|
86 | float fogDensity = 0.001f; |
---|
87 | |
---|
88 | // set fog near & far distance |
---|
89 | float fogStart = 0.0f; |
---|
90 | float fogEnd = 1000.0f; |
---|
91 | |
---|
92 | // Set fog color |
---|
93 | float fogColor[4] = {0.6f,0.58f,0.79f,0.0f}; |
---|
94 | |
---|
95 | |
---|
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"); |
---|
101 | |
---|
102 | // set the fog attributes |
---|
103 | glFogf (GL_FOG_START, fogStart); |
---|
104 | glFogf (GL_FOG_END, fogEnd); |
---|
105 | glFogfv(GL_FOG_COLOR, fogColor); |
---|
106 | glFogi (GL_FOG_MODE, fogMode); |
---|
107 | glFogf (GL_FOG_DENSITY,fogDensity); |
---|
108 | glFogi (GL_FOG_COORDINATE_SOURCE_EXT,GL_FOG_COORDINATE_EXT); |
---|
109 | |
---|
110 | // enable the fog |
---|
111 | glEnable(GL_FOG); |
---|
112 | |
---|
113 | if (glewInit() == GLEW_OK) |
---|
114 | PRINTF(0)("glewInit OK\n"); |
---|
115 | else |
---|
116 | PRINTF(0)("glewInit failed\n"); |
---|
117 | |
---|
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 | } |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | bool VolFogEffect::activate() |
---|
132 | { |
---|
133 | PRINTF(0)("Activating VolFogEffect\n"); |
---|
134 | } |
---|
135 | |
---|
136 | bool VolFogEffect::deactivate() |
---|
137 | { |
---|
138 | PRINTF(0)("Deactivating VolFogEffect\n"); |
---|
139 | |
---|
140 | glDisable(GL_FOG); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * draws the effect, if needed |
---|
146 | */ |
---|
147 | void VolFogEffect::draw() const |
---|
148 | { |
---|
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 | |
---|
156 | /* clear all pixels in colour buffer */ |
---|
157 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
158 | |
---|
159 | /* Enable blending */ |
---|
160 | //glEnable(GL_BLEND); |
---|
161 | //glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA); |
---|
162 | |
---|
163 | int i; |
---|
164 | glLoadIdentity(); |
---|
165 | |
---|
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]; |
---|
173 | |
---|
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); |
---|
180 | |
---|
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); |
---|
187 | |
---|
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); |
---|
194 | |
---|
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 | } |
---|
202 | glEnd(); |
---|
203 | |
---|
204 | glPopAttrib(); |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | /** |
---|
210 | * ticks the effect if there is any time dependancy |
---|
211 | */ |
---|
212 | void VolFogEffect::tick(float dt) |
---|
213 | { |
---|
214 | } |
---|