Render 3D Objects
C++, OpenGL, Computer Graphics, STL`1. Introduction
This project is a real-time 3D object rendering application built using OpenGL. It provides users with the ability to visualize and interact with 3D shapes, such as cubes, pyramids, spheres, and custom STL models. The application offers intuitive controls for rotation, zoom, and translation, accompanied by dynamic lighting effects. Additionally, users can capture the current rendering view and save it as a high-quality PNG file.
2. Installation
Prerequisites
- A C++ compiler compatible with OpenGL and GLUT (e.g., GCC or MinGW)
- Windows operating system
- A command-line interface (CLI) such as Command Prompt or a terminal
Installation Steps
- Download the Source Files: Obtain the project files in a zip archive from the provided link.
-
Navigate to the Source Directory: Open a terminal and
navigate to the
src
folder. -
Build the Application: Execute the following command to
compile the project:
g++ Main.cpp -o render3D -lfreeglut -lComdlg32 -lopengl32 -lglu32
-
Run the Application: Execute the compiled binary with:
./render3D
Sample Output
The application launches with a rotating cube rendered by default. Below is a sample rendering of a custom STL object:
video
3. Project Structure
CP411-FinalProject/ ├── Assets #SAMPLE STL FILES FOR TESTING ├── Binaries/ ├── Includes/ ├── src/ │ ├── Controller/ │ │ ├── controller.cpp │ │ ├── controller.hpp │ │ ├── menu.cpp │ │ ├── menu.hpp │ ├── include/ │ │ ├── assimp/ │ │ ├── stb_image_write.h │ ├── config.hpp │ ├── Main.cpp │ ├── model_loader.cpp │ ├── model_loader.hpp │ ├── model.cpp │ ├── model.hpp │ ├── utilities.cpp │ ├── utilities.hpp │ ├── view.cpp │ ├── view.hpp ├── Debug/ ├── Release/ ├── docs/
4. Features
- Predefined 3D Objects: Visualize and interact with predefined shapes, including cubes, pyramids, and spheres.
- Custom Model Support: Load and render user-provided binary STL files.
- Dynamic Viewpoints: Switch between predefined views like Isometric, Reverse Isometric, Top, Bottom, and Front Views.
- Real-Time Interactions: Utilize keyboard and mouse inputs to rotate, zoom, and translate objects dynamically.
- Right-Click Menu: Access essential functions such as resetting views, saving screenshots, and switching between shapes.
- PNG Export: Capture and save the current scene as a high-resolution PNG image with a timestamp.
- Responsive Design: The application adjusts seamlessly to window resizing.
- Extensibility: Modular architecture allows easy integration of additional features and shapes.
5. Usage Guide
Launching the Application
- Execute the compiled binary to launch the application.
- A default rotating cube will be displayed.
Switching Shapes
- Right-click to open the context menu.
- Choose one of the available shapes: Cube, Pyramid, Sphere, or Load Custom Model.
Loading Custom Models
- Select the "Load Custom Model" option from the menu.
- Use the file dialog to select a binary STL file.
- The chosen model will be rendered in the viewport.
Saving as PNG
- Select the "Save As PNG" option from the menu.
- The current frame will be saved in the working directory with a timestamped filename.
6. Controls
The 3D Object Rendering Application provides enhanced controls for interacting with and viewing 3D objects.
Keyboard Controls
Key | Action |
---|---|
+ |
Increase rotation speed |
- |
Decrease rotation speed |
Space | Pause/Resume rotation |
w |
Rotate the object upward |
s |
Rotate the object downward |
a |
Rotate the object to the left |
d |
Rotate the object to the right |
q |
Rotate the object counterclockwise along Z-axis |
e |
Rotate the object clockwise along Z-axis |
i |
Set Isometric view (35.26° X-axis, 45° Y-axis) |
r |
Set Reverse isometric view (-35.26° X-axis, -45° Y-axis) |
t |
Set Top view (-90° X-axis) |
b |
Set Bottom view (90° X-axis) |
f |
Set Front view (0° X, Y, Z axes) |
p |
Save the current frame as a PNG file |
Mouse Controls
Action | Description |
---|---|
Horizontal Movement | Rotate the object left or right based on mouse X-axis movement. |
Vertical Movement | Rotate the object up or down based on mouse Y-axis movement. |
Scroll Up/Down | Zoom in/out. |
Right-click | Open the context menu. |
Mouse Options
The right-click menu provides the following functionalities:
- Reset: Reset all transformations to their default values.
- Pause/Resume: Toggle rotation.
- Speed: Adjust the rotation speed dynamically.
- Zoom: Increase or decrease the zoom level.
- Load Custom Model: Load and render a binary STL model.
- Save As PNG: Save the current frame as an image file.
- Exit: Close the application.
7. Implementation Details
The implementation of the 3D Object Rendering Application focuses on modularity, maintainability, and efficiency, leveraging OpenGL for real-time rendering and modern C++ for backend logic
Modular Architecture
The application follows the Model-View-Controller (MVC) design pattern for modularity:
-
Model:
- Manages data structures for object vertices, transformations, and global states.
- Handles STL file loading
-
View:
- Responsible for rendering objects and managing visual elements like lighting
- Handles perspective adjustments and redisplay logic
-
Controller:
- Processes user inputs (keyboard, mouse) and updates the model accordingly
- Communicates state changes to the view for rendering updates
Core Compenents
-
OpenGL Initialization
-
The OpenGL environment is initialized using
initializeOpenGL()
- Sets up the viewport and perspective projection matrix for rendering
- Sets up default background color and depth testing for realistic rendering
-
The OpenGL environment is initialized using
-
Lighting Configuration
-
Lighting is initialized using
initializeLighting()
- It sets up Ambient lighting to illuminate all objects uniformly.
-
Also sets up Material properties through
glColorMaterial()
for rendering faces with dynamic colors.
-
Lighting is initialized using
-
Rendering Pipeline
-
The
onDisplay()
function handles the rendering pipeline - It clears the screen with glClear for each frame.
- Applies transformations (translation, rotation, scaling).
-
Renders objects based on the currently selected shape (currentShape)
using helper functions:
drawCube()
drawPyramid()
drawSphere()
drawSTL()
-
The
-
User Interaction
-
Keyboard Input:
handleKeyboard()
captures keystrokes for rotation, zoom, and special views (e.g., Isometric, Top). -
Mouse Motion:
handleMouseMotion()
allows smooth real-time rotation by mapping mouse movement to rotational axes. - Right-Click Menu: The context menu provides options for resetting, saving, and loading models.
-
Keyboard Input:
-
File Handling
-
Custom Model Loading:Binary STL files are parsed
using
loadSTL()
, which extracts vertex and normal data for rendering. -
Saving As PNG:Framebuffer data is captured using
glReadPixels
and saved as a PNG file usingstb_image_write
-
Custom Model Loading:Binary STL files are parsed
using
-
Dynamic Rendering
glTranslatef()
: Moves objects in 3D space.-
glRotatef()
: Applies rotations around the X, Y, and Z axes. -
gluPerspective()
: Adjusts the zoom level dynamically.
8. Dependencies
Dependency | Purpose |
---|---|
OpenGL | Core rendering pipeline. |
GLUT | Window creation, input, and event management. |
GLU | Utility functions for perspective and transformations. |
STB Image Write | Saves PNG screenshots from the framebuffer. |
iostream, fstream, cstdlib | Console I/O, file handling, random number generation. |
cmath, ctime, cstring | Math operations, timestamps, string manipulation. |
windows.h, commdlg.h | File dialog support on Windows. |
g++ | Compiles the project. |
9. Screenshots of the Application in Action
video
video
video



10. References
- CP411 Course Notes
- CP411 Project Details
- OpenGL Programming Guide
- FreeGLUT Documentation
- STB Image Write Library
- Learn OpenGL