Search code examples
opengltexturesopengl-3

How to draw 2D texture from 3D texture


I have a 2D game, and I want to use a 3D texture for fog to make it animated .... however, I don't want the entire 3D texture to be shown, I just want the part of the texture that's at Z 0.

How do I do this?


Solution

  • 3D textures work analogously to 2D ones. You initialize the texture using glTexImage3D(), bind it to a sampler3D, and access it in the fragment shader using texture().

    //fog.frag
    uniform sampler3D fog;
    uniform float z;
    
    in vec2 texCoord;
    
    void main(){
        vec4 color = texture(fog, vec3(texCoord, z));
        ...
    }