Giter VIP home page Giter VIP logo

computer-graphics-from-scratch-cpp's Introduction

"Computer Graphics from Scratch" in C++

Standalone C++ implementation of raytracing and rasterization, without external libraries or frameworks.

  • The demos come from "Computer Graphics from Scratch" (book), which provides Javascript demos.
  • Additionally implements features from "Extending the Raytracer" that weren't implemented in the original source code.

Raytracer Demos

These demos are all written in FP32, as opposed to the original Javascript demos that use FP64 by default. This results in shadow acne in all raytracing demos after Raytracer 04. To fix this, I added an epsilon reflection ray bias (e.g., start the reflection ray at an epsilon offset along the surface normal). The previews below include this fix.

Raytracer 01 - Basic Raytracer 02 - Diffuse Raytracer 03 - Specular
https://i.imgur.com/ziIToN5.png https://i.imgur.com/yaXHtUk.png https://i.imgur.com/bMobaeB.png
Raytracer 04 - Shadow Raytracer 05 - Reflection Raytracer 06 - Camera
https://i.imgur.com/LxoOVys.png https://i.imgur.com/RT13thh.png https://i.imgur.com/B5Tm1CX.png

"Extending the Raytracer" Demos

These implement various features from "Extending the Raytracer", which didn't previously have implementations.

Raytracer 07 - Triangle Raytracer 08 - CSG Raytracer 09 - Optimization
https://i.imgur.com/rp5QPvT.png https://i.imgur.com/9kdM0rv.png https://i.imgur.com/wts7Tjg.png

Rasterization Demos

The C++ implementations below differ slightly from their Javascript counterparts to improve efficiency.

Raster 01 - Lines Raster 02 - Lines (Fixed) Raster 03 - Triangle
https://i.imgur.com/pZXyTU3.png https://i.imgur.com/dALZfnD.png https://i.imgur.com/7WuJ6WR.png
Raster 04 - Shading Raster 05 - Perspective Raster 06 - Cube
https://i.imgur.com/EWAyk3K.png https://i.imgur.com/GpJoYPp.png https://i.imgur.com/tfZXeZV.png
Raster 07 - Instances Raster 08 - Homogenous Raster 09 - Clipping
https://i.imgur.com/KrbLL6Z.png https://i.imgur.com/O0ALrdz.png https://i.imgur.com/qMdhkBA.png
Raster 10 - Surface Raster 11 - Shading Raster 12 - Texture
https://i.imgur.com/tbswzYt.png https://i.imgur.com/Ja4mIuk.png https://i.imgur.com/G1vMZcN.png

Getting Started

Note: The instructions below assume you're in the repository root.

To get started, pick any demo script to build. For example, the first demo for raytracing is in demos/raytracer-01-basic.cpp. To build and run it, use the following

g++ demos/raytracer-01-basic.cpp -o main.out -std=c++20
./main.out
open output.bmp

This gives the following output, which matches the output from the original.

This Repo Original Reference on the author's website
https://i.imgur.com/yEfIoTv.png https://i.imgur.com/eJv6tLr.png

Before running raster-12-texture.cpp, download and convert the texture map using the provided Python script.

pip install Pillow requests
python tools/download_texture.py

Build and run all demos using the following, after installing Python dependencies above:

bash tools/run.sh

All scripts in this repository were tested using clang version 15.0.0.

❯ g++ --version
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: x86_64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.