ColorTable Headache

I am trying to create a texture using a color table and multi-texturing to change the opacity and color of another texture.

My problem occurs when I don’t use textures of the power of two and use gluScaleImage to scale the texture with the color table indicies. It seems the indicies are changed slightly so that a previous index is used, creating a border around the texture.

The overall idea is to texture map a polygon and highlight or change the opacity of certain portions of that polygon on the fly.

Here’s my code:

unsigned int m_PixTexObj;
unsigned int m_HighTexObj;

GLubyte Table[2564];
GLubyte
m_fTemp;
GLubyte* m_fImage;

int m_nHeight = 300;
int m_nWidth = 300;

int m_nSWidth = 256;
int m_nSHeight = 256;

void CreateMainTexture()
{
int i,j,k;
for(j = 0,k = 0; j < m_nHeight; j++)
{
for(i = 0; i < m_nWidth*3; i+=3,k+=3)
{
m_Image[k] = m_PixelData->GetArray(j,i);
m_Image[k+1] = m_PixelData->GetArray(j,i+1);
m_Image[k+2] = m_PixelData->GetArray(j,i+2);
}
}

glGenTextures(1,m_PixTexObj);

// Enable the combiners for both RGB and alpha.
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE4_NV);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_EXT, GL_REPLACE);

//// RGB texture //////
glActiveTextureARB(GL_TEXTURE0_ARB);

glBindTexture(GL_TEXTURE_2D, m_PixTexObj);
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

// RGB source is Texture0 color.
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_TEXTURE0_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_EXT, GL_SRC_COLOR);

gluScaleImage(GL_RGB,
m_nWidth,m_nHeight,GL_UNSIGNED_BYTE,m_Image,
m_nSWidth,m_nSHeight,GL_UNSIGNED_BYTE,m_TexTemp);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,m_nSWidth,m_nSHeight,0,GL_RGB,GL_UNSIGNED_BYTE,m_TexTemp);

}

bool CreateHighlightTextures()
{
int i,j,k;

// Setup the Color Table //
for(i = 0; i < 256; i+=4)
{
Table[i] = 255;
Table[i+1] = 255;
Table[i+2] = 255;
Table[i+3] = 0;
}

// First Table Entry //
Table[0] = 0;
Table[1] = 0;
Table[2] = 0;
Table[3] = 0;

// Second Table Entry //
Table[4] = 0;
Table[5] = 255;
Table[6] = 0;
Table[7] = 255;

// Third Table Entry //
Table[8] = 0;
Table[9] = 0;
Table[10] = 255;
Table[11] = 255;

// Forth Table Entry //
Table[12] = 255;
Table[13] = 0;
Table[14] = 0;
Table[15] = 255;

// Fifth Table Entry //
Table[16] = 0;
Table[17] = 0;
Table[18] = 0;
Table[19] = 0;

//Create texture palette
glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, Table);

int res;
//Make sure palette is of correct size
glGetColorTableParameterivEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_COLOR_TABLE_WIDTH_EXT , &res);
if(res != 256)
{
MessageBox(NULL,“Unable to create texture palette of size 256.”,NULL,MB_OK);
return false;
}//if

//Make sure palette is of correct format
glGetColorTableParameterivEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_COLOR_TABLE_FORMAT_EXT , &tab_format);
if(tab_format != GL_RGBA8)
{
MessageBox(NULL,“Texture palette: incorrect format.”,NULL,MB_OK);
return false;
}//if

// End Setup Color Table//

// Allocate memory //
m_Temp = new GLubyte[m_nSWidthm_nSHeight];
m_Image = new GLubyte[m_nHeight
m_nWidth];

//Create Texture//
CreateHighTexture();

// DeAllocate memory//
delete m_Temp;
delete m_Image;

return true;
}

bool CreateHighTexture()
{

int x = 0;
int TabNum;
unsigned short Total;
for(int j = 0; j < m_nHeight; j++)
{
for(int k = 0;k < m_nWidth; k++,x++)
{

  	// For testing purposes //
  	if((k > m_nWidth/4) && (j > m_nHeight/4))
  		m_Image[x] = 1; // Second Entry (green)//

  	else
  	{
  		if((k < m_nWidth/2) && (j < m_nHeight/2))
  			m_Image[x] = 3; // Forth Entry (red) //
  		else
  			m_Image[x] = 0; // First Entry (transparent) //
  	}
  	// END For testing purposes //

  }

}

glGenTextures(1,m_HighTexObj);

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);

glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glPixelStorei(GL_PACK_ALIGNMENT,1);

glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);

glBindTexture(GL_TEXTURE_2D, &m_HighTexObj);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

// Alpha source is Texture1 color.
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_EXT, GL_TEXTURE1_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_EXT, GL_SRC_COLOR);

gluScaleImage(GL_COLOR_INDEX,
m_nWidth,m_nHeight,GL_UNSIGNED_BYTE,m_Image,
m_nSWidth, m_nSHeight,GL_UNSIGNED_BYTE,m_Temp);

glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, m_nSWidth, m_nSHeight,
0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, m_Temp);

return true;
}

[This message has been edited by DarthPaul (edited 01-24-2003).]

Try using the GL_CLAMP_TO_EDGE mode.

-SirKnight

I tried that and it didn’t work. Thanks though.

Look if the index #2 is at the borders of the GL_COLOR_INDEX texture after you filtered it with gluScale.
See manual: “When shrinking an image, gluScaleImage uses a box filter to sample the source image and create pixels for the destination image. When magnifying an image, the pixels from the source image are linearly interpolated to create the destination image.”
If this is really done on GL_COLOR_INDEX you’re hosed.
Write your own function doing nearest filtering on indices instead the box filter.