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