Reading Integer Values from Texture

Hi all,



i’m trying to use a Texture as Data Storage and i want to read Integer values from it.



If i try this

[java](Fragment Shader)

int val = texture2D(m_tdata, texCoord).r;[/java]



i get an Error “implicit cast from float to int” (which is not surprising)



I found this





and tried to change the class com.jme3.renderer.lwjgl.TextureUtil.java to upload the texture with the “GL_LUMINANCE_INTEGER_EXT” flag but this flag doesent exist in lwjgl…



so any idea how to read integer values from a texture? It doesent even have to be via the real Texture Coordinate Position like in the code above, i just want to use the texture like some kind of array.

I already tried using uniform arrays and it works but the amount of data which can be uploaded seems to be very limited and it seems to be uploaded every rendering step. So i don’t think uniform arrays are a good solution for this.



Thanks for any help :slight_smile:

[java]int val = int(texture2D(m_tdata, texCoord).r);[/java]

Sorry i didn’t mention before, but i already tried that.

Lets say i put an int 10 into the texture image ByteBuffer:



[java]

(at buffer position 0)

byteBuffer.putInt(10);

[/java]



now i want to read that value in Fragment Shader:



[java]int val = int(texture2D(m_tdata, vec2(0,0)).r);[/java]



what i get is 0! Because the float value is always between 0 and 1…



Interestingly the following works at some (very few) positions of the texture (assumed i previously put those int values all over the buffer):



[java]



float val = texture2D(m_tdata, texCoord).r;



if(val*255 == 10.0 ){

// This will sometimes be executed

}



[/java]

try that

[java]

int val = int(round(texture2D(m_tdata, texCoord).r * 255.0));



if(val == 10){

//do your stuff

}

[/java]

Normal textures aren’t designed to hold integer values. You can feed them in your program with bytes in range 0 to 255 but in your shader a texture will always produce a float in range 0 to 1. If you want an integer in your shader you should multiply this float as you said, but only use texture coordinates that will be mapped to the center of a pixel! Else the GPU interpolates the values of the surrounding pixels and you will get something like 10.3 or 9.6.