How to Build a 2.5D Raycaster Graphics Engine from Scratch
Overview
We will learn everything about raycasting and create a ray-casting graphics engine by programming the classic algorithms in C (or C++).
Raycasting: The Key Concept
Both raycasting and raytracing take its origin from the mechanics of the optical cameras a photographer would use, which in turn, take ITS origin from the structure of the human eye. Here we have a lens that the rays of light pass through. But where do these rays come from? The light comes from the sun, bounces off the surfaces of the objects, and finally enters the lens. The lens then converts the rays into a 2-dimensional image. In a "raycaster" graphics engine we are "casting" theoretical rays of light onto the computer screen, but in a clever way. Based on the simplified theory that the rays of light travel in a (more or less) straight line we can be a little analytical about it and save a lot of computing power. After all, the number of pixels on the screen is limited by the screen resolution. So, instead of calculating all of the sun's rays onto the projected geometry, seeing which rays will or will not pass through the lens (or here, the computer screen) depending on their angles, we are doing it in reverse - from the viewpoint back to the geometry. Going backwards, from the source, we can only focus on the rays that will enter the lens, the only rays that we do need to display. In raycasting, initially we are working with walls only (forget about floor casting for now, I will explain it below). In the simplified case, the camera is always pointing straight, thus we can reduce our 3D problem to a 2D problem, because assuming no head tilting the Z coordinate will always be constant. Even though it is extremely easy to access the video memory byte by byte in linear fasion which is very effective when rendering horizontal spans of pixels, the walls are usually drawn in vertical spans per each column of the screen width. So if our screen resolution is 640 by 480, we will cast 640 vertical rays from the viewpoint onto the level data. starting from the left-hand side of the screen.
The Difference Between Raycaster and Raytracer.
It is easy to be confused by the terminology often used by people who are familiar with 3D graphics. Let's break the two words up into parts. The word ray is simply a ray of light. However, in computer graphics, the words "cast" and "trace" have two different connotations. It can be thought that casting is a subset of tracing, or in other words, it is possible to "trace" anything that can also be "cast". Casting is the simplified method of tracing. But what exactly do these methods imply?
Camera
The Data
This page is in the process of being written
If someone thumbs up your comment, your gain +1 mana (authority) on this page's subject.
If someone thumbs down your comment, you lose 1 mana.
If your comment is thumbed down by the author of this page, you will lose 3 manas.