Texture not applied to viewport-filling quad

The purpose in the below code is to grab the frame buffer, send it to the current texture as luminance values, and then modulate the texture onto a quad that fills the viewport. The idea being that the frame buffer is just replaced by the same image, but as luminance values in the color specified by redColor, greenColor, and blueColor. I know the texture gets loaded, because the code thats been commented out shows that the texture changes when the input frame buffer is changed. Problem is, this doesn’t work … it shows the quad, but only in the color, without the texture modulation. Anyone know what I might be missing?


                int[] viewportInfo = new int[4];
                g.glGetIntegerv(g.GL_VIEWPORT, viewportInfo,0);
                
                g.glDrawBuffer(g.GL_BACK);
                g.glReadBuffer(g.GL_BACK);
                            
                    //setup texture environment and enable
                    g.glEnable(g.GL_TEXTURE_2D);
                    g.glTexEnvf(g.GL_TEXTURE_ENV, g.GL_TEXTURE_ENV_MODE,g.GL_MODULATE);
                    
                    //Grab whole scene into current texture in luminance mode
                    g.glCopyTexImage2D(g.GL_TEXTURE_2D, 0, g.GL_LUMINANCE, viewportInfo[0], viewportInfo[1], viewportInfo[2], viewportInfo[3], 0);
                                        
                    g.glClear(g.GL_COLOR_BUFFER_BIT|g.GL_DEPTH_BUFFER_BIT);
                    
                    g.glDisable(g.GL_DEPTH_TEST);
                    g.glDisable(g.GL_CULL_FACE);
                    
                    
                    //Draw single quad filling screen with the texture mapped onto it, modulating the color
                    
                        g.glColor3f(this.colorRed,this.colorGreen,this.colorBlue);
                    
                    g.glMatrixMode(g.GL_PROJECTION);
                    g.glLoadIdentity();
                    g.glMatrixMode(g.GL_MODELVIEW);
                    g.glLoadIdentity();
                                    
                    /* TODO remove diagnostic code
                    FloatBuffer imageBuff = FloatBuffer.allocate(viewportInfo[2]*viewportInfo[3]);
                    g.glGetTexImage(g.GL_TEXTURE_2D,0,g.GL_LUMINANCE,g.GL_FLOAT,imageBuff);
                    float[] fla = imageBuff.array();int count = 0;
                    for (float f : fla) {
                        if (f!=0)
                            count++;
                    }
                    System.err.println("number of non-zero elements="+count);
                    */
                    
                    g.glBegin(g.GL_QUADS);
                    //lower-left
                    g.glTexCoord2f(0,0);
                    g.glVertex2f(-1,-1);
                    //lower-right
                    g.glTexCoord2f(1,0);
                    g.glVertex2f(1,-1);
                    //upper-right     
                    g.glTexCoord2f(1,1);
                    g.glVertex2f(1,1);
                    //upper-left
                    g.glTexCoord2f(0,1);
                    g.glVertex2f(-1,1);
                    g.glEnd();
                    
                    
                    g.glPopAttrib();
                    

I can see nowhere in your code a call to glBindTexture(), also I believe you have to set the GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER texture parameters (presumably, GL_NEAREST will do for both).

It isn’t neccessary to call glBindTexture, because I’m just using one texture at a time (glBindTexture is for saving textures into texture memory)… but you’re right that the filters had to be set. I added those two lines in, and now it works - thanks!

Unfortunately, this still isn’t doing what I hoped it would… the glCopyTexImage2D with the GL_LUMINANCE format only seems to grab the red value… does anyone know of a way to do that transfer in hardware?

Remind us what exactly you’re trying to achieve here! As far as I gathered, you’re doing a screen capture into a GL_LUMINANCE texture (at the moment, this will copy the red channel into luminance values of your texture), followed by drawing the texture modulated with an RGB colour value.

If you want some other combination of the original RGB framebuffer values then I’d guess you better copy into a 32-bit RGBA texture and use texture combiners, or even a fragment shader if your hardware allows for this.

For the use of the framebuffer as texture search for “OpenGL framebuffer objects” or “OpenGL FBO”. Here is a small example: http://www.codesampler.com/oglsrc/oglsrc_14.htm (second on this page)

I’m trying to capture into a GL_LUMINANCE texture, but instead of just taking the red channel, I want it to work like GL_LUMINANCE does for glReadPixels, and actually calculate the luminance value from the average R, G, and B values. I know I can do this with a vertex shader, but this is such a simple operation, so I’m hoping I can do this without resorting to a shader… (I think this mainly because glReadPixels does what I want, its just that it sends it to memory instead of a texture…)

And I don’t see how using an FBO helps here (other than maybe improving performance)… is there some way to extract the luminance values there that isn’t available by rendering directly to the back buffer and copying it as a texture?