1 /* $Id: t_vb_texgen.c,v 1.10 2001/12/14 02:51:45 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
27 * Brian Paul <brian@valinux.com>
28 * Keith Whitwell <keithw@valinux.com>
40 #include "math/m_xform.h"
42 #include "t_context.h"
43 #include "t_pipeline.h"
46 /***********************************************************************
47 * Automatic texture coordinate generation (texgen) code.
51 struct texgen_stage_data
;
53 typedef void (*texgen_func
)( GLcontext
*ctx
,
54 struct texgen_stage_data
*store
,
58 struct texgen_stage_data
{
60 /* Per-texunit derived state.
62 GLuint TexgenSize
[MAX_TEXTURE_UNITS
];
63 GLuint TexgenHoles
[MAX_TEXTURE_UNITS
];
64 texgen_func TexgenFunc
[MAX_TEXTURE_UNITS
];
66 /* Temporary values used in texgen.
71 /* Buffered outputs of the stage.
73 GLvector4f texcoord
[MAX_TEXTURE_UNITS
];
77 #define TEXGEN_STAGE_DATA(stage) ((struct texgen_stage_data *)stage->privatePtr)
81 static GLuint all_bits
[5] = {
89 #define VEC_SIZE_FLAGS (VEC_SIZE_1|VEC_SIZE_2|VEC_SIZE_3|VEC_SIZE_4)
91 #define TEXGEN_NEED_M (TEXGEN_SPHERE_MAP)
92 #define TEXGEN_NEED_F (TEXGEN_SPHERE_MAP | \
93 TEXGEN_REFLECTION_MAP_NV)
97 static void build_m3( GLfloat f
[][3], GLfloat m
[],
98 const GLvector3f
*normal
,
99 const GLvector4f
*eye
)
101 GLuint stride
= eye
->stride
;
102 GLfloat
*coord
= (GLfloat
*)eye
->start
;
103 GLuint count
= eye
->count
;
104 const GLfloat
*norm
= normal
->start
;
107 for (i
=0;i
<count
;i
++,STRIDE_F(coord
,stride
),STRIDE_F(norm
,normal
->stride
)) {
108 GLfloat u
[3], two_nu
, fx
, fy
, fz
;
111 two_nu
= 2.0F
* DOT3(norm
,u
);
112 fx
= f
[i
][0] = u
[0] - norm
[0] * two_nu
;
113 fy
= f
[i
][1] = u
[1] - norm
[1] * two_nu
;
114 fz
= f
[i
][2] = u
[2] - norm
[2] * two_nu
;
115 m
[i
] = fx
* fx
+ fy
* fy
+ (fz
+ 1.0F
) * (fz
+ 1.0F
);
117 m
[i
] = 0.5F
/ (GLfloat
) GL_SQRT(m
[i
]);
124 static void build_m2( GLfloat f
[][3], GLfloat m
[],
125 const GLvector3f
*normal
,
126 const GLvector4f
*eye
)
128 GLuint stride
= eye
->stride
;
129 GLfloat
*coord
= eye
->start
;
130 GLuint count
= eye
->count
;
132 GLfloat
*norm
= normal
->start
;
135 for (i
=0;i
<count
;i
++,STRIDE_F(coord
,stride
),STRIDE_F(norm
,normal
->stride
)) {
136 GLfloat u
[3], two_nu
, fx
, fy
, fz
;
140 two_nu
= 2.0F
* DOT3(norm
,u
);
141 fx
= f
[i
][0] = u
[0] - norm
[0] * two_nu
;
142 fy
= f
[i
][1] = u
[1] - norm
[1] * two_nu
;
143 fz
= f
[i
][2] = u
[2] - norm
[2] * two_nu
;
144 m
[i
] = fx
* fx
+ fy
* fy
+ (fz
+ 1.0F
) * (fz
+ 1.0F
);
146 m
[i
] = 0.5F
/ (GLfloat
) GL_SQRT(m
[i
]);
153 typedef void (*build_m_func
)( GLfloat f
[][3],
155 const GLvector3f
*normal
,
156 const GLvector4f
*eye
);
159 static build_m_func build_m_tab
[5] = {
168 /* This is unusual in that we respect the stride of the output vector
169 * (f). This allows us to pass in either a texcoord vector4f, or a
170 * temporary vector3f.
172 static void build_f3( GLfloat
*f
,
174 const GLvector3f
*normal
,
175 const GLvector4f
*eye
)
177 GLuint stride
= eye
->stride
;
178 GLfloat
*coord
= eye
->start
;
179 GLuint count
= eye
->count
;
181 GLfloat
*norm
= normal
->start
;
184 for (i
=0;i
<count
;i
++) {
185 GLfloat u
[3], two_nu
;
188 two_nu
= 2.0F
* DOT3(norm
,u
);
189 f
[0] = u
[0] - norm
[0] * two_nu
;
190 f
[1] = u
[1] - norm
[1] * two_nu
;
191 f
[2] = u
[2] - norm
[2] * two_nu
;
192 STRIDE_F(coord
,stride
);
194 STRIDE_F(norm
, normal
->stride
);
199 static void build_f2( GLfloat
*f
,
201 const GLvector3f
*normal
,
202 const GLvector4f
*eye
)
204 GLuint stride
= eye
->stride
;
205 GLfloat
*coord
= eye
->start
;
206 GLuint count
= eye
->count
;
207 GLfloat
*norm
= normal
->start
;
210 for (i
=0;i
<count
;i
++) {
212 GLfloat u
[3], two_nu
;
216 two_nu
= 2.0F
* DOT3(norm
,u
);
217 f
[0] = u
[0] - norm
[0] * two_nu
;
218 f
[1] = u
[1] - norm
[1] * two_nu
;
219 f
[2] = u
[2] - norm
[2] * two_nu
;
221 STRIDE_F(coord
,stride
);
223 STRIDE_F(norm
, normal
->stride
);
227 typedef void (*build_f_func
)( GLfloat
*f
,
229 const GLvector3f
*normal_vec
,
230 const GLvector4f
*eye
);
234 /* Just treat 4-vectors as 3-vectors.
236 static build_f_func build_f_tab
[5] = {
245 /* Special case texgen functions.
247 static void texgen_reflection_map_nv( GLcontext
*ctx
,
248 struct texgen_stage_data
*store
,
251 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
252 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
253 GLvector4f
*out
= &store
->texcoord
[unit
];
255 build_f_tab
[VB
->EyePtr
->size
]( out
->start
,
261 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_3
;
262 out
->count
= in
->count
;
263 out
->size
= MAX2(in
->size
, 3);
265 _mesa_copy_tab
[0x8]( out
, in
);
268 out
->flags
|= VEC_SIZE_3
;
270 out
->count
= in
->count
;
277 static void texgen_normal_map_nv( GLcontext
*ctx
,
278 struct texgen_stage_data
*store
,
281 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
282 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
283 GLvector4f
*out
= &store
->texcoord
[unit
];
284 GLvector3f
*normal
= VB
->NormalPtr
;
285 GLfloat (*texcoord
)[4] = (GLfloat (*)[4])out
->start
;
286 GLuint count
= VB
->Count
;
288 const GLfloat
*norm
= normal
->start
;
290 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
291 texcoord
[i
][0] = norm
[0];
292 texcoord
[i
][1] = norm
[1];
293 texcoord
[i
][2] = norm
[2];
298 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_3
;
299 out
->count
= in
->count
;
300 out
->size
= MAX2(in
->size
, 3);
302 _mesa_copy_tab
[0x8]( out
, in
);
305 out
->flags
|= VEC_SIZE_3
;
307 out
->count
= in
->count
;
312 static void texgen_sphere_map( GLcontext
*ctx
,
313 struct texgen_stage_data
*store
,
316 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
317 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
318 GLvector4f
*out
= &store
->texcoord
[unit
];
319 GLfloat (*texcoord
)[4] = (GLfloat (*)[4]) out
->start
;
320 GLuint count
= VB
->Count
;
322 GLfloat (*f
)[3] = store
->tmp_f
;
323 GLfloat
*m
= store
->tmp_m
;
325 /* fprintf(stderr, "%s normstride %d eyestride %d\n", */
326 /* __FUNCTION__, VB->NormalPtr->stride, */
327 /* VB->EyePtr->stride); */
329 (build_m_tab
[VB
->EyePtr
->size
])( store
->tmp_f
,
334 for (i
=0;i
<count
;i
++) {
335 texcoord
[i
][0] = f
[i
][0] * m
[i
] + 0.5F
;
336 texcoord
[i
][1] = f
[i
][1] * m
[i
] + 0.5F
;
340 out
->size
= MAX2(in
->size
,2);
341 out
->count
= in
->count
;
342 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_2
;
344 _mesa_copy_tab
[all_bits
[in
->size
] & ~0x3]( out
, in
);
347 out
->flags
|= VEC_SIZE_2
;
348 out
->count
= in
->count
;
354 static void texgen( GLcontext
*ctx
,
355 struct texgen_stage_data
*store
,
358 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
359 struct vertex_buffer
*VB
= &tnl
->vb
;
360 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
361 GLvector4f
*out
= &store
->texcoord
[unit
];
362 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
363 const GLvector4f
*obj
= VB
->ObjPtr
;
364 const GLvector4f
*eye
= VB
->EyePtr
;
365 const GLvector3f
*normal
= VB
->NormalPtr
;
366 GLfloat (*texcoord
)[4] = (GLfloat (*)[4])out
->data
;
368 GLuint count
= VB
->Count
;
369 GLfloat (*f
)[3] = store
->tmp_f
;
370 GLfloat
*m
= store
->tmp_m
;
374 if (texUnit
->_GenFlags
& TEXGEN_NEED_M
) {
375 build_m_tab
[in
->size
]( store
->tmp_f
, store
->tmp_m
, normal
, eye
);
376 } else if (texUnit
->_GenFlags
& TEXGEN_NEED_F
) {
377 build_f_tab
[in
->size
]( (GLfloat
*)store
->tmp_f
, 3, normal
, eye
);
383 in
->count
= VB
->Count
;
385 out
->size
= store
->TexgenSize
[unit
];
386 out
->flags
|= texUnit
->TexGenEnabled
;
387 out
->count
= VB
->Count
;
388 holes
= store
->TexgenHoles
[unit
];
391 GLuint copy
= (all_bits
[in
->size
] & ~texUnit
->TexGenEnabled
);
393 _mesa_copy_tab
[copy
]( out
, in
);
395 out
->size
= MAX2(in
->size
, store
->TexgenSize
[unit
]);
396 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | texUnit
->TexGenEnabled
;
397 out
->count
= in
->count
;
399 holes
= ~all_bits
[in
->size
] & store
->TexgenHoles
[unit
];
403 if (holes
& VEC_DIRTY_2
) _mesa_vector4f_clean_elem(out
, count
, 2);
404 if (holes
& VEC_DIRTY_1
) _mesa_vector4f_clean_elem(out
, count
, 1);
405 if (holes
& VEC_DIRTY_0
) _mesa_vector4f_clean_elem(out
, count
, 0);
408 if (texUnit
->TexGenEnabled
& S_BIT
) {
410 switch (texUnit
->GenModeS
) {
411 case GL_OBJECT_LINEAR
:
412 _mesa_dotprod_tab
[obj
->size
]( (GLfloat
*)out
->data
,
413 sizeof(out
->data
[0]), obj
,
414 texUnit
->ObjectPlaneS
);
417 _mesa_dotprod_tab
[eye
->size
]( (GLfloat
*)out
->data
,
418 sizeof(out
->data
[0]), eye
,
419 texUnit
->EyePlaneS
);
422 for (indata
=in
->start
,i
=0 ; i
<count
;i
++, STRIDE_F(indata
,in
->stride
))
423 texcoord
[i
][0] = indata
[0] * m
[i
] + 0.5F
;
425 case GL_REFLECTION_MAP_NV
:
426 for (i
=0;i
<count
;i
++)
427 texcoord
[i
][0] = f
[i
][0];
429 case GL_NORMAL_MAP_NV
: {
430 const GLfloat
*norm
= normal
->start
;
431 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
432 texcoord
[i
][0] = norm
[0];
437 _mesa_problem(ctx
, "Bad S texgen");
441 if (texUnit
->TexGenEnabled
& T_BIT
) {
443 switch (texUnit
->GenModeT
) {
444 case GL_OBJECT_LINEAR
:
445 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][1]),
446 sizeof(out
->data
[0]), obj
,
447 texUnit
->ObjectPlaneT
);
450 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][1]),
451 sizeof(out
->data
[0]), eye
,
452 texUnit
->EyePlaneT
);
455 for (indata
=in
->start
,i
=0; i
<count
;i
++,STRIDE_F(indata
,in
->stride
))
456 texcoord
[i
][1] = indata
[1] * m
[i
] + 0.5F
;
458 case GL_REFLECTION_MAP_NV
:
459 for (i
=0;i
<count
;i
++)
460 texcoord
[i
][0] = f
[i
][0];
462 case GL_NORMAL_MAP_NV
: {
463 const GLfloat
*norm
= normal
->start
;
464 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
465 texcoord
[i
][1] = norm
[1];
470 _mesa_problem(ctx
, "Bad T texgen");
474 if (texUnit
->TexGenEnabled
& R_BIT
) {
476 switch (texUnit
->GenModeR
) {
477 case GL_OBJECT_LINEAR
:
478 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][2]),
479 sizeof(out
->data
[0]), obj
,
480 texUnit
->ObjectPlaneR
);
483 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][2]),
484 sizeof(out
->data
[0]), eye
,
485 texUnit
->EyePlaneR
);
487 case GL_REFLECTION_MAP_NV
:
488 for (i
=0;i
<count
;i
++)
489 texcoord
[i
][2] = f
[i
][2];
491 case GL_NORMAL_MAP_NV
: {
492 const GLfloat
*norm
= normal
->start
;
493 for (i
=0;i
<count
;i
++,STRIDE_F(norm
, normal
->stride
)) {
494 texcoord
[i
][2] = norm
[2];
499 _mesa_problem(ctx
, "Bad R texgen");
503 if (texUnit
->TexGenEnabled
& Q_BIT
) {
504 switch (texUnit
->GenModeQ
) {
505 case GL_OBJECT_LINEAR
:
506 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][3]),
507 sizeof(out
->data
[0]), obj
,
508 texUnit
->ObjectPlaneQ
);
511 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][3]),
512 sizeof(out
->data
[0]), eye
,
513 texUnit
->EyePlaneQ
);
516 _mesa_problem(ctx
, "Bad Q texgen");
523 static GLboolean
run_texgen_stage( GLcontext
*ctx
,
524 struct gl_pipeline_stage
*stage
)
526 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
527 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA( stage
);
530 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
531 if (ctx
->Texture
._TexGenEnabled
& ENABLE_TEXGEN(i
)) {
532 if (stage
->changed_inputs
& (VERT_EYE
| VERT_NORMAL_BIT
| VERT_TEX(i
)))
533 store
->TexgenFunc
[i
]( ctx
, store
, i
);
535 VB
->TexCoordPtr
[i
] = &store
->texcoord
[i
];
544 static GLboolean
run_validate_texgen_stage( GLcontext
*ctx
,
545 struct gl_pipeline_stage
*stage
)
547 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
550 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
551 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
553 if (texUnit
->TexGenEnabled
) {
556 if (texUnit
->TexGenEnabled
& R_BIT
)
558 else if (texUnit
->TexGenEnabled
& Q_BIT
)
560 else if (texUnit
->TexGenEnabled
& T_BIT
)
565 store
->TexgenSize
[i
] = sz
;
566 store
->TexgenHoles
[i
] = (all_bits
[sz
] & ~texUnit
->TexGenEnabled
);
567 store
->TexgenFunc
[i
] = texgen
;
569 if (texUnit
->TexGenEnabled
== (S_BIT
|T_BIT
|R_BIT
)) {
570 if (texUnit
->_GenFlags
== TEXGEN_REFLECTION_MAP_NV
) {
571 store
->TexgenFunc
[i
] = texgen_reflection_map_nv
;
573 else if (texUnit
->_GenFlags
== TEXGEN_NORMAL_MAP_NV
) {
574 store
->TexgenFunc
[i
] = texgen_normal_map_nv
;
577 else if (texUnit
->TexGenEnabled
== (S_BIT
|T_BIT
) &&
578 texUnit
->_GenFlags
== TEXGEN_SPHERE_MAP
) {
579 store
->TexgenFunc
[i
] = texgen_sphere_map
;
584 stage
->run
= run_texgen_stage
;
585 return stage
->run( ctx
, stage
);
589 static void check_texgen( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
594 if (ctx
->Texture
._TexGenEnabled
&& !ctx
->VertexProgram
.Enabled
) {
598 if (ctx
->Texture
._GenFlags
& TEXGEN_OBJ_LINEAR
)
599 inputs
|= VERT_OBJ_BIT
;
601 if (ctx
->Texture
._GenFlags
& TEXGEN_NEED_EYE_COORD
)
604 if (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
)
605 inputs
|= VERT_NORMAL_BIT
;
607 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
608 if (ctx
->Texture
._TexGenEnabled
& ENABLE_TEXGEN(i
))
610 outputs
|= VERT_TEX(i
);
612 /* Need the original input in case it contains a Q coord:
615 inputs
|= VERT_TEX(i
);
617 /* Something for Feedback? */
620 if (stage
->privatePtr
)
621 stage
->run
= run_validate_texgen_stage
;
623 stage
->inputs
= inputs
;
624 stage
->outputs
= outputs
;
631 /* Called the first time stage->run() is invoked.
633 static GLboolean
alloc_texgen_data( GLcontext
*ctx
,
634 struct gl_pipeline_stage
*stage
)
636 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
637 struct texgen_stage_data
*store
;
640 stage
->privatePtr
= CALLOC(sizeof(*store
));
641 store
= TEXGEN_STAGE_DATA(stage
);
645 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
646 _mesa_vector4f_alloc( &store
->texcoord
[i
], 0, VB
->Size
, 32 );
648 store
->tmp_f
= (GLfloat (*)[3]) MALLOC(VB
->Size
* sizeof(GLfloat
) * 3);
649 store
->tmp_m
= (GLfloat
*) MALLOC(VB
->Size
* sizeof(GLfloat
));
651 /* Now validate and run the stage.
653 stage
->run
= run_validate_texgen_stage
;
654 return stage
->run( ctx
, stage
);
658 static void free_texgen_data( struct gl_pipeline_stage
*stage
)
661 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
665 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++)
666 if (store
->texcoord
[i
].data
)
667 _mesa_vector4f_free( &store
->texcoord
[i
] );
670 if (store
->tmp_f
) FREE( store
->tmp_f
);
671 if (store
->tmp_m
) FREE( store
->tmp_m
);
673 stage
->privatePtr
= NULL
;
679 const struct gl_pipeline_stage _tnl_texgen_stage
=
682 _NEW_TEXTURE
, /* when to call check() */
683 _NEW_TEXTURE
, /* when to invalidate stored data */
684 GL_FALSE
, /* active? */
687 0, /* changed_inputs */
688 NULL
, /* private data */
689 free_texgen_data
, /* destructor */
690 check_texgen
, /* check */
691 alloc_texgen_data
/* run -- initially set to alloc data */