Skip to content

Anubhav-Mondal/Graphics-Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

3D Graphics Engine (Built from Scratch in C++)

About the Project

This is a 3D graphics engine built entirely from scratch in C++ without using external graphics libraries (like OpenGL or DirectX).
The goal of this project is to learn 3D computer graphics through practical application by implementing everything from rendering pipelines to lighting models manually.


Features

Wireframe Rendering → Real-time rendering of wireframe models with smooth performance.
Flat Shading → Per-triangle lighting calculations with efficient shading.
Gouraud Shading → Per-vertex lighting with color interpolation across triangles for smoother shading.
Phong Shading → Per-vertex lighting with color interpolation across triangles for smoother shading + Specular Refletion.
Multiple Light Source → Multiple Light Source Support, each light can be anywhere, any colored, any intensity.
Deapth Buffer/ Z-Buffer → Accurate hidden surface removal using depth comparisons, ensuring correct visibility of objects.
OBJ File Support → Load and render external .obj 3D models.
Frustum Culling → Optimized rendering by culling triangles outside the camera’s frustum.
Gamma Correction & Color Saturation → Improved color accuracy and visual quality.
Efficient CPU Rendering → Entire rendering pipeline runs on the CPU, with smart optimizations for better performance.


Performance Highlights

  • Efficient Rendering: Renders .obj models with thousands of triangles at 100+ FPS even on a low-spec machine.
  • Optimized Culling & Lighting: FPS boost achieved through culling and optimized shading.
  • Scalable Performance: On higher-end CPUs, it will perform significantly faster due to better processing capabilities.

Screenshots

Wireframe Model Scorpion obj model - Wireframe

Flat Shaded Model Scorpion obj model - Flat Shading

Gouraud Shaded Model Scorpion obj model - Gouraud Shading with multiple lights

Phong Shaded Model Scorpion obj model - Phong Shading


How It Works

  1. Vertex Transformation:
    • Transforms model vertices using translation, rotation, and scaling matrices.
  2. Backface Culling:
    • Removes triangles facing away from the camera to reduce unnecessary rendering.
  3. Lighting & Shading:
    • Implements Additive and Multiplicative lighting models with adjustable light intensity and position.
  4. Frustum Culling:
    • Eliminates triangles outside the screen frustum to boost performance.
  5. Color Correction:
    • Applies gamma correction, exposure, and saturation for better visuals.

Installation

  1. Clone the repository:

    git clone https://github.com/Anubhav-Mondal/Graphics-Engine.git
    cd 3d-graphics-engine 

    clone the project then go to the project folder.

  2. Make /build folder:

       mkdir build
       cd build
  3. Build the Project:

       cmake ..
       cmake --build . --config Release
  4. Run the Project:

    ./Release/engine.exe

    The executable will be in /build/Release.


Explaination of Commands

  • cmake .. → Generates the build configuration files using the parent directory's CMakeLists.txt.
  • cmake --build . --config Release → Compiles the project in Release mode with full optimizations.
  • ./Release/engine.exe → Runs the compiled executable.

Requirements


Custom Model

  1. Place Your Model:
  • Add your custom 3D model (.obj file) to the models/ folder.
  1. Include the Model in Your Code:
  • Open main.cpp and add the following line anywhere before the main() function:

    Model modelDetail("../models/YourModelName.obj", baseColor, offset);
  • Replace the parameters with your desired values:

    • YourModelName.obj: The name of your model file.

    • baseColor: The base color of the model, using Color(r, g, b) format.

    • Example:

      Color(255, 255, 255)  // White  
      Color(255, 0, 0)      // Red
    • offset: The distance from the camera. You’ll need to tweak this value to avoid performance issues.

      • Guidelines for offset:
        • If all vertices are within the range [-1, 1], use an offset of around 50.0f.

        • If the range is larger, increase the offset (e.g., 1000.0f, 2000.0f, etc.) for proper rendering.


Controls

Current Controls:

  • 0 → Wireframe (WF)
  • 1 → Flat Shading (FS)
  • 2 → Gouraud Shading (GS)
  • 3 → Phong Shading (Specular + Gouraud)

(Left Arrow): Switch to the previous rendering mode (cyclic).

Example: GS → FS → WF → Phong ...

(Right Arrow): Switch to the next rendering mode (cyclic).

Example: WF → FS → GS → Phong → WF ...

(Up Arrow): Decrease the model offset (model moves closer to the camera).

(Down Arrow): Increase the model offset (model moves further from the camera).

Left Click + Drag Horizontally : Rotate model around Y-axis.

Left Click + Drag Vertically : Rotate model around X-axis.

Left Click + Drag Diagonally : Adds slight Z-axis rotation.

R: Reset all rotations to default position.

esc: Exit the application.

⚠️ More controls coming soon!


Author

About

A Graphics Engine made from scratch (without using any existing Graphics API - only used GLFW for window and input handling). This Engine can read .obj file (for better performance try to use under 20k trinagle). This Engine can render Wireframe and Flat-Shading with smooth perforrmance.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors