Original Post
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()?
#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;}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more