Search code examples
openglscaletextures

replacing gluScaleImage by a scaled texture quad : but texture not rendered


I am trying to scale an image without using the function gluScaleImage which is too long (and not achieving to include "glh" lib in my project (to use glhScaleImage)). I read this advice:

If this is just about displaying images with various sizes, why not create a textured quad and scale it to the requested size? The texture could always be the same size then.

And that is what I try to do with the following code : the result is that I see well written "BACK" in red but don't see "before glflush" and the background has a strange color (sometimes purple, sometimes green (concerning the green issue i suspect it to be a wrong initialization of the graphic card)) => can anyone tell me what is wrong in my texturing of the buffer obtained with glReadPixels ? => or is there another solution ? (I would prefer a correction of this code than another solution (framebuffer, ...)).

// ---
glColor4ub(255,0,0,200);
myPrintingFunction(200,200,"before glFlush");   
glFlush();

// Unload Bytes
if (mBuffer != NULL)
{
    delete[] mBuffer;
    mBuffer = NULL;
}

// Load Bytes from current window
mBuffer = new GLubyte[ClientWidth * ClientHeight * 4];
glReadBuffer(GL_BACK);
glReadPixels(0, 0, ClientWidth, ClientHeight, GL_BGRA, GL_UNSIGNED_BYTE, mBuffer);

SwapBuffers(ghDC);    

// Unload TextureId
if (mTextureId != 0)
{
    glDeleteTextures(1, &mTextureId);
    mTextureId = 0;
}

// Load TextureId
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &mTextureId);
glBindTexture(GL_TEXTURE_2D, mTextureId);
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, ClientWidth, ClientHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, mBuffer);

// ---
glViewport(-mBufferWidth, -mBufferHeight, mBufferWidth * 2, mBufferHeight * 2);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(double(-mBufferWidth), double(mBufferWidth), double(-mBufferHeight), double(mBufferHeight), 1000.0, 100000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0, 0.0, -50000.0);

glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, mTextureId);
mxIncrust0 = 0.0;
myIncrust0 = 0.0;
mxIncrust1 = mBufferWidth;
myIncrust1 = mBufferHeight;
glBegin(GL_QUADS);
    glColor4ub((unsigned char)255,(unsigned char)255,(unsigned char)255,(unsigned char)255);
    glTexCoord2d(0.0, 0.0);
    glVertex2d(mxIncrust0, myIncrust0);
    glTexCoord2d(0.0, 1.0);
    glVertex2d(mxIncrust0, myIncrust1);
    glTexCoord2d(1.0,1.0);
    glVertex2d(mxIncrust1, myIncrust1);
    glTexCoord2d(1.0,0.0);
    glVertex2d(mxIncrust1, myIncrust0);
glEnd();

glDisable(GL_TEXTURE_2D);  

glColor4ub(255,0,0,200);
myPrintingFunction(100,100,"BACK");                                        

glFlush();

if (mBufferPlayout != NULL)
{
    delete[] mBufferPlayout;
    mBufferPlayout = NULL;
}
mBufferPlayout = new GLubyte [mBufferWidth * mBufferHeight * 4];

glReadBuffer(GL_BACK);
glReadPixels(0, 0, mBufferWidth, mBufferHeight, GL_BGRA, GL_UNSIGNED_BYTE, mBufferPlayout);

// then I pass this mBufferPlayout to a graphic card (capture card in output in fact)

Solution

  • Ok I found the error (with glGetError()) :
    GL_BGRA does not exist
    So I replaced it with GL_RGBA and I have my texture well bound to the quad
    (GL_RGBA8 does not work)
    I still have a greenish taint issue but this must be another problem : frame initialized with YUV ?