fog.c revision 848b8605
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 "colormac.h" 28#include "context.h" 29#include "fog.h" 30#include "macros.h" 31#include "mtypes.h" 32 33 34 35void GLAPIENTRY 36_mesa_Fogf(GLenum pname, GLfloat param) 37{ 38 GLfloat fparam[4]; 39 fparam[0] = param; 40 fparam[1] = fparam[2] = fparam[3] = 0.0F; 41 _mesa_Fogfv(pname, fparam); 42} 43 44 45void GLAPIENTRY 46_mesa_Fogi(GLenum pname, GLint param ) 47{ 48 GLfloat fparam[4]; 49 fparam[0] = (GLfloat) param; 50 fparam[1] = fparam[2] = fparam[3] = 0.0F; 51 _mesa_Fogfv(pname, fparam); 52} 53 54 55void GLAPIENTRY 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 ASSIGN_4V(p, 0.0F, 0.0F, 0.0F, 0.0F); 77 } 78 _mesa_Fogfv(pname, p); 79} 80 81 82/** 83 * Update the gl_fog_attrib::_Scale field. 84 */ 85static void 86update_fog_scale(struct gl_context *ctx) 87{ 88 if (ctx->Fog.End == ctx->Fog.Start) 89 ctx->Fog._Scale = 1.0f; 90 else 91 ctx->Fog._Scale = 1.0f / (ctx->Fog.End - ctx->Fog.Start); 92} 93 94 95void GLAPIENTRY 96_mesa_Fogfv( GLenum pname, const GLfloat *params ) 97{ 98 GET_CURRENT_CONTEXT(ctx); 99 GLenum m; 100 101 switch (pname) { 102 case GL_FOG_MODE: 103 m = (GLenum) (GLint) *params; 104 switch (m) { 105 case GL_LINEAR: 106 case GL_EXP: 107 case GL_EXP2: 108 break; 109 default: 110 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" ); 111 return; 112 } 113 if (ctx->Fog.Mode == m) 114 return; 115 FLUSH_VERTICES(ctx, _NEW_FOG); 116 ctx->Fog.Mode = m; 117 break; 118 case GL_FOG_DENSITY: 119 if (*params<0.0) { 120 _mesa_error( ctx, GL_INVALID_VALUE, "glFog" ); 121 return; 122 } 123 if (ctx->Fog.Density == *params) 124 return; 125 FLUSH_VERTICES(ctx, _NEW_FOG); 126 ctx->Fog.Density = *params; 127 break; 128 case GL_FOG_START: 129 if (ctx->Fog.Start == *params) 130 return; 131 FLUSH_VERTICES(ctx, _NEW_FOG); 132 ctx->Fog.Start = *params; 133 update_fog_scale(ctx); 134 break; 135 case GL_FOG_END: 136 if (ctx->Fog.End == *params) 137 return; 138 FLUSH_VERTICES(ctx, _NEW_FOG); 139 ctx->Fog.End = *params; 140 update_fog_scale(ctx); 141 break; 142 case GL_FOG_INDEX: 143 if (ctx->API != API_OPENGL_COMPAT) 144 goto invalid_pname; 145 if (ctx->Fog.Index == *params) 146 return; 147 FLUSH_VERTICES(ctx, _NEW_FOG); 148 ctx->Fog.Index = *params; 149 break; 150 case GL_FOG_COLOR: 151 if (TEST_EQ_4V(ctx->Fog.Color, params)) 152 return; 153 FLUSH_VERTICES(ctx, _NEW_FOG); 154 ctx->Fog.ColorUnclamped[0] = params[0]; 155 ctx->Fog.ColorUnclamped[1] = params[1]; 156 ctx->Fog.ColorUnclamped[2] = params[2]; 157 ctx->Fog.ColorUnclamped[3] = params[3]; 158 ctx->Fog.Color[0] = CLAMP(params[0], 0.0F, 1.0F); 159 ctx->Fog.Color[1] = CLAMP(params[1], 0.0F, 1.0F); 160 ctx->Fog.Color[2] = CLAMP(params[2], 0.0F, 1.0F); 161 ctx->Fog.Color[3] = CLAMP(params[3], 0.0F, 1.0F); 162 break; 163 case GL_FOG_COORDINATE_SOURCE_EXT: { 164 GLenum p = (GLenum) (GLint) *params; 165 if (ctx->API != API_OPENGL_COMPAT || 166 (p != GL_FOG_COORDINATE_EXT && p != GL_FRAGMENT_DEPTH_EXT)) { 167 _mesa_error(ctx, GL_INVALID_ENUM, "glFog"); 168 return; 169 } 170 if (ctx->Fog.FogCoordinateSource == p) 171 return; 172 FLUSH_VERTICES(ctx, _NEW_FOG); 173 ctx->Fog.FogCoordinateSource = p; 174 break; 175 } 176 case GL_FOG_DISTANCE_MODE_NV: { 177 GLenum p = (GLenum) (GLint) *params; 178 if (ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.NV_fog_distance || 179 (p != GL_EYE_RADIAL_NV && p != GL_EYE_PLANE && p != GL_EYE_PLANE_ABSOLUTE_NV)) { 180 _mesa_error(ctx, GL_INVALID_ENUM, "glFog"); 181 return; 182 } 183 if (ctx->Fog.FogDistanceMode == p) 184 return; 185 FLUSH_VERTICES(ctx, _NEW_FOG); 186 ctx->Fog.FogDistanceMode = p; 187 break; 188 } 189 default: 190 goto invalid_pname; 191 } 192 193 if (ctx->Driver.Fogfv) { 194 (*ctx->Driver.Fogfv)( ctx, pname, params ); 195 } 196 197 return; 198 199invalid_pname: 200 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" ); 201 return; 202} 203 204 205/**********************************************************************/ 206/***** Initialization *****/ 207/**********************************************************************/ 208 209void _mesa_init_fog( struct gl_context * ctx ) 210{ 211 /* Fog group */ 212 ctx->Fog.Enabled = GL_FALSE; 213 ctx->Fog.Mode = GL_EXP; 214 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 ); 215 ASSIGN_4V( ctx->Fog.ColorUnclamped, 0.0, 0.0, 0.0, 0.0 ); 216 ctx->Fog.Index = 0.0; 217 ctx->Fog.Density = 1.0; 218 ctx->Fog.Start = 0.0; 219 ctx->Fog.End = 1.0; 220 ctx->Fog.ColorSumEnabled = GL_FALSE; 221 ctx->Fog.FogCoordinateSource = GL_FRAGMENT_DEPTH_EXT; 222 ctx->Fog._Scale = 1.0f; 223 ctx->Fog.FogDistanceMode = GL_EYE_PLANE_ABSOLUTE_NV; 224} 225