Documentation

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

Installation Steps

  1. Download the Source Files: Obtain the project files in a zip archive from the provided link.
  2. Navigate to the Source Directory: Open a terminal and navigate to the src folder.
  3. Build the Application: Execute the following command to compile the project:
    g++ Main.cpp -o render3D -lfreeglut -lComdlg32 -lopengl32 -lglu32
  4. 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:

Initial Startup Screen 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

5. Usage Guide

Launching the Application

  1. Execute the compiled binary to launch the application.
  2. A default rotating cube will be displayed.

Switching Shapes

  1. Right-click to open the context menu.
  2. Choose one of the available shapes: Cube, Pyramid, Sphere, or Load Custom Model.

Loading Custom Models

  1. Select the "Load Custom Model" option from the menu.
  2. Use the file dialog to select a binary STL file.
  3. The chosen model will be rendered in the viewport.

Saving as PNG

  1. Select the "Save As PNG" option from the menu.
  2. 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:

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:

  1. Model:
    • Manages data structures for object vertices, transformations, and global states.
    • Handles STL file loading
  2. View:
    • Responsible for rendering objects and managing visual elements like lighting
    • Handles perspective adjustments and redisplay logic
  3. Controller:
    • Processes user inputs (keyboard, mouse) and updates the model accordingly
    • Communicates state changes to the view for rendering updates

Core Compenents

  1. 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
  2. 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.
  3. 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()
  4. 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.
  5. 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 using stb_image_write
  6. 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

Custom Model Loaded in Application, Zoom in/out with mouse wheel video
Switch between standard veiws using keyboard video
Use the context menu to switch between shapes or load a custom model video
Custom STL model in application
Custom STL model in application
Context Menu

10. References

  1. CP411 Course Notes
  2. CP411 Project Details
  3. OpenGL Programming Guide
  4. FreeGLUT Documentation
  5. STB Image Write Library
  6. Learn OpenGL