View Full Version : getting integer value instead of 0-1

09-14-2010, 07:33 PM

i have question about glsl. so i have a 1D texture that holds a bunch of index. i had it with integer value (32 bit), so i save it to GPU memory using this command:

glTexImage1D(GL_TEXTURE_1D, 0, GL_ALPHA32F_ARB, size, 0, GL_ALPHA, GL_INT, texture);

is it legal to do that?

in the GLSL code, i want to fetch each value as an integer instead of a 0-1 decimal value, trying to avoid rounding problem. im using texelFectch() so im going to access the precise integer coordinate. how can i fetch the value as an integer?

09-14-2010, 07:59 PM
just curious i saw around the internet GL_R32I, so I can use

glTexImage1D(GL_TEXTURE_1D, 0, GL_R32I, size, 0, GL_RED, GL_INT, texture);

is it possible to do that? how can this affect the value that i save to the GPU memory? what should i do in the GLSL to get the integer value instead of 0-1?

09-14-2010, 08:42 PM
GL_RED_INTEGER as your external format

09-14-2010, 09:52 PM
ok thx,

and in the GLSL part, how can i access it to get an integer value? can i simply call

int red = texelFetch(tex, ivec, 0).r;

and get an 32 bit integer value?

Alfonse Reinheart
09-14-2010, 09:53 PM
Yes, but "tex" must be an integer sampler.

"sampler2d" is a floating-point sampler. "isampler2d" is a signed integer sampler, and "usampler2d" is an unsigned integer sampler.

09-14-2010, 10:12 PM
so to sum up, in the C++ side, i save the 1D texture using this command:

glTexImage1D(GL_TEXTURE_1D, 0, GL_R32I, size, 0, GL_RED, GL_INT, texture);

and on the GLSL side, i call:

isampler1d tex;
int red = texelFetch(tex, ivec, 0).r;

did i miss anything? sorry i asked too much. new to GLSL and it seems theres no easy way to debug using GLSL, and it seems like the compiler does not complain much like other language :-s

09-15-2010, 10:09 AM
Seems correct to me. Just the texture coordinate is a single 'int' instead of 'ivec'.

09-15-2010, 07:25 PM
i seems having problem try to get this work. so here is my case:

i have a texture, 500*500, each value is an integer. i save the texture using this command:

glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, header.textureWidth, header.textureHeight, 0, GL_RED, GL_INT, startingPoint2D);

and in the GLSL side, i call this command:

uniform isampler2D texStartPoint;

vertCoord.s = (floor(gl_TexCoord[0].x*500));
vertCoord.t = (floor(gl_TexCoord[0].y*500));

int startPointValue = texelFetch(texStartPoint, vertCoord, 0).r;

but no luck. the value always return 0. i checked and rechecked the array sent is not all zero. its normal array of integer with the right size. any idea why this happens and how to make it works?

thanks in advance

09-15-2010, 08:40 PM
As I said before, glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, header.textureWidth, header.textureHeight, 0, GL_RED_INTEGER, GL_INT, startingPoint2D);

09-15-2010, 11:25 PM

yeah sorry i think i missed that.
okay i (think) im successfully implement my 2D integer texture. but the weird thing is my i implement the same thing for 1D texture, it returns a constant 0 (while im sure the data is not all 0).

this is how i put the 1D integer texture:

glTexImage1D(GL_TEXTURE_1D, 0, GL_R8I, textureDepth, 0, GL_RED_INTEGER, GL_INT, index);

in the GLSL code i put this on the top:

uniform isampler1D texIndex;

and i read it with texelFetch:

int currIndex = texelFetch(texIndex, startPointValue, 0).r;

startPointValue is an int type variable. anyone notice whats wrong with the code? do i need to mention any extension for isampler1D?

thanks in advance

EDIT: also try this 32 bit integer format

glTexImage1D(GL_TEXTURE_1D, 0, GL_R32I, textureDepth, 0, GL_RED_INTEGER, GL_INT, index);

and not working either. any advise?

02-03-2011, 07:58 PM
Hi jos_t_tarigan

Have you ever succeeded on this issue? I'm facing the very same problem and it's driving me crazy! I'd be grateful if you could give me some heads-up. Thanks in advance.

Texture initialization:
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, 16, 256, 0, GL_RED_INTEGER, GL_INT, content);

Geometry shader:
#version 330 core

layout(points) in;
layout(triangle_strip) out;
layout(may_vertices = 16) out;

uniform vec4 InEdgeVertexOffset[12];
uniform isampler2D InTriangleTable;

main() {
for(int i = 0; i < 16; i++) {
int edgeIndex = texelFetch(InTriangleTable, ivec2(i, PassEdgeTableIndex[0]), 0).r;

if(edgeIndex != -1) {
gl_Position = gl_in[0].gl_Position + InEdgeVertexOffset[edgeIndex];

if(++counter % 3 == 0) {
} else {

I'm trying to implement the Marching Cubes algorithm. Therefore, I need to lookup a 2d-texture containing the triangle table. This texture is called 'InTriangleTable'.

I can check its contents by using gDEBugger GL by graphicREMEDY. So, I can for sure say that each texel has a different value and none of them equals 0. Also, I can confirm that 'InEdgeVertexOffset' is initialized correctly and every entry in that array differs from each other.

So, why does texelFetch ALWAYS return 0?

02-03-2011, 10:09 PM

i dont have my code right now, gonna check it in the morning. but i blogged it, you can see it here:


if it still doesnt work i'll check my latest code 2morrow

02-03-2011, 11:30 PM
Hi jos_t_tarigan

Thanks for the reply.

It's exactly like I do it, but for some reason it just doesn't work... I'd be glad to have a look through your code. There's got to be something I'm not aware of.


02-04-2011, 09:12 AM
here it is:


the glsl is a little bit long. the spFit is the texIndexStartPoint in the GLSL. i use the real enum value since using GL_R32I gives me compiling error. quick question: have you enable the extension on the top of the fragment shader like in the blog?

02-04-2011, 11:24 AM

By default, it is set to use mipmaps. If you didn't supply mipmaps, or change the filter to not use mipmaps, then the texture is incomplete and you'll get [0,0,0,1] when sampling.

02-07-2011, 02:57 AM
Thanks a lot to both of you. It turned that it really was a problem with filtering...

03-03-2011, 08:16 PM
Thank you, arekkusu! I was having this exact same problem as suttewal and the solution was to generate mipmaps.