48 double l_len_sq = lx * lx + ly * ly + lz * lz;
49 double l_inv_len = 1.0 /
sqrt(l_len_sq);
55 double dot_diff = norm.
x * lx + norm.
y * ly + norm.
z * lz;
56 double diffuse = (dot_diff > 0.0) ? dot_diff : 0.0;
59 double specular = 0.0;
62 if (diffuse > 0.0 && mat && mat->
shininess > 0.0) {
65 double vx = -face_center.
x;
66 double vy = -face_center.
y;
67 double vz = -face_center.
z;
70 double v_len_sq = vx * vx + vy * vy + vz * vz;
71 double v_inv_len = 1.0 /
sqrt(v_len_sq);
81 double h_len_sq = hx * hx + hy * hy + hz * hz;
84 if (h_len_sq > 0.000001) {
85 double h_inv_len = 1.0 /
sqrt(h_len_sq);
91 double dot_spec = norm.
x * hx + norm.
y * hy + norm.
z * hz;
105 double light_factor = (diffuse * intensity) + ambient;
107 double spec_factor = specular * intensity;
128 r = (int)((mr * light_factor + msr * spec_factor) * lr);
129 g = (int)((mg * light_factor + msg * spec_factor) * lg);
130 b = (int)((mb * light_factor + msb * spec_factor) * lb);
131 a = (int)(mat->
alpha * 255);
134 double k = light_factor + spec_factor;
135 r = (int)(lr * 255.0 * k);
136 g = (int)(lg * 255.0 * k);
137 b = (int)(lb * 255.0 * k);
155 return gm_rgba(r, g, b, a);
178 if (transform == NULL)
218 for (
size_t i = 0; i < mesh->
n_vertices; i++) {
224 scratch_world[i] = p;
228 double inv_z = 1.0 / p.
z;
229 double s = focal * inv_z;
236 output->
vertices[start_v + i].
x = -99999.0;
237 output->
vertices[start_v + i].
y = -99999.0;
248 size_t t_idx = start_t;
250 for (
size_t i = 0; i < mesh->
n_faces; i++) {
259 if (w0->
z < near_plane || w0->
z > far_plane)
261 if (w1->
z < 0.1 || w2->
z < 0.1)
266 gm3Pos center = {(w0->
x + w1->
x + w2->
x) * 0.333333,
267 (w0->
y + w1->
y + w2->
y) * 0.333333,
268 (w0->
z + w1->
z + w2->
z) * 0.333333};
291 output->
colors[t_idx] = gm3_calculate_lighting(norm, center, mat, scene);
299 output->
depths[t_idx] = center.
z;
#define gm3_pos_dot(a, b)
Calculates the dot product of two gm3Pos vectors.
Definition position.h:138
#define gm_blue(col)
Extracts the blue component from a color.
Definition color.h:41
#define gm_green(col)
Extracts the green component from a color.
Definition color.h:34
#define gm_red(col)
Extracts the red component from a color.
Definition color.h:27
uint32_t gmColor
Type definition for color values, stored as a 32-bit unsigned integer. The color components are packe...
Definition color.h:13
void * realloc(void *ptr, size_t size)
Custom implementation of realloc for memory allocated by malloc (this custom version).
Definition malloc.h:236
double pow(double base, double exp)
Calculates the base raised to the power of the exponent (base^exp).
Definition math.h:358
double sqrt(double x)
Calculates the square root of x.
Definition math.h:339
Defines structures for 3D materials and material libraries, and functions for loading MTL files.
int gm3_project(gm3Image *output, const gm3Mesh *mesh, const gm3Transform *transform, const gm3Scene *scene)
Projects a 3D mesh onto a 2D image buffer, applying transformations, lighting, and culling.
Definition project.h:176
Defines the 3D scene structure, encompassing lights, cameras, and viewport settings.
const gm3Scene gm3_default_scene
A default gm3Scene instance.
Definition scene.h:30
double near
Definition camera.h:12
double focal
Definition camera.h:11
double far
Definition camera.h:13
Structure representing the 2D projected output of a 3D scene.
Definition image.h:22
double * depths
Definition image.h:27
gmColor * colors
Definition image.h:25
gmPos * vertices
Definition image.h:24
size_t * triangles
Definition image.h:26
size_t n_vertices
Definition image.h:30
size_t cap_world
Definition image.h:45
gm3Pos * world_verts
Definition image.h:44
size_t n_triangles
Definition image.h:32
struct gm3Image::@171174116104107177311324262265324115365007060225 _internal
double intensity
Definition light.h:17
gm3Pos position
Definition light.h:14
gmColor color
Definition light.h:16
double ambient
Definition light.h:18
Represents a single 3D material with various rendering properties.
Definition mtl.h:26
double alpha
Definition mtl.h:31
gmColor diffuse
Definition mtl.h:28
gmColor specular
Definition mtl.h:29
double shininess
Definition mtl.h:30
Represents a single face (triangle) in a 3D mesh.
Definition mesh.h:10
int material
Definition mesh.h:13
int material_file
Definition mesh.h:14
size_t vertices[3]
Definition mesh.h:11
gm3Pos normal
Definition mesh.h:15
Represents a 3D mesh composed of vertices, faces, normals, and texture coordinates.
Definition mesh.h:30
gm3MeshFace * faces
Definition mesh.h:34
size_t n_vertices
Definition mesh.h:32
gm3Pos * vertices
Definition mesh.h:31
gm3MtlLib * mtllibs
Definition mesh.h:43
size_t n_faces
Definition mesh.h:35
gm3Material * materials
Definition mtl.h:56
Represents a 3D position or vector.
Definition position.h:11
double y
Definition position.h:12
double z
Definition position.h:12
double x
Definition position.h:12
Represents a complete 3D scene, including its camera, lights, and viewport.
Definition scene.h:18
gm3Camera camera
Definition scene.h:22
gm3Light light
Definition scene.h:21
double x
Definition position.h:9
double y
Definition position.h:9