rotation d'objet

This commit is contained in:
Frater 2026-05-11 17:53:07 +02:00
commit 69b06b5b14
3 changed files with 108 additions and 6 deletions

View file

@ -34,14 +34,71 @@ void mat3_rotate_x(Matrix3 *m, int angle) {
int a = ((angle % 360) + 360) % 360;
fixed c = costab[a], s = sintab[a];
mat3_identity(m);
m->m[1][1] = c; m->m[1][2] = -s; m->m[2][1] = s; m->m[2][2] = c;
m->m[1][1] = c;
m->m[1][2] = -s;
m->m[2][1] = s;
m->m[2][2] = c;
}
void mat3_rotate_y(Matrix3 *m, int angle) {
int a = ((angle % 360) + 360) % 360;
fixed c = costab[a], s = sintab[a];
mat3_identity(m);
m->m[0][0] = c; m->m[0][2] = s; m->m[2][0] = -s; m->m[2][2] = c;
m->m[0][0] = c;
m->m[0][2] = s;
m->m[2][0] = -s;
m->m[2][2] = c;
}
void mat3_rotate_z(Matrix3 *m, int angle) {
int a = ((angle % 360) + 360) % 360;
fixed c = costab[a], s = sintab[a];
mat3_identity(m);
m->m[0][0] = c;
m->m[0][1] = s;
m->m[1][0] = -s;
m->m[1][1] = c;
}
/* Fait pivoter la géométrie locale de l'objet de façon permanente */
void bake_object_rotation(Universe *uni, Object3D *obj, int ax, int ay, int az) {
Matrix3 rx, ry, rz, tmp, rot;
Vector3 *v, *n;
int i, vi;
fixed vx, vy, vz;
if (!obj || !uni) return;
/* Composition de la matrice de rotation */
mat3_rotate_x(&rx, ax);
mat3_rotate_y(&ry, ay);
mat3_rotate_z(&rz, az);
mat3_mul(&tmp, &rx, &ry);
mat3_mul(&rot, &tmp, &rz);
/* Application à tous les sommets et normales de l'objet */
for (i = 0; i < obj->num_verts; i++) {
vi = obj->vert_start + i;
v = &uni->verts[vi];
n = &uni->v_normals[vi];
/* Sommets */
vx = f_mul(v->x, rot.m[0][0]) + f_mul(v->y, rot.m[1][0]) + f_mul(v->z, rot.m[2][0]);
vy = f_mul(v->x, rot.m[0][1]) + f_mul(v->y, rot.m[1][1]) + f_mul(v->z, rot.m[2][1]);
vz = f_mul(v->x, rot.m[0][2]) + f_mul(v->y, rot.m[1][2]) + f_mul(v->z, rot.m[2][2]);
v->x = vx;
v->y = vy;
v->z = vz;
/* Normales (doivent aussi pivoter pour l'ombrage Gouraud !) */
vx = f_mul(n->x, rot.m[0][0]) + f_mul(n->y, rot.m[1][0]) + f_mul(n->z, rot.m[2][0]);
vy = f_mul(n->x, rot.m[0][1]) + f_mul(n->y, rot.m[1][1]) + f_mul(n->z, rot.m[2][1]);
vz = f_mul(n->x, rot.m[0][2]) + f_mul(n->y, rot.m[1][2]) + f_mul(n->z, rot.m[2][2]);
n->x = vx;
n->y = vy;
n->z = vz;
}
}
void render_universe(Universe *uni, Camera *cam, int mode) {