PDA

View Full Version : texture-lookup using ARB_fragment_program on ATI FireGL card



clunis_immensus
02-14-2005, 01:01 PM
I am trying to implement a texture palette on my ATI FireGL card using ARB_fragment_program. I tried modifying one of the samples provided by ATI, but all I get is a blank display.

My fragment program:

!!ARBfp1.0
ATTRIB TexCoord = fragment.texcoord;
TEMP Index;
TEX Index, TexCoord, texture, 2D;
TEX result.color, Index, texture[1], 1D;
END

In case there is something wrong with the setup, I've included the entire code.
Any help would be immensely appreciated.




#include <stdio.h>
#include <glut.h>
#include "ATIExtensions.h"

int width = 400;
int height = 400;
float distance = 0.0f;
float zoom = 60.0f;
GLuint fShad;
GLuint nTex, nPalette;

GLuint loadFragmentProgram( char* fn)
{
FILE *fp;
GLubyte *buf;
int length;
bool ret = true;
GLuint rp;

if (!(fp = fopen(fn,"rb")))
{
return false;
}

fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, 0, SEEK_SET);

buf = new GLubyte[length+1];

fread( buf, 1, length, fp);
buf[length] = '\0'; // make it a regular C string so str* functions work

glGenProgramsARB( 1, &amp;rp);
glBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, rp);

glProgramStringARB( GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, length, buf);

if (glGetError() != 0)
{
int position;
printf("%s\n", glGetString(GL_PROGRAM_ERROR_STRING_ARB));
glGetIntegerv( GL_PROGRAM_ERROR_POSITION_ARB, &amp;position);
printf(" @%d\n", position);
ret = false;
}
else
{
printf(" Load succeeded\n");
}

fclose(fp);

delete []buf;
return ret ? rp : 0;
}

void init()
{
// dependent texture
glGenTextures(1, &amp;nTex);
glActiveTextureARB( GL_TEXTURE0_ARB);
glBindTexture( GL_TEXTURE_2D, nTex);

unsigned char pSlice[256*256], *ptr;
ptr = pSlice;
for(int i=0; i<256; ++i)
{
memset(ptr, i, 256);
ptr += 256;
}

glTexImage2D(
GL_TEXTURE_2D, // target
0, // level
1, // internal format
256, // width
256, // height
0, // border
GL_LUMINANCE, // format
GL_UNSIGNED_BYTE, // type
(void*) pSlice
);

// texture palette
glGenTextures(1, &amp;nPalette);
glActiveTextureARB( GL_TEXTURE1_ARB);
glBindTexture( GL_TEXTURE_1D, nPalette );

float pfLUT[256*4], *pPtr;
pPtr = pfLUT;
for(int l=0; l<256; ++l)
{
pPtr[0] = l < 128 ? 0.0 : 1.0;
pPtr[1] = pPtr[2] = pPtr[3] = 1.0;
pPtr += 4;
}


glTexImage1D(
GL_TEXTURE_1D, // target
0, // level
4, // internal format
256, // width
0, // border
GL_RGBA, // format
GL_FLOAT, // type
(void*) pfLUT
);

fShad = loadFragmentProgram("lut.txt");
}


//////////////////////////////////////////////////////////////////////
void drawModel()
{
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor3f(0,1,0);

glActiveTextureARB( GL_TEXTURE0_ARB);
glBindTexture( GL_TEXTURE_2D, nTex);

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-.5, -.5);
glTexCoord2f(1, 0);
glVertex2f(.5, -.5);
glTexCoord2f(1, 1);
glVertex2f(.5, .5);
glTexCoord2f(0, 1);
glVertex2f(-.5, .5);
glEnd();
}

//////////////////////////////////////////////////////////////////////
void draw()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode( GL_PROJECTION);
glLoadIdentity();
gluPerspective( zoom, (float)width/(float)height, distance/10.0f, distance*2.0f);

glMatrixMode( GL_MODELVIEW);
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -distance);

glBindProgramARB( GL_FRAGMENT_PROGRAM_ARB, fShad);
glEnable( GL_FRAGMENT_PROGRAM_ARB);

drawModel();

glDisable( GL_FRAGMENT_PROGRAM_ARB);
glActiveTextureARB( GL_TEXTURE0_ARB);

glutSwapBuffers();
}


//////////////////////////////////////////////////////////////////////
int main( int argc, char **argv)
{
glutInit( &amp;argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize( width, height);

glutCreateWindow("Simple Shader- ATI Fragment Program loader");

glutDisplayFunc( draw);
glutIdleFunc( draw);

SetupGL1_4();
SetupATIExtensions();
SetupARBExtensions();

init();

glutMainLoop();

return 0;
}

3B
02-15-2005, 03:40 AM
My guess from a quick glance at the code, is that you need to turn off mipmapping, or generate mipmaps...

clunis_immensus
02-15-2005, 11:14 AM
Originally posted by 3B:
My guess from a quick glance at the code, is that you need to turn off mipmapping, or generate mipmaps...How would I turn off mipmapping?

clunis_immensus
02-16-2005, 10:23 AM
Thanks to a suggestion I received, it works now.
I had to change the palette to a 2D texture:

!!ARBfp1.0
TEMP dataTexCoord, dataColor;
TEX dataTexCoord, fragment.texcoord, texture[0], 2D;
TEX result.color, dataTexCoord, texture[1], 2D;
END

thanks for the suggestions, everyone!