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.
- 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
- GCC/G++ compiler
- OpenGL development libraries
- GLUT (OpenGL Utility Toolkit)
- Linux environment with development headers
sudo apt-get install build-essential freeglut3-devsudo dnf install gcc-c++ freeglut-devel mesa-libGL-develmakeOr manually compile:
g++ -std=c++11 -o cg_lab5 main.cpp -lGL -lGLU -lglut -lmmake runOr directly:
./cg_lab5| 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 |
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
3D points and vectors are represented using homogeneous coordinates (x, y, z, w), where w=1 for points and w=0 for vectors.
Each transformation is represented by a 4×4 matrix applied through matrix multiplication.
Parallel projection where projection lines are perpendicular to the projection plane, maintaining object size regardless of distance.
The program applies transformations in the following order:
- Translation
- Rotation (X-axis first, then Y-axis)
- Scaling
- Shearing
This order ensures predictable and intuitive transformation behavior.
lab5/
├── main.cpp # Main program implementation
├── Makefile # Build configuration
├── README.md # This file
└── AbhishekhCGLab5.pdf # Lab report documentation
The program consists of the following main functions:
init(): Initializes OpenGL context and lightingdisplay(): Renders the 3D scene and 2D UIreshape(int w, int h): Handles window resizingkeyboard(unsigned char key, int x, int y): Handles keyboard inputspecialKeys(int key, int x, int y): Handles arrow keysdrawAxes(): Renders coordinate axes (X-Red, Y-Green, Z-Blue)drawCube(): Renders the 3D cubeapplyShearMatrix(): Applies custom shear transformation matrix
glTranslatef(): Translation transformationglRotatef(): Rotation transformationglScalef(): Scaling transformationglMultMatrixf(): Custom matrix multiplication (for shearing)glOrtho(): Orthographic projectiongluLookAt(): Camera positioning
- X-axis: Red (horizontal left-right)
- Y-axis: Green (vertical up-down)
- Z-axis: Blue (depth front-back)
- 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
Ensure OpenGL development libraries are installed:
sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev freeglut3-devEnsure you have a valid X11 display. If running remotely:
export DISPLAY=:0Try running with:
./cg_lab5 &- OpenGL Documentation: https://www.opengl.org/documentation/
- GLUT Documentation: https://www.opengl.org/resources/libraries/glut/
- Computer Graphics Principles and Practice (Foley, Van Dam, Feiner, Hughes)
Abhishekh Joshi (Roll No: 27)