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
|
/*
* $Id: trans.c,v 2.0 2004/11/09 12:32:35 bernhard Exp $
*/
/* NOTE: This file should be REMOVED and any calls to the functions in this
** file should be replaced with appropriate OpenGL calls.
*/
/*
** Written by Dave Gerdes Jan 1990
** Copyright Dave Gerdes 1990 All rights reserved
**
**
** Matrix Transformation library.
**
** P_pushmatrix ()
** P_popmatrix ()
** P_scale ()
** P_translate ()
** P_rot ()
** P_rotate ()
** P_transform () transform array of vectors using current T matrix
** This routine should be available in GL!
**
** Arguments are same as GL counterparts
**
** I threw this code together in January at the beginning of this
** class. I was still learning about GL at the time.
** There are many places where the code could be improved.
**
*/
#include <stdio.h>
#include <math.h>
#include "gstypes.h"
#define MAX_STACK 20
static float c_stack[MAX_STACK][4][4]; /* matrix stack */
static int stack_ptr = -1; /* index of curr matrix depth */
static float d[4][4]; /* tmp matrix */
#define NPI 3.14159265358979323846
/*
** Current transformation matrix
*/
static float trans_mat[4][4] = {
{1., 0., 0., 0.},
{0., 1., 0., 0.},
{0., 0., 1., 0.},
{0., 0., 0., 1.}
};
static float ident[4][4] = {
{1., 0., 0., 0.},
{0., 1., 0., 0.},
{0., 0., 1., 0.},
{0., 0., 0., 1.}
};
/************************************************************************/
void P_scale(float x, float y, float z)
{
d[0][0] = x;
d[0][1] = 0.;
d[0][2] = 0.;
d[0][3] = 0.;
d[1][0] = 0.;
d[1][1] = y;
d[1][2] = 0.;
d[1][3] = 0.;
d[2][0] = 0.;
d[2][1] = 0.;
d[2][2] = z;
d[2][3] = 0.;
d[3][0] = 0.;
d[3][1] = 0.;
d[3][2] = 0.;
d[3][3] = 1.;
/*
** will write into 1 down on matrix stack
** and then the popmatrix() will place it as the current T matrix
*/
P_pushmatrix();
P__transform(4, d, c_stack[stack_ptr], trans_mat);
P_popmatrix();
return;
}
/************************************************************************/
void P_translate(float x, float y, float z)
{
d[0][0] = 1.;
d[0][1] = 0.;
d[0][2] = 0.;
d[0][3] = 0.;
d[1][0] = 0.;
d[1][1] = 1.;
d[1][2] = 0.;
d[1][3] = 0.;
d[2][0] = 0.;
d[2][1] = 0.;
d[2][2] = 1.;
d[2][3] = 0.;
d[3][0] = x;
d[3][1] = y;
d[3][2] = z;
d[3][3] = 1.;
P_pushmatrix();
P__transform(4, d, c_stack[stack_ptr], trans_mat);
P_popmatrix();
return;
}
/************************************************************************/
/*
** multiply 'in' matrix (homogenous coordinate generally) by
** the current transformation matrix, placing the result in 'out'
**
** [in][trans_mat] => [out]
*/
void P_transform(int num_vert, float (*in)[4], float (*out)[4])
{
P__transform(num_vert, in, out, trans_mat);
return;
}
/************************************************************************/
void P__transform(int num_vert, float (*in)[4], float (*out)[4],
float (*c)[4])
{
register int k, j, i;
for (i = 0; i < num_vert; i++) {
for (j = 0; j < 4; j++) {
out[i][j] = 0.;
for (k = 0; k < 4; k++) {
out[i][j] += in[i][k] * c[k][j];
}
}
}
return;
}
/************************************************************************/
void P_matrix_copy(float (*from)[4], float (*to)[4], int size)
{
register int i, j;
for (i = 0; i < size; i++) {
for (j = 0; j < 4; j++) {
to[i][j] = from[i][j];
}
}
return;
}
/************************************************************************/
/*
** push current transformation matrix onto matrix stack
*/
int P_pushmatrix(void)
{
if (stack_ptr >= MAX_STACK) {
fprintf(stderr, "Out of matrix stack space\n");
return (-1);
}
stack_ptr++;
P_matrix_copy(trans_mat, c_stack[stack_ptr], 4);
return (0);
}
/************************************************************************/
/*
** pop top of matrix stack, placing it into the current transformation matrix
*/
int P_popmatrix(void)
{
if (stack_ptr < 0) {
fprintf(stderr, "Tried to pop an empty stack\n");
return (-1);
}
P_matrix_copy(c_stack[stack_ptr], trans_mat, 4);
stack_ptr--;
return (0);
}
/************************************************************************/
/* angle is expressed in tenths of degrees */
void P_rotate(int angle, char axis)
{
P_rot(angle / 10., axis);
return;
}
/************************************************************************/
void P_rot(float angle, char axis)
{
double theta;
P_matrix_copy(ident, d, 4);
theta = (NPI / 180.) * angle; /* convert to radians */
/* optimize to handle rotations of mutliples of 90 deg */
switch (axis) {
case 'X':
case 'x':
d[1][1] = cos(theta);
d[1][2] = sin(theta);
d[2][1] = -sin(theta);
d[2][2] = cos(theta);
break;
case 'Y':
case 'y':
d[0][0] = cos(theta);
d[0][2] = -sin(theta);
d[2][0] = sin(theta);
d[2][2] = cos(theta);
break;
case 'Z':
case 'z':
d[0][0] = cos(theta);
d[0][1] = sin(theta);
d[1][0] = -sin(theta);
d[1][1] = cos(theta);
break;
}
P_pushmatrix();
P__transform(4, d, c_stack[stack_ptr], trans_mat);
P_popmatrix();
return;
}
/************************************************************************/
/* angle is expressed in radians */
void P_rad_rotate(double theta, char axis)
{
P_matrix_copy(ident, d, 4);
/* optimize to handle rotations of mutliples of 90 deg */
switch (axis) {
case 'x':
d[1][1] = cos(theta);
d[1][2] = sin(theta);
d[2][1] = -sin(theta);
d[2][2] = cos(theta);
break;
case 'y':
d[0][0] = cos(theta);
d[0][2] = -sin(theta);
d[2][0] = sin(theta);
d[2][2] = cos(theta);
break;
case 'z':
d[0][0] = cos(theta);
d[0][1] = sin(theta);
d[1][0] = -sin(theta);
d[1][1] = cos(theta);
break;
}
P_pushmatrix();
P__transform(4, d, c_stack[stack_ptr], trans_mat);
P_popmatrix();
}
|