home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / src / xform.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  8KB  |  284 lines

  1. /* $Id: xform.c,v 1.3 1999/11/05 06:43:11 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.1
  6.  *
  7.  * Copyright (C) 1999  Brian Paul   All Rights Reserved.
  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.  
  28. /* $XFree86: xc/lib/GL/mesa/src/xform.c,v 1.4 1999/04/04 00:20:36 dawes Exp $ */
  29.  
  30. /*
  31.  * Matrix/vertex/vector transformation stuff
  32.  *
  33.  *
  34.  * NOTES:
  35.  * 1. 4x4 transformation matrices are stored in memory in column major order.
  36.  * 2. Points/vertices are to be thought of as column vectors.
  37.  * 3. Transformation of a point p by a matrix M is: p' = M * p
  38.  */
  39.  
  40.  
  41. #ifdef PC_HEADER
  42. #include "all.h"
  43. #else
  44. #ifndef XFree86Server
  45. #include <math.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #else
  49. #include "GL/xf86glx.h"
  50. #endif
  51. #include "vb.h"
  52. #include "context.h"
  53. #include "mmath.h"
  54. #include "types.h"
  55. #include "vb.h"
  56. #include "xform.h"
  57. #endif
  58.  
  59. #ifdef DEBUG
  60. #include "debug_xform.h"
  61. #endif
  62.  
  63. #ifdef USE_X86_ASM
  64. #include "X86/common_x86asm.h"
  65. #endif
  66.  
  67. clip_func gl_clip_tab[5];
  68. dotprod_func gl_dotprod_tab[2][5];
  69. vec_copy_func gl_copy_tab[2][0x10];
  70. normal_func gl_normal_tab[0xf][0x4];
  71. transform_func **(gl_transform_tab[2]);
  72. static transform_func *cull_transform_tab[5];
  73. static transform_func *raw_transform_tab[5];
  74.  
  75.  
  76. /* Raw data format used for:
  77.  *    - Object-to-eye transform prior to culling, although this too
  78.  *      could be culled under some circumstances.
  79.  *    - Eye-to-clip transform (via the function above).
  80.  *    - Cliptesting
  81.  *    - And everything else too, if culling happens to be disabled.
  82.  */
  83. #define TAG(x) x##_raw
  84. #define TAG2(x,y) x##y##_raw
  85. #define IDX 0
  86. #define STRIDE_LOOP for (i=0;i<count;i++, STRIDE_F(from, stride))
  87. #define LOOP for (i=0;i<n;i++)
  88. #define CULL_CHECK
  89. #define CLIP_CHECK
  90. #define ARGS
  91. #include "xform_tmp.h"
  92. #include "clip_tmp.h"
  93. #include "norm_tmp.h"
  94. #include "dotprod_tmp.h"
  95. #include "copy_tmp.h"
  96. #undef TAG
  97. #undef TAG2
  98. #undef LOOP
  99. #undef CULL_CHECK
  100. #undef CLIP_CHECK
  101. #undef ARGS
  102. #undef IDX
  103.  
  104. /* Culled data used for:
  105.  *    - texture transformations
  106.  *    - viewport map transformation
  107.  *    - normal transformations prior to lighting
  108.  *    - user cliptests
  109.  */
  110. #define TAG(x) x##_masked
  111. #define TAG2(x,y) x##y##_masked
  112. #define IDX CULL_MASK_ACTIVE
  113. #define STRIDE_LOOP for (i=0;i<count;i++, STRIDE_F(from, stride))
  114. #define LOOP for (i=0;i<n;i++)
  115. #define CULL_CHECK if (mask[i])
  116. #define CLIP_CHECK if ((mask[i] & flag) == 0)
  117. #define ARGS , const GLubyte mask[]
  118. #include "xform_tmp.h"
  119. #include "norm_tmp.h"
  120. #include "dotprod_tmp.h"
  121. #include "copy_tmp.h"
  122. #undef TAG
  123. #undef TAG2
  124. #undef LOOP
  125. #undef CULL_CHECK
  126. #undef CLIP_CHECK
  127. #undef ARGS
  128. #undef IDX
  129.  
  130.  
  131.  
  132.  
  133. #if 0
  134.  
  135. #define TAG(x) x##_raw_compacted
  136. #define TAG2(x,y) x##y##_raw_compacted
  137. #define IDX COMPACTED_NORMALS
  138. #define STRIDE_LOOP for (i=0;i<count;i++, STRIDE_F(from, stride))
  139. #define LOOP for (i=0;i<n;i++)
  140. #define CHECK if (flag[i] & VERT_NORM)
  141. #define ARGS
  142. #include "norm_tmp.h"
  143. #undef TAG
  144. #undef TAG2
  145. #undef LOOP
  146. #undef CHECK
  147. #undef ARGS
  148. #undef IDX
  149.  
  150.  
  151. #define TAG(x) x##_masked
  152. #define TAG2(x,y) x##y##_masked
  153. #define IDX CULL_MASK_ACTIVE|COMPACTED_NORMALS
  154. #define DUPLICATE_FUNCTIONS
  155. #include "norm_tmp.h"
  156. #undef TAG
  157. #undef TAG2
  158. #undef LOOP
  159. #undef CHECK
  160. #undef ARGS
  161. #undef IDX
  162.  
  163. #endif
  164.  
  165.  
  166. GLvector4f *gl_project_points( GLvector4f *proj_vec,
  167.                    const GLvector4f *clip_vec )
  168. {
  169.    const GLuint stride = clip_vec->stride;
  170.    const GLfloat *from = (GLfloat *)clip_vec->start;
  171.    const GLuint count = clip_vec->count;
  172.    GLfloat (*vProj)[4] = (GLfloat (*)[4])proj_vec->start;
  173.    GLuint i;
  174.  
  175.    for (i = 0 ; i < count ; i++, STRIDE_F(from, stride))
  176.    {
  177.      GLfloat oow = 1.0F / from[3];
  178.      vProj[i][3] = oow;
  179.      vProj[i][0] = from[0] * oow;
  180.      vProj[i][1] = from[1] * oow;
  181.      vProj[i][2] = from[2] * oow;
  182.    }
  183.  
  184.    proj_vec->flags |= VEC_SIZE_4;
  185.    proj_vec->size = 3;
  186.    proj_vec->count = clip_vec->count;
  187.    return proj_vec;
  188. }
  189.  
  190.  
  191.  
  192. /*
  193.  * This is called only once.  It initializes several tables with pointers
  194.  * to optimized transformation functions.  This is where we can test for
  195.  * AMD 3Dnow! capability, Intel Katmai, etc. and hook in the right code.
  196.  */
  197. void gl_init_transformation( void )
  198. {
  199.    gl_transform_tab[0] = raw_transform_tab;
  200.    gl_transform_tab[1] = cull_transform_tab;
  201.  
  202.    init_c_transformations_raw();
  203.    init_c_transformations_masked();
  204.    init_c_norm_transform_raw();
  205.    init_c_norm_transform_masked();
  206.    init_c_cliptest_raw();
  207.    init_copy0_raw();
  208.    init_copy0_masked();
  209.    init_dotprod_raw();
  210.    init_dotprod_masked();
  211.  
  212. #ifdef DEBUG
  213.    gl_test_all_transform_functions ("default");
  214.    gl_test_all_normal_transform_functions ("default");
  215. #endif
  216.  
  217. #ifdef USE_X86_ASM
  218.    gl_init_all_x86_asm ();
  219. #endif
  220. }
  221.  
  222.  
  223.  
  224. /*
  225.  * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix.  This
  226.  * function is used for transforming clipping plane equations and spotlight
  227.  * directions.
  228.  * Mathematically,  u = v * m.
  229.  * Input:  v - input vector
  230.  *         m - transformation matrix
  231.  * Output:  u - transformed vector
  232.  */
  233. void gl_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
  234. {
  235.    GLfloat v0=v[0], v1=v[1], v2=v[2], v3=v[3];
  236. #define M(row,col)  m[row + col*4]
  237.    u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
  238.    u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
  239.    u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
  240.    u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);
  241. #undef M
  242. }
  243.  
  244.  
  245. /* Useful for one-off point transformations, as in clipping.
  246.  * Note that because the matrix isn't analyzed we do too many
  247.  * multiplies, and that the result is always 4-clean.
  248.  */
  249. void gl_transform_point_sz( GLfloat Q[4], const GLfloat M[16],
  250.                 const GLfloat P[4], GLuint sz )
  251. {
  252.    if (Q == P)
  253.       return;
  254.  
  255.    if (sz == 4)
  256.    {
  257.       Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] *  P[2] + M[12] * P[3];
  258.       Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] *  P[2] + M[13] * P[3];
  259.       Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14] * P[3];
  260.       Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15] * P[3];
  261.    }
  262.    else if (sz == 3)
  263.    {
  264.       Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] *  P[2] + M[12];
  265.       Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] *  P[2] + M[13];
  266.       Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14];
  267.       Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15];
  268.    }
  269.    else if (sz == 2)
  270.    {
  271.       Q[0] = M[0] * P[0] + M[4] * P[1] +                M[12];
  272.       Q[1] = M[1] * P[0] + M[5] * P[1] +                M[13];
  273.       Q[2] = M[2] * P[0] + M[6] * P[1] +                M[14];
  274.       Q[3] = M[3] * P[0] + M[7] * P[1] +                M[15];
  275.    }
  276.    else if (sz == 1)
  277.    {
  278.       Q[0] = M[0] * P[0] +                              M[12];
  279.       Q[1] = M[1] * P[0] +                              M[13];
  280.       Q[2] = M[2] * P[0] +                              M[14];
  281.       Q[3] = M[3] * P[0] +                              M[15];
  282.    }
  283. }
  284.