2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keith@tungstengraphics.com>
30 * Regarding GL_NV_texgen_reflection:
32 * Portions of this software may use or implement intellectual
33 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims
34 * any and all warranties with respect to such intellectual property,
35 * including any use thereof or modifications thereto.
38 #include "main/glheader.h"
39 #include "main/colormac.h"
40 #include "main/macros.h"
41 #include "main/imports.h"
42 #include "main/mtypes.h"
44 #include "math/m_xform.h"
46 #include "t_context.h"
47 #include "t_pipeline.h"
50 /***********************************************************************
51 * Automatic texture coordinate generation (texgen) code.
55 struct texgen_stage_data
;
57 typedef void (*texgen_func
)( struct gl_context
*ctx
,
58 struct texgen_stage_data
*store
,
62 struct texgen_stage_data
{
64 /* Per-texunit derived state.
66 GLuint TexgenSize
[MAX_TEXTURE_COORD_UNITS
];
67 texgen_func TexgenFunc
[MAX_TEXTURE_COORD_UNITS
];
69 /* Temporary values used in texgen.
74 /* Buffered outputs of the stage.
76 GLvector4f texcoord
[MAX_TEXTURE_COORD_UNITS
];
80 #define TEXGEN_STAGE_DATA(stage) ((struct texgen_stage_data *)stage->privatePtr)
84 static GLuint all_bits
[5] = {
92 #define VEC_SIZE_FLAGS (VEC_SIZE_1|VEC_SIZE_2|VEC_SIZE_3|VEC_SIZE_4)
94 #define TEXGEN_NEED_M (TEXGEN_SPHERE_MAP)
95 #define TEXGEN_NEED_F (TEXGEN_SPHERE_MAP | \
96 TEXGEN_REFLECTION_MAP_NV)
100 static void build_m3( GLfloat f
[][3], GLfloat m
[],
101 const GLvector4f
*normal
,
102 const GLvector4f
*eye
)
104 GLuint stride
= eye
->stride
;
105 GLfloat
*coord
= (GLfloat
*)eye
->start
;
106 GLuint count
= eye
->count
;
107 const GLfloat
*norm
= normal
->start
;
110 for (i
=0;i
<count
;i
++,STRIDE_F(coord
,stride
),STRIDE_F(norm
,normal
->stride
)) {
111 GLfloat u
[3], two_nu
, fx
, fy
, fz
;
114 two_nu
= 2.0F
* DOT3(norm
,u
);
115 fx
= f
[i
][0] = u
[0] - norm
[0] * two_nu
;
116 fy
= f
[i
][1] = u
[1] - norm
[1] * two_nu
;
117 fz
= f
[i
][2] = u
[2] - norm
[2] * two_nu
;
118 m
[i
] = fx
* fx
+ fy
* fy
+ (fz
+ 1.0F
) * (fz
+ 1.0F
);
120 m
[i
] = 0.5F
* INV_SQRTF(m
[i
]);
127 static void build_m2( GLfloat f
[][3], GLfloat m
[],
128 const GLvector4f
*normal
,
129 const GLvector4f
*eye
)
131 GLuint stride
= eye
->stride
;
132 GLfloat
*coord
= eye
->start
;
133 GLuint count
= eye
->count
;
135 GLfloat
*norm
= normal
->start
;
138 for (i
=0;i
<count
;i
++,STRIDE_F(coord
,stride
),STRIDE_F(norm
,normal
->stride
)) {
139 GLfloat u
[3], two_nu
, fx
, fy
, fz
;
143 two_nu
= 2.0F
* DOT3(norm
,u
);
144 fx
= f
[i
][0] = u
[0] - norm
[0] * two_nu
;
145 fy
= f
[i
][1] = u
[1] - norm
[1] * two_nu
;
146 fz
= f
[i
][2] = u
[2] - norm
[2] * two_nu
;
147 m
[i
] = fx
* fx
+ fy
* fy
+ (fz
+ 1.0F
) * (fz
+ 1.0F
);
149 m
[i
] = 0.5F
* INV_SQRTF(m
[i
]);
156 typedef void (*build_m_func
)( GLfloat f
[][3],
158 const GLvector4f
*normal
,
159 const GLvector4f
*eye
);
162 static build_m_func build_m_tab
[5] = {
171 /* This is unusual in that we respect the stride of the output vector
172 * (f). This allows us to pass in either a texcoord vector4f, or a
173 * temporary vector3f.
175 static void build_f3( GLfloat
*f
,
177 const GLvector4f
*normal
,
178 const GLvector4f
*eye
)
180 GLuint stride
= eye
->stride
;
181 GLfloat
*coord
= eye
->start
;
182 GLuint count
= eye
->count
;
184 GLfloat
*norm
= normal
->start
;
187 for (i
=0;i
<count
;i
++) {
188 GLfloat u
[3], two_nu
;
191 two_nu
= 2.0F
* DOT3(norm
,u
);
192 f
[0] = u
[0] - norm
[0] * two_nu
;
193 f
[1] = u
[1] - norm
[1] * two_nu
;
194 f
[2] = u
[2] - norm
[2] * two_nu
;
195 STRIDE_F(coord
,stride
);
197 STRIDE_F(norm
, normal
->stride
);
202 static void build_f2( GLfloat
*f
,
204 const GLvector4f
*normal
,
205 const GLvector4f
*eye
)
207 GLuint stride
= eye
->stride
;
208 GLfloat
*coord
= eye
->start
;
209 GLuint count
= eye
->count
;
210 GLfloat
*norm
= normal
->start
;
213 for (i
=0;i
<count
;i
++) {
215 GLfloat u
[3], two_nu
;
219 two_nu
= 2.0F
* DOT3(norm
,u
);
220 f
[0] = u
[0] - norm
[0] * two_nu
;
221 f
[1] = u
[1] - norm
[1] * two_nu
;
222 f
[2] = u
[2] - norm
[2] * two_nu
;
224 STRIDE_F(coord
,stride
);
226 STRIDE_F(norm
, normal
->stride
);
230 typedef void (*build_f_func
)( GLfloat
*f
,
232 const GLvector4f
*normal_vec
,
233 const GLvector4f
*eye
);
237 /* Just treat 4-vectors as 3-vectors.
239 static build_f_func build_f_tab
[5] = {
249 /* Special case texgen functions.
251 static void texgen_reflection_map_nv( struct gl_context
*ctx
,
252 struct texgen_stage_data
*store
,
255 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
256 GLvector4f
*in
= VB
->AttribPtr
[VERT_ATTRIB_TEX0
+ unit
];
257 GLvector4f
*out
= &store
->texcoord
[unit
];
259 build_f_tab
[VB
->EyePtr
->size
]( out
->start
,
261 VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
],
264 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_3
;
265 out
->count
= VB
->Count
;
266 out
->size
= MAX2(in
->size
, 3);
268 _mesa_copy_tab
[0x8]( out
, in
);
273 static void texgen_normal_map_nv( struct gl_context
*ctx
,
274 struct texgen_stage_data
*store
,
277 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
278 GLvector4f
*in
= VB
->AttribPtr
[VERT_ATTRIB_TEX0
+ unit
];
279 GLvector4f
*out
= &store
->texcoord
[unit
];
280 GLvector4f
*normal
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
];
281 GLfloat (*texcoord
)[4] = (GLfloat (*)[4])out
->start
;
282 GLuint count
= VB
->Count
;
284 const GLfloat
*norm
= normal
->start
;
286 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
287 texcoord
[i
][0] = norm
[0];
288 texcoord
[i
][1] = norm
[1];
289 texcoord
[i
][2] = norm
[2];
293 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_3
;
295 out
->size
= MAX2(in
->size
, 3);
297 _mesa_copy_tab
[0x8]( out
, in
);
301 static void texgen_sphere_map( struct gl_context
*ctx
,
302 struct texgen_stage_data
*store
,
305 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
306 GLvector4f
*in
= VB
->AttribPtr
[VERT_ATTRIB_TEX0
+ unit
];
307 GLvector4f
*out
= &store
->texcoord
[unit
];
308 GLfloat (*texcoord
)[4] = (GLfloat (*)[4]) out
->start
;
309 GLuint count
= VB
->Count
;
311 GLfloat (*f
)[3] = store
->tmp_f
;
312 GLfloat
*m
= store
->tmp_m
;
314 (build_m_tab
[VB
->EyePtr
->size
])( store
->tmp_f
,
316 VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
],
319 out
->size
= MAX2(in
->size
,2);
321 for (i
=0;i
<count
;i
++) {
322 texcoord
[i
][0] = f
[i
][0] * m
[i
] + 0.5F
;
323 texcoord
[i
][1] = f
[i
][1] * m
[i
] + 0.5F
;
327 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_2
;
329 _mesa_copy_tab
[all_bits
[in
->size
] & ~0x3]( out
, in
);
334 static void texgen( struct gl_context
*ctx
,
335 struct texgen_stage_data
*store
,
338 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
339 struct vertex_buffer
*VB
= &tnl
->vb
;
340 GLvector4f
*in
= VB
->AttribPtr
[VERT_ATTRIB_TEX0
+ unit
];
341 GLvector4f
*out
= &store
->texcoord
[unit
];
342 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
343 const GLvector4f
*obj
= VB
->AttribPtr
[_TNL_ATTRIB_POS
];
344 const GLvector4f
*eye
= VB
->EyePtr
;
345 const GLvector4f
*normal
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
];
346 const GLfloat
*m
= store
->tmp_m
;
347 const GLuint count
= VB
->Count
;
348 GLfloat (*texcoord
)[4] = (GLfloat (*)[4])out
->data
;
349 GLfloat (*f
)[3] = store
->tmp_f
;
352 if (texUnit
->_GenFlags
& TEXGEN_NEED_M
) {
353 build_m_tab
[eye
->size
]( store
->tmp_f
, store
->tmp_m
, normal
, eye
);
354 } else if (texUnit
->_GenFlags
& TEXGEN_NEED_F
) {
355 build_f_tab
[eye
->size
]( (GLfloat
*)store
->tmp_f
, 3, normal
, eye
);
359 out
->size
= MAX2(in
->size
, store
->TexgenSize
[unit
]);
360 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | texUnit
->TexGenEnabled
;
363 copy
= (all_bits
[in
->size
] & ~texUnit
->TexGenEnabled
);
365 _mesa_copy_tab
[copy
]( out
, in
);
367 if (texUnit
->TexGenEnabled
& S_BIT
) {
369 switch (texUnit
->GenS
.Mode
) {
370 case GL_OBJECT_LINEAR
:
371 _mesa_dotprod_tab
[obj
->size
]( (GLfloat
*)out
->data
,
372 sizeof(out
->data
[0]), obj
,
373 texUnit
->GenS
.ObjectPlane
);
376 _mesa_dotprod_tab
[eye
->size
]( (GLfloat
*)out
->data
,
377 sizeof(out
->data
[0]), eye
,
378 texUnit
->GenS
.EyePlane
);
381 for (i
= 0; i
< count
; i
++)
382 texcoord
[i
][0] = f
[i
][0] * m
[i
] + 0.5F
;
384 case GL_REFLECTION_MAP_NV
:
385 for (i
=0;i
<count
;i
++)
386 texcoord
[i
][0] = f
[i
][0];
388 case GL_NORMAL_MAP_NV
: {
389 const GLfloat
*norm
= normal
->start
;
390 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
391 texcoord
[i
][0] = norm
[0];
396 _mesa_problem(ctx
, "Bad S texgen");
400 if (texUnit
->TexGenEnabled
& T_BIT
) {
402 switch (texUnit
->GenT
.Mode
) {
403 case GL_OBJECT_LINEAR
:
404 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][1]),
405 sizeof(out
->data
[0]), obj
,
406 texUnit
->GenT
.ObjectPlane
);
409 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][1]),
410 sizeof(out
->data
[0]), eye
,
411 texUnit
->GenT
.EyePlane
);
414 for (i
= 0; i
< count
; i
++)
415 texcoord
[i
][1] = f
[i
][1] * m
[i
] + 0.5F
;
417 case GL_REFLECTION_MAP_NV
:
418 for (i
=0;i
<count
;i
++)
419 texcoord
[i
][1] = f
[i
][1];
421 case GL_NORMAL_MAP_NV
: {
422 const GLfloat
*norm
= normal
->start
;
423 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
424 texcoord
[i
][1] = norm
[1];
429 _mesa_problem(ctx
, "Bad T texgen");
433 if (texUnit
->TexGenEnabled
& R_BIT
) {
435 switch (texUnit
->GenR
.Mode
) {
436 case GL_OBJECT_LINEAR
:
437 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][2]),
438 sizeof(out
->data
[0]), obj
,
439 texUnit
->GenR
.ObjectPlane
);
442 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][2]),
443 sizeof(out
->data
[0]), eye
,
444 texUnit
->GenR
.EyePlane
);
446 case GL_REFLECTION_MAP_NV
:
447 for (i
=0;i
<count
;i
++)
448 texcoord
[i
][2] = f
[i
][2];
450 case GL_NORMAL_MAP_NV
: {
451 const GLfloat
*norm
= normal
->start
;
452 for (i
=0;i
<count
;i
++,STRIDE_F(norm
, normal
->stride
)) {
453 texcoord
[i
][2] = norm
[2];
458 _mesa_problem(ctx
, "Bad R texgen");
462 if (texUnit
->TexGenEnabled
& Q_BIT
) {
463 switch (texUnit
->GenQ
.Mode
) {
464 case GL_OBJECT_LINEAR
:
465 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][3]),
466 sizeof(out
->data
[0]), obj
,
467 texUnit
->GenQ
.ObjectPlane
);
470 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][3]),
471 sizeof(out
->data
[0]), eye
,
472 texUnit
->GenQ
.EyePlane
);
475 _mesa_problem(ctx
, "Bad Q texgen");
483 static GLboolean
run_texgen_stage( struct gl_context
*ctx
,
484 struct tnl_pipeline_stage
*stage
)
486 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
487 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
490 if (!ctx
->Texture
._TexGenEnabled
|| ctx
->VertexProgram
._Current
)
493 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
494 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
496 if (texUnit
->TexGenEnabled
) {
498 store
->TexgenFunc
[i
]( ctx
, store
, i
);
500 VB
->AttribPtr
[VERT_ATTRIB_TEX0
+ i
] = &store
->texcoord
[i
];
508 static void validate_texgen_stage( struct gl_context
*ctx
,
509 struct tnl_pipeline_stage
*stage
)
511 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
514 if (!ctx
->Texture
._TexGenEnabled
|| ctx
->VertexProgram
._Current
)
517 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
518 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
520 if (texUnit
->TexGenEnabled
) {
523 if (texUnit
->TexGenEnabled
& Q_BIT
)
525 else if (texUnit
->TexGenEnabled
& R_BIT
)
527 else if (texUnit
->TexGenEnabled
& T_BIT
)
532 store
->TexgenSize
[i
] = sz
;
533 store
->TexgenFunc
[i
] = texgen
; /* general solution */
535 /* look for special texgen cases */
536 if (texUnit
->TexGenEnabled
== (S_BIT
|T_BIT
|R_BIT
)) {
537 if (texUnit
->_GenFlags
== TEXGEN_REFLECTION_MAP_NV
) {
538 store
->TexgenFunc
[i
] = texgen_reflection_map_nv
;
540 else if (texUnit
->_GenFlags
== TEXGEN_NORMAL_MAP_NV
) {
541 store
->TexgenFunc
[i
] = texgen_normal_map_nv
;
544 else if (texUnit
->TexGenEnabled
== (S_BIT
|T_BIT
) &&
545 texUnit
->_GenFlags
== TEXGEN_SPHERE_MAP
) {
546 store
->TexgenFunc
[i
] = texgen_sphere_map
;
556 /* Called the first time stage->run() is invoked.
558 static GLboolean
alloc_texgen_data( struct gl_context
*ctx
,
559 struct tnl_pipeline_stage
*stage
)
561 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
562 struct texgen_stage_data
*store
;
565 stage
->privatePtr
= calloc(1, sizeof(*store
));
566 store
= TEXGEN_STAGE_DATA(stage
);
570 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
571 _mesa_vector4f_alloc( &store
->texcoord
[i
], 0, VB
->Size
, 32 );
573 store
->tmp_f
= (GLfloat (*)[3]) malloc(VB
->Size
* sizeof(GLfloat
) * 3);
574 store
->tmp_m
= (GLfloat
*) malloc(VB
->Size
* sizeof(GLfloat
));
580 static void free_texgen_data( struct tnl_pipeline_stage
*stage
)
583 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
587 for (i
= 0 ; i
< MAX_TEXTURE_COORD_UNITS
; i
++)
588 if (store
->texcoord
[i
].data
)
589 _mesa_vector4f_free( &store
->texcoord
[i
] );
592 if (store
->tmp_f
) FREE( store
->tmp_f
);
593 if (store
->tmp_m
) FREE( store
->tmp_m
);
595 stage
->privatePtr
= NULL
;
601 const struct tnl_pipeline_stage _tnl_texgen_stage
=
604 NULL
, /* private data */
605 alloc_texgen_data
, /* destructor */
606 free_texgen_data
, /* destructor */
607 validate_texgen_stage
, /* check */
608 run_texgen_stage
/* run -- initially set to alloc data */