   How to render 32-bit integer into textures?
  Hello guys~n my application, I want to render integer numbers into texture, and the number will be pretty huge(maybe millions), that's the reason I wanna use 32-bit uints.Actually, the numbers are IDs of every polygon in the scene. I want to store some of these IDs in a texture channel and then do some calculation in GPU via shaders.    First, I tried RGBA32UI as internal format, GL_RGBA_INTEGER as format, and GL_UNSIGNED_INT as type in texture.    But RGBA32UI is a non-normalized color format. So if I use glColor()(which uses te normalized color format) to set current color and render the scene, then use glReadPixels() to read the texture data back into a uint array, the data was strange.   So my question is, how to render 32-bit integer into textures?   Thank you all very much.    The whole code will be pasted as follows:   
  The code is very simple:Code:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <GL/glew.h>#include <windows.h>#include <GL/glut.h>typedef unsigned char BYTE;int main(int argc, char **argv) {int texSize = 4;int i;//to store read back dataGLuint result[4*texSize*texSize];//to store source dataGLuint* data = (GLuint*)malloc(4*texSize*texSize*sizeof(GLuint));for (i=0; i<texSize*texSize*4; i++){data = 1; //whatever values}glutInit (&argc, argv);glutCreateWindow("TEST1");glewInit();glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,texSize,0.0,texSize);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glViewport(0,0,texSize,texSize);// Create FBOGLuint fb;glGenFramebuffersEXT(1,&fb;glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fb);//create texture bufferGLuint fboTex;glGenTextures (1, &fboTex);glBindTexture(GL_TEXTURE_RECTANGLE_ARB,fboTex);glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_WRAP_S, GL_CLAMP);glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_WRAP_T, GL_CLAMP);glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGBA32UI,texSize,texSize,0,GL_RGBA_INTEGER,GL_UNSIGNED_INT,data);//create render bufferGLuint rboId;glGenRenderbuffersEXT(1, &rboId);glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rboId);glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, texSize, texSize);//bind FBO with render buffer and texutre bufferglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT,GL_TEXTURE_RECTANGLE_ARB,fboTex,0);glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,rboId);glClearColor(1, 1, 1, 1);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glColor4ui(1,2,3,4);    glBegin(GL_QUADS);   glVertex2i(0,0);   glVertex2i(0,4);   glVertex2i(4,4);   glVertex2i(4,0);    glEnd();// Read back the texture dataglReadBuffer(GL_COLOR_ATTACHMENT0_EXT);glReadPixels(0, 0, texSize, texSize,GL_RGBA_INTEGER,GL_UNSIGNED_INT,result);//out put datafor (i=0; i<texSize*texSize*4; i++)printf("%u\n",result);free(data);glDeleteFramebuffersEXT (1,&fb;glDeleteTextures(1,&fboTex);return 0;};  I'll try your program later. What result are you getting?What do you mean "the data was strange"?   ;Quote:Original post by V-manI'll try your program later. What result are you getting?What do you mean "the data was strange"?Hi~I used glColor4ui(1,2,3,4) to set vertex color and type is unsigned integer, so I need the output is 1,2,3,4 for RGBA respectely per pixel.But the actual output of the codes is not 1,2,3,4.So any solutions? Thanks!   ;  I think you might need to use a shader, glColor won't work with integers like that:Quote:Current color values are stored in floating-point format, with unspecified mantissa and exponent sizes.Unsigned integer color components, when specified, are linearly mapped to floating-point values such that the largest representable value maps to 1.0 (full intensity), and 0 maps to 0.0 (zero intensity). Signed integer color components, when specified, are linearly mapped to floating-point values such that the most posit representable value maps to 1.0, and the most negative representable value maps to -1.0. (Note that this mapping does not convert 0 precisely to 0.0.) Floating-point values are mapped directly.My guess is you will end up wanting one of these:    void VertexAttribI1iEXT(uint index, int x);    void VertexAttribI2iEXT(uint index, int x, int y);    void VertexAttribI3iEXT(uint index, int x, int y, int z);    void VertexAttribI4iEXT(uint index, int x, int y, int z, int w);gpu_shader4 Extension[Edited by - karwosts on May 19, 2010 11:44:56 PM];Quote:Original post by karwostsI think you might need to use a shader, glColor won't work with integers like that:Quote:Current color values are stored in floating-point format, with unspecified mantissa and exponent sizes.Unsigned integer color components, when specified, are linearly mapped to floating-point values such that the largest representable value maps to 1.0 (full intensity), and 0 maps to 0.0 (zero intensity). Signed integer color components, when specified, are linearly mapped to floating-point values such that the most posit representable value maps to 1.0, and the most negative representable value maps to -1.0. (Note that this mapping does not convert 0 precisely to 0.0.) Floating-point values are mapped directly.My guess is you will end up wanting one of these:    void VertexAttribI1iEXT(uint index, int x);    void VertexAttribI2iEXT(uint index, int x, int y);    void VertexAttribI3iEXT(uint index, int x, int y, int z);    void VertexAttribI4iEXT(uint index, int x, int y, int z, int w);gpu_shader4 ExtensionThank you for your reference!So my only chance is to use shader? There's nothing I can do with Vertex color? Cause if I can give vertex the integer colors, I can use OpenGL default pipe-line to render it into textures.   ;  Well the problem is that when you use glColor it is always converted into floating point by the gpu before your texture ever sees it. Integer support in gpus is a relatively recent development, so not all of the old functions support it. glColor doesn't really make sense as a large integer anyway.One quick trick you might be able to try is to use "fake" floats, and hope that the gpu doesn't modify them at all.For example if you have the integer 16 which is 0x00000010 binary.You can possibly use reinterpret cast it to a float such that it is still 0x00000010 and then send that as your color. As a float this number will be totally bogus and meaningless, but it might be stupid enough to write this value to your texture. The only issues I see is that you might crash if you end up with NaN or INF or some weird float special values. You'd have to get it to not scale or clamp the float at all and accept negative numbers which I'm not even sure is possible.I'm very skeptical that this would work, but if you're desperate enough might be worth a try.I suggest you go for a shader.   ;  Thanks for your detailed information, Karwost! I'll try that.   ;  You can also use 0.0 to 1.0 can convert it to integer by multiplying by (color * 255.0) or similar. I've had to do that in the past.   