Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygent_vb_texgen.c
Go to the documentation of this file.
00001 /* 00002 * Mesa 3-D graphics library 00003 * Version: 6.5 00004 * 00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. 00006 * 00007 * Permission is hereby granted, free of charge, to any person obtaining a 00008 * copy of this software and associated documentation files (the "Software"), 00009 * to deal in the Software without restriction, including without limitation 00010 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 * and/or sell copies of the Software, and to permit persons to whom the 00012 * Software is furnished to do so, subject to the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included 00015 * in all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00018 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 00021 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00022 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00023 * 00024 * Authors: 00025 * Brian Paul 00026 * Keith Whitwell <keith@tungstengraphics.com> 00027 */ 00028 00029 /* 00030 * Regarding GL_NV_texgen_reflection: 00031 * 00032 * Portions of this software may use or implement intellectual 00033 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims 00034 * any and all warranties with respect to such intellectual property, 00035 * including any use thereof or modifications thereto. 00036 */ 00037 00038 #include "main/glheader.h" 00039 #include "main/colormac.h" 00040 #include "main/context.h" 00041 #include "main/macros.h" 00042 #include "main/imports.h" 00043 #include "main/mtypes.h" 00044 00045 #include "math/m_xform.h" 00046 00047 #include "t_context.h" 00048 #include "t_pipeline.h" 00049 00050 00051 /*********************************************************************** 00052 * Automatic texture coordinate generation (texgen) code. 00053 */ 00054 00055 00056 struct texgen_stage_data; 00057 00058 typedef void (*texgen_func)( GLcontext *ctx, 00059 struct texgen_stage_data *store, 00060 GLuint unit); 00061 00062 00063 struct texgen_stage_data { 00064 00065 /* Per-texunit derived state. 00066 */ 00067 GLuint TexgenSize[MAX_TEXTURE_COORD_UNITS]; 00068 texgen_func TexgenFunc[MAX_TEXTURE_COORD_UNITS]; 00069 00070 /* Temporary values used in texgen. 00071 */ 00072 GLfloat (*tmp_f)[3]; 00073 GLfloat *tmp_m; 00074 00075 /* Buffered outputs of the stage. 00076 */ 00077 GLvector4f texcoord[MAX_TEXTURE_COORD_UNITS]; 00078 }; 00079 00080 00081 #define TEXGEN_STAGE_DATA(stage) ((struct texgen_stage_data *)stage->privatePtr) 00082 00083 00084 00085 static GLuint all_bits[5] = { 00086 0, 00087 VEC_SIZE_1, 00088 VEC_SIZE_2, 00089 VEC_SIZE_3, 00090 VEC_SIZE_4, 00091 }; 00092 00093 #define VEC_SIZE_FLAGS (VEC_SIZE_1|VEC_SIZE_2|VEC_SIZE_3|VEC_SIZE_4) 00094 00095 #define TEXGEN_NEED_M (TEXGEN_SPHERE_MAP) 00096 #define TEXGEN_NEED_F (TEXGEN_SPHERE_MAP | \ 00097 TEXGEN_REFLECTION_MAP_NV) 00098 00099 00100 00101 static void build_m3( GLfloat f[][3], GLfloat m[], 00102 const GLvector4f *normal, 00103 const GLvector4f *eye ) 00104 { 00105 GLuint stride = eye->stride; 00106 GLfloat *coord = (GLfloat *)eye->start; 00107 GLuint count = eye->count; 00108 const GLfloat *norm = normal->start; 00109 GLuint i; 00110 00111 for (i=0;i<count;i++,STRIDE_F(coord,stride),STRIDE_F(norm,normal->stride)) { 00112 GLfloat u[3], two_nu, fx, fy, fz; 00113 COPY_3V( u, coord ); 00114 NORMALIZE_3FV( u ); 00115 two_nu = 2.0F * DOT3(norm,u); 00116 fx = f[i][0] = u[0] - norm[0] * two_nu; 00117 fy = f[i][1] = u[1] - norm[1] * two_nu; 00118 fz = f[i][2] = u[2] - norm[2] * two_nu; 00119 m[i] = fx * fx + fy * fy + (fz + 1.0F) * (fz + 1.0F); 00120 if (m[i] != 0.0F) { 00121 m[i] = 0.5F * _mesa_inv_sqrtf(m[i]); 00122 } 00123 } 00124 } 00125 00126 00127 00128 static void build_m2( GLfloat f[][3], GLfloat m[], 00129 const GLvector4f *normal, 00130 const GLvector4f *eye ) 00131 { 00132 GLuint stride = eye->stride; 00133 GLfloat *coord = eye->start; 00134 GLuint count = eye->count; 00135 00136 GLfloat *norm = normal->start; 00137 GLuint i; 00138 00139 for (i=0;i<count;i++,STRIDE_F(coord,stride),STRIDE_F(norm,normal->stride)) { 00140 GLfloat u[3], two_nu, fx, fy, fz; 00141 COPY_2V( u, coord ); 00142 u[2] = 0; 00143 NORMALIZE_3FV( u ); 00144 two_nu = 2.0F * DOT3(norm,u); 00145 fx = f[i][0] = u[0] - norm[0] * two_nu; 00146 fy = f[i][1] = u[1] - norm[1] * two_nu; 00147 fz = f[i][2] = u[2] - norm[2] * two_nu; 00148 m[i] = fx * fx + fy * fy + (fz + 1.0F) * (fz + 1.0F); 00149 if (m[i] != 0.0F) { 00150 m[i] = 0.5F * _mesa_inv_sqrtf(m[i]); 00151 } 00152 } 00153 } 00154 00155 00156 00157 typedef void (*build_m_func)( GLfloat f[][3], 00158 GLfloat m[], 00159 const GLvector4f *normal, 00160 const GLvector4f *eye ); 00161 00162 00163 static build_m_func build_m_tab[5] = { 00164 NULL, 00165 NULL, 00166 build_m2, 00167 build_m3, 00168 build_m3 00169 }; 00170 00171 00172 /* This is unusual in that we respect the stride of the output vector 00173 * (f). This allows us to pass in either a texcoord vector4f, or a 00174 * temporary vector3f. 00175 */ 00176 static void build_f3( GLfloat *f, 00177 GLuint fstride, 00178 const GLvector4f *normal, 00179 const GLvector4f *eye ) 00180 { 00181 GLuint stride = eye->stride; 00182 GLfloat *coord = eye->start; 00183 GLuint count = eye->count; 00184 00185 GLfloat *norm = normal->start; 00186 GLuint i; 00187 00188 for (i=0;i<count;i++) { 00189 GLfloat u[3], two_nu; 00190 COPY_3V( u, coord ); 00191 NORMALIZE_3FV( u ); 00192 two_nu = 2.0F * DOT3(norm,u); 00193 f[0] = u[0] - norm[0] * two_nu; 00194 f[1] = u[1] - norm[1] * two_nu; 00195 f[2] = u[2] - norm[2] * two_nu; 00196 STRIDE_F(coord,stride); 00197 STRIDE_F(f,fstride); 00198 STRIDE_F(norm, normal->stride); 00199 } 00200 } 00201 00202 00203 static void build_f2( GLfloat *f, 00204 GLuint fstride, 00205 const GLvector4f *normal, 00206 const GLvector4f *eye ) 00207 { 00208 GLuint stride = eye->stride; 00209 GLfloat *coord = eye->start; 00210 GLuint count = eye->count; 00211 GLfloat *norm = normal->start; 00212 GLuint i; 00213 00214 for (i=0;i<count;i++) { 00215 00216 GLfloat u[3], two_nu; 00217 COPY_2V( u, coord ); 00218 u[2] = 0; 00219 NORMALIZE_3FV( u ); 00220 two_nu = 2.0F * DOT3(norm,u); 00221 f[0] = u[0] - norm[0] * two_nu; 00222 f[1] = u[1] - norm[1] * two_nu; 00223 f[2] = u[2] - norm[2] * two_nu; 00224 00225 STRIDE_F(coord,stride); 00226 STRIDE_F(f,fstride); 00227 STRIDE_F(norm, normal->stride); 00228 } 00229 } 00230 00231 typedef void (*build_f_func)( GLfloat *f, 00232 GLuint fstride, 00233 const GLvector4f *normal_vec, 00234 const GLvector4f *eye ); 00235 00236 00237 00238 /* Just treat 4-vectors as 3-vectors. 00239 */ 00240 static build_f_func build_f_tab[5] = { 00241 NULL, 00242 NULL, 00243 build_f2, 00244 build_f3, 00245 build_f3 00246 }; 00247 00248 00249 00250 /* Special case texgen functions. 00251 */ 00252 static void texgen_reflection_map_nv( GLcontext *ctx, 00253 struct texgen_stage_data *store, 00254 GLuint unit ) 00255 { 00256 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 00257 GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit]; 00258 GLvector4f *out = &store->texcoord[unit]; 00259 00260 build_f_tab[VB->EyePtr->size]( out->start, 00261 out->stride, 00262 VB->AttribPtr[_TNL_ATTRIB_NORMAL], 00263 VB->EyePtr ); 00264 00265 out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_3; 00266 out->count = VB->Count; 00267 out->size = MAX2(in->size, 3); 00268 if (in->size == 4) 00269 _mesa_copy_tab[0x8]( out, in ); 00270 } 00271 00272 00273 00274 static void texgen_normal_map_nv( GLcontext *ctx, 00275 struct texgen_stage_data *store, 00276 GLuint unit ) 00277 { 00278 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 00279 GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit]; 00280 GLvector4f *out = &store->texcoord[unit]; 00281 GLvector4f *normal = VB->AttribPtr[_TNL_ATTRIB_NORMAL]; 00282 GLfloat (*texcoord)[4] = (GLfloat (*)[4])out->start; 00283 GLuint count = VB->Count; 00284 GLuint i; 00285 const GLfloat *norm = normal->start; 00286 00287 for (i=0;i<count;i++, STRIDE_F(norm, normal->stride)) { 00288 texcoord[i][0] = norm[0]; 00289 texcoord[i][1] = norm[1]; 00290 texcoord[i][2] = norm[2]; 00291 } 00292 00293 00294 out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_3; 00295 out->count = count; 00296 out->size = MAX2(in->size, 3); 00297 if (in->size == 4) 00298 _mesa_copy_tab[0x8]( out, in ); 00299 } 00300 00301 00302 static void texgen_sphere_map( GLcontext *ctx, 00303 struct texgen_stage_data *store, 00304 GLuint unit ) 00305 { 00306 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 00307 GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit]; 00308 GLvector4f *out = &store->texcoord[unit]; 00309 GLfloat (*texcoord)[4] = (GLfloat (*)[4]) out->start; 00310 GLuint count = VB->Count; 00311 GLuint i; 00312 GLfloat (*f)[3] = store->tmp_f; 00313 GLfloat *m = store->tmp_m; 00314 00315 (build_m_tab[VB->EyePtr->size])( store->tmp_f, 00316 store->tmp_m, 00317 VB->AttribPtr[_TNL_ATTRIB_NORMAL], 00318 VB->EyePtr ); 00319 00320 out->size = MAX2(in->size,2); 00321 00322 for (i=0;i<count;i++) { 00323 texcoord[i][0] = f[i][0] * m[i] + 0.5F; 00324 texcoord[i][1] = f[i][1] * m[i] + 0.5F; 00325 } 00326 00327 out->count = count; 00328 out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_2; 00329 if (in->size > 2) 00330 _mesa_copy_tab[all_bits[in->size] & ~0x3]( out, in ); 00331 } 00332 00333 00334 00335 static void texgen( GLcontext *ctx, 00336 struct texgen_stage_data *store, 00337 GLuint unit ) 00338 { 00339 TNLcontext *tnl = TNL_CONTEXT(ctx); 00340 struct vertex_buffer *VB = &tnl->vb; 00341 GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit]; 00342 GLvector4f *out = &store->texcoord[unit]; 00343 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; 00344 const GLvector4f *obj = VB->ObjPtr; 00345 const GLvector4f *eye = VB->EyePtr; 00346 const GLvector4f *normal = VB->AttribPtr[_TNL_ATTRIB_NORMAL]; 00347 const GLfloat *m = store->tmp_m; 00348 const GLuint count = VB->Count; 00349 GLfloat (*texcoord)[4] = (GLfloat (*)[4])out->data; 00350 GLfloat (*f)[3] = store->tmp_f; 00351 GLuint copy; 00352 00353 if (texUnit->_GenFlags & TEXGEN_NEED_M) { 00354 build_m_tab[eye->size]( store->tmp_f, store->tmp_m, normal, eye ); 00355 } else if (texUnit->_GenFlags & TEXGEN_NEED_F) { 00356 build_f_tab[eye->size]( (GLfloat *)store->tmp_f, 3, normal, eye ); 00357 } 00358 00359 00360 out->size = MAX2(in->size, store->TexgenSize[unit]); 00361 out->flags |= (in->flags & VEC_SIZE_FLAGS) | texUnit->TexGenEnabled; 00362 out->count = count; 00363 00364 copy = (all_bits[in->size] & ~texUnit->TexGenEnabled); 00365 if (copy) 00366 _mesa_copy_tab[copy]( out, in ); 00367 00368 if (texUnit->TexGenEnabled & S_BIT) { 00369 GLuint i; 00370 switch (texUnit->GenModeS) { 00371 case GL_OBJECT_LINEAR: 00372 _mesa_dotprod_tab[obj->size]( (GLfloat *)out->data, 00373 sizeof(out->data[0]), obj, 00374 texUnit->ObjectPlaneS ); 00375 break; 00376 case GL_EYE_LINEAR: 00377 _mesa_dotprod_tab[eye->size]( (GLfloat *)out->data, 00378 sizeof(out->data[0]), eye, 00379 texUnit->EyePlaneS ); 00380 break; 00381 case GL_SPHERE_MAP: 00382 for (i = 0; i < count; i++) 00383 texcoord[i][0] = f[i][0] * m[i] + 0.5F; 00384 break; 00385 case GL_REFLECTION_MAP_NV: 00386 for (i=0;i<count;i++) 00387 texcoord[i][0] = f[i][0]; 00388 break; 00389 case GL_NORMAL_MAP_NV: { 00390 const GLfloat *norm = normal->start; 00391 for (i=0;i<count;i++, STRIDE_F(norm, normal->stride)) { 00392 texcoord[i][0] = norm[0]; 00393 } 00394 break; 00395 } 00396 default: 00397 _mesa_problem(ctx, "Bad S texgen"); 00398 } 00399 } 00400 00401 if (texUnit->TexGenEnabled & T_BIT) { 00402 GLuint i; 00403 switch (texUnit->GenModeT) { 00404 case GL_OBJECT_LINEAR: 00405 _mesa_dotprod_tab[obj->size]( &(out->data[0][1]), 00406 sizeof(out->data[0]), obj, 00407 texUnit->ObjectPlaneT ); 00408 break; 00409 case GL_EYE_LINEAR: 00410 _mesa_dotprod_tab[eye->size]( &(out->data[0][1]), 00411 sizeof(out->data[0]), eye, 00412 texUnit->EyePlaneT ); 00413 break; 00414 case GL_SPHERE_MAP: 00415 for (i = 0; i < count; i++) 00416 texcoord[i][1] = f[i][1] * m[i] + 0.5F; 00417 break; 00418 case GL_REFLECTION_MAP_NV: 00419 for (i=0;i<count;i++) 00420 texcoord[i][1] = f[i][1]; 00421 break; 00422 case GL_NORMAL_MAP_NV: { 00423 const GLfloat *norm = normal->start; 00424 for (i=0;i<count;i++, STRIDE_F(norm, normal->stride)) { 00425 texcoord[i][1] = norm[1]; 00426 } 00427 break; 00428 } 00429 default: 00430 _mesa_problem(ctx, "Bad T texgen"); 00431 } 00432 } 00433 00434 if (texUnit->TexGenEnabled & R_BIT) { 00435 GLuint i; 00436 switch (texUnit->GenModeR) { 00437 case GL_OBJECT_LINEAR: 00438 _mesa_dotprod_tab[obj->size]( &(out->data[0][2]), 00439 sizeof(out->data[0]), obj, 00440 texUnit->ObjectPlaneR ); 00441 break; 00442 case GL_EYE_LINEAR: 00443 _mesa_dotprod_tab[eye->size]( &(out->data[0][2]), 00444 sizeof(out->data[0]), eye, 00445 texUnit->EyePlaneR ); 00446 break; 00447 case GL_REFLECTION_MAP_NV: 00448 for (i=0;i<count;i++) 00449 texcoord[i][2] = f[i][2]; 00450 break; 00451 case GL_NORMAL_MAP_NV: { 00452 const GLfloat *norm = normal->start; 00453 for (i=0;i<count;i++,STRIDE_F(norm, normal->stride)) { 00454 texcoord[i][2] = norm[2]; 00455 } 00456 break; 00457 } 00458 default: 00459 _mesa_problem(ctx, "Bad R texgen"); 00460 } 00461 } 00462 00463 if (texUnit->TexGenEnabled & Q_BIT) { 00464 switch (texUnit->GenModeQ) { 00465 case GL_OBJECT_LINEAR: 00466 _mesa_dotprod_tab[obj->size]( &(out->data[0][3]), 00467 sizeof(out->data[0]), obj, 00468 texUnit->ObjectPlaneQ ); 00469 break; 00470 case GL_EYE_LINEAR: 00471 _mesa_dotprod_tab[eye->size]( &(out->data[0][3]), 00472 sizeof(out->data[0]), eye, 00473 texUnit->EyePlaneQ ); 00474 break; 00475 default: 00476 _mesa_problem(ctx, "Bad Q texgen"); 00477 } 00478 } 00479 } 00480 00481 00482 00483 00484 static GLboolean run_texgen_stage( GLcontext *ctx, 00485 struct tnl_pipeline_stage *stage ) 00486 { 00487 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 00488 struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage); 00489 GLuint i; 00490 00491 if (!ctx->Texture._TexGenEnabled || ctx->VertexProgram._Current) 00492 return GL_TRUE; 00493 00494 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) { 00495 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i]; 00496 00497 if (texUnit->TexGenEnabled) { 00498 00499 store->TexgenFunc[i]( ctx, store, i ); 00500 00501 VB->TexCoordPtr[i] = 00502 VB->AttribPtr[VERT_ATTRIB_TEX0 + i] = &store->texcoord[i]; 00503 } 00504 } 00505 00506 return GL_TRUE; 00507 } 00508 00509 00510 static void validate_texgen_stage( GLcontext *ctx, 00511 struct tnl_pipeline_stage *stage ) 00512 { 00513 struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage); 00514 GLuint i; 00515 00516 if (!ctx->Texture._TexGenEnabled || ctx->VertexProgram._Current) 00517 return; 00518 00519 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) { 00520 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i]; 00521 00522 if (texUnit->TexGenEnabled) { 00523 GLuint sz; 00524 00525 if (texUnit->TexGenEnabled & Q_BIT) 00526 sz = 4; 00527 else if (texUnit->TexGenEnabled & R_BIT) 00528 sz = 3; 00529 else if (texUnit->TexGenEnabled & T_BIT) 00530 sz = 2; 00531 else 00532 sz = 1; 00533 00534 store->TexgenSize[i] = sz; 00535 store->TexgenFunc[i] = texgen; /* general solution */ 00536 00537 /* look for special texgen cases */ 00538 if (texUnit->TexGenEnabled == (S_BIT|T_BIT|R_BIT)) { 00539 if (texUnit->_GenFlags == TEXGEN_REFLECTION_MAP_NV) { 00540 store->TexgenFunc[i] = texgen_reflection_map_nv; 00541 } 00542 else if (texUnit->_GenFlags == TEXGEN_NORMAL_MAP_NV) { 00543 store->TexgenFunc[i] = texgen_normal_map_nv; 00544 } 00545 } 00546 else if (texUnit->TexGenEnabled == (S_BIT|T_BIT) && 00547 texUnit->_GenFlags == TEXGEN_SPHERE_MAP) { 00548 store->TexgenFunc[i] = texgen_sphere_map; 00549 } 00550 } 00551 } 00552 } 00553 00554 00555 00556 00557 00558 /* Called the first time stage->run() is invoked. 00559 */ 00560 static GLboolean alloc_texgen_data( GLcontext *ctx, 00561 struct tnl_pipeline_stage *stage ) 00562 { 00563 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 00564 struct texgen_stage_data *store; 00565 GLuint i; 00566 00567 stage->privatePtr = CALLOC(sizeof(*store)); 00568 store = TEXGEN_STAGE_DATA(stage); 00569 if (!store) 00570 return GL_FALSE; 00571 00572 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) 00573 _mesa_vector4f_alloc( &store->texcoord[i], 0, VB->Size, 32 ); 00574 00575 store->tmp_f = (GLfloat (*)[3]) MALLOC(VB->Size * sizeof(GLfloat) * 3); 00576 store->tmp_m = (GLfloat *) MALLOC(VB->Size * sizeof(GLfloat)); 00577 00578 return GL_TRUE; 00579 } 00580 00581 00582 static void free_texgen_data( struct tnl_pipeline_stage *stage ) 00583 00584 { 00585 struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage); 00586 GLuint i; 00587 00588 if (store) { 00589 for (i = 0 ; i < MAX_TEXTURE_COORD_UNITS ; i++) 00590 if (store->texcoord[i].data) 00591 _mesa_vector4f_free( &store->texcoord[i] ); 00592 00593 00594 if (store->tmp_f) FREE( store->tmp_f ); 00595 if (store->tmp_m) FREE( store->tmp_m ); 00596 FREE( store ); 00597 stage->privatePtr = NULL; 00598 } 00599 } 00600 00601 00602 00603 const struct tnl_pipeline_stage _tnl_texgen_stage = 00604 { 00605 "texgen", /* name */ 00606 NULL, /* private data */ 00607 alloc_texgen_data, /* destructor */ 00608 free_texgen_data, /* destructor */ 00609 validate_texgen_stage, /* check */ 00610 run_texgen_stage /* run -- initially set to alloc data */ 00611 }; Generated on Thu May 24 2012 04:20:59 for ReactOS by
1.7.6.1
|