Source

srb2ztm1094 / engine / src / hardware / r_d3d / mesa / xform.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* $Id: xform.c,v 3.3 1998/08/20 04:15:48 brianp Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.0
 * Copyright (C) 1995-1998  Brian Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/*
 * $Log: xform.c,v $
 * Revision 3.3  1998/08/20 04:15:48  brianp
 * added prototype 3.1 transformation functions
 *
 * Revision 3.2  1998/04/18 05:00:56  brianp
 * renamed USE_ASM to USE_X86_ASM
 *
 * Revision 3.1  1998/02/01 16:37:19  brianp
 * added GL_EXT_rescale_normal extension
 *
 * Revision 3.0  1998/01/31 21:08:31  brianp
 * initial rev
 *
 */


/*
 * Matrix/vertex/vector transformation stuff
 *
 *
 * NOTES:
 * 1. 4x4 transformation matrices are stored in memory in column major order.
 * 2. Points/vertices are to be thought of as column vectors.
 * 3. Transformation of a point p by a matrix M is: p' = M * p
 *
 */


#ifdef PC_HEADER
#include "all.h"
#else
#include <math.h>
#include "context.h"
#include "mmath.h"
#include "types.h"
#include "xform.h"
#endif



/*
 * Apply a transformation matrix to an array of [X Y Z W] coordinates:
 *   for i in 0 to n-1 do   q[i] = m * p[i]
 * where p[i] and q[i] are 4-element column vectors and m is a 16-element
 * transformation matrix.
 */
void gl_xform_points_4fv( GLuint n, GLfloat q[][4], const GLfloat m[16],
                          GLfloat p[][4] )
{
   /* This function has been carefully crafted to maximize register usage
    * and use loop unrolling with IRIX 5.3's cc.  Hopefully other compilers
    * will like this code too.
    */
   {
      GLuint i;
      GLfloat m0 = m[0],  m4 = m[4],  m8 = m[8],  m12 = m[12];
      GLfloat m1 = m[1],  m5 = m[5],  m9 = m[9],  m13 = m[13];
      if (m12==0.0F && m13==0.0F) {
         /* common case */
         for (i=0;i<n;i++) {
            GLfloat p0 = p[i][0], p1 = p[i][1], p2 = p[i][2];
            q[i][0] = m0 * p0 + m4  * p1 + m8 * p2;
            q[i][1] = m1 * p0 + m5  * p1 + m9 * p2;
         }
      }
      else {
         /* general case */
         for (i=0;i<n;i++) {
            GLfloat p0 = p[i][0], p1 = p[i][1], p2 = p[i][2], p3 = p[i][3];
            q[i][0] = m0 * p0 + m4  * p1 + m8 * p2 + m12 * p3;
            q[i][1] = m1 * p0 + m5  * p1 + m9 * p2 + m13 * p3;
         }
      }
   }
   {
      GLuint i;
      GLfloat m2 = m[2],  m6 = m[6],  m10 = m[10],  m14 = m[14];
      GLfloat m3 = m[3],  m7 = m[7],  m11 = m[11],  m15 = m[15];
      if (m3==0.0F && m7==0.0F && m11==0.0F && m15==1.0F) {
         /* common case */
         for (i=0;i<n;i++) {
            GLfloat p0 = p[i][0], p1 = p[i][1], p2 = p[i][2], p3 = p[i][3];
            q[i][2] = m2 * p0 + m6 * p1 + m10 * p2 + m14 * p3;
            q[i][3] = p3;
         }
      }
      else {
         /* general case */
         for (i=0;i<n;i++) {
            GLfloat p0 = p[i][0], p1 = p[i][1], p2 = p[i][2], p3 = p[i][3];
            q[i][2] = m2 * p0 + m6 * p1 + m10 * p2 + m14 * p3;
            q[i][3] = m3 * p0 + m7 * p1 + m11 * p2 + m15 * p3;
         }
      }
   }
}



/*
 * Apply a transformation matrix to an array of [X Y Z] coordinates:
 *   for i in 0 to n-1 do   q[i] = m * p[i]
 */
