I created an OpenGL scene based on the Spider-Man PS2 video game, but using the characters from Spider-Man: Into the Spider-Verse. Within this scene, I implemented three .obj files with their corresponding textures. I also implemented a fragment and vertex shader that incorporated point lighting, directional illumination and spotlight lighting.
This code defines two GLSL functions for Phong-style lighting using textures. PointLight() calculates lighting from a point light by computing the vector from the fragment to the light, applying distance-based attenuation, and combining ambient, diffuse (Lambertian), and specular (Phong) components, modulated by diffuse and specular textures. DirectionalIllumination() does similar calculations for a directional light with a fixed direction, but without distance attenuation, using a material-defined shininess for the specular highlight. In both functions, the final color multiplies the texture-modulated lighting by the light’s color, producing realistic shaded surfaces that respond to light position, direction, and viewer perspective.
Fragment Shader - Point and Directional light code
.obj loader
The load_model_from_file function loads a 3D OBJ model into a ModelData structure by reading vertex positions, normals, and texture coordinates using TinyObjLoader. It applies an optional preTransform matrix to each vertex position and stores all vertex attributes - position (x, y, z), normal (x, y, z), and texture coordinates (u, v) - sequentially in a flat array. Normals and texture coordinates default to zero if not present in the file. The function also loads the model’s materials, reports any warnings or errors during loading, and returns a ModelData object ready for use in OpenGL rendering.