I've created a class called TextureEditable
Bellow are the 2 functions I made. SetData, and SetSubData(). formatType can be TEX_RGB, TEX_RGBA, or TEX_ALPHA
bool TextureEditable::SetData(int width_, int height_, unsigned char *pixels, int formatType)
{
if ( !math::IsPower2(width_) || !math::IsPower2(height_) )
return false;
if ( !pixels )
return false;
if ( formatType != TEX_RGB && formatType != TEX_RGBA && formatType != TEX_ALPHA )
return false;
//ok, so the data that was passed in is good, only error that can possibly happen is if the data has less elements that width*height*bpp
format = formatType;
width = width_;
height = height_;
if ( data )
delete [] data;
int bpp = 1;
if ( format == TEX_RGB )
bpp = 3;
else if ( format == TEX_RGBA )
bpp = 4;
cout << "bpp = " << bpp << endl;
data = new unsigned char[width*height*bpp];
for ( int i=0; i < width*height*bpp; i++ )
data[i] = pixels[i];
//now we have the data
if ( texID == 0 ) //then we haven't generated a gl texture yet, so generate one
{
Texture::EnableGLTextures();
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
Texture::lastBoundTexture = texID;
cout << "creating tex " << texID << endl;
}
Texture::EnableGLTextures();
glBindTexture(GL_TEXTURE_2D, texID);
Texture::lastBoundTexture = texID;
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
if ( format == TEX_RGB )
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else if ( format == TEX_RGBA )
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
}
else if ( format == TEX_ALPHA )
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE , GL_UNSIGNED_BYTE, data);
}
return true;
}
bool TextureEditable::SetSubData(int x, int y, int w, int h, unsigned char *pixels)
{
if ( !pixels )
return false;
if ( x < 0 || x >= width || y < 0 || y >= height )
return false;
Texture::EnableGLTextures();
glBindTexture(GL_TEXTURE_2D, texID);
Texture::lastBoundTexture = texID;
if (format == TEX_RGBA )
{
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
}
else if ( format == TEX_RGB )
{
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
}
else if ( format == TEX_ALPHA )
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
}
OK, so those are the main functions I'm working with in that class.
in my main program I do this...
tex2 = new TextureEditable("");
unsigned char pixels[4*4] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
tex2->SetData(4,4,pixels,TEX_ALPHA);
unsigned char sub[2*2] = {255,255,255,255};
tex2->SetSubData(1,1,2,2,sub);
I render a quad with the texture mapped on it. What I should see is a grey square with a white square in the middle
Instead I see a grey square with a square in the middle that is shaded white in the first 2 texels and shaded different shades of grey for the other 2 texels
Note, the desired effect works when I use a texture that has GL_RGBA data. I think whats happening is when I call glTexSubImage2D its taking the pixel data i send it assumes its a RGBA texture when its a greyscale texture. But I'm not sure. Any help would be greatly appreciated.