void gl_xform_points_3fv( GLuint n, GLfloat q[][4], const GLfloat m[16],
                          GLfloat p[][3] )
{
   /* This function has been carefully crafted to maximize register usage
    * and use loop unrolling with IRIX 5.3's cc.  Hopefully other compilers
    * will like this code too.
    */
   {
      GLuint i;
      GLfloat m0 = m[0],  m4 = m[4],  m8 = m[8],  m12 = m[12];
      GLfloat m1 = m[1],  m5 = m[5],  m9 = m[9],  m13 = m[13];
      for (i=0;i<n;i++) {
         GLfloat p0 = p[i][0], p1 = p[i][1], p2 = p[i][2];
         q[i][0] = m0 * p0 + m4  * p1 + m8 * p2 + m12;
         q[i][1] = m1 * p0 + m5  * p1 + m9 * p2 + m13;
      }
   }
   {
      GLuint i;
      GLfloat m2 = m[2],  m6 = m[6],  m10 = m[10],  m14 = m[14];
      GLfloat m3 = m[3],  m7 = m[7],  m11 = m[11],  m15 = m[15];
      if (m3==0.0F && m7==0.0F && m11==0.0F && m15==1.0F) {
         /* common case */
         for (i=0;i<n;i++) {
            GLfloat p0 = p[i][0], p1 = p[i][1], p2 = p[i][2];
            q[i][2] = m2 * p0 + m6 * p1 + m10 * p2 + m14;
            q[i][3] = 1.0F;
         }
      }
      else {
         /* general case */
         for (i=0;i<n;i++) {
            GLfloat p0 = p[i][0], p1 = p[i][1], p2 = p[i][2];
            q[i][2] = m2 * p0 + m6 * p1 + m10 * p2 + m14;
            q[i][3] = m3 * p0 + m7 * p1 + m11 * p2 + m15;
         }
      }
   }
}



#ifndef USE_X86_ASM
/*
 * Apply a transformation matrix to an array of normal vectors:
 *   for i in 0 to n-1 do  v[i] = u[i] * m
 * where u[i] and v[i] are 3-element row vectors and m is a 16-element
 * transformation matrix.
 * If the normalize flag is true the normals will be scaled to length 1.
 * If the rescale flag is true then do normal rescaling.
 */
