1 /* $Id: m_vector.c,v 1.1 2000/11/16 21:05:41 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
28 * New (3.1) transformation code written by Keith Whitwell.
41 * Given a vector [count][4] of floats, set all the [][elt] values
42 * to 0 (if elt = 0, 1, 2) or 1.0 (if elt = 3).
44 void gl_vector4f_clean_elem( GLvector4f
*vec
, GLuint count
, GLuint elt
)
46 static const GLubyte elem_bits
[4] = {
52 static const GLfloat clean
[4] = { 0, 0, 0, 1 };
53 const GLfloat v
= clean
[elt
];
54 GLfloat (*data
)[4] = (GLfloat (*)[4])vec
->start
;
57 for (i
= 0 ; i
< count
; i
++)
60 vec
->flags
&= ~elem_bits
[elt
];
63 static const GLubyte size_bits
[5] = {
74 * Initialize GLvector objects.
75 * Input: v - the vector object to initialize.
76 * flags - bitwise-OR of VEC_* flags
77 * storage - pointer to storage for the vector's data
81 void gl_vector4f_init( GLvector4f
*v
, GLuint flags
, GLfloat (*storage
)[4] )
83 v
->stride
= 4 * sizeof(GLfloat
);
84 v
->size
= 2; /* may change: 2-4 for vertices and 1-4 for texcoords */
86 v
->start
= (GLfloat
*) storage
;
88 v
->flags
= size_bits
[4] | flags
| VEC_GOOD_STRIDE
;
91 void gl_vector3f_init( GLvector3f
*v
, GLuint flags
, GLfloat (*storage
)[3] )
93 v
->stride
= 3 * sizeof(GLfloat
);
95 v
->start
= (GLfloat
*) storage
;
97 v
->flags
= flags
| VEC_GOOD_STRIDE
;
100 void gl_vector1f_init( GLvector1f
*v
, GLuint flags
, GLfloat
*storage
)
102 v
->stride
= 1*sizeof(GLfloat
);
104 v
->start
= (GLfloat
*)storage
;
106 v
->flags
= flags
| VEC_GOOD_STRIDE
;
109 void gl_vector4ub_init( GLvector4ub
*v
, GLuint flags
, GLubyte (*storage
)[4] )
111 v
->stride
= 4 * sizeof(GLubyte
);
113 v
->start
= (GLubyte
*) storage
;
115 v
->flags
= flags
| VEC_GOOD_STRIDE
;
118 void gl_vector1ub_init( GLvector1ub
*v
, GLuint flags
, GLubyte
*storage
)
120 v
->stride
= 1 * sizeof(GLubyte
);
122 v
->start
= (GLubyte
*) storage
;
124 v
->flags
= flags
| VEC_GOOD_STRIDE
;
127 void gl_vector1ui_init( GLvector1ui
*v
, GLuint flags
, GLuint
*storage
)
129 v
->stride
= 1 * sizeof(GLuint
);
131 v
->start
= (GLuint
*) storage
;
133 v
->flags
= flags
| VEC_GOOD_STRIDE
;
138 * Initialize GLvector objects and allocate storage.
139 * Input: v - the vector object
141 * flags - bitwise-OR of VEC_* flags
142 * count - number of elements to allocate in vector
143 * alignment - desired memory alignment for the data (in bytes)
147 void gl_vector4f_alloc( GLvector4f
*v
, GLuint flags
, GLuint count
,
150 v
->stride
= 4 * sizeof(GLfloat
);
152 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLfloat
), alignment
);
153 v
->start
= (GLfloat
*) v
->storage
;
154 v
->data
= (GLfloat (*)[4]) v
->storage
;
156 v
->flags
= size_bits
[4] | flags
| VEC_MALLOC
| VEC_GOOD_STRIDE
;
159 void gl_vector3f_alloc( GLvector3f
*v
, GLuint flags
, GLuint count
,
162 v
->stride
= 3 * sizeof(GLfloat
);
163 v
->storage
= ALIGN_MALLOC( count
* 3 * sizeof(GLfloat
), alignment
);
164 v
->start
= (GLfloat
*) v
->storage
;
165 v
->data
= (GLfloat (*)[3]) v
->storage
;
167 v
->flags
= flags
| VEC_MALLOC
| VEC_GOOD_STRIDE
;
170 void gl_vector1f_alloc( GLvector1f
*v
, GLuint flags
, GLuint count
,
173 v
->stride
= sizeof(GLfloat
);
174 v
->storage
= v
->start
= (GLfloat
*)
175 ALIGN_MALLOC( count
* sizeof(GLfloat
), alignment
);
178 v
->flags
= flags
| VEC_MALLOC
| VEC_GOOD_STRIDE
;
181 void gl_vector4ub_alloc( GLvector4ub
*v
, GLuint flags
, GLuint count
,
184 v
->stride
= 4 * sizeof(GLubyte
);
185 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLubyte
), alignment
);
186 v
->start
= (GLubyte
*) v
->storage
;
187 v
->data
= (GLubyte (*)[4]) v
->storage
;
189 v
->flags
= flags
| VEC_MALLOC
| VEC_GOOD_STRIDE
;
192 void gl_vector1ub_alloc( GLvector1ub
*v
, GLuint flags
, GLuint count
,
195 v
->stride
= 1 * sizeof(GLubyte
);
196 v
->storage
= ALIGN_MALLOC( count
* sizeof(GLubyte
), alignment
);
197 v
->start
= (GLubyte
*) v
->storage
;
198 v
->data
= (GLubyte
*) v
->storage
;
200 v
->flags
= flags
| VEC_MALLOC
| VEC_GOOD_STRIDE
;
203 void gl_vector1ui_alloc( GLvector1ui
*v
, GLuint flags
, GLuint count
,
206 v
->stride
= 1 * sizeof(GLuint
);
207 v
->storage
= ALIGN_MALLOC( count
* sizeof(GLuint
), alignment
);
208 v
->start
= (GLuint
*) v
->storage
;
209 v
->data
= (GLuint
*) v
->storage
;
211 v
->flags
= flags
| VEC_MALLOC
| VEC_GOOD_STRIDE
;
217 * Vector deallocation. Free whatever memory is pointed to by the
218 * vector's storage field if the VEC_MALLOC flag is set.
219 * DO NOT free the GLvector object itself, though.
223 void gl_vector4f_free( GLvector4f
*v
)
225 if (v
->flags
& VEC_MALLOC
) {
226 ALIGN_FREE( v
->storage
);
230 v
->flags
&= ~VEC_MALLOC
;
234 void gl_vector3f_free( GLvector3f
*v
)
236 if (v
->flags
& VEC_MALLOC
) {
237 ALIGN_FREE( v
->storage
);
241 v
->flags
&= ~VEC_MALLOC
;
245 void gl_vector1f_free( GLvector1f
*v
)
247 if (v
->flags
& VEC_MALLOC
) {
248 ALIGN_FREE( v
->storage
);
252 v
->flags
&= ~VEC_MALLOC
;
256 void gl_vector4ub_free( GLvector4ub
*v
)
258 if (v
->flags
& VEC_MALLOC
) {
259 ALIGN_FREE( v
->storage
);
263 v
->flags
&= ~VEC_MALLOC
;
267 void gl_vector1ub_free( GLvector1ub
*v
)
269 if (v
->flags
& VEC_MALLOC
) {
270 ALIGN_FREE( v
->storage
);
274 v
->flags
&= ~VEC_MALLOC
;
278 void gl_vector1ui_free( GLvector1ui
*v
)
280 if (v
->flags
& VEC_MALLOC
) {
281 ALIGN_FREE( v
->storage
);
285 v
->flags
&= ~VEC_MALLOC
;
293 void gl_vector4f_print( GLvector4f
*v
, GLubyte
*cullmask
, GLboolean culling
)
295 GLfloat c
[4] = { 0, 0, 0, 1 };
296 const char *templates
[5] = {
298 "%d:\t%f, 0, 0, 1\n",
299 "%d:\t%f, %f, 0, 1\n",
300 "%d:\t%f, %f, %f, 1\n",
301 "%d:\t%f, %f, %f, %f\n"
304 const char *t
= templates
[v
->size
];
305 GLfloat
*d
= (GLfloat
*)v
->data
;
306 GLuint j
, i
= 0, count
;
308 printf("data-start\n");
309 for ( ; d
!= v
->start
; STRIDE_F(d
, v
->stride
), i
++)
310 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
312 printf("start-count(%u)\n", v
->count
);
313 count
= i
+ v
->count
;
316 for ( ; i
< count
; STRIDE_F(d
, v
->stride
), i
++)
318 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
321 for ( ; i
< count
; STRIDE_F(d
, v
->stride
), i
++)
322 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
325 for (j
= v
->size
; j
< 4; j
++) {
326 if ((v
->flags
& (1<<j
)) == 0) {
328 printf("checking col %u is clean as advertised ", j
);
330 for (i
= 0, d
= (GLfloat
*) v
->data
;
331 i
< count
&& d
[j
] == c
[j
] ;
332 i
++, STRIDE_F(d
, v
->stride
)) {};
337 printf(" --> Failed at %u ******\n", i
);
346 void gl_vector3f_print( GLvector3f
*v
, GLubyte
*cullmask
, GLboolean culling
)
348 GLfloat
*d
= (GLfloat
*)v
->data
;
351 printf("data-start\n");
352 for ( ; d
!= v
->start
; STRIDE_F(d
,v
->stride
), i
++)
353 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);
355 printf("start-count(%u)\n", v
->count
);
356 count
= i
+ v
->count
;
359 for ( ; i
< count
; STRIDE_F(d
,v
->stride
), i
++)
361 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);
364 for ( ; i
< count
; STRIDE_F(d
,v
->stride
), i
++)
365 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);