1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "context.h"
28 #include "fog.h"
29 #include "macros.h"
30 #include "mtypes.h"
31
32
33
34 void GLAPIENTRY
_mesa_Fogf(GLenum pname,GLfloat param)35 _mesa_Fogf(GLenum pname, GLfloat param)
36 {
37 GLfloat fparam[4];
38 fparam[0] = param;
39 fparam[1] = fparam[2] = fparam[3] = 0.0F;
40 _mesa_Fogfv(pname, fparam);
41 }
42
43
44 void GLAPIENTRY
_mesa_Fogi(GLenum pname,GLint param)45 _mesa_Fogi(GLenum pname, GLint param )
46 {
47 GLfloat fparam[4];
48 fparam[0] = (GLfloat) param;
49 fparam[1] = fparam[2] = fparam[3] = 0.0F;
50 _mesa_Fogfv(pname, fparam);
51 }
52
53
54 void GLAPIENTRY
_mesa_Fogiv(GLenum pname,const GLint * params)55 _mesa_Fogiv(GLenum pname, const GLint *params )
56 {
57 GLfloat p[4];
58 switch (pname) {
59 case GL_FOG_MODE:
60 case GL_FOG_DENSITY:
61 case GL_FOG_START:
62 case GL_FOG_END:
63 case GL_FOG_INDEX:
64 case GL_FOG_COORDINATE_SOURCE_EXT:
65 p[0] = (GLfloat) *params;
66 break;
67 case GL_FOG_COLOR:
68 p[0] = INT_TO_FLOAT( params[0] );
69 p[1] = INT_TO_FLOAT( params[1] );
70 p[2] = INT_TO_FLOAT( params[2] );
71 p[3] = INT_TO_FLOAT( params[3] );
72 break;
73 default:
74 /* Error will be caught later in _mesa_Fogfv */
75 ASSIGN_4V(p, 0.0F, 0.0F, 0.0F, 0.0F);
76 }
77 _mesa_Fogfv(pname, p);
78 }
79
80
81 /**
82 * Update the gl_fog_attrib::_Scale field.
83 */
84 static void
update_fog_scale(struct gl_context * ctx)85 update_fog_scale(struct gl_context *ctx)
86 {
87 if (ctx->Fog.End == ctx->Fog.Start)
88 ctx->Fog._Scale = 1.0f;
89 else
90 ctx->Fog._Scale = 1.0f / (ctx->Fog.End - ctx->Fog.Start);
91 }
92
93
94 void GLAPIENTRY
_mesa_Fogfv(GLenum pname,const GLfloat * params)95 _mesa_Fogfv( GLenum pname, const GLfloat *params )
96 {
97 GET_CURRENT_CONTEXT(ctx);
98 GLenum m;
99
100 switch (pname) {
101 case GL_FOG_MODE:
102 m = (GLenum) (GLint) *params;
103 switch (m) {
104 case GL_LINEAR:
105 ctx->Fog._PackedMode = FOG_LINEAR;
106 break;
107 case GL_EXP:
108 ctx->Fog._PackedMode = FOG_EXP;
109 break;
110 case GL_EXP2:
111 ctx->Fog._PackedMode = FOG_EXP2;
112 break;
113 default:
114 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" );
115 return;
116 }
117 if (ctx->Fog.Mode == m)
118 return;
119 FLUSH_VERTICES(ctx, _NEW_FOG);
120 ctx->Fog.Mode = m;
121 ctx->Fog._PackedEnabledMode = ctx->Fog.Enabled ?
122 ctx->Fog._PackedMode : FOG_NONE;
123 break;
124 case GL_FOG_DENSITY:
125 if (*params<0.0F) {
126 _mesa_error( ctx, GL_INVALID_VALUE, "glFog" );
127 return;
128 }
129 if (ctx->Fog.Density == *params)
130 return;
131 FLUSH_VERTICES(ctx, _NEW_FOG);
132 ctx->Fog.Density = *params;
133 break;
134 case GL_FOG_START:
135 if (ctx->Fog.Start == *params)
136 return;
137 FLUSH_VERTICES(ctx, _NEW_FOG);
138 ctx->Fog.Start = *params;
139 update_fog_scale(ctx);
140 break;
141 case GL_FOG_END:
142 if (ctx->Fog.End == *params)
143 return;
144 FLUSH_VERTICES(ctx, _NEW_FOG);
145 ctx->Fog.End = *params;
146 update_fog_scale(ctx);
147 break;
148 case GL_FOG_INDEX:
149 if (ctx->API != API_OPENGL_COMPAT)
150 goto invalid_pname;
151 if (ctx->Fog.Index == *params)
152 return;
153 FLUSH_VERTICES(ctx, _NEW_FOG);
154 ctx->Fog.Index = *params;
155 break;
156 case GL_FOG_COLOR:
157 if (TEST_EQ_4V(ctx->Fog.Color, params))
158 return;
159 FLUSH_VERTICES(ctx, _NEW_FOG);
160 ctx->Fog.ColorUnclamped[0] = params[0];
161 ctx->Fog.ColorUnclamped[1] = params[1];
162 ctx->Fog.ColorUnclamped[2] = params[2];
163 ctx->Fog.ColorUnclamped[3] = params[3];
164 ctx->Fog.Color[0] = CLAMP(params[0], 0.0F, 1.0F);
165 ctx->Fog.Color[1] = CLAMP(params[1], 0.0F, 1.0F);
166 ctx->Fog.Color[2] = CLAMP(params[2], 0.0F, 1.0F);
167 ctx->Fog.Color[3] = CLAMP(params[3], 0.0F, 1.0F);
168 break;
169 case GL_FOG_COORDINATE_SOURCE_EXT: {
170 GLenum p = (GLenum) (GLint) *params;
171 if (ctx->API != API_OPENGL_COMPAT ||
172 (p != GL_FOG_COORDINATE_EXT && p != GL_FRAGMENT_DEPTH_EXT)) {
173 _mesa_error(ctx, GL_INVALID_ENUM, "glFog");
174 return;
175 }
176 if (ctx->Fog.FogCoordinateSource == p)
177 return;
178 FLUSH_VERTICES(ctx, _NEW_FOG);
179 ctx->Fog.FogCoordinateSource = p;
180 break;
181 }
182 case GL_FOG_DISTANCE_MODE_NV: {
183 GLenum p = (GLenum) (GLint) *params;
184 if (ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.NV_fog_distance ||
185 (p != GL_EYE_RADIAL_NV && p != GL_EYE_PLANE && p != GL_EYE_PLANE_ABSOLUTE_NV)) {
186 _mesa_error(ctx, GL_INVALID_ENUM, "glFog");
187 return;
188 }
189 if (ctx->Fog.FogDistanceMode == p)
190 return;
191 FLUSH_VERTICES(ctx, _NEW_FOG);
192 ctx->Fog.FogDistanceMode = p;
193 break;
194 }
195 default:
196 goto invalid_pname;
197 }
198
199 if (ctx->Driver.Fogfv) {
200 ctx->Driver.Fogfv( ctx, pname, params );
201 }
202
203 return;
204
205 invalid_pname:
206 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" );
207 return;
208 }
209
210
211 /**********************************************************************/
212 /***** Initialization *****/
213 /**********************************************************************/
214
_mesa_init_fog(struct gl_context * ctx)215 void _mesa_init_fog( struct gl_context * ctx )
216 {
217 /* Fog group */
218 ctx->Fog.Enabled = GL_FALSE;
219 ctx->Fog.Mode = GL_EXP;
220 ctx->Fog._PackedMode = FOG_EXP;
221 ctx->Fog._PackedEnabledMode = FOG_NONE;
222 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
223 ASSIGN_4V( ctx->Fog.ColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
224 ctx->Fog.Index = 0.0;
225 ctx->Fog.Density = 1.0;
226 ctx->Fog.Start = 0.0;
227 ctx->Fog.End = 1.0;
228 ctx->Fog.ColorSumEnabled = GL_FALSE;
229 ctx->Fog.FogCoordinateSource = GL_FRAGMENT_DEPTH_EXT;
230 ctx->Fog._Scale = 1.0f;
231 ctx->Fog.FogDistanceMode = GL_EYE_PLANE_ABSOLUTE_NV;
232 }
233