Minimal test case not working - using intermediate framebuffer

Started by
9 comments, last by Ploppz 9 years, 3 months ago

Hi! I've been struggling with this for quite a while now, mostly in a bit bigger test I had. At first I was rendering a 3D scene directly to screen which worked fine. Then I tried to first render it to an intermediate framebuffer before rendering the framebuffer to screen (as a texture). When I did that, then the scene didn't render to screen anymore. Nothing was rendered to screen. If I would modify the framebuffer texture directly by for example clearing the background to some colour, then that colour would show. Thus, the problem lies in drawing the scene to the framebuffer.

I have now modified my program to be more minimal. I'm simply trying to draw a coloured triangle to screen via a framebuffer. I get the same results: with framebuffer: nothing on screen, without framebuffer: triangle is rendered correctly.

main.cpp: http://sprunge.us/FjAg

Shaders:

For the triangle:

  • Vertex (shaders::vert)

#version 150
in vec3 position;
in vec3 color;


out vec3 Color;

void main()
{
    Color = color;
    gl_Position = vec4(position, 1.0);
}

  • Fragment (shaders::frag)

#version 150
in vec3 Color;

out vec4 outColor;

void main()
{
	outColor = vec4(Color, 1.0);
}

For drawing the framebuffer texture image thing to screen:

  • Vertex (shaders::vert2)

#version 150

in vec2 position;
in vec2 texcoord;

out vec2 Texcoord;

void main() {
    Texcoord = texcoord;
    gl_Position = vec4(position, 0.0, 1.0);
}

  • Fragment (shaders::frag2)

#version 150

in vec2 Texcoord;

out vec4 outColor;

uniform sampler2D texFramebuffer;

void main() {
    outColor = texture(texFramebuffer, Texcoord);
}

Thanks in advance.

Advertisement
You're drawing the full screen quad using glDrawElements but your VAO doesn't have an index buffer bound, and also saying that there are only 4 indices.

Did you mean to use glDrawArrays instead? You'd be half way there if you did. Last part of the problem after that would be that you're specifying GL_TRIANGLES and saying it has 4 vertices. You could fix this by using GL_TRIANGLE_FAN instead.

Thanks for the reply! It should be 6 indices indeed. I know it's better to draw triangle fans, but I want elements to work for now, since I'm learning. The glDrawElements line looks like this now:


glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);

And after this line:


glBindVertexArray(vaoQuad);

I put this:


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

But it still doesn't work. Here is the current source: http://sprunge.us/CDPR

Your glDrawElements call says your indices are unsigned bytes, but when you upload them to the index buffer, they're unsigned ints. You need to make sure they're matching, i.e. Change it to GL_UNSIGNED_INT or changes the indices to GLubyte

FIxed it (changed the glDrawElements parameter), but unfortunately it still doesn't work.

Hmm... running out ideas now. I know some drivers (incorrectly) don't save the index buffer state with the vertex array object. What happens if you rebind the index buffer after binding vaoQuad at render-time?

That didn't do it either. Here's a little update: When I exchange the glDrawElements line with `glDrawArrays(GL_TRIANGLE_FAN, 0, 4);`, then the scene does get drawn correctly. However, it would be nice and maybe necessary to find out exactly what is wrong when trying to draw elements.

But it still doesn't work. Here is the current source: http://sprunge.us/CDPR


I think it might be something to do with the actual ordering values for your indices and the data your quad is using.
Assuming you did the fixes Xycaleth pointed out and that the left corner of the screen is -1,-1, maybe the following will work:



GLfloat quad[] = {
	-1.0f, -1.0f, 1.0f, 0.0f,
	-1.0f,  1.0f, 1.0f, 0.0f,
	 1.0f,  1.0f, 1.0f, 0.0f,
	 1.0f, -1.0f, 1.0f, 0.0f
};

Still doesn't work. Notice though how the quad vertex array is read: first two entries are 2D coordinates, and the next 2 entries are texture coordinates. (if I'm not wrong)


glVertexAttribPointer(posAttrib,	2, GL_FLOAT, GL_FALSE, 4*sizeof(float), 0);
glVertexAttribPointer(texcoorAttrib,	2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(2*sizeof(float)));

Edit: Maybe it doesn't matter, but the screen isn't just black; it's not rendered at all; so it contains randomy fragments of other things that has been on my screen, like in this instance mtpaint:

yyvbps.png

(screenshot of the window that my C++ program runs in)

You don't fill index buffer with data.

Notice that after creating `ebo` buffer you're immediately calling glBufferData.

But you didn't bind that newly created buffer yet, so nothing is uploaded to index buffer.

This topic is closed to new replies.

Advertisement