A cubemap is a spherical image representation method used in 3D graphics programming, especially to implement effects such as reflection, skybox/sky sphere, and environment mapping. In this article, we will provide a comprehensive answer to the questions of what a cubemap is, how to create it, which formats are supported, and how to apply it using DirectX 9 and C++.
What is a Cubemap?
A cubemap is a special texture created by combining 6 different images (like 6 cameras oriented towards the faces of a cube) taken from the center of a sphere outwards. These images represent:
-
+X (right)
-
-X (left)
-
+Y (up)
-
-Y (down)
-
+Z (forward)
-
-Z (back)
directions.
These textures are used together to create environmental reflection or sky effects on 3D objects.
️ What is 4x3 Cross Format?
Cubemaps are often stored in special arrangements:
The 4x3 Cross Format can be visualized as follows:
+Y
-X +Z +X -Z
-Y
This format is especially used to create cubemaps from offline rendered sky images. It contains 6 faces in a single image. DirectX can automatically separate the faces to use this format, or they must be manually cut with software.
⚙️ How to Create a Cubemap with DirectX 9?
-
Cubemap Texture Definition:
LPDIRECT3DCUBETEXTURE9 pCubeTexture = NULL;
d3ddev->CreateCubeTexture(
256, 0, D3DUSAGE_RENDERTARGET,
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture, NULL);
-
Loading Data to Faces:
D3DXLoadSurfaceFromFile(
pCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_X, 0),
NULL, NULL, "posx.jpg", NULL, D3DX_DEFAULT, 0, NULL);
You must do the same for all 6 faces.
-
Usage in Shader (VS/PS 2.0)
samplerCUBE CubeMapSampler = sampler_state
{
Texture = ;
};
float4 PS_Main(float3 reflectDir : TEXCOORD0) : COLOR0
{
return texCUBE(CubeMapSampler, reflectDir);
}
-
Calculating Cube Mapping Direction (C++) When calculating the reflection direction:
float3 view = normalize(eyePos - worldPos);
float3 reflectDir = reflect(view, normal);
Cubemap Usage Areas
Usage Area | Description |
---|---|
Skybox | Sky and distant landscapes in the game |
Reflection Map | Environmental reflection on surfaces such as metal and glass |
Refraction Map | Refraction effect in semi-transparent objects such as water |
IBL (Image Based Lighting) | Environmental lighting in PBR systems |
Cubemap Conversion and Tools
-
Photoshop + NVIDIA DDS Plugin
-
CMFT Studio (https://github.com/dariomanesku/cmftStudio)
-
DirectX Texture Tool (DxTex.exe): You can manually load the faces and create
.dds
Cubemap Render-to-Texture (RTT) for Dynamic Reflection
-
Render your scene 6 times with 90° FOV in different directions
-
Render to each face with
SetRenderTarget()
-
Create real-time reflection using this dynamic cubemap with Shader
This method is especially used to reflect the surroundings of moving objects.
✅ Conclusion
Cubemap technology is a very powerful technique for creating impressive environmental visuals in the DirectX 9 environment. It can be used in many areas such as reflection, sky, and lighting. It can be used with formats such as 4x3 Cross or DDS
based textures. The above steps provide a basic starting point for loading a cubemap into your D3D device and using it in a shader when developing an application with C++.