Skip to content

Lazy-MoMo/3D-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Computer Graphics Lab 5: 3D Transformations

Project Overview

This project demonstrates the implementation of fundamental 3D computer graphics transformations using OpenGL and GLUT (OpenGL Utility Toolkit). The program allows interactive manipulation of 3D objects through translation, rotation, scaling, and shearing transformations, along with orthographic projection.

Features

  • Translation: Move objects along X, Y, and Z axes
  • Rotation: Rotate objects around X and Y axes
  • Scaling: Scale objects independently along each axis
  • Shearing: Apply shear transformations (XY and XZ shearing)
  • Orthographic Projection: 3D visualization with parallel projection
  • Real-time Interaction: Keyboard-controlled transformations
  • Visual Feedback: On-screen display of current mode and transformation values

Requirements

  • GCC/G++ compiler
  • OpenGL development libraries
  • GLUT (OpenGL Utility Toolkit)
  • Linux environment with development headers

Installation (Debian/Ubuntu)

sudo apt-get install build-essential freeglut3-dev

Installation (Fedora/RHEL)

sudo dnf install gcc-c++ freeglut-devel mesa-libGL-devel

Building

make

Or manually compile:

g++ -std=c++11 -o cg_lab5 main.cpp -lGL -lGLU -lglut -lm

Running

make run

Or directly:

./cg_lab5

Controls

Key Action
1 Switch to Translation mode
2 Switch to Rotation mode
3 Switch to Scaling mode
4 Switch to Shearing mode
Arrow Keys Apply the current transformation
R Reset all transformations to default values
Q / ESC Exit the program

Mode-Specific Controls

Translation Mode:

  • Left/Right arrows: Move along X-axis
  • Up/Down arrows: Move along Y-axis
  • Note: Z-axis movement can be added (currently controlled by code)

Rotation Mode:

  • Left/Right arrows: Rotate around Y-axis
  • Up/Down arrows: Rotate around X-axis

Scaling Mode:

  • Left/Right arrows: Scale along X-axis
  • Up/Down arrows: Scale along Y-axis
  • Note: Z-axis scaling can be added (currently controlled by code)

Shearing Mode:

  • Left/Right arrows: XY shearing
  • Up/Down arrows: XZ shearing

Key Concepts Implemented

Homogeneous Coordinates

3D points and vectors are represented using homogeneous coordinates (x, y, z, w), where w=1 for points and w=0 for vectors.

Transformation Matrices

Each transformation is represented by a 4×4 matrix applied through matrix multiplication.

Orthographic Projection

Parallel projection where projection lines are perpendicular to the projection plane, maintaining object size regardless of distance.

Order of Transformations

The program applies transformations in the following order:

  1. Translation
  2. Rotation (X-axis first, then Y-axis)
  3. Scaling
  4. Shearing

This order ensures predictable and intuitive transformation behavior.

File Structure

lab5/
├── main.cpp            # Main program implementation
├── Makefile            # Build configuration
├── README.md           # This file
└── AbhishekhCGLab5.pdf # Lab report documentation

Code Structure

The program consists of the following main functions:

  • init(): Initializes OpenGL context and lighting
  • display(): Renders the 3D scene and 2D UI
  • reshape(int w, int h): Handles window resizing
  • keyboard(unsigned char key, int x, int y): Handles keyboard input
  • specialKeys(int key, int x, int y): Handles arrow keys
  • drawAxes(): Renders coordinate axes (X-Red, Y-Green, Z-Blue)
  • drawCube(): Renders the 3D cube
  • applyShearMatrix(): Applies custom shear transformation matrix

Technical Details

OpenGL Functions Used

  • glTranslatef(): Translation transformation
  • glRotatef(): Rotation transformation
  • glScalef(): Scaling transformation
  • glMultMatrixf(): Custom matrix multiplication (for shearing)
  • glOrtho(): Orthographic projection
  • gluLookAt(): Camera positioning

Coordinate System

  • X-axis: Red (horizontal left-right)
  • Y-axis: Green (vertical up-down)
  • Z-axis: Blue (depth front-back)

Performance Notes

  • The program uses immediate mode OpenGL (deprecated but suitable for learning)
  • Depth testing is enabled for proper 3D rendering
  • Lighting is enabled to enhance visual appearance

Troubleshooting

Program won't compile

Ensure OpenGL development libraries are installed:

sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev

Segmentation fault

Ensure you have a valid X11 display. If running remotely:

export DISPLAY=:0

Window doesn't appear

Try running with:

./cg_lab5 &

References

Author

Abhishekh Joshi (Roll No: 27)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors