Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / main / fog.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "colormac.h"
28 #include "context.h"
29 #include "fog.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 #define UPDATE_FOG_SCALE(ctx) do {\
82 if (ctx->Fog.End == ctx->Fog.Start)\
83 ctx->Fog._Scale = 1.0f;\
84 else\
85 ctx->Fog._Scale = 1.0f / (ctx->Fog.End - ctx->Fog.Start);\
86 } while(0)
87
88
89 void GLAPIENTRY
90 _mesa_Fogfv( GLenum pname, const GLfloat *params )
91 {
92 GET_CURRENT_CONTEXT(ctx);
93 GLenum m;
94 ASSERT_OUTSIDE_BEGIN_END(ctx);
95
96 switch (pname) {
97 case GL_FOG_MODE:
98 m = (GLenum) (GLint) *params;
99 switch (m) {
100 case GL_LINEAR:
101 case GL_EXP:
102 case GL_EXP2:
103 break;
104 default:
105 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" );
106 return;
107 }
108 if (ctx->Fog.Mode == m)
109 return;
110 FLUSH_VERTICES(ctx, _NEW_FOG);
111 ctx->Fog.Mode = m;
112 break;
113 case GL_FOG_DENSITY:
114 if (*params<0.0) {
115 _mesa_error( ctx, GL_INVALID_VALUE, "glFog" );
116 return;
117 }
118 if (ctx->Fog.Density == *params)
119 return;
120 FLUSH_VERTICES(ctx, _NEW_FOG);
121 ctx->Fog.Density = *params;
122 break;
123 case GL_FOG_START:
124 if (ctx->Fog.Start == *params)
125 return;
126 FLUSH_VERTICES(ctx, _NEW_FOG);
127 ctx->Fog.Start = *params;
128 UPDATE_FOG_SCALE(ctx);
129 break;
130 case GL_FOG_END:
131 if (ctx->Fog.End == *params)
132 return;
133 FLUSH_VERTICES(ctx, _NEW_FOG);
134 ctx->Fog.End = *params;
135 UPDATE_FOG_SCALE(ctx);
136 break;
137 case GL_FOG_INDEX:
138 if (ctx->Fog.Index == *params)
139 return;
140 FLUSH_VERTICES(ctx, _NEW_FOG);
141 ctx->Fog.Index = *params;
142 break;
143 case GL_FOG_COLOR:
144 if (TEST_EQ_4V(ctx->Fog.Color, params))
145 return;
146 FLUSH_VERTICES(ctx, _NEW_FOG);
147 ctx->Fog.Color[0] = CLAMP(params[0], 0.0F, 1.0F);
148 ctx->Fog.Color[1] = CLAMP(params[1], 0.0F, 1.0F);
149 ctx->Fog.Color[2] = CLAMP(params[2], 0.0F, 1.0F);
150 ctx->Fog.Color[3] = CLAMP(params[3], 0.0F, 1.0F);
151 break;
152 case GL_FOG_COORDINATE_SOURCE_EXT: {
153 GLenum p = (GLenum) (GLint) *params;
154 if (!ctx->Extensions.EXT_fog_coord ||
155 (p != GL_FOG_COORDINATE_EXT && p != GL_FRAGMENT_DEPTH_EXT)) {
156 _mesa_error(ctx, GL_INVALID_ENUM, "glFog");
157 return;
158 }
159 if (ctx->Fog.FogCoordinateSource == p)
160 return;
161 FLUSH_VERTICES(ctx, _NEW_FOG);
162 ctx->Fog.FogCoordinateSource = p;
163 break;
164 }
165 default:
166 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" );
167 return;
168 }
169
170 if (ctx->Driver.Fogfv) {
171 (*ctx->Driver.Fogfv)( ctx, pname, params );
172 }
173 }
174
175
176 /**********************************************************************/
177 /***** Initialization *****/
178 /**********************************************************************/
179
180 void _mesa_init_fog( GLcontext * ctx )
181 {
182 /* Fog group */
183 ctx->Fog.Enabled = GL_FALSE;
184 ctx->Fog.Mode = GL_EXP;
185 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
186 ctx->Fog.Index = 0.0;
187 ctx->Fog.Density = 1.0;
188 ctx->Fog.Start = 0.0;
189 ctx->Fog.End = 1.0;
190 ctx->Fog.ColorSumEnabled = GL_FALSE;
191 ctx->Fog.FogCoordinateSource = GL_FRAGMENT_DEPTH_EXT;
192 ctx->Fog._Scale = 1.0f;
193 }