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

  1. /* $Id: vector.c,v 1.6 1999/11/08 07:36:45 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.  * New (3.1) transformation code written by Keith Whitwell.
  29.  */
  30.  
  31.  
  32. #ifndef XFree86Server
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #else
  36. #include "GL/xf86glx.h"
  37. #endif
  38. #include "config.h"
  39. #include "macros.h"
  40. #include "vector.h"
  41.  
  42. static const GLubyte elem_bits[4] = {
  43.    VEC_DIRTY_0,
  44.    VEC_DIRTY_1, 
  45.    VEC_DIRTY_2, 
  46.    VEC_DIRTY_3
  47. };
  48.  
  49. void gl_vector4f_clean_elem( GLvector4f *vec, GLuint count, GLuint elt )
  50. {
  51.    static GLfloat clean[4] = { 0, 0, 0, 1 };
  52.    GLfloat v = clean[elt];
  53.    GLfloat (*data)[4] = (GLfloat (*)[4])vec->start;
  54.    GLuint i; 
  55.  
  56.    for (i = 0 ; i < count ; i++)
  57.       data[i][elt] = v;
  58.  
  59.    vec->flags &= ~elem_bits[elt];
  60. }
  61.  
  62. static const GLubyte size_bits[5] = {
  63.    0,
  64.    VEC_SIZE_1,
  65.    VEC_SIZE_2,
  66.    VEC_SIZE_3,
  67.    VEC_SIZE_4,
  68. };
  69.  
  70. void gl_vector4f_init( GLvector4f *v, GLuint flags, GLfloat (*storage)[4] )
  71. {
  72.    v->stride = 4*sizeof(GLfloat);
  73.    v->size = 2;
  74.    v->data = storage;
  75.    v->start = (GLfloat *)storage;
  76.    v->count = 0;
  77.    v->flags = size_bits[4] | flags | VEC_GOOD_STRIDE;
  78. }
  79.  
  80.  
  81. void gl_vector3f_init( GLvector3f *v, GLuint flags, GLfloat (*storage)[3] )
  82. {
  83.    v->stride = 3*sizeof(GLfloat);
  84.    v->data = storage;
  85.    v->start = (GLfloat *)storage;
  86.    v->count = 0;
  87.    v->flags = flags | VEC_GOOD_STRIDE;
  88. }
  89.  
  90. void gl_vector4ub_init( GLvector4ub *v, GLuint flags, GLubyte (*storage)[4] )
  91. {
  92.    v->stride = 4*sizeof(GLubyte);
  93.    v->data = storage;
  94.    v->start = (GLubyte *)storage;
  95.    v->count = 0;
  96.    v->flags = flags | VEC_GOOD_STRIDE;
  97. }
  98.  
  99. void gl_vector1ub_init( GLvector1ub *v, GLuint flags, GLubyte *storage )
  100. {
  101.    v->stride = 1*sizeof(GLubyte);
  102.    v->data = storage;
  103.    v->start = (GLubyte *)storage;
  104.    v->count = 0;
  105.    v->flags = flags | VEC_GOOD_STRIDE;
  106. }
  107.  
  108. void gl_vector1ui_init( GLvector1ui *v, GLuint flags, GLuint *storage )
  109. {
  110.    v->stride = 1*sizeof(GLuint);
  111.    v->data = storage;
  112.    v->start = (GLuint *)storage;
  113.    v->count = 0;
  114.    v->flags = flags | VEC_GOOD_STRIDE;
  115. }
  116.  
  117.  
  118. #define ALIGN_MALLOC(vec, type, bytes, alignment)            \
  119. do {                                    \
  120.    vec->storage = MALLOC( bytes + alignment - 1 );            \
  121.    vec->start = type (((unsigned long)vec->storage + alignment - 1)    \
  122.                  & ~(unsigned long)(alignment - 1));    \
  123. } while (0)
  124.  
  125.  
  126. void gl_vector4f_alloc( GLvector4f *v, GLuint sz, GLuint flags, GLuint count,
  127.             GLuint alignment )
  128. {
  129.    (void) sz;
  130.    v->stride = 4*sizeof(GLfloat);
  131.    v->size = 2;
  132.    ALIGN_MALLOC(v, (GLfloat *), count * 4 * sizeof(GLfloat), alignment);
  133.    v->data = (GLfloat (*)[4])v->start;
  134.    v->count = 0;
  135.    v->flags = size_bits[4] | flags | VEC_MALLOC | VEC_GOOD_STRIDE;
  136. }
  137.  
  138.  
  139. void gl_vector3f_alloc( GLvector3f *v, GLuint flags, GLuint count,
  140.             GLuint alignment )
  141. {
  142.    v->stride = 3*sizeof(GLfloat);
  143.    ALIGN_MALLOC(v, (GLfloat *), count * 3 * sizeof(GLfloat), alignment);
  144.    v->data = (GLfloat (*)[3])v->start;
  145.    v->count = 0;
  146.    v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
  147. }
  148.  
  149. void gl_vector4ub_alloc( GLvector4ub *v, GLuint flags, GLuint count,
  150.              GLuint alignment )
  151. {
  152.    v->stride = 4*sizeof(GLubyte);
  153.    ALIGN_MALLOC(v, (GLubyte *), count * 4 * sizeof(GLubyte), alignment);
  154.    v->data = (GLubyte (*)[4])v->start;
  155.    v->count = 0;
  156.    v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
  157. }
  158.  
  159. void gl_vector1ub_alloc( GLvector1ub *v, GLuint flags, GLuint count,
  160.              GLuint alignment )
  161. {
  162.    v->stride = 1*sizeof(GLubyte);
  163.    ALIGN_MALLOC(v, (GLubyte *), count * sizeof(GLubyte), alignment);
  164.    v->data = v->start;
  165.    v->count = 0;
  166.    v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
  167. }
  168.  
  169. void gl_vector1ui_alloc( GLvector1ui *v, GLuint flags, GLuint count,
  170.              GLuint alignment )
  171. {
  172.    v->stride = 1*sizeof(GLuint);
  173.    ALIGN_MALLOC(v, (GLuint *), count * sizeof(GLuint), alignment);
  174.    v->data = v->start;
  175.    v->count = 0;
  176.    v->flags = flags | VEC_MALLOC | VEC_GOOD_STRIDE;
  177. }
  178.  
  179.  
  180.  
  181. void gl_vector4f_free( GLvector4f *v )
  182. {
  183.    if (v->flags & VEC_MALLOC) {
  184.       FREE( v->storage );
  185.       v->data = 0;
  186.       v->start = 0;
  187.       v->storage = 0;
  188.       v->flags &= ~VEC_MALLOC;
  189.    }
  190. }
  191.  
  192. void gl_vector3f_free( GLvector3f *v )
  193. {
  194.    if (v->flags & VEC_MALLOC) {
  195.       FREE( v->storage );
  196.       v->data = 0;
  197.       v->start = 0;
  198.       v->storage = 0;
  199.       v->flags &= ~VEC_MALLOC;
  200.    }
  201. }
  202.  
  203. void gl_vector4ub_free( GLvector4ub *v )
  204. {
  205.    if (v->flags & VEC_MALLOC) {
  206.       FREE( v->storage );
  207.       v->data = 0;
  208.       v->start = 0;
  209.       v->storage = 0;
  210.       v->flags &= ~VEC_MALLOC;
  211.    }
  212. }
  213.  
  214. void gl_vector1ub_free( GLvector1ub *v )
  215. {
  216.    if (v->flags & VEC_MALLOC) {
  217.       FREE( v->storage );
  218.       v->data = 0;
  219.       v->start = 0;
  220.       v->storage = 0;
  221.       v->flags &= ~VEC_MALLOC;
  222.    }
  223. }
  224.  
  225. void gl_vector1ui_free( GLvector1ui *v )
  226. {
  227.    if (v->flags & VEC_MALLOC) {
  228.       FREE( v->storage );
  229.       v->data = 0;
  230.       v->start = 0;
  231.       v->storage = 0;
  232.       v->flags &= ~VEC_MALLOC;
  233.    }
  234. }
  235.  
  236. void gl_vector4f_print( GLvector4f *v, GLubyte *cullmask, GLboolean culling )
  237. {
  238.    GLfloat c[4] = { 0, 0, 0, 1 };
  239.    const char *templates[5] = {
  240.       "%d:\t0, 0, 0, 1\n",
  241.       "%d:\t%f, 0, 0, 1\n",
  242.       "%d:\t%f, %f, 0, 1\n",
  243.       "%d:\t%f, %f, %f, 1\n",
  244.       "%d:\t%f, %f, %f, %f\n"
  245.    };
  246.  
  247.    const char *t = templates[v->size];
  248.    GLfloat *d = (GLfloat *)v->data;
  249.    GLuint j, i = 0, count;
  250.  
  251.    printf("data-start\n");
  252.    for ( ; d != v->start ; STRIDE_F(d, v->stride), i++) 
  253.       printf( t, i, d[0], d[1], d[2], d[3]);
  254.    
  255.    printf("start-count(%u)\n", v->count);
  256.    count = i + v->count;
  257.  
  258.    if (culling) {
  259.       for ( ; i < count ; STRIDE_F(d, v->stride), i++) 
  260.      if (cullmask[i]) 
  261.         printf( t, i, d[0], d[1], d[2], d[3]);      
  262.    } else {
  263.       for ( ; i < count ; STRIDE_F(d, v->stride), i++) 
  264.      printf( t, i, d[0], d[1], d[2], d[3]);      
  265.    }
  266.  
  267.    for (j = v->size ; j < 4; j++) {
  268.       if ((v->flags & (1<<j)) == 0) {
  269.  
  270.      printf("checking col %u is clean as advertised ", j);
  271.  
  272.      for (i = 0, d = (GLfloat *) v->data ; 
  273.           i < count && d[j] == c[j] ; 
  274.           i++, STRIDE_F(d, v->stride)) {};
  275.  
  276.      if (i == count) 
  277.         printf(" --> ok\n");
  278.      else 
  279.         printf(" --> Failed at %u ******\n", i);
  280.       }
  281.    }
  282. }
  283.  
  284.  
  285. void gl_vector3f_print( GLvector3f *v, GLubyte *cullmask, GLboolean culling )
  286. {
  287.    GLfloat *d = (GLfloat *)v->data;
  288.    GLuint i = 0, count;
  289.  
  290.    printf("data-start\n");
  291.    for ( ; d != v->start ; STRIDE_F(d,v->stride), i++) 
  292.       printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);
  293.    
  294.    printf("start-count(%u)\n", v->count);
  295.    count = i + v->count;
  296.  
  297.    if (culling) {
  298.       for ( ; i < count ; STRIDE_F(d,v->stride), i++) 
  299.      if (cullmask[i])
  300.         printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);      
  301.    } else {
  302.       for ( ; i < count ; STRIDE_F(d,v->stride), i++) 
  303.      printf( "%u:\t%f, %f, %f\n", i, d[0], d[1], d[2]);      
  304.    }
  305. }
  306.