Overview
Cub3D uses XPM (X PixMap) textures to render realistic walls. Textures are loaded via the MiniLibX library and mapped onto wall surfaces based on the ray intersection point.XPM Texture Format
What is XPM?
XPM (X PixMap) is an image file format that represents images as C source code. It’s human-readable and was designed for the X Window System. MiniLibX natively supports this format.Available Textures
The Cub3D project includes several texture files:DOOM Textures
doom.xpmdoom2.xpmdoom3.xpmdoom4.xpm
Custom Textures
eagle.xpmpizza.xpmgang.xpmblen_textura.xpm
Stone Textures
greystone.xpmpurplestone.xpm
Misc Textures
puro_c.xpm
Texture Data Structure
Textures are stored in thet_texture structure:
cub3.h:68-77
| Field | Type | Description |
|---|---|---|
img_ptr | void* | MiniLibX image pointer |
data | char* | Pixel data buffer |
width | int | Texture width in pixels |
height | int | Texture height in pixels |
bpp | int | Bits per pixel |
line_len | int | Bytes per row |
endian | int | Endianness (0=little, 1=big) |
Loading Textures
Single Texture Loading
Textures are loaded using MiniLibX’smlx_xpm_file_to_image function:
main.c:29-41
Load image from file
mlx_xpm_file_to_image reads the XPM file and returns an image pointer. The width and height are automatically filled.Loading All Textures
All four directional textures are loaded during initialization:main.c:43-49
main.c:51-72
Texture Selection
The correct texture is selected based on which side of the wall was hit:raycasting.c:84-100
side == 0means the ray hit a vertical wall (East or West)side == 1means the ray hit a horizontal wall (North or South)- The ray direction determines which specific texture to use
Texture Mapping
X-Coordinate Mapping
The texture’s X coordinate is determined by where exactly the ray hit the wall:drawing.c:35-44
wall_xis the fractional part of the hit position (0.0 to 1.0)- This is multiplied by texture width to get the pixel column
- The texture is flipped for certain wall orientations to maintain consistency
Y-Coordinate Mapping
The texture’s Y coordinate is calculated for each pixel based on screen position:drawing.c:15-33
Calculate texture Y coordinate
Uses a scaled calculation to map screen Y position to texture Y coordinate. The
* 256 is for fixed-point arithmetic precision.Bounds checking
Validates that texture coordinates are within the texture dimensions to prevent buffer overflows.
Calculate buffer offset
Computes the byte offset in the texture data:
row * line_length + column * bytes_per_pixelDrawing Textured Walls
The complete wall drawing process:drawing.c:52-66
- Draw ceiling (above wall) - uses hardcoded color
0x87CEEB(sky blue) - Draw textured wall section
- Draw floor (below wall) - uses hardcoded color
0x444444(dark grey)
Texture Storage in t_map
The main map structure stores all texture information:cub3.h:156-161
- Four texture structures store the directional textures
current_texis a pointer to the currently selected texture during rendering
Memory Management
Textures must be properly freed when the program exits:cub3.h:168
mlx_destroy_image to prevent memory leaks.
Performance Considerations
Direct Memory Access
Pixel data is accessed directly via the
data pointer for fast rendering without function call overhead.Bounds Checking
All texture coordinate accesses include bounds checking to prevent crashes from invalid coordinates.
Fixed-Point Math
The texture mapping uses fixed-point arithmetic (
* 256) for faster integer-based calculations.Single Texture Load
Textures are loaded once at startup and reused throughout the game for efficiency.
Next Steps
Rendering
Learn how the complete frame rendering works
Map Format
Understand how textures are defined in map files