Program for computing texture coordinates.

Hi,

I have to fetch data from my texture images using texture coordinates. Does anyone here know how to fetch data from texture images using texture coordinates. Here is what I really intend to do:

I create a NPOT floating-point texture like TexA:
TexA:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

So, this TexA is 4x4. I am aware that in OpenGL texture coordinates are central pixel values, I mean like (0.5, 0.5)…(3.5, 3.5). So, I want to write a program which computes these texture coordinates values and when passed to the texture image behaves like:
(0.5, 0.5) fetches 0.
(0.5, 1.5) fetches 1.
(0.5, 2.5) fetches 2.

(3,5, 2.5) fetches 14.
(3.5, 3.5) fetches 15.

Please help me to create this program. I would appreciate if someone could pass their code if they have already done so.

Thanks.

0, 0 fetches the lower left, 1,1 fetches the upper right corner and not at the center of the texel it may be filtered half way between the edge and border or two opposite edges depending on the wrap mode and filters.

You can use the texture matrix to adjust this behavior by scaling etc.

Moving this to beginners, it’s not an advanced topic.

I think I didn’t make it clear enough. Here is what I intend to do:
I store my index values in a texture image. Then I fetch those index values to perform further texture indirection. This is where I get lost. I have written the following program but I don’t see all the correct results.

I create a texture like:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

Later, when I use the fragment program to perform texture indirection, I get results as: 0 1 2 3 0 0 0 0 0 0 0 0 0 0 0 0 instead of getting
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15.

Here is the relevant pieces of the source code:

#define WIDTH 4
#define HEIGHT 4

GLuint Tex;
GLuint FB;
GLuint FBO;
GLuint program;
GLfloat FakeTexture[HEIGHT][WIDTH];
float buffer[WIDTH * HEIGHT * 4];

void CreateFBO (void)
{
glEnable (GL_TEXTURE_RECTANGLE_NV);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glViewport (0, 0, WIDTH, HEIGHT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, WIDTH, 0, HEIGHT, -1, 1);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glGenFramebuffersEXT (1, &FB);
FrameBufferStatus ();
glGenTextures (1, &FBO);
glActiveTexture (GL_TEXTURE0_ARB);
glBindTexture (GL_TEXTURE_RECTANGLE_NV, FBO);
glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D (GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
} // CreateFBO

void Display (void)
{
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, FB);
glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_NV, Tex, 0);
glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_RECTANGLE_NV, FBO, 0);
glEnable (GL_FRAGMENT_PROGRAM_ARB);
glBindProgramARB (GL_FRAGMENT_PROGRAM_ARB, program);
glProgramEnvParameter4fARB (GL_FRAGMENT_PROGRAM_ARB, 0, WIDTH, 0.0, 0.0, 1.0);

glDrawBuffer (GL_COLOR_ATTACHMENT1_EXT);
Draw ();
ReadBack ();
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
glDisable (GL_FRAGMENT_PROGRAM_ARB);
} // Display

void Draw (void)
{
glActiveTexture (GL_TEXTURE1_ARB);
glEnable (GL_TEXTURE_RECTANGLE_NV);
glBindTexture (GL_TEXTURE_RECTANGLE_NV, Tex);
glBegin (GL_QUADS);
glMultiTexCoord2f (GL_TEXTURE1_ARB, 0.0, 0.0);
glVertex2f (0.0, 0.0);
glMultiTexCoord2f (GL_TEXTURE1_ARB, WIDTH, 0.0);
glVertex2f (WIDTH, 0.0);
glMultiTexCoord2f (GL_TEXTURE1_ARB, WIDTH, HEIGHT);
glVertex2f (WIDTH, HEIGHT);
glMultiTexCoord2f (GL_TEXTURE1_ARB, 0.0, HEIGHT);
glVertex2f (0.0, HEIGHT);
glEnd ();
glFinish ();
} // Draw

void CreateProgram (void)
{
const char* strProgram =
"!!ARBfp1.0
"
"OPTION NV_fragment_program2;
"
"ATTRIB ParticleIndex = fragment.texcoord[1];
"
"OUTPUT COLOR = result.color;
"
"PARAM Width = program.env[0];
"
"TEMP GridIndex;
"
"TEMP Index;
"
"TEMP test;
"
"TEMP temp;
"
"TEX Index, ParticleIndex, texture[1], RECT;
"
"DIVR GridIndex.y, Index.x, Width.x;
"
"FLRR GridIndex.y, GridIndex.y;
"
"MULR temp.x, GridIndex.y, Width.x;
"
"SUBR GridIndex.x, Index.x, temp.x;
"
"ADDR GridIndex, GridIndex, 0.5;
"
"TEX test, GridIndex, texture[1], RECT;
"
"MOVR COLOR, test;
"
"END
";

glGenProgramsARB (1, &program);
glBindProgramARB (GL_FRAGMENT_PROGRAM_ARB, program);
glProgramStringARB (GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei) strlen (strProgram), strProgram);
} // CreateProgram

void CreateTexture (void)
{
int i;
int j;
int c;

c = 0;
for (i = 0; i < HEIGHT; i++)
{
for (j = 0; j < WIDTH; j++)
{
FakeTexture[i][j] = c++;
}
}

glGenTextures (1, &Tex);
glActiveTexture (GL_TEXTURE1_ARB);
glBindTexture (GL_TEXTURE_RECTANGLE_NV, Tex);
glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D (GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, &FakeTexture);
} // CreateTexture

Thanks