ASCII Ray Tracer – C++

For this project, I wanted to gauge how fast I could display content on the terminal assuming that the ASCII colors were limited to black and white. In my other post I attempted a real-time colored version using the scroll bars and blitting damaged areas to skip repetitive rendering.RayTracerASCII

Although a ray tracer is not real-time, I needed content to display to the screen. The findings below will explain the approach for real-time outputting to the terminal. After calculating the colors for each pixel of the screen in RGB space I processed the results using a simple luminance factor (0.21*R + 0.72*G + 0.07*B) to get each pixels brightness and mapped it with a set of ASCII characters ranging from light to dark.

Real-time standard output: The best approach I found using black/white ASCII characters is to buffer all your content and use fputs to display it all at once. Afterwards just reset the cursors location to the top left of the terminal.

Code Snippet: char screen[2000];  fputs( screen, stdout );  fflush( stdout );

Ideas: Depending how traditional you want to go, some approaches to create cool terminal real-time graphics would be using the GPU with Graphics API’s to render the image and then just ASCIIfy the content, use CUDA/OpenCL to ray trace a scene and move around it. To be more traditional create a CPU variant of Graphics API’s and use basic algorithms such as Bresenham’s line algorithm, etc and create the whole rendering pipeline and display that.

Note: Set: width – 160, height – 120, font size 4×6
Download: RayTracingCG.exe

Advertisements