void gl_xform_normals_3fv( GLuint n, GLfloat v[][3], const GLfloat m[16],
                           GLfloat u[][3], GLboolean normalize,
                           GLboolean rescale )
{
   if (normalize) {
      if (rescale) {
         /* Transform normals, rescale and normalize */
         GLuint i;
         GLfloat m0 = m[0],  m4 = m[4],  m8 = m[8];
         GLfloat m1 = m[1],  m5 = m[5],  m9 = m[9];
         GLfloat m2 = m[2],  m6 = m[6],  m10 = m[10];
         GLfloat f = GL_SQRT( m2*m2 + m6*m6 + m10*m10 );
         f = (f == 0.0F) ? 1.0F : (1.0F / f);
         for (i=0;i<n;i++) {
            GLdouble tx, ty, tz;
            {
               GLfloat ux = u[i][0],  uy = u[i][1],  uz = u[i][2];
               tx = f * (ux * m0 + uy * m1 + uz * m2);
               ty = f * (ux * m4 + uy * m5 + uz * m6);
               tz = f * (ux * m8 + uy * m9 + uz * m10);
            }
            {
               GLdouble len, scale;
               len = GL_SQRT( tx*tx + ty*ty + tz*tz );
               scale = (len>1E-30) ? (1.0 / len) : 1.0;
               v[i][0] = tx * scale;
               v[i][1] = ty * scale;
               v[i][2] = tz * scale;
            }
         }
      }
      else {
         /* Transform and normalize */
         GLuint i;
         GLfloat m0 = m[0],  m4 = m[4],  m8 = m[8];
         GLfloat m1 = m[1],  m5 = m[5],  m9 = m[9];
         GLfloat m2 = m[2],  m6 = m[6],  m10 = m[10];
         for (i=0;i<n;i++) {
            GLdouble tx, ty, tz;
            {
               GLfloat ux = u[i][0],  uy = u[i][1],  uz = u[i][2];
               tx = ux * m0 + uy * m1 + uz * m2;
               ty = ux * m4 + uy * m5 + uz * m6;
               tz = ux * m8 + uy * m9 + uz * m10;
            }
            {
               GLdouble len, scale;
               len = GL_SQRT( tx*tx + ty*ty + tz*tz );
               scale = (len>1E-30) ? (1.0 / len) : 1.0;
               v[i][0] = tx * scale;
               v[i][1] = ty * scale;
               v[i][2] = tz * scale;
            }
         }
      }
   }
   else {
      if (rescale) {
         /* Transform and rescale */
         GLuint i;
         GLfloat m0 = m[0],  m4 = m[4],  m8 = m[8];
         GLfloat m1 = m[1],  m5 = m[5],  m9 = m[9];
         GLfloat m2 = m[2],  m6 = m[6],  m10 = m[10];
         GLfloat f = GL_SQRT( m2*m2 + m6*m6 + m10*m10 );
         f = (f == 0.0F) ? 1.0F : (1.0F / f);
         for (i=0;i<n;i++) {
            GLfloat ux = u[i][0],  uy = u[i][1],  uz = u[i][2];
            v[i][0] = f * (ux * m0 + uy * m1 + uz * m2);
            v[i][1] = f * (ux * m4 + uy * m5 + uz * m6);
            v[i][2] = f * (ux * m8 + uy * m9 + uz * m10);
         }
      }
      else {
         /* Just transform */
         GLuint i;
         GLfloat m0 = m[0],  m4 = m[4],  m8 = m[8];
         GLfloat m1 = m[1],  m5 = m[5],  m9 = m[9];
         GLfloat m2 = m[2],  m6 = m[6],  m10 = m[10];
         for (i=0;i<n;i++) {
            GLfloat ux = u[i][0],  uy = u[i][1],  uz = u[i][2];
            v[i][0] = ux * m0 + uy * m1 + uz * m2;
            v[i][1] = ux * m4 + uy * m5 + uz * m6;
            v[i][2] = ux * m8 + uy * m9 + uz * m10;
         }
      }
   }
}
#endif


/*
 * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix.  This
 * function is used for transforming clipping plane equations and spotlight
 * directions.
 * Mathematically,  u = v * m.
 * Input:  v - input vector
 *         m - transformation matrix
 * Output:  u - transformed vector
 */
void gl_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
{
   GLfloat v0=v[0], v1=v[1], v2=v[2], v3=v[3];
#define M(row,col)  m[col*4+row]
   u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
   u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
   u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
   u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);
#undef M
}



/**********************************************************************
 *
 * Mesa 3.1 prototype transformation code
 *
 * Objectives:
 *     Move all functions which may be asm-accelerated out of vbxform.c
 *       into this file.
 *
 *     Add support for arbitrary strides in source coordinates to better
 *       support vertex arrays.
 *
 **********************************************************************/



/*
 * Use the current modelview matrix to transform XYZ vertices from object
 * to eye coordinates.
 * Input:  ctx - the context
 *         n - number of vertices to transform
 *         stride - stride in bytes between subsequent vObj vertices
 *         vObj - pointer to first vertex (in object coordinates)
 * Output;  vEye - array [n][4] of eye coordinates
 */
