-
Notifications
You must be signed in to change notification settings - Fork 17
/
grguFog.cpp
225 lines (191 loc) · 5.55 KB
/
grguFog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//**************************************************************
//* OpenGLide - Glide to OpenGL Wrapper
//* http://openglide.sourceforge.net
//*
//* Fog Functions
//*
//* OpenGLide is OpenSource under LGPL license
//* Originally made by Fabio Barros
//* Modified by Paul for Glidos (http://www.glidos.net)
//* Linux version by Simon White
//**************************************************************
#include <math.h>
#include <string.h>
#include "GlOgl.h"
#include "GLRender.h"
#include "OGLTables.h"
//*************************************************
//* download a fog table
//* Fog is applied after color combining and before alpha blending.
//*************************************************
FX_ENTRY void FX_CALL
grFogTable( const GrFog_t *ft )
{
#ifdef OGL_DONE
GlideMsg( "grFogTable( --- )\n" );
#endif
if ( InternalConfig.FogEnable )
{
memcpy( Glide.FogTable, (GrFog_t *)ft, GR_FOG_TABLE_SIZE * sizeof( FxU8 ) );
Glide.FogTable[ GR_FOG_TABLE_SIZE ] = 255;
for ( int i = 0; i < GR_FOG_TABLE_SIZE; i++ )
{
for ( unsigned int j = intStartEnd[ i ]; j < intStartEnd[ i + 1 ]; j++ )
{
OpenGL.FogTable[ j ] = (FxU8)( Glide.FogTable[ i ] +
( Glide.FogTable[ i + 1 ] - Glide.FogTable[ i ] ) * ( j - intStartEnd[ i ] ) /
intEndMinusStart[ i ] );
}
}
}
}
//*************************************************
FX_ENTRY void FX_CALL
grFogColorValue( GrColor_t fogcolor )
{
#ifdef OGL_PARTDONE
GlideMsg( "grFogColorValue( %x )\n", fogcolor );
#endif
RenderDrawTriangles( );
Glide.State.FogColorValue = fogcolor;
ConvertColorF( fogcolor,
OpenGL.FogColor[ 0 ],
OpenGL.FogColor[ 1 ],
OpenGL.FogColor[ 2 ],
OpenGL.FogColor[ 3 ] );
glFogfv( GL_FOG_COLOR, &OpenGL.FogColor[0] );
}
//*************************************************
FX_ENTRY void FX_CALL
grFogMode( GrFogMode_t mode )
{
#ifdef OGL_PARTDONE
GlideMsg( "grFogMode( %d )\n", mode );
#endif
static GrFogMode_t modeSource,
modeAdd;
static GLfloat ZeroColor[ 4 ] = { 0.0f, 0.0f, 0.0f, 0.0f };
RenderDrawTriangles( );
modeSource = mode & ( GR_FOG_WITH_TABLE | GR_FOG_WITH_ITERATED_ALPHA );
modeAdd = mode & ( GR_FOG_MULT2 | GR_FOG_ADD2 );
if ( modeSource )
{
OpenGL.Fog = true;
if ( InternalConfig.EXT_fog_coord )
{
glEnable( GL_FOG );
}
}
else
{
OpenGL.Fog = false;
glDisable( GL_FOG );
}
switch ( modeAdd )
{
case GR_FOG_MULT2:
case GR_FOG_ADD2:
glFogfv( GL_FOG_COLOR, &ZeroColor[ 0 ] );
break;
default:
glFogfv( GL_FOG_COLOR, &OpenGL.FogColor[ 0 ] );
break;
}
Glide.State.FogMode = modeSource;
// Glide.State.FogMode = mode;
}
//*************************************************
FX_ENTRY void FX_CALL
guFogGenerateExp( GrFog_t *fogtable, float density )
{
#ifdef OGL_PARTDONE
GlideMsg( "guFogGenerateExp( ---, %-4.2f )\n", density );
#endif
float f;
float scale;
float dp;
dp = density * guFogTableIndexToW( GR_FOG_TABLE_SIZE - 1 );
scale = 255.0F / ( 1.0F - (float) exp( -dp ) );
for ( int i = 0; i < GR_FOG_TABLE_SIZE; i++ )
{
dp = density * guFogTableIndexToW( i );
f = ( 1.0F - (float) exp( -dp ) ) * scale;
if ( f > 255.0F )
{
f = 255.0F;
}
else if ( f < 0.0F )
{
f = 0.0F;
}
fogtable[ i ] = (GrFog_t) f;
}
}
//*************************************************
FX_ENTRY void FX_CALL
guFogGenerateExp2( GrFog_t *fogtable, float density )
{
#ifdef OGL_PARTDONE
GlideMsg( "guFogGenerateExp2( ---, %-4.2f )\n", density );
#endif
float Temp;
for ( int i = 0; i < GR_FOG_TABLE_SIZE; i++ )
{
Temp = ( 1.0f - (float) exp( ( -density) * guFogTableIndexToW( i ) ) *
(float)exp( (-density) * guFogTableIndexToW( i ) ) ) * 255.0f;
fogtable[ i ] = (FxU8) Temp;
}
}
//*************************************************
FX_ENTRY void FX_CALL
guFogGenerateLinear( GrFog_t *fogtable,
float nearZ, float farZ )
{
#ifdef OGL_PARTDONE
GlideMsg( "guFogGenerateLinear( ---, %-4.2f, %-4.2f )\n", nearZ, farZ );
#endif
int Start,
End,
i;
for( Start = 0; Start < GR_FOG_TABLE_SIZE; Start++ )
{
if ( guFogTableIndexToW( Start ) >= nearZ )
{
break;
}
}
for( End = 0; End < GR_FOG_TABLE_SIZE; End++ )
{
if ( guFogTableIndexToW( End ) >= farZ )
{
break;
}
}
ZeroMemory( fogtable, GR_FOG_TABLE_SIZE );
for( i = Start; i <= End; i++ )
{
fogtable[ i ] = (FxU8)((float)( End - Start ) / 255.0f * (float)( i - Start ));
}
for( i = End; i < GR_FOG_TABLE_SIZE; i++ )
{
fogtable[ i ] = 255;
}
}
//*************************************************
//* convert a fog table index to a floating point eye-space w value
//*************************************************
FX_ENTRY float FX_CALL
guFogTableIndexToW( int i )
{
#ifdef OGL_DONE
GlideMsg( "guFogTableIndexToW( %d )\n", i );
#endif
#ifdef OGL_DEBUG
if ( ( i < 0 ) ||
( i >= GR_FOG_TABLE_SIZE ) )
{
Error( "Error on guFogTableIndexToW( %d )\n", i );
}
#endif
return tableIndexToW[ i ];
}