Major rework of tnl module
[mesa.git] / src / mesa / main / fog.c
1 /* $Id: fog.c,v 1.31 2000/12/26 05:09:28 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "fog.h"
35 #include "mtypes.h"
36 #endif
37
38
39
40 void
41 _mesa_Fogf(GLenum pname, GLfloat param)
42 {
43 _mesa_Fogfv(pname, &param);
44 }
45
46
47 void
48 _mesa_Fogi(GLenum pname, GLint param )
49 {
50 GLfloat fparam = (GLfloat) param;
51 _mesa_Fogfv(pname, &fparam);
52 }
53
54
55 void
56 _mesa_Fogiv(GLenum pname, const GLint *params )
57 {
58 GLfloat p[4];
59 switch (pname) {
60 case GL_FOG_MODE:
61 case GL_FOG_DENSITY:
62 case GL_FOG_START:
63 case GL_FOG_END:
64 case GL_FOG_INDEX:
65 case GL_FOG_COORDINATE_SOURCE_EXT:
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 ;
77 }
78 _mesa_Fogfv(pname, p);
79 }
80
81
82 void
83 _mesa_Fogfv( GLenum pname, const GLfloat *params )
84 {
85 GET_CURRENT_CONTEXT(ctx);
86 GLenum m;
87 ASSERT_OUTSIDE_BEGIN_END(ctx);
88
89 switch (pname) {
90 case GL_FOG_MODE:
91 m = (GLenum) (GLint) *params;
92 if (ctx->Fog.Mode == m)
93 return;
94 switch (m) {
95 case GL_LINEAR:
96 case GL_EXP:
97 case GL_EXP2:
98 break;
99 default:
100 gl_error( ctx, GL_INVALID_ENUM, "glFog" );
101 return;
102 }
103 FLUSH_VERTICES(ctx, _NEW_FOG);
104 ctx->Fog.Mode = m;
105 break;
106 case GL_FOG_DENSITY:
107 if (*params<0.0) {
108 gl_error( ctx, GL_INVALID_VALUE, "glFog" );
109 return;
110 }
111 if (ctx->Fog.Density == *params)
112 return;
113 FLUSH_VERTICES(ctx, _NEW_FOG);
114 ctx->Fog.Density = *params;
115 break;
116 case GL_FOG_START:
117 if (ctx->Fog.Start == *params)
118 return;
119 FLUSH_VERTICES(ctx, _NEW_FOG);
120 ctx->Fog.Start = *params;
121 break;
122 case GL_FOG_END:
123 if (ctx->Fog.End == *params)
124 return;
125 FLUSH_VERTICES(ctx, _NEW_FOG);
126 ctx->Fog.End = *params;
127 break;
128 case GL_FOG_INDEX:
129 if (ctx->Fog.Index == *params)
130 return;
131 FLUSH_VERTICES(ctx, _NEW_FOG);
132 ctx->Fog.Index = *params;
133 break;
134 case GL_FOG_COLOR:
135 if (TEST_EQ_4V(ctx->Fog.Color, params))
136 return;
137 FLUSH_VERTICES(ctx, _NEW_FOG);
138 ctx->Fog.Color[0] = params[0];
139 ctx->Fog.Color[1] = params[1];
140 ctx->Fog.Color[2] = params[2];
141 ctx->Fog.Color[3] = params[3];
142 break;
143 case GL_FOG_COORDINATE_SOURCE_EXT: {
144 GLenum p = (GLenum)(GLint) *params;
145 if (ctx->Fog.FogCoordinateSource == p)
146 return;
147 if (p != GL_FOG_COORDINATE_EXT && p != GL_FRAGMENT_DEPTH_EXT) {
148 gl_error( ctx, GL_INVALID_ENUM, "glFog" );
149 return;
150 }
151 FLUSH_VERTICES(ctx, _NEW_FOG);
152 ctx->Fog.FogCoordinateSource = p;
153 break;
154 }
155 default:
156 gl_error( ctx, GL_INVALID_ENUM, "glFog" );
157 return;
158 }
159
160 if (ctx->Driver.Fogfv) {
161 (*ctx->Driver.Fogfv)( ctx, pname, params );
162 }
163
164 ctx->NewState |= _NEW_FOG;
165 }
166
167