An interactive 3D OBJ model viewer that supports real-time rendering, Phong illumination, and user-friendly controls.
The viewer automatically centers and scales models (cube, bunny, dragon, Buddha, etc.) for optimal display, with options for rotation, shading, and lighting adjustments.
![]() |
![]() |
|---|
-
Perspective Projection
Realistic depth rendering with perspective projection. -
Automatic Centering & Scaling
Models are centered in the viewport and uniformly scaled to fit the screen. -
Interactive Rotation
Spacebarโ Toggle continuous rotationMouse Buttonsโ Switch rotation axis (X, Y, Z)
-
Shading Modes
1โ Phong Shading (smooth shading using vertex normals)2โ Flat Shading (faceted shading using surface normals)
-
Specular Controls
3 / 4โ Increase / decrease specular intensity5 / 6โ Adjust shininess exponent (highlight size)
| Key / Mouse | Action |
|---|---|
| Spacebar | Toggle auto-rotation |
| Mouse Buttons | Change rotation axis (X, Y, Z) |
| 1 | Phong shading |
| 2 | Flat shading |
| 3 / 4 | Increase / decrease specular intensity |
| 5 / 6 | Increase / decrease shininess |
- Models are loaded via
MyObjand stored inMyModelinstances. - All faces are treated as triangles.
- Vertex array size =
f.size() * 3.
- Surface Normals
p = b - a
q = c - a
n = p ร q
- Vertex Normals
Accumulated from adjacent faces, averaged and normalized.
- Bounding box (min/max coordinates) is computed.
- Largest dimension is used for uniform scaling (to prevent aspect ratio distortion).
- Model is translated to the origin by subtracting its center.
Phong illumination model:
I = Lamb * Mamb + Ldiff * Mdiff * (N ยท L) + Lspec * Mspec * (V ยท R)^n
-
Scaling Distortion
Initially scaled each axis independently โ fixed by scaling uniformly based on the largest dimension. -
Shader Uniform Errors
PassingisFlatas a float caused issues โ fixed withglUniform1i. -
Variable Management
Careful naming and type consistency were crucial due to many shader uniforms.

