1 /* $Id: m_vector.c,v 1.3 2001/01/24 00:04:59 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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
;
91 void gl_vector3f_init( GLvector3f
*v
, GLuint flags
, GLfloat (*storage
)[3] )
93 v
->stride
= 3 * sizeof(GLfloat
);
95 v
->start
= (GLfloat
*) storage
;
100 void gl_vector1f_init( GLvector1f
*v
, GLuint flags
, GLfloat
*storage
)
102 v
->stride
= 1*sizeof(GLfloat
);
104 v
->start
= (GLfloat
*)storage
;
109 void gl_vector4ub_init( GLvector4ub
*v
, GLuint flags
, GLubyte (*storage
)[4] )
111 v
->stride
= 4 * sizeof(GLubyte
);
113 v
->start
= (GLubyte
*) storage
;
118 void gl_vector4us_init( GLvector4us
*v
, GLuint flags
, GLushort (*storage
)[4] )
120 v
->stride
= 4 * sizeof(GLushort
);
122 v
->start
= (GLushort
*) storage
;
127 void gl_vector1ub_init( GLvector1ub
*v
, GLuint flags
, GLubyte
*storage
)
129 v
->stride
= 1 * sizeof(GLubyte
);
131 v
->start
= (GLubyte
*) storage
;
136 void gl_vector1ui_init( GLvector1ui
*v
, GLuint flags
, GLuint
*storage
)
138 v
->stride
= 1 * sizeof(GLuint
);
140 v
->start
= (GLuint
*) storage
;
147 * Initialize GLvector objects and allocate storage.
148 * Input: v - the vector object
150 * flags - bitwise-OR of VEC_* flags
151 * count - number of elements to allocate in vector
152 * alignment - desired memory alignment for the data (in bytes)
156 void gl_vector4f_alloc( GLvector4f
*v
, GLuint flags
, GLuint count
,
159 v
->stride
= 4 * sizeof(GLfloat
);
161 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLfloat
), alignment
);
162 v
->start
= (GLfloat
*) v
->storage
;
163 v
->data
= (GLfloat (*)[4]) v
->storage
;
165 v
->flags
= size_bits
[4] | flags
| VEC_MALLOC
;
168 void gl_vector3f_alloc( GLvector3f
*v
, GLuint flags
, GLuint count
,
171 v
->stride
= 3 * sizeof(GLfloat
);
172 v
->storage
= ALIGN_MALLOC( count
* 3 * sizeof(GLfloat
), alignment
);
173 v
->start
= (GLfloat
*) v
->storage
;
174 v
->data
= (GLfloat (*)[3]) v
->storage
;
176 v
->flags
= flags
| VEC_MALLOC
;
179 void gl_vector1f_alloc( GLvector1f
*v
, GLuint flags
, GLuint count
,
182 v
->stride
= sizeof(GLfloat
);
183 v
->storage
= v
->start
= (GLfloat
*)
184 ALIGN_MALLOC( count
* sizeof(GLfloat
), alignment
);
187 v
->flags
= flags
| VEC_MALLOC
;
190 void gl_vector4ub_alloc( GLvector4ub
*v
, GLuint flags
, GLuint count
,
193 v
->stride
= 4 * sizeof(GLubyte
);
194 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLubyte
), alignment
);
195 v
->start
= (GLubyte
*) v
->storage
;
196 v
->data
= (GLubyte (*)[4]) v
->storage
;
198 v
->flags
= flags
| VEC_MALLOC
;
201 void gl_vector4us_alloc( GLvector4us
*v
, GLuint flags
, GLuint count
,
204 v
->stride
= 4 * sizeof(GLushort
);
205 v
->storage
= ALIGN_MALLOC( count
* 4 * sizeof(GLushort
), alignment
);
206 v
->start
= (GLushort
*) v
->storage
;
207 v
->data
= (GLushort (*)[4]) v
->storage
;
209 v
->flags
= flags
| VEC_MALLOC
;
212 void gl_vector1ub_alloc( GLvector1ub
*v
, GLuint flags
, GLuint count
,
215 v
->stride
= 1 * sizeof(GLubyte
);
216 v
->storage
= ALIGN_MALLOC( count
* sizeof(GLubyte
), alignment
);
217 v
->start
= (GLubyte
*) v
->storage
;
218 v
->data
= (GLubyte
*) v
->storage
;
220 v
->flags
= flags
| VEC_MALLOC
;
223 void gl_vector1ui_alloc( GLvector1ui
*v
, GLuint flags
, GLuint count
,
226 v
->stride
= 1 * sizeof(GLuint
);
227 v
->storage
= ALIGN_MALLOC( count
* sizeof(GLuint
), alignment
);
228 v
->start
= (GLuint
*) v
->storage
;
229 v
->data
= (GLuint
*) v
->storage
;
231 v
->flags
= flags
| VEC_MALLOC
;
237 * Vector deallocation. Free whatever memory is pointed to by the
238 * vector's storage field if the VEC_MALLOC flag is set.
239 * DO NOT free the GLvector object itself, though.
243 void gl_vector4f_free( GLvector4f
*v
)
245 if (v
->flags
& VEC_MALLOC
) {
246 ALIGN_FREE( v
->storage
);
250 v
->flags
&= ~VEC_MALLOC
;
254 void gl_vector3f_free( GLvector3f
*v
)
256 if (v
->flags
& VEC_MALLOC
) {
257 ALIGN_FREE( v
->storage
);
261 v
->flags
&= ~VEC_MALLOC
;
265 void gl_vector1f_free( GLvector1f
*v
)
267 if (v
->flags
& VEC_MALLOC
) {
268 ALIGN_FREE( v
->storage
);
272 v
->flags
&= ~VEC_MALLOC
;
276 void gl_vector4ub_free( GLvector4ub
*v
)
278 if (v
->flags
& VEC_MALLOC
) {
279 ALIGN_FREE( v
->storage
);
283 v
->flags
&= ~VEC_MALLOC
;
287 void gl_vector4us_free( GLvector4us
*v
)
289 if (v
->flags
& VEC_MALLOC
) {
290 ALIGN_FREE( v
->storage
);
294 v
->flags
&= ~VEC_MALLOC
;
298 void gl_vector1ub_free( GLvector1ub
*v
)
300 if (v
->flags
& VEC_MALLOC
) {
301 ALIGN_FREE( v
->storage
);
305 v
->flags
&= ~VEC_MALLOC
;
309 void gl_vector1ui_free( GLvector1ui
*v
)
311 if (v
->flags
& VEC_MALLOC
) {
312 ALIGN_FREE( v
->storage
);
316 v
->flags
&= ~VEC_MALLOC
;
324 void gl_vector4f_print( GLvector4f
*v
, GLubyte
*cullmask
, GLboolean culling
)
326 GLfloat c
[4] = { 0, 0, 0, 1 };
327 const char *templates
[5] = {
329 "%d:\t%f, 0, 0, 1\n",
330 "%d:\t%f, %f, 0, 1\n",
331 "%d:\t%f, %f, %f, 1\n",
332 "%d:\t%f, %f, %f, %f\n"
335 const char *t
= templates
[v
->size
];
336 GLfloat
*d
= (GLfloat
*)v
->data
;
337 GLuint j
, i
= 0, count
;
339 printf("data-start\n");
340 for ( ; d
!= v
->start
; STRIDE_F(d
, v
->stride
), i
++)
341 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
343 printf("start-count(%u)\n", v
->count
);
344 count
= i
+ v
->count
;
347 for ( ; i
< count
; STRIDE_F(d
, v
->stride
), i
++)
349 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
352 for ( ; i
< count
; STRIDE_F(d
, v
->stride
), i
++)
353 printf( t
, i
, d
[0], d
[1], d
[2], d
[3]);
356 for (j
= v
->size
; j
< 4; j
++) {
357 if ((v
->flags
& (1<<j
)) == 0) {
359 printf("checking col %u is clean as advertised ", j
);
361 for (i
= 0, d
= (GLfloat
*) v
->data
;
362 i
< count
&& d
[j
] == c
[j
] ;
363 i
++, STRIDE_F(d
, v
->stride
)) {};
368 printf(" --> Failed at %u ******\n", i
);
377 void gl_vector3f_print( GLvector3f
*v
, GLubyte
*cullmask
, GLboolean culling
)
379 GLfloat
*d
= (GLfloat
*)v
->data
;
382 printf("data-start\n");
383 for ( ; d
!= v
->start
; STRIDE_F(d
,v
->stride
), i
++)
384 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);
386 printf("start-count(%u)\n", v
->count
);
387 count
= i
+ v
->count
;
390 for ( ; i
< count
; STRIDE_F(d
,v
->stride
), i
++)
392 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);
395 for ( ; i
< count
; STRIDE_F(d
,v
->stride
), i
++)
396 printf( "%u:\t%f, %f, %f\n", i
, d
[0], d
[1], d
[2]);