| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1 | /* $Id: light.c,v 1.24 2000/11/13 20:02:56 keithw Exp $ */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Mesa 3-D graphics library |
| Brian Paul | ba643a2 | 2000-10-28 18:34:48 +0000 | [diff] [blame] | 5 | * Version: 3.5 |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 6 | * |
| Brian Paul | 16a9efe | 2000-01-13 00:29:02 +0000 | [diff] [blame] | 7 | * Copyright (C) 1999-2000 Brian Paul All Rights Reserved. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 28 | #ifdef PC_HEADER |
| 29 | #include "all.h" |
| 30 | #else |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 31 | #include "glheader.h" |
| Brian Paul | c893a01 | 2000-10-28 20:41:13 +0000 | [diff] [blame] | 32 | #include "colormac.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 33 | #include "context.h" |
| 34 | #include "enums.h" |
| 35 | #include "light.h" |
| 36 | #include "macros.h" |
| 37 | #include "matrix.h" |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 38 | #include "mem.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 39 | #include "mmath.h" |
| 40 | #include "simple_list.h" |
| 41 | #include "types.h" |
| 42 | #include "vb.h" |
| 43 | #include "xform.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | |
| Brian Paul | 075398b | 2000-01-31 23:33:53 +0000 | [diff] [blame] | 47 | /* XXX this is a bit of a hack needed for compilation within XFree86 */ |
| 48 | #ifndef FLT_MIN |
| 49 | #define FLT_MIN 1e-37 |
| 50 | #endif |
| 51 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 52 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 53 | void |
| 54 | _mesa_ShadeModel( GLenum mode ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 55 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 56 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 57 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glShadeModel"); |
| 58 | |
| 59 | if (MESA_VERBOSE & VERBOSE_API) |
| 60 | fprintf(stderr, "glShadeModel %s\n", gl_lookup_enum_by_nr(mode)); |
| 61 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 62 | if (mode == GL_FLAT || mode == GL_SMOOTH) { |
| Brian Paul | 8acb3a1 | 1999-11-22 18:58:53 +0000 | [diff] [blame] | 63 | if (ctx->Light.ShadeModel != mode) { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 64 | ctx->Light.ShadeModel = mode; |
| Brian Paul | 8acb3a1 | 1999-11-22 18:58:53 +0000 | [diff] [blame] | 65 | if (ctx->Light.ShadeModel == GL_FLAT) |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 66 | SET_BITS(ctx->_TriangleCaps, DD_FLATSHADE); |
| Brian Paul | 8acb3a1 | 1999-11-22 18:58:53 +0000 | [diff] [blame] | 67 | else |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 68 | CLEAR_BITS(ctx->_TriangleCaps, DD_FLATSHADE); |
| Keith Whitwell | a96308c | 2000-10-30 13:31:59 +0000 | [diff] [blame] | 69 | |
| 70 | ctx->NewState |= _NEW_LIGHT; |
| 71 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 72 | if (ctx->Driver.ShadeModel) |
| 73 | (*ctx->Driver.ShadeModel)( ctx, mode ); |
| Keith Whitwell | 1bf9dfa | 1999-09-18 20:41:22 +0000 | [diff] [blame] | 74 | } |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 75 | } |
| 76 | else { |
| Keith Whitwell | 1bf9dfa | 1999-09-18 20:41:22 +0000 | [diff] [blame] | 77 | gl_error( ctx, GL_INVALID_ENUM, "glShadeModel" ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 78 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 83 | void |
| 84 | _mesa_Lightf( GLenum light, GLenum pname, GLfloat param ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 85 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 86 | _mesa_Lightfv( light, pname, ¶m ); |
| 87 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 88 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 89 | |
| 90 | void |
| 91 | _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params ) |
| 92 | { |
| 93 | GET_CURRENT_CONTEXT(ctx); |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 94 | GLint i = (GLint) (light - GL_LIGHT0); |
| 95 | struct gl_light *l = &ctx->Light.Light[i]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 96 | |
| 97 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLight"); |
| 98 | |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 99 | if (i < 0 || i >= MAX_LIGHTS) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 100 | gl_error( ctx, GL_INVALID_ENUM, "glLight" ); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | switch (pname) { |
| 105 | case GL_AMBIENT: |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 106 | COPY_4V( l->Ambient, params ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 107 | break; |
| 108 | case GL_DIFFUSE: |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 109 | COPY_4V( l->Diffuse, params ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 110 | break; |
| 111 | case GL_SPECULAR: |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 112 | COPY_4V( l->Specular, params ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 113 | break; |
| 114 | case GL_POSITION: |
| 115 | /* transform position by ModelView matrix */ |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 116 | TRANSFORM_POINT( l->EyePosition, ctx->ModelView.m, params ); |
| 117 | if (l->EyePosition[3] != 0.0F) |
| 118 | l->_Flags |= LIGHT_POSITIONAL; |
| 119 | else |
| 120 | l->_Flags &= ~LIGHT_POSITIONAL; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 121 | break; |
| 122 | case GL_SPOT_DIRECTION: |
| 123 | /* transform direction by inverse modelview */ |
| 124 | if (ctx->ModelView.flags & MAT_DIRTY_INVERSE) { |
| 125 | gl_matrix_analyze( &ctx->ModelView ); |
| 126 | } |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 127 | TRANSFORM_NORMAL( l->EyeDirection, params, ctx->ModelView.inv ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 128 | break; |
| 129 | case GL_SPOT_EXPONENT: |
| 130 | if (params[0]<0.0 || params[0]>128.0) { |
| 131 | gl_error( ctx, GL_INVALID_VALUE, "glLight" ); |
| 132 | return; |
| 133 | } |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 134 | if (l->SpotExponent != params[0]) { |
| 135 | l->SpotExponent = params[0]; |
| 136 | gl_compute_spot_exp_table( l ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 137 | } |
| 138 | break; |
| 139 | case GL_SPOT_CUTOFF: |
| 140 | if ((params[0]<0.0 || params[0]>90.0) && params[0]!=180.0) { |
| 141 | gl_error( ctx, GL_INVALID_VALUE, "glLight" ); |
| 142 | return; |
| 143 | } |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 144 | l->SpotCutoff = params[0]; |
| 145 | l->_CosCutoff = cos(params[0]*DEG2RAD); |
| 146 | if (l->_CosCutoff < 0) |
| 147 | l->_CosCutoff = 0; |
| 148 | if (l->SpotCutoff != 180.0F) |
| 149 | l->_Flags |= LIGHT_SPOT; |
| 150 | else |
| 151 | l->_Flags &= ~LIGHT_SPOT; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 152 | break; |
| 153 | case GL_CONSTANT_ATTENUATION: |
| 154 | if (params[0]<0.0) { |
| 155 | gl_error( ctx, GL_INVALID_VALUE, "glLight" ); |
| 156 | return; |
| 157 | } |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 158 | l->ConstantAttenuation = params[0]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 159 | break; |
| 160 | case GL_LINEAR_ATTENUATION: |
| 161 | if (params[0]<0.0) { |
| 162 | gl_error( ctx, GL_INVALID_VALUE, "glLight" ); |
| 163 | return; |
| 164 | } |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 165 | l->LinearAttenuation = params[0]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 166 | break; |
| 167 | case GL_QUADRATIC_ATTENUATION: |
| 168 | if (params[0]<0.0) { |
| 169 | gl_error( ctx, GL_INVALID_VALUE, "glLight" ); |
| 170 | return; |
| 171 | } |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 172 | l->QuadraticAttenuation = params[0]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 173 | break; |
| 174 | default: |
| 175 | gl_error( ctx, GL_INVALID_ENUM, "glLight" ); |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 176 | return; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 179 | if (ctx->Driver.Lightfv) |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 180 | ctx->Driver.Lightfv( ctx, light, pname, params ); |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 181 | |
| Keith Whitwell | a96308c | 2000-10-30 13:31:59 +0000 | [diff] [blame] | 182 | ctx->NewState |= _NEW_LIGHT; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 186 | void |
| 187 | _mesa_Lighti( GLenum light, GLenum pname, GLint param ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 188 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 189 | _mesa_Lightiv( light, pname, ¶m ); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void |
| 194 | _mesa_Lightiv( GLenum light, GLenum pname, const GLint *params ) |
| 195 | { |
| 196 | GLfloat fparam[4]; |
| 197 | |
| 198 | switch (pname) { |
| 199 | case GL_AMBIENT: |
| 200 | case GL_DIFFUSE: |
| 201 | case GL_SPECULAR: |
| 202 | fparam[0] = INT_TO_FLOAT( params[0] ); |
| 203 | fparam[1] = INT_TO_FLOAT( params[1] ); |
| 204 | fparam[2] = INT_TO_FLOAT( params[2] ); |
| 205 | fparam[3] = INT_TO_FLOAT( params[3] ); |
| 206 | break; |
| 207 | case GL_POSITION: |
| 208 | fparam[0] = (GLfloat) params[0]; |
| 209 | fparam[1] = (GLfloat) params[1]; |
| 210 | fparam[2] = (GLfloat) params[2]; |
| 211 | fparam[3] = (GLfloat) params[3]; |
| 212 | break; |
| 213 | case GL_SPOT_DIRECTION: |
| 214 | fparam[0] = (GLfloat) params[0]; |
| 215 | fparam[1] = (GLfloat) params[1]; |
| 216 | fparam[2] = (GLfloat) params[2]; |
| 217 | break; |
| 218 | case GL_SPOT_EXPONENT: |
| 219 | case GL_SPOT_CUTOFF: |
| 220 | case GL_CONSTANT_ATTENUATION: |
| 221 | case GL_LINEAR_ATTENUATION: |
| 222 | case GL_QUADRATIC_ATTENUATION: |
| 223 | fparam[0] = (GLfloat) params[0]; |
| 224 | break; |
| 225 | default: |
| 226 | /* error will be caught later in gl_Lightfv */ |
| 227 | ; |
| 228 | } |
| 229 | |
| 230 | _mesa_Lightfv( light, pname, fparam ); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | void |
| 236 | _mesa_GetLightfv( GLenum light, GLenum pname, GLfloat *params ) |
| 237 | { |
| 238 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 239 | GLint l = (GLint) (light - GL_LIGHT0); |
| 240 | |
| 241 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetLight"); |
| 242 | |
| 243 | if (l<0 || l>=MAX_LIGHTS) { |
| 244 | gl_error( ctx, GL_INVALID_ENUM, "glGetLightfv" ); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | switch (pname) { |
| 249 | case GL_AMBIENT: |
| 250 | COPY_4V( params, ctx->Light.Light[l].Ambient ); |
| 251 | break; |
| 252 | case GL_DIFFUSE: |
| 253 | COPY_4V( params, ctx->Light.Light[l].Diffuse ); |
| 254 | break; |
| 255 | case GL_SPECULAR: |
| 256 | COPY_4V( params, ctx->Light.Light[l].Specular ); |
| 257 | break; |
| 258 | case GL_POSITION: |
| 259 | COPY_4V( params, ctx->Light.Light[l].EyePosition ); |
| 260 | break; |
| 261 | case GL_SPOT_DIRECTION: |
| 262 | COPY_3V( params, ctx->Light.Light[l].EyeDirection ); |
| 263 | break; |
| 264 | case GL_SPOT_EXPONENT: |
| 265 | params[0] = ctx->Light.Light[l].SpotExponent; |
| 266 | break; |
| 267 | case GL_SPOT_CUTOFF: |
| 268 | params[0] = ctx->Light.Light[l].SpotCutoff; |
| 269 | break; |
| 270 | case GL_CONSTANT_ATTENUATION: |
| 271 | params[0] = ctx->Light.Light[l].ConstantAttenuation; |
| 272 | break; |
| 273 | case GL_LINEAR_ATTENUATION: |
| 274 | params[0] = ctx->Light.Light[l].LinearAttenuation; |
| 275 | break; |
| 276 | case GL_QUADRATIC_ATTENUATION: |
| 277 | params[0] = ctx->Light.Light[l].QuadraticAttenuation; |
| 278 | break; |
| 279 | default: |
| 280 | gl_error( ctx, GL_INVALID_ENUM, "glGetLightfv" ); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 287 | void |
| 288 | _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 289 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 290 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 291 | GLint l = (GLint) (light - GL_LIGHT0); |
| 292 | |
| 293 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetLight"); |
| 294 | |
| 295 | if (l<0 || l>=MAX_LIGHTS) { |
| 296 | gl_error( ctx, GL_INVALID_ENUM, "glGetLightiv" ); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | switch (pname) { |
| 301 | case GL_AMBIENT: |
| 302 | params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[0]); |
| 303 | params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[1]); |
| 304 | params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[2]); |
| 305 | params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Ambient[3]); |
| 306 | break; |
| 307 | case GL_DIFFUSE: |
| 308 | params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[0]); |
| 309 | params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[1]); |
| 310 | params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[2]); |
| 311 | params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Diffuse[3]); |
| 312 | break; |
| 313 | case GL_SPECULAR: |
| 314 | params[0] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[0]); |
| 315 | params[1] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[1]); |
| 316 | params[2] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[2]); |
| 317 | params[3] = FLOAT_TO_INT(ctx->Light.Light[l].Specular[3]); |
| 318 | break; |
| 319 | case GL_POSITION: |
| 320 | params[0] = (GLint) ctx->Light.Light[l].EyePosition[0]; |
| 321 | params[1] = (GLint) ctx->Light.Light[l].EyePosition[1]; |
| 322 | params[2] = (GLint) ctx->Light.Light[l].EyePosition[2]; |
| 323 | params[3] = (GLint) ctx->Light.Light[l].EyePosition[3]; |
| 324 | break; |
| 325 | case GL_SPOT_DIRECTION: |
| 326 | params[0] = (GLint) ctx->Light.Light[l].EyeDirection[0]; |
| 327 | params[1] = (GLint) ctx->Light.Light[l].EyeDirection[1]; |
| 328 | params[2] = (GLint) ctx->Light.Light[l].EyeDirection[2]; |
| 329 | break; |
| 330 | case GL_SPOT_EXPONENT: |
| 331 | params[0] = (GLint) ctx->Light.Light[l].SpotExponent; |
| 332 | break; |
| 333 | case GL_SPOT_CUTOFF: |
| 334 | params[0] = (GLint) ctx->Light.Light[l].SpotCutoff; |
| 335 | break; |
| 336 | case GL_CONSTANT_ATTENUATION: |
| 337 | params[0] = (GLint) ctx->Light.Light[l].ConstantAttenuation; |
| 338 | break; |
| 339 | case GL_LINEAR_ATTENUATION: |
| 340 | params[0] = (GLint) ctx->Light.Light[l].LinearAttenuation; |
| 341 | break; |
| 342 | case GL_QUADRATIC_ATTENUATION: |
| 343 | params[0] = (GLint) ctx->Light.Light[l].QuadraticAttenuation; |
| 344 | break; |
| 345 | default: |
| 346 | gl_error( ctx, GL_INVALID_ENUM, "glGetLightiv" ); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | |
| 352 | |
| 353 | /**********************************************************************/ |
| 354 | /*** Light Model ***/ |
| 355 | /**********************************************************************/ |
| 356 | |
| 357 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 358 | void |
| 359 | _mesa_LightModelfv( GLenum pname, const GLfloat *params ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 360 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 361 | GET_CURRENT_CONTEXT(ctx); |
| 362 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLightModelfv"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 363 | |
| 364 | switch (pname) { |
| 365 | case GL_LIGHT_MODEL_AMBIENT: |
| 366 | COPY_4V( ctx->Light.Model.Ambient, params ); |
| 367 | break; |
| 368 | case GL_LIGHT_MODEL_LOCAL_VIEWER: |
| 369 | if (params[0]==0.0) |
| 370 | ctx->Light.Model.LocalViewer = GL_FALSE; |
| 371 | else |
| 372 | ctx->Light.Model.LocalViewer = GL_TRUE; |
| 373 | break; |
| 374 | case GL_LIGHT_MODEL_TWO_SIDE: |
| 375 | if (params[0]==0.0) |
| 376 | ctx->Light.Model.TwoSide = GL_FALSE; |
| 377 | else |
| 378 | ctx->Light.Model.TwoSide = GL_TRUE; |
| 379 | break; |
| 380 | case GL_LIGHT_MODEL_COLOR_CONTROL: |
| Brian Paul | f3f9b77 | 1999-10-19 20:32:40 +0000 | [diff] [blame] | 381 | if (params[0] == (GLfloat) GL_SINGLE_COLOR) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 382 | ctx->Light.Model.ColorControl = GL_SINGLE_COLOR; |
| Keith Whitwell | fe5d67d | 2000-10-27 16:44:40 +0000 | [diff] [blame] | 383 | if (!ctx->Fog.ColorSumEnabled) |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 384 | CLEAR_BITS(ctx->_TriangleCaps, DD_SEPERATE_SPECULAR); |
| Brian Paul | f3f9b77 | 1999-10-19 20:32:40 +0000 | [diff] [blame] | 385 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 386 | else if (params[0] == (GLfloat) GL_SEPARATE_SPECULAR_COLOR) { |
| 387 | ctx->Light.Model.ColorControl = GL_SEPARATE_SPECULAR_COLOR; |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 388 | SET_BITS(ctx->_TriangleCaps, DD_SEPERATE_SPECULAR); |
| Brian Paul | f3f9b77 | 1999-10-19 20:32:40 +0000 | [diff] [blame] | 389 | } |
| 390 | else { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 391 | gl_error( ctx, GL_INVALID_ENUM, "glLightModel(param)" ); |
| Brian Paul | f3f9b77 | 1999-10-19 20:32:40 +0000 | [diff] [blame] | 392 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 393 | break; |
| 394 | default: |
| 395 | gl_error( ctx, GL_INVALID_ENUM, "glLightModel" ); |
| 396 | break; |
| 397 | } |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 398 | |
| 399 | if (ctx->Driver.LightModelfv) |
| 400 | ctx->Driver.LightModelfv( ctx, pname, params ); |
| 401 | |
| Keith Whitwell | a96308c | 2000-10-30 13:31:59 +0000 | [diff] [blame] | 402 | ctx->NewState |= _NEW_LIGHT; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 406 | void |
| 407 | _mesa_LightModeliv( GLenum pname, const GLint *params ) |
| 408 | { |
| 409 | GLfloat fparam[4]; |
| 410 | GET_CURRENT_CONTEXT(ctx); |
| 411 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLightModeliv"); |
| 412 | |
| 413 | switch (pname) { |
| 414 | case GL_LIGHT_MODEL_AMBIENT: |
| 415 | fparam[0] = INT_TO_FLOAT( params[0] ); |
| 416 | fparam[1] = INT_TO_FLOAT( params[1] ); |
| 417 | fparam[2] = INT_TO_FLOAT( params[2] ); |
| 418 | fparam[3] = INT_TO_FLOAT( params[3] ); |
| 419 | break; |
| 420 | case GL_LIGHT_MODEL_LOCAL_VIEWER: |
| 421 | case GL_LIGHT_MODEL_TWO_SIDE: |
| 422 | case GL_LIGHT_MODEL_COLOR_CONTROL: |
| 423 | fparam[0] = (GLfloat) params[0]; |
| 424 | break; |
| 425 | default: |
| 426 | /* Error will be caught later in gl_LightModelfv */ |
| 427 | ; |
| 428 | } |
| 429 | _mesa_LightModelfv( pname, fparam ); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | void |
| 434 | _mesa_LightModeli( GLenum pname, GLint param ) |
| 435 | { |
| 436 | _mesa_LightModeliv( pname, ¶m ); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | void |
| 441 | _mesa_LightModelf( GLenum pname, GLfloat param ) |
| 442 | { |
| 443 | _mesa_LightModelfv( pname, ¶m ); |
| 444 | } |
| 445 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 446 | |
| 447 | |
| 448 | /********** MATERIAL **********/ |
| 449 | |
| 450 | |
| 451 | /* |
| 452 | * Given a face and pname value (ala glColorMaterial), compute a bitmask |
| 453 | * of the targeted material values. |
| 454 | */ |
| 455 | GLuint gl_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname, |
| 456 | GLuint legal, |
| 457 | const char *where ) |
| 458 | { |
| 459 | GLuint bitmask = 0; |
| 460 | |
| 461 | /* Make a bitmask indicating what material attribute(s) we're updating */ |
| 462 | switch (pname) { |
| 463 | case GL_EMISSION: |
| 464 | bitmask |= FRONT_EMISSION_BIT | BACK_EMISSION_BIT; |
| 465 | break; |
| 466 | case GL_AMBIENT: |
| 467 | bitmask |= FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT; |
| 468 | break; |
| 469 | case GL_DIFFUSE: |
| 470 | bitmask |= FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT; |
| 471 | break; |
| 472 | case GL_SPECULAR: |
| 473 | bitmask |= FRONT_SPECULAR_BIT | BACK_SPECULAR_BIT; |
| 474 | break; |
| 475 | case GL_SHININESS: |
| 476 | bitmask |= FRONT_SHININESS_BIT | BACK_SHININESS_BIT; |
| 477 | break; |
| 478 | case GL_AMBIENT_AND_DIFFUSE: |
| 479 | bitmask |= FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT; |
| 480 | bitmask |= FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT; |
| 481 | break; |
| 482 | case GL_COLOR_INDEXES: |
| 483 | bitmask |= FRONT_INDEXES_BIT | BACK_INDEXES_BIT; |
| 484 | break; |
| 485 | default: |
| 486 | gl_error( ctx, GL_INVALID_ENUM, where ); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | if (face==GL_FRONT) { |
| 491 | bitmask &= FRONT_MATERIAL_BITS; |
| 492 | } |
| 493 | else if (face==GL_BACK) { |
| 494 | bitmask &= BACK_MATERIAL_BITS; |
| 495 | } |
| 496 | else if (face != GL_FRONT_AND_BACK) { |
| 497 | gl_error( ctx, GL_INVALID_ENUM, where ); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | if (bitmask & ~legal) { |
| 502 | gl_error( ctx, GL_INVALID_ENUM, where ); |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | return bitmask; |
| 507 | } |
| 508 | |
| 509 | |
| 510 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 511 | /* |
| 512 | * Check if the global material has to be updated with info that was |
| 513 | * associated with a vertex via glMaterial. |
| 514 | * This function is used when any material values get changed between |
| 515 | * glBegin/glEnd either by calling glMaterial() or by calling glColor() |
| 516 | * when GL_COLOR_MATERIAL is enabled. |
| 517 | * |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 518 | * src[0] is front material, src[1] is back material |
| 519 | * |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 520 | * KW: Added code here to keep the precomputed variables uptodate. |
| 521 | * This means we can use the faster shade functions when using |
| 522 | * GL_COLOR_MATERIAL, and we can also now use the precomputed |
| 523 | * values in the slower shading functions, which further offsets |
| 524 | * the cost of doing this here. |
| 525 | */ |
| 526 | void gl_update_material( GLcontext *ctx, |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 527 | const struct gl_material src[2], |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 528 | GLuint bitmask ) |
| 529 | { |
| 530 | struct gl_light *light, *list = &ctx->Light.EnabledList; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 531 | |
| 532 | if (ctx->Light.ColorMaterialEnabled) |
| 533 | bitmask &= ~ctx->Light.ColorMaterialBitmask; |
| 534 | |
| Keith Whitwell | 06ac592 | 1999-11-10 06:29:44 +0000 | [diff] [blame] | 535 | if (MESA_VERBOSE&VERBOSE_IMMEDIATE) |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 536 | fprintf(stderr, "gl_update_material, mask 0x%x\n", bitmask); |
| Keith Whitwell | 06ac592 | 1999-11-10 06:29:44 +0000 | [diff] [blame] | 537 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 538 | if (!bitmask) |
| 539 | return; |
| 540 | |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 541 | /* update material emission */ |
| 542 | if (bitmask & FRONT_EMISSION_BIT) { |
| 543 | struct gl_material *mat = &ctx->Light.Material[0]; |
| 544 | COPY_4FV( mat->Emission, src[0].Emission ); |
| 545 | } |
| 546 | if (bitmask & BACK_EMISSION_BIT) { |
| 547 | struct gl_material *mat = &ctx->Light.Material[1]; |
| 548 | COPY_4FV( mat->Emission, src[1].Emission ); |
| 549 | } |
| 550 | |
| 551 | /* update material ambience */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 552 | if (bitmask & FRONT_AMBIENT_BIT) { |
| 553 | struct gl_material *mat = &ctx->Light.Material[0]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 554 | COPY_4FV( mat->Ambient, src[0].Ambient ); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 555 | foreach (light, list) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 556 | SCALE_3V( light->_MatAmbient[0], light->Ambient, src[0].Ambient); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 557 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 558 | } |
| 559 | if (bitmask & BACK_AMBIENT_BIT) { |
| 560 | struct gl_material *mat = &ctx->Light.Material[1]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 561 | COPY_4FV( mat->Ambient, src[1].Ambient ); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 562 | foreach (light, list) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 563 | SCALE_3V( light->_MatAmbient[1], light->Ambient, src[1].Ambient); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 564 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 565 | } |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 566 | |
| 567 | /* update BaseColor = emission + scene's ambience * material's ambience */ |
| 568 | if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) { |
| 569 | struct gl_material *mat = &ctx->Light.Material[0]; |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 570 | COPY_3V( ctx->Light._BaseColor[0], mat->Emission ); |
| 571 | ACC_SCALE_3V( ctx->Light._BaseColor[0], mat->Ambient, ctx->Light.Model.Ambient ); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 572 | } |
| 573 | if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) { |
| 574 | struct gl_material *mat = &ctx->Light.Material[1]; |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 575 | COPY_3V( ctx->Light._BaseColor[1], mat->Emission ); |
| 576 | ACC_SCALE_3V( ctx->Light._BaseColor[1], mat->Ambient, ctx->Light.Model.Ambient ); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* update material diffuse values */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 580 | if (bitmask & FRONT_DIFFUSE_BIT) { |
| 581 | struct gl_material *mat = &ctx->Light.Material[0]; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 582 | GLfloat tmp[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 583 | SUB_3V( tmp, src[0].Diffuse, mat->Diffuse ); |
| 584 | foreach (light, list) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 585 | ACC_SCALE_3V( light->_MatDiffuse[0], light->Diffuse, tmp ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 586 | } |
| 587 | COPY_4FV( mat->Diffuse, src[0].Diffuse ); |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 588 | FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 589 | } |
| 590 | if (bitmask & BACK_DIFFUSE_BIT) { |
| 591 | struct gl_material *mat = &ctx->Light.Material[1]; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 592 | GLfloat tmp[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 593 | SUB_3V( tmp, src[1].Diffuse, mat->Diffuse ); |
| 594 | foreach (light, list) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 595 | ACC_SCALE_3V( light->_MatDiffuse[1], light->Diffuse, tmp ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 596 | } |
| 597 | COPY_4FV( mat->Diffuse, src[1].Diffuse ); |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 598 | FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 599 | } |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 600 | |
| 601 | /* update material specular values */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 602 | if (bitmask & FRONT_SPECULAR_BIT) { |
| 603 | struct gl_material *mat = &ctx->Light.Material[0]; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 604 | GLfloat tmp[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 605 | SUB_3V( tmp, src[0].Specular, mat->Specular ); |
| 606 | foreach (light, list) { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 607 | ACC_SCALE_3V( light->_MatSpecular[0], light->Specular, tmp ); |
| 608 | light->_IsMatSpecular[0] = |
| 609 | (LEN_SQUARED_3FV(light->_MatSpecular[0]) > 1e-16); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 610 | } |
| 611 | COPY_4FV( mat->Specular, src[0].Specular ); |
| 612 | } |
| 613 | if (bitmask & BACK_SPECULAR_BIT) { |
| 614 | struct gl_material *mat = &ctx->Light.Material[1]; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 615 | GLfloat tmp[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 616 | SUB_3V( tmp, src[1].Specular, mat->Specular ); |
| 617 | foreach (light, list) { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 618 | ACC_SCALE_3V( light->_MatSpecular[1], light->Specular, tmp ); |
| 619 | light->_IsMatSpecular[1] = |
| 620 | (LEN_SQUARED_3FV(light->_MatSpecular[1]) > 1e-16); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 621 | } |
| 622 | COPY_4FV( mat->Specular, src[1].Specular ); |
| 623 | } |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 624 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 625 | if (bitmask & FRONT_SHININESS_BIT) { |
| 626 | GLfloat shininess = ctx->Light.Material[0].Shininess = src[0].Shininess; |
| 627 | gl_compute_shine_table( ctx, 0, shininess ); |
| 628 | gl_compute_shine_table( ctx, 2, shininess * .5 ); |
| 629 | } |
| 630 | if (bitmask & BACK_SHININESS_BIT) { |
| 631 | GLfloat shininess = ctx->Light.Material[1].Shininess = src[1].Shininess; |
| 632 | gl_compute_shine_table( ctx, 1, shininess ); |
| 633 | gl_compute_shine_table( ctx, 3, shininess * .5 ); |
| 634 | } |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 635 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 636 | if (bitmask & FRONT_INDEXES_BIT) { |
| 637 | ctx->Light.Material[0].AmbientIndex = src[0].AmbientIndex; |
| 638 | ctx->Light.Material[0].DiffuseIndex = src[0].DiffuseIndex; |
| 639 | ctx->Light.Material[0].SpecularIndex = src[0].SpecularIndex; |
| 640 | } |
| 641 | if (bitmask & BACK_INDEXES_BIT) { |
| 642 | ctx->Light.Material[1].AmbientIndex = src[1].AmbientIndex; |
| 643 | ctx->Light.Material[1].DiffuseIndex = src[1].DiffuseIndex; |
| 644 | ctx->Light.Material[1].SpecularIndex = src[1].SpecularIndex; |
| 645 | } |
| 646 | |
| Keith Whitwell | 06ac592 | 1999-11-10 06:29:44 +0000 | [diff] [blame] | 647 | if (0) |
| 648 | { |
| 649 | struct gl_material *mat = &ctx->Light.Material[0]; |
| 650 | fprintf(stderr, "update_mat emission : %f %f %f\n", |
| 651 | mat->Emission[0], |
| 652 | mat->Emission[1], |
| 653 | mat->Emission[2]); |
| 654 | fprintf(stderr, "update_mat specular : %f %f %f\n", |
| 655 | mat->Specular[0], |
| 656 | mat->Specular[1], |
| 657 | mat->Specular[2]); |
| 658 | fprintf(stderr, "update_mat diffuse : %f %f %f\n", |
| 659 | mat->Diffuse[0], |
| 660 | mat->Diffuse[1], |
| 661 | mat->Diffuse[2]); |
| 662 | fprintf(stderr, "update_mat ambient : %f %f %f\n", |
| 663 | mat->Ambient[0], |
| 664 | mat->Ambient[1], |
| 665 | mat->Ambient[2]); |
| 666 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 672 | /* |
| 673 | * Update the current materials from the given rgba color |
| 674 | * according to the bitmask in ColorMaterialBitmask, which is |
| 675 | * set by glColorMaterial(). |
| 676 | */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 677 | void gl_update_color_material( GLcontext *ctx, |
| Brian Paul | ba643a2 | 2000-10-28 18:34:48 +0000 | [diff] [blame] | 678 | const GLchan rgba[4] ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 679 | { |
| 680 | struct gl_light *light, *list = &ctx->Light.EnabledList; |
| 681 | GLuint bitmask = ctx->Light.ColorMaterialBitmask; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 682 | GLfloat color[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 683 | |
| Brian Paul | c893a01 | 2000-10-28 20:41:13 +0000 | [diff] [blame] | 684 | color[0] = CHAN_TO_FLOAT(rgba[0]); |
| 685 | color[1] = CHAN_TO_FLOAT(rgba[1]); |
| 686 | color[2] = CHAN_TO_FLOAT(rgba[2]); |
| 687 | color[3] = CHAN_TO_FLOAT(rgba[3]); |
| 688 | |
| Keith Whitwell | 06ac592 | 1999-11-10 06:29:44 +0000 | [diff] [blame] | 689 | if (MESA_VERBOSE&VERBOSE_IMMEDIATE) |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 690 | fprintf(stderr, "gl_update_color_material, mask 0x%x\n", bitmask); |
| Keith Whitwell | 06ac592 | 1999-11-10 06:29:44 +0000 | [diff] [blame] | 691 | |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 692 | /* update emissive colors */ |
| 693 | if (bitmask & FRONT_EMISSION_BIT) { |
| 694 | struct gl_material *mat = &ctx->Light.Material[0]; |
| 695 | COPY_4FV( mat->Emission, color ); |
| 696 | } |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 697 | |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 698 | if (bitmask & BACK_EMISSION_BIT) { |
| 699 | struct gl_material *mat = &ctx->Light.Material[1]; |
| 700 | COPY_4FV( mat->Emission, color ); |
| 701 | } |
| 702 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 703 | /* update light->_MatAmbient = light's ambient * material's ambient */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 704 | if (bitmask & FRONT_AMBIENT_BIT) { |
| 705 | struct gl_material *mat = &ctx->Light.Material[0]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 706 | foreach (light, list) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 707 | SCALE_3V( light->_MatAmbient[0], light->Ambient, color); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 708 | } |
| 709 | COPY_4FV( mat->Ambient, color ); |
| 710 | } |
| 711 | |
| 712 | if (bitmask & BACK_AMBIENT_BIT) { |
| 713 | struct gl_material *mat = &ctx->Light.Material[1]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 714 | foreach (light, list) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 715 | SCALE_3V( light->_MatAmbient[1], light->Ambient, color); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 716 | } |
| 717 | COPY_4FV( mat->Ambient, color ); |
| 718 | } |
| 719 | |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 720 | /* update BaseColor = emission + scene's ambience * material's ambience */ |
| 721 | if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) { |
| 722 | struct gl_material *mat = &ctx->Light.Material[0]; |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 723 | COPY_3V( ctx->Light._BaseColor[0], mat->Emission ); |
| 724 | ACC_SCALE_3V( ctx->Light._BaseColor[0], mat->Ambient, ctx->Light.Model.Ambient ); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) { |
| 728 | struct gl_material *mat = &ctx->Light.Material[1]; |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 729 | COPY_3V( ctx->Light._BaseColor[1], mat->Emission ); |
| 730 | ACC_SCALE_3V( ctx->Light._BaseColor[1], mat->Ambient, ctx->Light.Model.Ambient ); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 733 | /* update light->_MatDiffuse = light's diffuse * material's diffuse */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 734 | if (bitmask & FRONT_DIFFUSE_BIT) { |
| 735 | struct gl_material *mat = &ctx->Light.Material[0]; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 736 | GLfloat tmp[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 737 | SUB_3V( tmp, color, mat->Diffuse ); |
| 738 | foreach (light, list) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 739 | ACC_SCALE_3V( light->_MatDiffuse[0], light->Diffuse, tmp ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 740 | } |
| 741 | COPY_4FV( mat->Diffuse, color ); |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 742 | FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | if (bitmask & BACK_DIFFUSE_BIT) { |
| 746 | struct gl_material *mat = &ctx->Light.Material[1]; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 747 | GLfloat tmp[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 748 | SUB_3V( tmp, color, mat->Diffuse ); |
| 749 | foreach (light, list) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 750 | ACC_SCALE_3V( light->_MatDiffuse[1], light->Diffuse, tmp ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 751 | } |
| 752 | COPY_4FV( mat->Diffuse, color ); |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 753 | FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 756 | /* update light->_MatSpecular = light's specular * material's specular */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 757 | if (bitmask & FRONT_SPECULAR_BIT) { |
| 758 | struct gl_material *mat = &ctx->Light.Material[0]; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 759 | GLfloat tmp[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 760 | SUB_3V( tmp, color, mat->Specular ); |
| 761 | foreach (light, list) { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 762 | ACC_SCALE_3V( light->_MatSpecular[0], light->Specular, tmp ); |
| 763 | light->_IsMatSpecular[0] = |
| 764 | (LEN_SQUARED_3FV(light->_MatSpecular[0]) > 1e-16); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 765 | } |
| 766 | COPY_4FV( mat->Specular, color ); |
| 767 | } |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 768 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 769 | if (bitmask & BACK_SPECULAR_BIT) { |
| 770 | struct gl_material *mat = &ctx->Light.Material[1]; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 771 | GLfloat tmp[4]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 772 | SUB_3V( tmp, color, mat->Specular ); |
| 773 | foreach (light, list) { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 774 | ACC_SCALE_3V( light->_MatSpecular[1], light->Specular, tmp ); |
| 775 | light->_IsMatSpecular[1] = |
| 776 | (LEN_SQUARED_3FV(light->_MatSpecular[1]) > 1e-16); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 777 | } |
| 778 | COPY_4FV( mat->Specular, color ); |
| 779 | } |
| Keith Whitwell | 06ac592 | 1999-11-10 06:29:44 +0000 | [diff] [blame] | 780 | |
| 781 | if (0) |
| 782 | { |
| 783 | struct gl_material *mat = &ctx->Light.Material[0]; |
| 784 | fprintf(stderr, "update_color_mat emission : %f %f %f\n", |
| 785 | mat->Emission[0], |
| 786 | mat->Emission[1], |
| 787 | mat->Emission[2]); |
| 788 | fprintf(stderr, "update_color_mat specular : %f %f %f\n", |
| 789 | mat->Specular[0], |
| 790 | mat->Specular[1], |
| 791 | mat->Specular[2]); |
| 792 | fprintf(stderr, "update_color_mat diffuse : %f %f %f\n", |
| 793 | mat->Diffuse[0], |
| 794 | mat->Diffuse[1], |
| 795 | mat->Diffuse[2]); |
| 796 | fprintf(stderr, "update_color_mat ambient : %f %f %f\n", |
| 797 | mat->Ambient[0], |
| 798 | mat->Ambient[1], |
| 799 | mat->Ambient[2]); |
| 800 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 806 | void |
| 807 | _mesa_ColorMaterial( GLenum face, GLenum mode ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 808 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 809 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 810 | GLuint bitmask; |
| 811 | GLuint legal = (FRONT_EMISSION_BIT | BACK_EMISSION_BIT | |
| 812 | FRONT_SPECULAR_BIT | BACK_SPECULAR_BIT | |
| 813 | FRONT_DIFFUSE_BIT | BACK_DIFFUSE_BIT | |
| 814 | FRONT_AMBIENT_BIT | BACK_AMBIENT_BIT); |
| 815 | |
| 816 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorMaterial"); |
| 817 | |
| Keith Whitwell | 06ac592 | 1999-11-10 06:29:44 +0000 | [diff] [blame] | 818 | if (MESA_VERBOSE&VERBOSE_API) |
| 819 | fprintf(stderr, "glColorMaterial %s %s\n", |
| 820 | gl_lookup_enum_by_nr(face), |
| 821 | gl_lookup_enum_by_nr(mode)); |
| 822 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 823 | bitmask = gl_material_bitmask( ctx, face, mode, legal, "glColorMaterial" ); |
| 824 | |
| 825 | if (bitmask != 0) { |
| 826 | ctx->Light.ColorMaterialBitmask = bitmask; |
| 827 | ctx->Light.ColorMaterialFace = face; |
| 828 | ctx->Light.ColorMaterialMode = mode; |
| 829 | } |
| Keith Whitwell | 06ac592 | 1999-11-10 06:29:44 +0000 | [diff] [blame] | 830 | |
| 831 | if (ctx->Light.ColorMaterialEnabled) |
| Brian Paul | 1930053 | 2000-10-29 19:02:23 +0000 | [diff] [blame] | 832 | gl_update_color_material( ctx, ctx->Current.Color ); |
| Keith Whitwell | a96308c | 2000-10-30 13:31:59 +0000 | [diff] [blame] | 833 | |
| 834 | ctx->NewState |= _NEW_LIGHT; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | |
| 838 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 839 | |
| 840 | void |
| 841 | _mesa_Materialf( GLenum face, GLenum pname, GLfloat param ) |
| 842 | { |
| 843 | _mesa_Materialfv( face, pname, ¶m ); |
| 844 | } |
| 845 | |
| 846 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 847 | /* KW: This is now called directly (ie by name) from the glMaterial* |
| 848 | * API functions. |
| 849 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 850 | void |
| 851 | _mesa_Materialfv( GLenum face, GLenum pname, const GLfloat *params ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 852 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 853 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 854 | struct immediate *IM; |
| 855 | struct gl_material *mat; |
| 856 | GLuint bitmask; |
| 857 | GLuint count; |
| 858 | |
| 859 | bitmask = gl_material_bitmask( ctx, face, pname, ~0, "gl_Materialfv" ); |
| 860 | if (bitmask == 0) |
| 861 | return; |
| 862 | |
| 863 | IM = ctx->input; |
| 864 | count = IM->Count; |
| 865 | |
| Keith Whitwell | d471473 | 1999-10-19 18:37:02 +0000 | [diff] [blame] | 866 | if (!IM->Material) { |
| 867 | IM->Material = |
| 868 | (struct gl_material (*)[2]) MALLOC( sizeof(struct gl_material) * |
| 869 | VB_SIZE * 2 ); |
| 870 | IM->MaterialMask = (GLuint *) MALLOC( sizeof(GLuint) * VB_SIZE ); |
| 871 | } |
| 872 | |
| 873 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 874 | if (!(IM->Flag[count] & VERT_MATERIAL)) { |
| 875 | IM->Flag[count] |= VERT_MATERIAL; |
| Keith Whitwell | d471473 | 1999-10-19 18:37:02 +0000 | [diff] [blame] | 876 | IM->MaterialMask[count] = 0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 879 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 880 | IM->MaterialMask[count] |= bitmask; |
| 881 | mat = IM->Material[count]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 882 | |
| 883 | if (bitmask & FRONT_AMBIENT_BIT) { |
| 884 | COPY_4FV( mat[0].Ambient, params ); |
| 885 | } |
| 886 | if (bitmask & BACK_AMBIENT_BIT) { |
| 887 | COPY_4FV( mat[1].Ambient, params ); |
| 888 | } |
| 889 | if (bitmask & FRONT_DIFFUSE_BIT) { |
| 890 | COPY_4FV( mat[0].Diffuse, params ); |
| 891 | } |
| 892 | if (bitmask & BACK_DIFFUSE_BIT) { |
| 893 | COPY_4FV( mat[1].Diffuse, params ); |
| 894 | } |
| 895 | if (bitmask & FRONT_SPECULAR_BIT) { |
| 896 | COPY_4FV( mat[0].Specular, params ); |
| 897 | } |
| 898 | if (bitmask & BACK_SPECULAR_BIT) { |
| 899 | COPY_4FV( mat[1].Specular, params ); |
| 900 | } |
| 901 | if (bitmask & FRONT_EMISSION_BIT) { |
| 902 | COPY_4FV( mat[0].Emission, params ); |
| 903 | } |
| 904 | if (bitmask & BACK_EMISSION_BIT) { |
| 905 | COPY_4FV( mat[1].Emission, params ); |
| 906 | } |
| 907 | if (bitmask & FRONT_SHININESS_BIT) { |
| 908 | GLfloat shininess = CLAMP( params[0], 0.0F, 128.0F ); |
| 909 | mat[0].Shininess = shininess; |
| 910 | } |
| 911 | if (bitmask & BACK_SHININESS_BIT) { |
| 912 | GLfloat shininess = CLAMP( params[0], 0.0F, 128.0F ); |
| 913 | mat[1].Shininess = shininess; |
| 914 | } |
| 915 | if (bitmask & FRONT_INDEXES_BIT) { |
| 916 | mat[0].AmbientIndex = params[0]; |
| 917 | mat[0].DiffuseIndex = params[1]; |
| 918 | mat[0].SpecularIndex = params[2]; |
| 919 | } |
| 920 | if (bitmask & BACK_INDEXES_BIT) { |
| 921 | mat[1].AmbientIndex = params[0]; |
| 922 | mat[1].DiffuseIndex = params[1]; |
| 923 | mat[1].SpecularIndex = params[2]; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 928 | void |
| 929 | _mesa_Materiali(GLenum face, GLenum pname, GLint param ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 930 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 931 | _mesa_Materialiv(face, pname, ¶m); |
| 932 | } |
| 933 | |
| 934 | |
| 935 | void |
| 936 | _mesa_Materialiv(GLenum face, GLenum pname, const GLint *params ) |
| 937 | { |
| 938 | GLfloat fparam[4]; |
| 939 | switch (pname) { |
| 940 | case GL_AMBIENT: |
| 941 | case GL_DIFFUSE: |
| 942 | case GL_SPECULAR: |
| 943 | case GL_EMISSION: |
| 944 | case GL_AMBIENT_AND_DIFFUSE: |
| 945 | fparam[0] = INT_TO_FLOAT( params[0] ); |
| 946 | fparam[1] = INT_TO_FLOAT( params[1] ); |
| 947 | fparam[2] = INT_TO_FLOAT( params[2] ); |
| 948 | fparam[3] = INT_TO_FLOAT( params[3] ); |
| 949 | break; |
| 950 | case GL_SHININESS: |
| 951 | fparam[0] = (GLfloat) params[0]; |
| 952 | break; |
| 953 | case GL_COLOR_INDEXES: |
| 954 | fparam[0] = (GLfloat) params[0]; |
| 955 | fparam[1] = (GLfloat) params[1]; |
| 956 | fparam[2] = (GLfloat) params[2]; |
| 957 | break; |
| 958 | default: |
| 959 | /* Error will be caught later in gl_Materialfv */ |
| 960 | ; |
| 961 | } |
| 962 | _mesa_Materialfv(face, pname, fparam); |
| 963 | } |
| 964 | |
| 965 | |
| 966 | void |
| 967 | _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params ) |
| 968 | { |
| 969 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 970 | GLuint f; |
| 971 | |
| 972 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetMaterialfv"); |
| 973 | |
| 974 | if (face==GL_FRONT) { |
| 975 | f = 0; |
| 976 | } |
| 977 | else if (face==GL_BACK) { |
| 978 | f = 1; |
| 979 | } |
| 980 | else { |
| 981 | gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" ); |
| 982 | return; |
| 983 | } |
| 984 | switch (pname) { |
| 985 | case GL_AMBIENT: |
| 986 | COPY_4FV( params, ctx->Light.Material[f].Ambient ); |
| 987 | break; |
| 988 | case GL_DIFFUSE: |
| 989 | COPY_4FV( params, ctx->Light.Material[f].Diffuse ); |
| 990 | break; |
| 991 | case GL_SPECULAR: |
| 992 | COPY_4FV( params, ctx->Light.Material[f].Specular ); |
| 993 | break; |
| 994 | case GL_EMISSION: |
| 995 | COPY_4FV( params, ctx->Light.Material[f].Emission ); |
| 996 | break; |
| 997 | case GL_SHININESS: |
| 998 | *params = ctx->Light.Material[f].Shininess; |
| 999 | break; |
| 1000 | case GL_COLOR_INDEXES: |
| 1001 | params[0] = ctx->Light.Material[f].AmbientIndex; |
| 1002 | params[1] = ctx->Light.Material[f].DiffuseIndex; |
| 1003 | params[2] = ctx->Light.Material[f].SpecularIndex; |
| 1004 | break; |
| 1005 | default: |
| 1006 | gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" ); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1012 | void |
| 1013 | _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1014 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1015 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1016 | GLuint f; |
| 1017 | |
| 1018 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetMaterialiv"); |
| 1019 | |
| 1020 | if (face==GL_FRONT) { |
| 1021 | f = 0; |
| 1022 | } |
| 1023 | else if (face==GL_BACK) { |
| 1024 | f = 1; |
| 1025 | } |
| 1026 | else { |
| 1027 | gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" ); |
| 1028 | return; |
| 1029 | } |
| 1030 | switch (pname) { |
| 1031 | case GL_AMBIENT: |
| 1032 | params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[0] ); |
| 1033 | params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[1] ); |
| 1034 | params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[2] ); |
| 1035 | params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Ambient[3] ); |
| 1036 | break; |
| 1037 | case GL_DIFFUSE: |
| 1038 | params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[0] ); |
| 1039 | params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[1] ); |
| 1040 | params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[2] ); |
| 1041 | params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Diffuse[3] ); |
| 1042 | break; |
| 1043 | case GL_SPECULAR: |
| 1044 | params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[0] ); |
| 1045 | params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[1] ); |
| 1046 | params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[2] ); |
| 1047 | params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Specular[3] ); |
| 1048 | break; |
| 1049 | case GL_EMISSION: |
| 1050 | params[0] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[0] ); |
| 1051 | params[1] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[1] ); |
| 1052 | params[2] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[2] ); |
| 1053 | params[3] = FLOAT_TO_INT( ctx->Light.Material[f].Emission[3] ); |
| 1054 | break; |
| 1055 | case GL_SHININESS: |
| 1056 | *params = ROUNDF( ctx->Light.Material[f].Shininess ); |
| 1057 | break; |
| 1058 | case GL_COLOR_INDEXES: |
| 1059 | params[0] = ROUNDF( ctx->Light.Material[f].AmbientIndex ); |
| 1060 | params[1] = ROUNDF( ctx->Light.Material[f].DiffuseIndex ); |
| 1061 | params[2] = ROUNDF( ctx->Light.Material[f].SpecularIndex ); |
| 1062 | break; |
| 1063 | default: |
| 1064 | gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" ); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | /**********************************************************************/ |
| 1072 | /***** Lighting computation *****/ |
| 1073 | /**********************************************************************/ |
| 1074 | |
| 1075 | |
| 1076 | /* |
| 1077 | * Notes: |
| 1078 | * When two-sided lighting is enabled we compute the color (or index) |
| 1079 | * for both the front and back side of the primitive. Then, when the |
| 1080 | * orientation of the facet is later learned, we can determine which |
| 1081 | * color (or index) to use for rendering. |
| 1082 | * |
| 1083 | * KW: We now know orientation in advance and only shade for |
| 1084 | * the side or sides which are actually required. |
| 1085 | * |
| 1086 | * Variables: |
| 1087 | * n = normal vector |
| 1088 | * V = vertex position |
| 1089 | * P = light source position |
| 1090 | * Pe = (0,0,0,1) |
| 1091 | * |
| 1092 | * Precomputed: |
| 1093 | * IF P[3]==0 THEN |
| 1094 | * // light at infinity |
| 1095 | * IF local_viewer THEN |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1096 | * _VP_inf_norm = unit vector from V to P // Precompute |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1097 | * ELSE |
| 1098 | * // eye at infinity |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1099 | * _h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1100 | * ENDIF |
| 1101 | * ENDIF |
| 1102 | * |
| 1103 | * Functions: |
| 1104 | * Normalize( v ) = normalized vector v |
| 1105 | * Magnitude( v ) = length of vector v |
| 1106 | */ |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | /* |
| 1111 | * Whenever the spotlight exponent for a light changes we must call |
| 1112 | * this function to recompute the exponent lookup table. |
| 1113 | */ |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1114 | void |
| 1115 | gl_compute_spot_exp_table( struct gl_light *l ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1116 | { |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1117 | GLint i; |
| 1118 | GLdouble exponent = l->SpotExponent; |
| 1119 | GLdouble tmp = 0; |
| 1120 | GLint clamp = 0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1121 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1122 | l->_SpotExpTable[0][0] = 0.0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1123 | |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1124 | for (i = EXP_TABLE_SIZE - 1; i > 0 ;i--) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1125 | if (clamp == 0) { |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1126 | tmp = pow(i / (GLdouble) (EXP_TABLE_SIZE - 1), exponent); |
| 1127 | if (tmp < FLT_MIN * 100.0) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1128 | tmp = 0.0; |
| 1129 | clamp = 1; |
| 1130 | } |
| 1131 | } |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1132 | l->_SpotExpTable[i][0] = tmp; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1133 | } |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1134 | for (i = 0; i < EXP_TABLE_SIZE - 1; i++) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1135 | l->_SpotExpTable[i][1] = l->_SpotExpTable[i+1][0] - l->_SpotExpTable[i][0]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1136 | } |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1137 | l->_SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | /* Calculate a new shine table. Doing this here saves a branch in |
| 1144 | * lighting, and the cost of doing it early may be partially offset |
| 1145 | * by keeping a MRU cache of shine tables for various shine values. |
| 1146 | */ |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1147 | static void |
| 1148 | compute_shine_table( struct gl_shine_tab *tab, GLfloat shininess ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1149 | { |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1150 | GLint i; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1151 | GLfloat *m = tab->tab; |
| 1152 | |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1153 | m[0] = 0.0; |
| 1154 | if (shininess == 0.0) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1155 | for (i = 1 ; i <= SHINE_TABLE_SIZE ; i++) |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1156 | m[i] = 1.0; |
| 1157 | } |
| 1158 | else { |
| 1159 | for (i = 1 ; i < SHINE_TABLE_SIZE ; i++) { |
| 1160 | GLdouble t = pow(i / (GLfloat) (SHINE_TABLE_SIZE - 1), shininess); |
| 1161 | if (t > 1e-20) |
| 1162 | m[i] = t; |
| 1163 | else |
| 1164 | m[i] = 0.0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1165 | } |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1166 | m[SHINE_TABLE_SIZE] = 1.0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | tab->shininess = shininess; |
| 1170 | } |
| 1171 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1172 | |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1173 | void |
| 1174 | gl_compute_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1175 | { |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1176 | #define DISTSQR(a,b) ((a-b)*(a-b)) |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1177 | struct gl_shine_tab *list = ctx->_ShineTabList; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1178 | struct gl_shine_tab *s; |
| 1179 | |
| 1180 | foreach(s, list) |
| 1181 | if ( DISTSQR(s->shininess, shininess) < 1e-4 ) |
| 1182 | break; |
| 1183 | |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1184 | if (s == list) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1185 | foreach(s, list) |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1186 | if (s->refcount == 0) |
| 1187 | break; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1188 | |
| 1189 | compute_shine_table( s, shininess ); |
| 1190 | } |
| 1191 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1192 | ctx->_ShineTable[i]->refcount--; |
| 1193 | ctx->_ShineTable[i] = s; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1194 | move_to_tail( list, s ); |
| 1195 | s->refcount++; |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1196 | #undef DISTSQR |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1201 | |
| 1202 | /* |
| 1203 | * Examine current lighting parameters to determine if the optimized lighting |
| 1204 | * function can be used. |
| 1205 | * Also, precompute some lighting values such as the products of light |
| 1206 | * source and material ambient, diffuse and specular coefficients. |
| 1207 | */ |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1208 | void |
| 1209 | gl_update_lighting( GLcontext *ctx ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1210 | { |
| 1211 | struct gl_light *light; |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1212 | ctx->_TriangleCaps &= ~(DD_TRI_LIGHT_TWOSIDE|DD_LIGHTING_CULL); |
| 1213 | ctx->_NeedEyeCoords &= ~NEED_EYE_LIGHT; |
| 1214 | ctx->_NeedNormals &= ~NEED_NORMALS_LIGHT; |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1215 | ctx->Light._Flags = 0; |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1216 | |
| 1217 | if (!ctx->Light.Enabled) |
| 1218 | return; |
| 1219 | |
| 1220 | ctx->_NeedNormals |= NEED_NORMALS_LIGHT; |
| 1221 | |
| 1222 | if (ctx->Light.Model.TwoSide) |
| 1223 | ctx->_TriangleCaps |= (DD_TRI_LIGHT_TWOSIDE|DD_LIGHTING_CULL); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1224 | |
| 1225 | foreach(light, &ctx->Light.EnabledList) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1226 | ctx->Light._Flags |= light->_Flags; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1229 | ctx->Light._NeedVertices = |
| 1230 | ((ctx->Light._Flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) || |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1231 | ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR || |
| 1232 | ctx->Light.Model.LocalViewer); |
| 1233 | |
| 1234 | if ((ctx->Light._Flags & LIGHT_POSITIONAL) || |
| 1235 | ctx->Light.Model.LocalViewer) |
| 1236 | ctx->_NeedEyeCoords |= NEED_EYE_LIGHT; |
| 1237 | |
| 1238 | |
| 1239 | /* XXX: This test is overkill & needs to be fixed both for software and |
| 1240 | * hardware t&l drivers. The above should be sufficient & should |
| 1241 | * be tested to verify this. |
| 1242 | */ |
| 1243 | if (ctx->Light._NeedVertices) |
| 1244 | ctx->_NeedEyeCoords |= NEED_EYE_LIGHT; |
| 1245 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1246 | |
| 1247 | /* Precompute some shading values. |
| 1248 | */ |
| Brian Paul | b1394fa | 2000-09-26 20:53:53 +0000 | [diff] [blame] | 1249 | if (ctx->Visual.RGBAflag) { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1250 | GLuint sides = ctx->Light.Model.TwoSide ? 2 : 1; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1251 | GLuint side; |
| 1252 | for (side=0; side < sides; side++) { |
| 1253 | struct gl_material *mat = &ctx->Light.Material[side]; |
| Brian Paul | c535ba5 | 2000-06-29 04:56:30 +0000 | [diff] [blame] | 1254 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1255 | COPY_3V(ctx->Light._BaseColor[side], mat->Emission); |
| 1256 | ACC_SCALE_3V(ctx->Light._BaseColor[side], |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1257 | ctx->Light.Model.Ambient, |
| 1258 | mat->Ambient); |
| 1259 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1260 | FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[side], |
| Brian Paul | c893a01 | 2000-10-28 20:41:13 +0000 | [diff] [blame] | 1261 | ctx->Light.Material[side].Diffuse[3] ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | foreach (light, &ctx->Light.EnabledList) { |
| 1265 | for (side=0; side< sides; side++) { |
| Brian Paul | c535ba5 | 2000-06-29 04:56:30 +0000 | [diff] [blame] | 1266 | const struct gl_material *mat = &ctx->Light.Material[side]; |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1267 | SCALE_3V( light->_MatDiffuse[side], light->Diffuse, mat->Diffuse ); |
| 1268 | SCALE_3V( light->_MatAmbient[side], light->Ambient, mat->Ambient ); |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1269 | SCALE_3V( light->_MatSpecular[side], light->Specular, |
| 1270 | mat->Specular); |
| 1271 | light->_IsMatSpecular[side] = |
| 1272 | (LEN_SQUARED_3FV(light->_MatSpecular[side]) > 1e-16); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | } |
| Brian Paul | c535ba5 | 2000-06-29 04:56:30 +0000 | [diff] [blame] | 1276 | else { |
| 1277 | static const GLfloat ci[3] = { .30, .59, .11 }; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1278 | foreach(light, &ctx->Light.EnabledList) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1279 | light->_dli = DOT3(ci, light->Diffuse); |
| 1280 | light->_sli = DOT3(ci, light->Specular); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1283 | |
| 1284 | gl_update_lighting_function(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| Brian Paul | c535ba5 | 2000-06-29 04:56:30 +0000 | [diff] [blame] | 1287 | |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1288 | /* _NEW_MODELVIEW |
| 1289 | * _NEW_LIGHT |
| 1290 | * _TNL_NEW_NEED_EYE_COORDS |
| 1291 | * |
| 1292 | * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled. |
| 1293 | * Also update on lighting space changes. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1294 | */ |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1295 | void |
| 1296 | gl_compute_light_positions( GLcontext *ctx ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1297 | { |
| 1298 | struct gl_light *light; |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1299 | static const GLfloat eye_z[3] = { 0, 0, 1 }; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1300 | |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1301 | if (!ctx->Light.Enabled) |
| 1302 | return; |
| 1303 | |
| 1304 | if (ctx->_NeedEyeCoords) { |
| 1305 | COPY_3V( ctx->_EyeZDir, eye_z ); |
| 1306 | } |
| 1307 | else { |
| 1308 | TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelView.m ); |
| 1309 | } |
| 1310 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1311 | foreach (light, &ctx->Light.EnabledList) { |
| 1312 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1313 | if (ctx->_NeedEyeCoords) { |
| 1314 | COPY_4FV( light->_Position, light->EyePosition ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1315 | } |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 1316 | else { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1317 | TRANSFORM_POINT( light->_Position, ctx->ModelView.inv, |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 1318 | light->EyePosition ); |
| 1319 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1320 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1321 | if (!(light->_Flags & LIGHT_POSITIONAL)) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1322 | /* VP (VP) = Normalize( Position ) */ |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1323 | COPY_3V( light->_VP_inf_norm, light->_Position ); |
| 1324 | NORMALIZE_3FV( light->_VP_inf_norm ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1325 | |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 1326 | if (!ctx->Light.Model.LocalViewer) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1327 | /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */ |
| 1328 | ADD_3V( light->_h_inf_norm, light->_VP_inf_norm, ctx->_EyeZDir); |
| 1329 | NORMALIZE_3FV( light->_h_inf_norm ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1330 | } |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1331 | light->_VP_inf_spot_attenuation = 1.0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1334 | if (light->_Flags & LIGHT_SPOT) { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1335 | if (ctx->_NeedEyeCoords) { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1336 | COPY_3V( light->_NormDirection, light->EyeDirection ); |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 1337 | } |
| 1338 | else { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1339 | TRANSFORM_NORMAL( light->_NormDirection, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1340 | light->EyeDirection, |
| 1341 | ctx->ModelView.m); |
| 1342 | } |
| 1343 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1344 | NORMALIZE_3FV( light->_NormDirection ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1345 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1346 | if (!(light->_Flags & LIGHT_POSITIONAL)) { |
| 1347 | GLfloat PV_dot_dir = - DOT3(light->_VP_inf_norm, |
| 1348 | light->_NormDirection); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1349 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1350 | if (PV_dot_dir > light->_CosCutoff) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1351 | double x = PV_dot_dir * (EXP_TABLE_SIZE-1); |
| 1352 | int k = (int) x; |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1353 | light->_VP_inf_spot_attenuation = |
| 1354 | (light->_SpotExpTable[k][0] + |
| 1355 | (x-k)*light->_SpotExpTable[k][1]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1356 | } |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 1357 | else { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1358 | light->_VP_inf_spot_attenuation = 0; |
| Brian Paul | de82d06 | 2000-06-26 23:37:46 +0000 | [diff] [blame] | 1359 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1366 | /* _NEW_TRANSFORM |
| 1367 | * _NEW_MODELVIEW |
| 1368 | * _TNL_NEW_NEED_NORMALS |
| 1369 | * _TNL_NEW_NEED_EYE_COORDS |
| 1370 | * |
| 1371 | * Update on (_NEW_TRANSFORM|_NEW_MODELVIEW) |
| 1372 | * And also on NewLightingSpaces() callback. |
| 1373 | */ |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1374 | void |
| 1375 | gl_update_normal_transform( GLcontext *ctx ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1376 | { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1377 | |
| 1378 | if (!ctx->_NeedNormals) { |
| 1379 | ctx->_NormalTransform = 0; |
| 1380 | return; |
| 1381 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1382 | |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1383 | if (ctx->_NeedEyeCoords) { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1384 | GLuint transform = NORM_TRANSFORM_NO_ROT; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1385 | |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1386 | if (ctx->ModelView.flags & (MAT_FLAG_GENERAL | |
| 1387 | MAT_FLAG_ROTATION | |
| 1388 | MAT_FLAG_GENERAL_3D | |
| 1389 | MAT_FLAG_PERSPECTIVE)) |
| 1390 | transform = NORM_TRANSFORM; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1391 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1392 | |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1393 | if (ctx->Transform.Normalize) { |
| 1394 | ctx->_NormalTransform = gl_normal_tab[transform | NORM_NORMALIZE]; |
| 1395 | } |
| 1396 | else if (ctx->Transform.RescaleNormals && |
| 1397 | ctx->_ModelViewInvScale != 1.0) { |
| 1398 | ctx->_NormalTransform = gl_normal_tab[transform | NORM_RESCALE]; |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1399 | } |
| 1400 | else { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1401 | ctx->_NormalTransform = gl_normal_tab[transform]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | else { |
| Keith Whitwell | 1e1aac0 | 2000-11-13 20:02:56 +0000 | [diff] [blame^] | 1405 | if (ctx->Transform.Normalize) { |
| 1406 | ctx->_NormalTransform = gl_normal_tab[NORM_NORMALIZE]; |
| 1407 | } |
| 1408 | else if (!ctx->Transform.RescaleNormals && |
| 1409 | ctx->_ModelViewInvScale != 1.0) { |
| 1410 | ctx->_NormalTransform = gl_normal_tab[NORM_RESCALE]; |
| Brian Paul | a0faa7f | 2000-07-18 16:55:56 +0000 | [diff] [blame] | 1411 | } |
| 1412 | else { |
| Keith Whitwell | 14940c4 | 2000-11-05 18:40:57 +0000 | [diff] [blame] | 1413 | ctx->_NormalTransform = 0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1414 | } |
| 1415 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1416 | } |