raycasting1-capp.
Ray casting is one of the most important computer graphics rendering algorithms that use the geometric algorithm of ray tracing. Ray tracing-based rendering algorithms operate in image order to render three-dimensional scenes to two-dimensional images. Geometric rays are traced from the eye of the observer to sample the light (radiance) travelling toward the observer from the ray direction. The speed and simplicity of ray casting comes from computing the color of the light without recursively tracing additional rays that sample the radiance incident on the point that the ray hit. This eliminates the possibility of accurately rendering reflections, refractions, or the natural falloff of shadows; however all of these elements can be faked to a degree, by creative use of texture maps or other methods. The high speed of calculation made ray casting a handy rendering method in early real-time 3D video games.
Note that although "ray casting" and "ray tracing" were often used interchangeably in early computer graphics literature, more recent usage tries to distinguish the two. The distinction is that ray casting is a rendering algorithm that never recursively traces secondary rays, whereas other ray tracing-based rendering algorithms may.
Code Snippet// int w=131072, h=-10 // float posX = 22, posY = 12, dirX = -1, dirY = 0, planeX = 0, planeY = 0.66 __attribute__((aligned(64))) int worldMap[mapWidth][mapHeight] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1}, {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1}, {1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} };
for(int x = 0; x < w; x++) { float cameraX = 2 * x / float(w) - 1; float rayPosX = posX; float rayPosY = posY; float rayDirX = dirX + planeX * cameraX; float rayDirY = dirY + planeY * cameraX; int mapX = int(rayPosX); int mapY = int(rayPosY); float sideDistX; float sideDistY; float deltaDistX = sqrtf(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX)); float deltaDistY = sqrtf(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY)); float perpWallDist; int stepX; int stepY; int hit = 0; int side; if (rayDirX < 0) { // Always taken. stepX = -1; sideDistX = (rayPosX - mapX) * deltaDistX; } else { stepX = 1; sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX; } if (rayDirY < 0) { // Taken exactly half the times stepY = -1; sideDistY = (rayPosY - mapY) * deltaDistY; } else { stepY = 1; sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY; } while (hit == 0) { // Taken 20 times per iteration of outer loop, on average. if (sideDistX < sideDistY) { // Taken 77% of the times. sideDistX += deltaDistX; mapX += stepX; side = 0; } else { sideDistY += deltaDistY; mapY += stepY; side = 1; } if (worldMap[mapX][mapY] > 0) hit = 1; // Taken once in every run of the above while loop. } if (side == 0) // Taken 73% of the times. perpWallDist = fabs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX); else perpWallDist = fabs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY); int lineHeight = abs(int(h / perpWallDist)); int drawStart = -lineHeight / 2 + h / 2; if(drawStart < 0) { // Always taken. drawStart = 0; } int drawEnd = lineHeight / 2 + h / 2; if(drawEnd >= h) { // Always taken. drawEnd = h - 1; } checksum += drawStart + drawEnd; }