Those two functions perform quite different tasks on quite different arguments.
texture
expects normalized floating point texture coordinates in [0,1] (i.e. 0 being left/bottom and 1 being right/top) and performs proper wrapping (so the coordinates don't necessarily have to be in [0,1]) and filtering (so you don't necessarily get the exact texel color) according to whatever sampling modes you set.
texelFetch
on the other hand expects integer texel indices in [0,width-1] (and respectively height/depth for y and z) and doesn't perform any kind of filtering or wrapping, accessing the exact texel value at the specified index in a specified mipmap level.
So texelFetch(texture_3D, ivec3(coord * vec3(256.0), 0)
should be equivalent to texture(texture_3D, coord)
with filter modes GL_NEAREST
and wrapping modes GL_CLAMP_TO_EDGE
. However, also keep in mind possible rounding problems in case coord is exactly 1.0
or on texel boundaries, maybe texelFetch(texture_3D, ivec3(coord * vec3(255.0) + vec3(0.5), 0)
might be advisable to be more secure in that regard.
However, just changing it this way seems a rather useless aproach compared to simply keeping your current texture access with a GL_NEAREST
filter. It is more useful if you already have integer texel coordinates and already know the texture size. Just emulating nearest filtering this way won't really buy you anything. It's basically two conceptually different approaches, one filters a texture with size-independent floating point coordinates and the other one accesses a 3D-array with integer indices. It depends on your surrounding code which approach is more appropriate for your use case.
texture()
. When using texelFetch I do not have to care whether I useGL_NEAREST
orGL_LINEAR
? – Whydah