If im trying to create an obj. file loader in c++ and draw it with opengl i need to use multiple textures in the fragment shader.I know i can just use:

`uniform sampler2D Texture1` 
`uniform sampler2D Texture2` 
`uniform sampler2D Texture3` 
`uniform sampler2D Texture4`

But i think there must be an elegant way to do it.Like a vector of samplers or something.Im asking this because i dont want to declare n textures in my fragment shader and type n if's where i test which texture i should use for the current fragment.It just looks ugly.


1 Answer 1


There is, 3d textures. It is the same as a 2d, except it has layers of textures on top if each other. You sample it with texture3d(Sampler2d sampler, vec3 texture coord) You still need zo create these, but you only load 1 uniform integer.

To bind a 3d texture, you bind the GL_TEXTURE_3D instead of a texture_2d

  • \$\begingroup\$ Also, note that you can use z positions other than 1, 2, 3 etc, but if you set the texture filter mod to liner, it will interpolate between them. Nearest does the same thing as round() \$\endgroup\$
    – Bálint
    Feb 10, 2016 at 21:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .