0
votes

I attempting to implement deferred rendering, and my test code has run into a problem I am unable to solve:

I use the following data for my fullscreen quad:

glm::vec3 verts[] = { 
    glm::vec3(-1.0f,-1.0f,0.0f),    //0
    glm::vec3( 1.0f,-1.0f,0.0f),    //1
    glm::vec3( 1.0f, 1.0f,0.0f),    //2
    glm::vec3(-1.0f, 1.0f,0.0f) };  //3

glm::vec2 texCoords[] = { 
    glm::vec2(0.0f,1.0f),
    glm::vec2(1.0f,1.0f),
    glm::vec2(1.0f,0.0f),
    glm::vec2(0.0f,1.0f),
    glm::vec2(1.0f,0.0f),
    glm::vec2(0.0f,0.0f) };

unsigned int indices[] = { 0,1,2,0,2,3 };

Which renders like so:

quad

As you can see, the vertices indexed by indices[3] and indices[5] appear to be using the same uv coordinate, causing the sampling in the second(top left) triangle of the quad to stretch, but as you can also see, the uv data is fine(albeit flipped vertically).

I've used Nsight to examine the data stored in the buffers in graphics memory to confirm the data is correct for the full screen quad, and that the error is definitely happening during the following shader execution:

Vertex shader:

#version 430
// Some drivers require the following
precision highp float;
layout (location = 0)in vec3 MSVertex;
layout (location = 4)in vec2 MSTexCoord;

out xferBlock
{
    vec3 VSVertex;
    vec2 VSTexCoord;
} outdata;

void main()
{
    outdata.VSVertex = MSVertex;
    outdata.VSTexCoord = MSTexCoord;
    gl_Position = vec4(MSVertex,1.0);
}

Fragment shader:

#version 430
// Some drivers require the following
precision highp float;

layout (location = 2) uniform sampler2D colourMap;
layout (location = 4) uniform sampler2D glowMap;

out vec4 colour;

in xferBlock
{
    vec3 VSVertex;
    vec2 VSTexCoord;
} indata;

void main()
{
    vec4 dcolour = texture(colourMap, indata.VSTexCoord).rgba;
    vec4 gcolour = texture(glowMap, indata.VSTexCoord).rgba;

    colour = dcolour*gcolour;
}

As you can see, there is nothing fancy about the shader which alters the texture coordinates, so I'm at a loss as to explain why this is happening.

Update:

I modified the shader to output R,G (blue is always zero) colours based on UV coordinates for the quad, and My initial impression was correct: The coordinate being referenced for the left corner of the quad is (0,0), which is incorrect:

incorrect

What it should look like, is this:

enter image description here

1
So, you have two attribute arrays of different size. How are you sending this to the GL in the first place? A vertex is the set of all attributes, not just the position, so you will need 6 vertices for the GL. - derhass
It's an indexed array, being drawn with glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - Ian Young

1 Answers

0
votes

I figured out the problem:

normals/texture coords/etc must all share the same index as the vertex. I was arranging my texture coordinate data to be unindexed, and so it was only using the first 4 elements of texCoords.

Once I changed the 4th element to correspond with the 4th vertex element, everything worked fine.