void
gl_transform_points3( const GLcontext *ctx, GLuint n, GLuint stride,
                      const GLfloat *vObj, GLfloat vEye[][4] )
{
   ASSERT((stride & 0x3) == 0);  /* multiple of 4 bytes */

   switch (ctx->ModelViewMatrixType) {
      case MATRIX_GENERAL:
         {
            const GLfloat *m = ctx->ModelViewMatrix;
            const GLfloat m0 = m[0],  m4 = m[4],  m8 = m[8],  m12 = m[12];
            const GLfloat m1 = m[1],  m5 = m[5],  m9 = m[9],  m13 = m[13];
            const GLfloat m2 = m[2],  m6 = m[6],  m10 = m[10],  m14 = m[14];
            const GLfloat m3 = m[3],  m7 = m[7],  m11 = m[11],  m15 = m[15];
            GLuint i;
            for (i=0;i<n;i++) {
               const GLfloat ox = vObj[0], oy = vObj[1], oz = vObj[2];
               vEye[i][0] = m0 * ox + m4 * oy + m8  * oz + m12;
               vEye[i][1] = m1 * ox + m5 * oy + m9  * oz + m13;
               vEye[i][2] = m2 * ox + m6 * oy + m10 * oz + m14;
               vEye[i][3] = m3 * ox + m7 * oy + m11 * oz + m15;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      case MATRIX_IDENTITY:
         {
            GLuint i;
            for (i=0;i<n;i++) {
               vEye[i][0] = vObj[0];
               vEye[i][1] = vObj[1];
               vEye[i][2] = vObj[2];
               vEye[i][3] = 1.0F;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      case MATRIX_2D:
         {
            const GLfloat *m = ctx->ModelViewMatrix;
            const GLfloat m0 = m[0], m1 = m[1], m4 = m[4], m5 = m[5];
            const GLfloat m12 = m[12], m13 = m[13];
            GLuint i;
            for (i=0;i<n;i++) {
               const GLfloat ox = vObj[0], oy = vObj[1], oz = vObj[2];
               vEye[i][0] = m0 * ox + m4 * oy            + m12       ;
               vEye[i][1] = m1 * ox + m5 * oy            + m13       ;
               vEye[i][2] =                   +       oz             ;
               vEye[i][3] =                                      1.0F;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      case MATRIX_2D_NO_ROT:
         {
            const GLfloat *m = ctx->ModelViewMatrix;
            const GLfloat m0 = m[0], m5 = m[5], m12 = m[12], m13 = m[13];
            GLuint i;
            for (i=0;i<n;i++) {
               const GLfloat ox = vObj[0], oy = vObj[1], oz = vObj[2];
               vEye[i][0] = m0 * ox                      + m12       ;
               vEye[i][1] =           m5 * oy            + m13       ;
               vEye[i][2] =                   +       oz             ;
               vEye[i][3] =                                      1.0F;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      case MATRIX_3D:
         {
            const GLfloat *m = ctx->ModelViewMatrix;
            const GLfloat m0 = m[0], m1 = m[1], m2 = m[2], m4 = m[4];
            const GLfloat m5 = m[5], m6 = m[6], m8 = m[8], m9 = m[9];
            const GLfloat m10 = m[10], m12 = m[12], m13 = m[13], m14 = m[14];
            GLuint i;
            for (i=0;i<n;i++) {
               const GLfloat ox = vObj[0], oy = vObj[1], oz = vObj[2];
               vEye[i][0] = m0 * ox + m4 * oy +  m8 * oz + m12       ;
               vEye[i][1] = m1 * ox + m5 * oy +  m9 * oz + m13       ;
               vEye[i][2] = m2 * ox + m6 * oy + m10 * oz + m14       ;
               vEye[i][3] =                                      1.0F;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      default:
         /* should never get here */
         gl_problem( NULL, "invalid matrix type in gl_transform_points3()" );
   }
}


/*
 * Use the current modelview matrix to transform XYZW vertices from object
 * to eye coordinates.
 * Input:  ctx - the context
 *         n - number of vertices to transform
 *         stride - stride in bytes between subsequent vObj vertices
 *         vObj - pointer to first vertex (in object coordinates)
 * Output;  vEye - array [n][4] of eye coordinates
 */
void
gl_transform_points4( const GLcontext *ctx, GLuint n, GLuint stride,
                      const GLfloat *vObj, GLfloat vEye[][4] )
{
   ASSERT((stride & 0x3) == 0);  /* multiple of 4 bytes */

   switch (ctx->ModelViewMatrixType) {
      case MATRIX_GENERAL:
         {
            const GLfloat *m = ctx->ModelViewMatrix;
            const GLfloat m0 = m[0],  m4 = m[4],  m8 = m[8],  m12 = m[12];
            const GLfloat m1 = m[1],  m5 = m[5],  m9 = m[9],  m13 = m[13];
            const GLfloat m2 = m[2],  m6 = m[6],  m10 = m[10],  m14 = m[14];
            const GLfloat m3 = m[3],  m7 = m[7],  m11 = m[11],  m15 = m[15];
            GLuint i;
            for (i=0;i<n;i++) {
               const GLfloat ox = vObj[0], oy = vObj[1];
               const GLfloat oz = vObj[2], ow = vObj[3];
               vEye[i][0] = m0 * ox + m4 * oy + m8  * oz + m12 * ow;
               vEye[i][1] = m1 * ox + m5 * oy + m9  * oz + m13 * ow;
               vEye[i][2] = m2 * ox + m6 * oy + m10 * oz + m14 * ow;
               vEye[i][3] = m3 * ox + m7 * oy + m11 * oz + m15 * ow;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      case MATRIX_IDENTITY:
         {
            GLuint i;
            for (i=0;i<n;i++) {
               vEye[i][0] = vObj[0];
               vEye[i][1] = vObj[1];
               vEye[i][2] = vObj[2];
               vEye[i][3] = vObj[3];
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      case MATRIX_2D:
         {
            const GLfloat *m = ctx->ModelViewMatrix;
            const GLfloat m0 = m[0], m1 = m[1], m4 = m[4], m5 = m[5];
            const GLfloat m12 = m[12], m13 = m[13];
            GLuint i;
            for (i=0;i<n;i++) {
               const GLfloat ox = vObj[0], oy = vObj[1];
               const GLfloat oz = vObj[2], ow = vObj[3];
               vEye[i][0] = m0 * ox + m4 * oy            + m12 * ow;
               vEye[i][1] = m1 * ox + m5 * oy            + m13 * ow;
               vEye[i][2] =                   +       oz           ;
               vEye[i][3] =                                      ow;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      case MATRIX_2D_NO_ROT:
         {
            const GLfloat *m = ctx->ModelViewMatrix;
            const GLfloat m0 = m[0], m5 = m[5], m12 = m[12], m13 = m[13];
            GLuint i;
            for (i=0;i<n;i++) {
               const GLfloat ox = vObj[0], oy = vObj[1];
               const GLfloat oz = vObj[2], ow = vObj[3];
               vEye[i][0] = m0 * ox                      + m12 * ow;
               vEye[i][1] =           m5 * oy            + m13 * ow;
               vEye[i][2] =                   +       oz           ;
               vEye[i][3] =                                      ow;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      case MATRIX_3D:
         {
            const GLfloat *m = ctx->ModelViewMatrix;
            const GLfloat m0 = m[0], m1 = m[1], m2 = m[2], m4 = m[4];
            const GLfloat m5 = m[5], m6 = m[6], m8 = m[8], m9 = m[9];
            const GLfloat m10 = m[10], m12 = m[12], m13 = m[13], m14 = m[14];
            GLuint i;
            for (i=0;i<n;i++) {
               const GLfloat ox = vObj[0], oy = vObj[1];
               const GLfloat oz = vObj[2], ow = vObj[3];
               vEye[i][0] = m0 * ox + m4 * oy +  m8 * oz + m12 * ow;
               vEye[i][1] = m1 * ox + m5 * oy +  m9 * oz + m13 * ow;
               vEye[i][2] = m2 * ox + m6 * oy + m10 * oz + m14 * ow;
               vEye[i][3] =                                      ow;
               vObj = (const GLfloat *) ((GLubyte *) vObj + stride);
            }
         }
         break;
      default:
         /* should never get here */
         gl_problem( NULL, "invalid matrix type in gl_transform_points4()" );
   }
}