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