mesa: add packing support for setting uniforms
[mesa.git] / src / mesa / main / fog.c
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
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
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
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 void GLAPIENTRY
82 _mesa_Fogfv( GLenum pname, const GLfloat *params )
83 {
84 GET_CURRENT_CONTEXT(ctx);
85 GLenum m;
86
87 switch (pname) {
88 case GL_FOG_MODE:
89 m = (GLenum) (GLint) *params;
90 switch (m) {
91 case GL_LINEAR:
92 ctx->Fog._PackedMode = FOG_LINEAR;
93 break;
94 case GL_EXP:
95 ctx->Fog._PackedMode = FOG_EXP;
96 break;
97 case GL_EXP2:
98 ctx->Fog._PackedMode = FOG_EXP2;
99 break;
100 default:
101 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" );
102 return;
103 }
104 if (ctx->Fog.Mode == m)
105 return;
106 FLUSH_VERTICES(ctx, _NEW_FOG);
107 ctx->Fog.Mode = m;
108 ctx->Fog._PackedEnabledMode = ctx->Fog.Enabled ?
109 ctx->Fog._PackedMode : FOG_NONE;
110 break;
111 case GL_FOG_DENSITY:
112 if (*params<0.0F) {
113 _mesa_error( ctx, GL_INVALID_VALUE, "glFog" );
114 return;
115 }
116 if (ctx->Fog.Density == *params)
117 return;
118 FLUSH_VERTICES(ctx, _NEW_FOG);
119 ctx->Fog.Density = *params;
120 break;
121 case GL_FOG_START:
122 if (ctx->Fog.Start == *params)
123 return;
124 FLUSH_VERTICES(ctx, _NEW_FOG);
125 ctx->Fog.Start = *params;
126 break;
127 case GL_FOG_END:
128 if (ctx->Fog.End == *params)
129 return;
130 FLUSH_VERTICES(ctx, _NEW_FOG);
131 ctx->Fog.End = *params;
132 break;
133 case GL_FOG_INDEX:
134 if (ctx->API != API_OPENGL_COMPAT)
135 goto invalid_pname;
136 if (ctx->Fog.Index == *params)
137 return;
138 FLUSH_VERTICES(ctx, _NEW_FOG);
139 ctx->Fog.Index = *params;
140 break;
141 case GL_FOG_COLOR:
142 if (TEST_EQ_4V(ctx->Fog.Color, params))
143 return;
144 FLUSH_VERTICES(ctx, _NEW_FOG);
145 ctx->Fog.ColorUnclamped[0] = params[0];
146 ctx->Fog.ColorUnclamped[1] = params[1];
147 ctx->Fog.ColorUnclamped[2] = params[2];
148 ctx->Fog.ColorUnclamped[3] = params[3];
149 ctx->Fog.Color[0] = CLAMP(params[0], 0.0F, 1.0F);
150 ctx->Fog.Color[1] = CLAMP(params[1], 0.0F, 1.0F);
151 ctx->Fog.Color[2] = CLAMP(params[2], 0.0F, 1.0F);
152 ctx->Fog.Color[3] = CLAMP(params[3], 0.0F, 1.0F);
153 break;
154 case GL_FOG_COORDINATE_SOURCE_EXT: {
155 GLenum p = (GLenum) (GLint) *params;
156 if (ctx->API != API_OPENGL_COMPAT ||
157 (p != GL_FOG_COORDINATE_EXT && p != GL_FRAGMENT_DEPTH_EXT)) {
158 _mesa_error(ctx, GL_INVALID_ENUM, "glFog");
159 return;
160 }
161 if (ctx->Fog.FogCoordinateSource == p)
162 return;
163 FLUSH_VERTICES(ctx, _NEW_FOG);
164 ctx->Fog.FogCoordinateSource = p;
165 break;
166 }
167 case GL_FOG_DISTANCE_MODE_NV: {
168 GLenum p = (GLenum) (GLint) *params;
169 if (ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.NV_fog_distance ||
170 (p != GL_EYE_RADIAL_NV && p != GL_EYE_PLANE && p != GL_EYE_PLANE_ABSOLUTE_NV)) {
171 _mesa_error(ctx, GL_INVALID_ENUM, "glFog");
172 return;
173 }
174 if (ctx->Fog.FogDistanceMode == p)
175 return;
176 FLUSH_VERTICES(ctx, _NEW_FOG);
177 ctx->Fog.FogDistanceMode = p;
178 break;
179 }
180 default:
181 goto invalid_pname;
182 }
183
184 if (ctx->Driver.Fogfv) {
185 ctx->Driver.Fogfv( ctx, pname, params );
186 }
187
188 return;
189
190 invalid_pname:
191 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" );
192 return;
193 }
194
195
196 /**********************************************************************/
197 /***** Initialization *****/
198 /**********************************************************************/
199
200 void _mesa_init_fog( struct gl_context * ctx )
201 {
202 /* Fog group */
203 ctx->Fog.Enabled = GL_FALSE;
204 ctx->Fog.Mode = GL_EXP;
205 ctx->Fog._PackedMode = FOG_EXP;
206 ctx->Fog._PackedEnabledMode = FOG_NONE;
207 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
208 ASSIGN_4V( ctx->Fog.ColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
209 ctx->Fog.Index = 0.0;
210 ctx->Fog.Density = 1.0;
211 ctx->Fog.Start = 0.0;
212 ctx->Fog.End = 1.0;
213 ctx->Fog.ColorSumEnabled = GL_FALSE;
214 ctx->Fog.FogCoordinateSource = GL_FRAGMENT_DEPTH_EXT;
215 ctx->Fog.FogDistanceMode = GL_EYE_PLANE_ABSOLUTE_NV;
216 }