running ok

This commit is contained in:
Frater 2026-05-10 11:49:01 +02:00
commit 8e30b7692f
8 changed files with 371 additions and 356 deletions

View file

@ -4,36 +4,28 @@ fixed sintab[360], costab[360];
Point2D v_cache[MAX_VERTEX];
void mat3_identity(Matrix3 *mat) {
memset(mat, 0, sizeof(Matrix3));
mat->m[0][0] = mat->m[1][1] = mat->m[2][2] = int_to_f(1);
memset(mat, 0, sizeof(Matrix3));
mat->m[0][0] = mat->m[1][1] = mat->m[2][2] = int_to_f(1);
}
void init_engine_math() {
int i;
for (i = 0; i < 360; i++) {
double r = (double)i * 3.14159265 / 180.0;
sintab[i] = (fixed)(sin(r) * 65536.0);
costab[i] = (fixed)(cos(r) * 65536.0);
}
}
void normalize(Vector3 *v) {
double dx = (double)v->x, dy = (double)v->y, dz = (double)v->z;
double d = sqrt(dx*dx + dy*dy + dz*dz);
if (d > 0) {
v->x = (fixed)((dx/d)*65536.0); v->y = (fixed)((dy/d)*65536.0); v->z = (fixed)((dz/d)*65536.0);
}
void init_engine_math(void) {
int i;
for (i = 0; i < 360; i++) {
double r = (double)i * 3.14159265 / 180.0;
sintab[i] = (fixed)(sin(r) * 65536.0);
costab[i] = (fixed)(cos(r) * 65536.0);
}
}
void mat3_mul(Matrix3 *res, Matrix3 *a, Matrix3 *b) {
Matrix3 tmp;
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
res->m[i][j] = f_mul(a->m[i][0], b->m[0][j]) +
f_mul(a->m[i][1], b->m[1][j]) +
f_mul(a->m[i][2], b->m[2][j]);
}
}
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
tmp.m[i][j] = f_mul(a->m[i][0], b->m[0][j]) +
f_mul(a->m[i][1], b->m[1][j]) +
f_mul(a->m[i][2], b->m[2][j]);
*res = tmp;
}
void mat3_rotate_x(Matrix3 *m, int angle) {
@ -50,36 +42,45 @@ void mat3_rotate_y(Matrix3 *m, int angle) {
m->m[0][0] = c; m->m[0][2] = s; m->m[2][0] = -s; m->m[2][2] = c;
}
void render_universe(Object3D *objs, int num_objs, Camera *cam, int mode) {
int o, i, front;
void render_universe(Universe *uni, Camera *cam, int mode) {
int o, i, vi, front;
fixed rx, ry, rz, pz, nz;
uint8_t wire_col;
Vector3 *v, *n;
Object3D *obj;
Face *f;
Point2D *p1, *p2, *p3;
for (o = 0; o < num_objs; o++) {
Object3D *obj = &objs[o];
Mesh *m = obj->mesh;
for (o = 0; o < uni->num_objects; o++) {
obj = &uni->objects[o];
for (i = 0; i < m->num_verts; i++) {
Vector3 *v = &m->verts[i], *n = &m->v_normals[i];
/* Transformation + projection de tous les sommets de cet objet */
for (i = 0; i < obj->num_verts; i++) {
vi = obj->vert_start + i;
v = &uni->verts[vi];
n = &uni->v_normals[vi];
rx = f_mul(v->x, obj->rot.m[0][0]) + f_mul(v->y, obj->rot.m[1][0]) + f_mul(v->z, obj->rot.m[2][0]) + obj->pos.x - cam->pos.x;
ry = f_mul(v->x, obj->rot.m[0][1]) + f_mul(v->y, obj->rot.m[1][1]) + f_mul(v->z, obj->rot.m[2][1]) + obj->pos.y - cam->pos.y;
rz = f_mul(v->x, obj->rot.m[0][2]) + f_mul(v->y, obj->rot.m[1][2]) + f_mul(v->z, obj->rot.m[2][2]) + obj->pos.z - cam->pos.z;
pz = (rz < int_to_f(20)) ? int_to_f(20) : rz;
v_cache[i].z = pz;
v_cache[i].x = 160 + f_to_int(f_div(rx << 8, pz));
v_cache[i].y = 100 - f_to_int(f_div(ry << 8, pz));
v_cache[vi].z = pz;
v_cache[vi].x = 160 + f_to_int(f_div(rx << 8, pz));
v_cache[vi].y = 100 - f_to_int(f_div(ry << 8, pz));
nz = f_mul(n->x, obj->rot.m[0][2]) + f_mul(n->y, obj->rot.m[1][2]) + f_mul(n->z, obj->rot.m[2][2]);
v_cache[i].intensity = (nz >= 0) ? int_to_f(1) : ((-nz) * 14) + int_to_f(1);
v_cache[vi].intensity = (nz >= 0) ? int_to_f(1) : f_mul(-nz, int_to_f(14)) + int_to_f(1);
}
wire_col = (uint8_t)((obj->color << 4) | 15);
for (i = 0; i < m->num_faces; i++) {
Face *f = &m->faces[i];
Point2D *p1 = &v_cache[f->a], *p2 = &v_cache[f->b], *p3 = &v_cache[f->c];
/* Rendu des faces */
for (i = 0; i < obj->num_faces; i++) {
f = &uni->faces[obj->face_start + i];
p1 = &v_cache[f->a];
p2 = &v_cache[f->b];
p3 = &v_cache[f->c];
front = ((p2->x - p1->x) * (p3->y - p1->y) - (p2->y - p1->y) * (p3->x - p1->x)) > 0;
switch (mode) {