This commit is contained in:
Frater 2026-05-10 02:29:00 +02:00
commit c6d6f787f7
8 changed files with 667 additions and 485 deletions

View file

@ -9,72 +9,106 @@ void add_face(Mesh *m, int a, int b, int c, uint8_t col, uint8_t wire) {
f->force_wire = wire;
}
/* Génère une sphère avec des normales unitaires */
void add_sphere(Mesh *m, fixed x, fixed y, fixed z, fixed r, int det, uint8_t col, uint8_t wire) {
int i, j, v_base;
v_base = m->num_verts;
int i, j, v_base = m->num_verts;
for (j = 0; j <= det; j++) {
fixed phi = (j * 180 / det) % 360;
for (i = 0; i < det; i++) {
fixed th = (i * 360 / det) % 360;
Vector3 *v = &m->verts[m->num_verts];
Vector3 *n = &m->v_normals[m->num_verts];
for (j = 0; j <= det; j++) {
fixed sin_phi = sintab[(j * 180 / det) % 360];
fixed cos_phi = costab[(j * 180 / det) % 360];
for (i = 0; i < det; i++) {
Vector3 *v = &m->verts[m->num_verts];
Vector3 *n = &m->v_normals[m->num_verts];
fixed sin_th = sintab[(i * 360 / det) % 360];
fixed cos_th = costab[(i * 360 / det) % 360];
n->x = f_mul(sintab[phi], costab[th]);
n->y = costab[phi];
n->z = f_mul(sintab[phi], sintab[th]);
v->x = x + f_mul(r, f_mul(sin_phi, cos_th));
v->y = y + f_mul(r, cos_phi);
v->z = z + f_mul(r, f_mul(sin_phi, sin_th));
n->x = f_mul(sin_phi, cos_th);
n->y = cos_phi;
n->z = f_mul(sin_phi, sin_th);
m->num_verts++;
}
}
for (j = 0; j < det; j++) {
for (i = 0; i < det; i++) {
int a = v_base + j * det + i;
int b = v_base + j * det + (i + 1) % det;
int c = v_base + (j + 1) * det + i;
int d = v_base + (j + 1) * det + (i + 1) % det;
add_face(m, a, b, c, col, wire);
add_face(m, b, d, c, col, wire);
}
}
v->x = x + f_mul(r, n->x);
v->y = y + f_mul(r, n->y);
v->z = z + f_mul(r, n->z);
m->num_verts++;
}
}
for (j = 0; j < det; j++) {
for (i = 0; i < det; i++) {
int a = v_base + j * det + i;
int b = v_base + j * det + (i + 1) % det;
int c = v_base + (j + 1) * det + i;
int d = v_base + (j + 1) * det + (i + 1) % det;
add_face(m, a, b, c, col, wire);
add_face(m, b, d, c, col, wire);
}
}
}
/* Génère un cylindre fermé (corps + fonds) avec normales et winding corrects */
void add_cylinder(Mesh *m, fixed x, fixed y, fixed z, fixed r, fixed h, int det, uint8_t col, uint8_t wire) {
int i, v_base;
v_base = m->num_verts;
int i, v_base = m->num_verts;
int cap_bot_center, cap_bot_rim, cap_top_center, cap_top_rim;
fixed half_h = h / 2;
fixed th, s, c;
for (i = 0; i < det; i++) {
Vector3 *v1 = &m->verts[m->num_verts];
Vector3 *n1 = &m->v_normals[m->num_verts];
fixed s = sintab[(i * 360 / det) % 360];
fixed c = costab[(i * 360 / det) % 360];
/* --- Corps : paires bas/haut, normales radiales --- */
for (i = 0; i < det; i++) {
th = (i * 360 / det) % 360;
s = sintab[th]; c = costab[th];
v1->x = x + f_mul(r, c); v1->y = y - h / 2; v1->z = z + f_mul(r, s);
n1->x = c; n1->y = 0; n1->z = s;
m->num_verts++;
m->v_normals[m->num_verts].x = c; m->v_normals[m->num_verts].y = 0; m->v_normals[m->num_verts].z = s;
m->verts[m->num_verts].x = x + f_mul(r, c); m->verts[m->num_verts].y = y - half_h; m->verts[m->num_verts].z = z + f_mul(r, s);
m->num_verts++;
{ // Bloc pour v2/n2
Vector3 *v2 = &m->verts[m->num_verts];
Vector3 *n2 = &m->v_normals[m->num_verts];
v2->x = x + f_mul(r, c); v2->y = y + h / 2; v2->z = z + f_mul(r, s);
n2->x = c; n2->y = 0; n2->z = s;
m->num_verts++;
}
}
for (i = 0; i < det; i++) {
int a = v_base + i * 2;
int b = v_base + ((i * 2 + 2) % (det * 2));
int c = a + 1;
int d = b + 1;
add_face(m, a, b, c, col, wire);
add_face(m, b, d, c, col, wire);
}
m->v_normals[m->num_verts].x = c; m->v_normals[m->num_verts].y = 0; m->v_normals[m->num_verts].z = s;
m->verts[m->num_verts].x = x + f_mul(r, c); m->verts[m->num_verts].y = y + half_h; m->verts[m->num_verts].z = z + f_mul(r, s);
m->num_verts++;
}
/* Faces corps (winding CCW = cross > 0 pour la face avant) */
for (i = 0; i < det; i++) {
int a = v_base + (i * 2);
int b = v_base + ((i * 2 + 2) % (det * 2));
int c_v = a + 1;
int d = b + 1;
add_face(m, a, c_v, b, col, wire); /* bas_i, haut_i, bas_{i+1} */
add_face(m, c_v, d, b, col, wire); /* haut_i, haut_{i+1}, bas_{i+1} */
}
/* --- Fond bas : centre + rebord, normale (0,-1,0) --- */
cap_bot_center = m->num_verts;
m->v_normals[m->num_verts].x = 0; m->v_normals[m->num_verts].y = -int_to_f(1); m->v_normals[m->num_verts].z = 0;
m->verts[m->num_verts].x = x; m->verts[m->num_verts].y = y - half_h; m->verts[m->num_verts].z = z;
m->num_verts++;
cap_bot_rim = m->num_verts;
for (i = 0; i < det; i++) {
th = (i * 360 / det) % 360;
s = sintab[th]; c = costab[th];
m->v_normals[m->num_verts].x = 0; m->v_normals[m->num_verts].y = -int_to_f(1); m->v_normals[m->num_verts].z = 0;
m->verts[m->num_verts].x = x + f_mul(r, c); m->verts[m->num_verts].y = y - half_h; m->verts[m->num_verts].z = z + f_mul(r, s);
m->num_verts++;
}
for (i = 0; i < det; i++) {
add_face(m, cap_bot_center, cap_bot_rim + i, cap_bot_rim + (i + 1) % det, col, wire);
}
/* --- Fond haut : centre + rebord, normale (0,+1,0) --- */
cap_top_center = m->num_verts;
m->v_normals[m->num_verts].x = 0; m->v_normals[m->num_verts].y = int_to_f(1); m->v_normals[m->num_verts].z = 0;
m->verts[m->num_verts].x = x; m->verts[m->num_verts].y = y + half_h; m->verts[m->num_verts].z = z;
m->num_verts++;
cap_top_rim = m->num_verts;
for (i = 0; i < det; i++) {
th = (i * 360 / det) % 360;
s = sintab[th]; c = costab[th];
m->v_normals[m->num_verts].x = 0; m->v_normals[m->num_verts].y = int_to_f(1); m->v_normals[m->num_verts].z = 0;
m->verts[m->num_verts].x = x + f_mul(r, c); m->verts[m->num_verts].y = y + half_h; m->verts[m->num_verts].z = z + f_mul(r, s);
m->num_verts++;
}
for (i = 0; i < det; i++) {
add_face(m, cap_top_center, cap_top_rim + (i + 1) % det, cap_top_rim + i, col, wire);
}
}
void save_mesh(Mesh *m, const char *fn) {
@ -96,7 +130,6 @@ Mesh* load_mesh(const char *fname) {
m = (Mesh*)malloc(sizeof(Mesh));
fread(&m->num_verts, sizeof(int), 1, f);
fread(&m->num_faces, sizeof(int), 1, f);
// Suppression de is_wire_only car absent du .h
m->verts = (Vector3*)malloc(sizeof(Vector3) * m->num_verts);
m->v_normals = (Vector3*)malloc(sizeof(Vector3) * m->num_verts);