GL_TEXTURE_2D vs GL_TEXTURE_RECTANGLE
Asked Answered
B

1

6

I have a little program that render a yellow triangle twice, once on the left half of a framebuffer and once on the right side.

Dump of the texture

enter image description here

Now, after that I render the content of this framebuffer on the screen.

It works if I use GL_TEXTURE_RECTANGLE in the framebuffer constructor:

https://github.com/elect86/Joglus/blob/master/Joglolus/src/joglus/example1/FrameBuffer.java

In binding the texture, function renderFullScreenQuad, line 372:

https://github.com/elect86/Joglus/blob/master/Joglolus/src/joglus/example1/GlViewer.java

And using sampler2DRect in the fragment shader:

#version 330

out vec4 outputColor;
uniform sampler2DRect texture0;

void main() {
    outputColor = texture(texture0, gl_FragCoord.xy);
}

But if I change all the RECTANGLE to 2D and I use sample2D in the fs, I get a total black image at the end of the display(), although the dump of the texture shows always the correct image... I would like to know why.

Bur answered 6/8, 2014 at 9:55 Comment(0)
D
13

Texture coordinates work differently between textures of types GL_TEXTURE_RECTANGLE and GL_TEXTURE_2D:

  • For GL_TEXTURE_RECTANGLE, the range of texture coordinates corresponding to the entire texture image is [0.0, width] x [0.0, height]. In other words, the unit of the texture coordinates is in pixels of the texture image.
  • For GL_TEXTURE_2D, the range of texture coordinates is [0.0, 1.0] x [0.0, 1.0].

With this statement in your fragment shader:

outputColor = texture(texture0, gl_FragCoord.xy);

you are using coordinates in pixel units as texture coordinates. Based on the above, this will work for the RECTANGLE texture, but not for 2D.

Since your original input coordinates in the vertex shader appear to be in the range [0.0, 1.0], the easiest approach to fix this is to pass the untransformed coordinates from vertex shader to fragment shader, and use them as texture coordinates. The vertex shader would then look like this:

#version 330

layout (location = 0) in vec2 position;
out vec2 texCoord;
uniform mat4 modelToClipMatrix;

void main() {
    gl_Position = modelToClipMatrix * vec4(position, 0, 1);
    texCoord = position;
}

And the fragment shader:

#version 330

in vec2 texCoord;
out vec4 outputColor;
uniform sampler2D texture0;

void main() {
    outputColor = texture(texture0, texCoord);
}
Donoho answered 7/8, 2014 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.