Wall Texture Setup Function

Top-Down Grid View
Texture Column Selection

Ray Controls

N (0°)
E (90°)
S (180°)
W (270°)
NORTH
Texture
SOUTH
Texture
EAST
Texture
WEST
Texture

Function Breakdown:

1. Side Detection (Vertical vs Horizontal Wall)

ray->side == 0: Hit a vertical wall (NS-facing)

ray->side == 1: Hit a horizontal wall (EW-facing)

2. Direction-Based Texture Selection

For Vertical Walls (side == 0):

For Horizontal Walls (side == 1):

3. Wall Hit Position Calculation

Vertical walls: wall_hit = fmod(ray_y, BLOCK) / BLOCK

Horizontal walls: wall_hit = fmod(ray_x, BLOCK) / BLOCK

This gives a value 0.0-1.0 representing where along the wall edge the ray hit.

4. Texture X Coordinate

tex_x = (int)(wall_hit * tex->width)

Converts the 0.0-1.0 hit position to actual texture pixel coordinate.

5. Texture Flipping Logic

Flips texture for consistent lighting/orientation:

if ((side == 0 && ray_dx > 0) || (side == 1 && ray_dy < 0))

tex_x = tex->width - tex_x - 1;

This ensures textures appear consistent from the player's perspective.

Why This Function is Needed: