fistcommit

This commit is contained in:
Frater 2026-05-09 14:37:14 +02:00
commit 80d8141b5a
8 changed files with 497 additions and 0 deletions

56
src/part3D/engine.c Normal file
View file

@ -0,0 +1,56 @@
#ifndef ENGINE_H
#define ENGINE_H
#include "defines.h"
#define f_to_int(a) ((a) >> 16)
#define int_to_f(a) ((a) << 16)
// #define f_mul(a, b) ((fixed)(((int64_t)(a) * (b)) >> 16))
inline fixed f_mul(fixed a, fixed b) { return (fixed)(((int64_t)a * b) >> 16); }
inline fixed f_div(fixed a, fixed b) {
if (b == 0) return 0; // Évite la division par zéro
return (fixed)(((int64_t)a << 16) / b);
}
// Matrice 3x3 pour la rotation des normales et des sommets
typedef struct { fixed m[3][3]; } Matrix3;
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);
}
static fixed sintab[360], costab[360], inv_table[321];
void init_engine_math() {
for (int 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);
}
inv_table[0] = 0;
for (int i = 1; i <= 320; i++) inv_table[i] = (int_to_f(1) / i);
}
typedef struct { fixed x, y, z; } Vector3;
typedef struct { int a, b, c; uint8_t color; uint8_t force_wire; } Face;
typedef struct { int x, y; fixed z; int intensity; } Point2D;
typedef struct {
Vector3 *verts;
Vector3 *v_normals;
Face *faces;
int num_verts, num_faces;
} Mesh;
// Normalisation pour le Gouraud (à la création uniquement)
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);
}
}
#endif