PDA

View Full Version : Switching textures in opengl??



Randulf
18th September 2006, 16:42
Hi!
Im using qt and opengl to show data from a buffer as textures in opengl. The buffer has several sub images that I want to switch between. The problem is that It wont switch.
My QGLWidget has a method for updating the gl content where a pointer to the beginning of the buffer as argument. Then the textures made.


void GLWidget::setTexture(unsigned char *_data, float transfer, int dispChannelOne, int dispChannelTwo, int dispChannelThree)
{
displayChannels = dispChannelOne + dispChannelTwo + dispChannelThree;
channelOne = dispChannelOne;
channelTwo = dispChannelTwo;
channelThree = dispChannelThree;
textureData = _data;

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

// Update the texture for channel one
if(channelOne == 1) {
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0,
GL_LUMINANCE,
512, 512, 0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
textureData+0*512*512);
}
// Update the texture for channel two
if(channelTwo == 1) {
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0,
GL_LUMINANCE,
512, 512, 0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
textureData + 1*512*512); // + subchannels * widht * height
}
// Update the texture for channel three
if(channelThree == 1) {
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexImage2D(GL_TEXTURE_2D, 0,
GL_LUMINANCE,
512, 512, 0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
textureData + 2*512*512); // + subchannels * widht * height
}
updateGL();
}
void GLWidget::paintGL()
{

// Clear color buffer
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClear( GL_COLOR_BUFFER_BIT );

// Set up the camera and projection
// Select and setup the projection matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();

gluOrtho2D( -1.0f, 1.0f, -1.0f, 1.0f );
glViewport( 0, 0, 512, 512);
if(hasTexture) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
if(channelOne == 1 && channelTwo == 0)
glBindTexture(GL_TEXTURE_2D, texture[0]);
if(channelOne == 0 && channelTwo == 1)
glBindTexture(GL_TEXTURE_2D, texture[1]);
}
glBegin( GL_QUADS );
glTexCoord2f( 1.0f, 0.0f );
glVertex2f( -1.0f, -1.0f );

glTexCoord2f( 0.0f, 0.0f );
glVertex2f( 1.0f, -1.0f );

glTexCoord2f( 0.0f, 1.0f );
glVertex2f( 1.0f, 1.0f );

glTexCoord2f( 1.0f, 1.0f );
glVertex2f( -1.0f, 1.0f );
glEnd();
}

I can se the second texture but cannot switch to the first.

wysota
18th September 2006, 17:40
if(channelOne == 1 && channelTwo == 0)
Maybe this condition is never met?

Randulf
18th September 2006, 17:59
Found the problem, it only took me 4 hours : )

When generating the textures glTexParameteri must be set for all textures.