Skip to content

yujin24300/Obj-Model-Viewer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 

Repository files navigation

Obj-Model-Viewer

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.


๐Ÿ“ธ Preview


โœจ Features

  • 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 rotation
    • Mouse 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 intensity
    • 5 / 6 โ†’ Adjust shininess exponent (highlight size)

๐ŸŽฎ Controls

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

โš™๏ธ Implementation Details

1. Model Data

  • Models are loaded via MyObj and stored in MyModel instances.
  • All faces are treated as triangles.
  • Vertex array size = f.size() * 3.

2. Normal Calculation

  • Surface Normals
p = b - a
q = c - a
n = p ร— q
  • Vertex Normals
    Accumulated from adjacent faces, averaged and normalized.

3. Model Placement

  • 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.

4. Shading & Lighting

Phong illumination model:

I = Lamb * Mamb + Ldiff * Mdiff * (N ยท L) + Lspec * Mspec * (V ยท R)^n

๐Ÿš€ Troubleshooting

  • Scaling Distortion
    Initially scaled each axis independently โ†’ fixed by scaling uniformly based on the largest dimension.

  • Shader Uniform Errors
    Passing isFlat as a float caused issues โ†’ fixed with glUniform1i.

  • Variable Management
    Careful naming and type consistency were crucial due to many shader uniforms.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors