Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem on texture data type

Started by rosicky2005 Sep 25, 2006 at 1:05 AM 3 replies 600+ views
Original Post
rosicky2005
rosicky2005
Is it possible to declare a texture of type "int"? If I have a piece of data array which is int. Then how can I map the int array to a texture using the function glTexImage2D()?
Lord_Evil
Lord_Evil
If you mean that each pixel is represented by an int, use something like glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, data);

Please check the OpenGL reference for all possible color and pixel formats.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
rosicky2005
rosicky2005
thanks
but it doesn't work, here is my code
#include <stdio.h>#include <stdlib.h>#include <GL/glew.h>#include <GL/glut.h>int main(int argc, char **argv) {    // declare texture size, the actual data will be a vector     // of size texSize*texSize*4    int texSize = 2;    // create test data    int* data = (int*)malloc(4*texSize*texSize*sizeof(int));    int* result = (int*)malloc(4*texSize*texSize*sizeof(int));    for (int i=0; i<texSize*texSize*4; i++)        data = i+1;    // set up glut to get valid GL context and     // get extension entry points    glutInit (&argc, argv);    glutCreateWindow("TEST1");    glewInit();    // viewport transform for 1:1 pixel=texel=data mapping    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluOrtho2D(0.0,texSize,0.0,texSize);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    glViewport(0,0,texSize,texSize);    // create FBO and bind it (that is, use offscreen render target)    GLuint fb;    glGenFramebuffersEXT(1,&fb);     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fb);    // create texture    GLuint tex;    glGenTextures (1, &tex);    glBindTexture(GL_TEXTURE_RECTANGLE_ARB,tex);    // set texture parameters    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);		//modify    // define texture with integer format        glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGBA,                 texSize,texSize,0,GL_RGBA,GL_UNSIGNED_INT,0);    	// attach texture    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,                               GL_COLOR_ATTACHMENT0_EXT,                               GL_TEXTURE_RECTANGLE_ARB,tex,0);    // transfer data to texture        glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,0,0,0,texSize,texSize,                    GL_RGBA,GL_UNSIGNED_INT,data);    // and read back    glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);    glReadPixels(0, 0, texSize, texSize,GL_RGBA,GL_UNSIGNED_INT,result);       // print out results    printf("Data before roundtrip:\n");    for (int i=0; i<texSize*texSize*4; i++)        printf("%d\n",data);        printf("Data after roundtrip:\n");    for (int i=0; i<texSize*texSize*4; i++)        printf("%d\n",result);	system("pause");    // clean up    free(data);    free(result);    glDeleteFramebuffersEXT (1,&fb);    glDeleteTextures (1,&tex);    return 0;}

then the result data, result print out all zero.

[Edited by - rosicky2005 on September 26, 2006 3:59:47 AM]
Brother Bob
Brother Bob
When using the type GL_UNSIGNED_INT for texture data, the full range of the color data is [0...232-1]. The range [0, 255] applies only when you use GL_UNSIGNED_BYTE data. The range expands exponentially as the size of the data type increases.

Assume the texture's internal format is 8 bits. That means the 24 lower bits in the integers representing the data is effectivel unused. Value 0-16777215 maps to color 0, 16777216-33554431 maps to color 1, and so on. So you can see, unless your texture is larger than 2048×2048, all colors in your image will be 0 when converted to the internal 8-bit precision.
rosicky2005
rosicky2005
Here is my new coding:

#include
#include
#include
#include

int main(int argc, char **argv) {
// declare texture size, the actual data will be a vector
// of size texSize*texSize*4
int texSize = 1024;

// create test data
int* data = (int*)malloc(4*texSize*texSize*sizeof(int));
int* result = (int*)malloc(4*texSize*texSize*sizeof(int));
for (int i=0; i data = i+1;
// set up glut to get valid GL context and
// get extension entry points
glutInit (&argc, argv);
glutCreateWindow("TEST1");
glewInit();
// viewport transform for 1:1 pixel=texel=data mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,texSize,0.0,texSize);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,texSize,texSize);
// create FBO and bind it (that is, use offscreen render target)
GLuint fb;
glGenFramebuffersEXT(1,&fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fb);
// create texture
GLuint tex;
glGenTextures (1, &tex);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,tex);
// set texture parameters
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);

//modified
// define texture with floating point format
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGBA32F_ARB,
texSize,texSize,0,GL_RGBA,GL_FLOAT,0);
//glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,
// texSize,texSize,0,GL_RGBA,GL_UNSIGNED_INT,0);

// attach texture
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_RECTANGLE_ARB,tex,0);
//glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
// GL_COLOR_ATTACHMENT0_EXT,
// GL_TEXTURE_2D,tex,0);
// transfer data to texture

//modified
glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,0,0,0,texSize,texSize,
GL_RGBA,GL_FLOAT,data);
//glTexSubImage2D(GL_TEXTURE_2D,0,0,0,texSize,texSize,
// GL_RGBA,GL_UNSIGNED_INT,data);
// and read back
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);

//modified
glReadPixels(0, 0, texSize, texSize,GL_RGBA,GL_FLOAT,result);
//glReadPixels(0, 0, texSize, texSize,GL_RGBA,GL_UNSIGNED_INT,result);

// print out results
printf("Data before roundtrip:\n");
for (int i=0; i printf("%d\n",data);
printf("Data after roundtrip:\n");
for (int i=0; i printf("%d\n",result);
system("pause");
// clean up
free(data);
free(result);
glDeleteFramebuffersEXT (1,&fb);
glDeleteTextures (1,&tex);
return 0;
}

The array "data" and "result" maps the same value, but after I use GLSL(shading language) to do calculation in the texture value, I cannot map back
data=result .Instead all result turns zero.I don't know why.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.