Hi,
I just want to copy a part of a texture to another. (the original texture size is 640x480, but only the lower left quarter is used - thus the actual texture size is 320x240). Let’s call this first texture texA. I want to store this 320x240 texture (lower right part of texA) to another (640x480) texture (called texB). Of course the texture will be magnified and look a bit “pixelated”.
I bind texA, and map the texture to a quad. (assume the identity matrices). The current viewport is 640x480 (0,0,640,480) - I think it matters, doesn’t it?
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texA);
// draw to the framebuffer
glBegin(GL_QUADS);
//lower left
glTexCoord2f( 0, 0); glVertex2f(-1, -1);
//upper left
glTexCoord2f( 0, 0.5); glVertex2f(-1, 1);
//upper right
glTexCoord2f( 0.5, 0.5); glVertex2f(1, 1);
//lower right
glTexCoord2f( 0.5, 0); glVertex2f(1, -1);
glEnd();
// create init texB
glGenTextures(1,texB);
glBindTexture(GL_TEXTURE_2D, texB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, deltaWidth, deltaHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// copy content
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 640, 480);
Then I copy the current framebuffer to the other (640x480) texture using glCopyTexSubImage2D. I expect that texB holds the magnified 320x240 texture of texA. Can anybody confirm this?
Later I use texB to finally draw the texture (again) to the framebuffer. (Actually I use 3 textures which get combined in a fragment shader) - In general it works, but the final result looks like texA. (only the lower left part of the screen shows the texture). Can anybody please give me a hint? - Do I miss something essential?
// draw the final texB, texC, and texD combined (see shader)
glBegin(GL_QUADS);
//lower left
glTexCoord2d(0.0, 0.0); glVertex2f(-1,-1);
//upper left
glTexCoord2d(0.0, 1.0); glVertex2f(-1,1);
//upper right
glTexCoord2d(1.0, 1.0); glVertex2f(1,1);
//lower right
glTexCoord2d(1.0, 0.0); glVertex2f(1,-1);
glEnd();
the vertex shader:
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
}
the fragment shader:
uniform sampler2D redTexture;
uniform sampler2D greenTexture;
uniform sampler2D blueTexture;
uniform float opacityR;
uniform float opacityG;
uniform float opacityB;
uniform float bypassShader; // is used to draw the source texture to the framebuffer. the redTexture holds the original frame in this case! TODO: change to boolean
void main( void )
{
if (bypassShader > 0.0f) {
vec4 colorRed = texture2D(redTexture,gl_TexCoord[0].st);
gl_FragColor = colorRed;
}
else {
vec4 colorRed = texture2D(redTexture, gl_TexCoord[0].st);
vec4 colorGreen = texture2D(greenTexture, gl_TexCoord[0].st);
vec4 colorBlue = texture2D(blueTexture, gl_TexCoord[0].st);
vec4 result;
result.r = colorRed.r * opacityR;
result.g = colorGreen.g * opacityG;
result.b = colorBlue.b * opacityB;
result.a = max(max(colorRed.a, colorGreen.a), colorBlue.a);
gl_FragColor = result;
}
}