OpenGL: Why these codes draws nothing, with interleaved VBO and GLSL

364 views Asked by At

I've looked many papers and blogs, and I finally reached these code to generate a quad: program stores in gProgram

    vert shader:---------------------
    #version 330
    layout(location = 0) in vec3 attrib_position;  
    layout(location = 6) in vec2 attrib_texcoord;  
    out Varing
    {
        vec4    pos;
        vec2    texcoord;
    } VS_StateOutput;

    uniform mat4 gtransform;
    void main(void)
    {
        VS_StateOutput.pos = gtransform * vec4(attrib_position,1.0f);
        VS_StateOutput.texcoord = attrib_texcoord;
    }

    frag shader:--------------------
    #version 330
    uniform sampler2D texUnit;

    in  Varing
    {
        vec4    pos;
        vec2    texcoord;
    } PS_StateInput;
    layout(location = 0) out vec4 color0;
    void main(void)
    {
        color0 = texture(texUnit, PS_StateInput.texcoord);
    }

Here is my vertex data: stores in gVertexVBO

    float data[] =
    { 
        -1.0f,  1.0f, 0.0f,   0.0f, 0.0f,
         1.0f,  1.0f, 0.0f,   1.0f, 0.0f,
         1.0f, -1.0f, 0.0f,   1.0f, 1.0f,
        -1.0f, -1.0f, 0.0f,   0.0f, 1.0f
    };

and index data: stores in gIndexVBO

    unsigned short idata[] =
    {
        0, 3, 2,  0, 2, 1
    };

in the drawing section, I do like this:

ps: the gtransform is set as an identity matrix

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        
    glUseProgram(gProgram);        
    glProgramUniformMatrix4fv(gProgram, gtLoc, 1, GL_FALSE, matrix);
    glProgramUniform1uiv(gProgram, texLoc, 1, &colorTex);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gIndexVBO);

    glBindBuffer(GL_ARRAY_BUFFER, gVertexVBO);
    char* offset = NULL;
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*5, offset);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(6, 2, GL_FLOAT, GL_FALSE, sizeof(float)*5, offset+sizeof(float)*3);
    glEnableVertexAttribArray(6);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);  

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(6);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

I get nothing but the cleared back color, I think the errors located within these codes, but I can't figure out where is it, I appreciate any help.

If you need to check codes in initialization section, I will post them in the reply, thanks!

1

There are 1 answers

5
finlaybob On BEST ANSWER

You're not supplying the built in gl_Position in the vertex shader.

uniform mat4 gtransform;
void main(void)
{
    vec4 outPosition = gtransform * vec4(attrib_position,1.0f);
    gl_Position = outPosition;
    // if you need the position in the fragment shader
    VS_StateOutput.pos = outPosition;
    VS_StateOutput.texcoord = attrib_texcoord;
}

OpenGL wont know about your VS_StateOutput struct and won't have any idea where to put the new vertices. See here for a quick reference. (Page 7)

This link gives a better detail of the built in structure.

EDIT:

Next I would change the fragment shader main to:

void main(void)
{
    color0 = vec4(1.0); //texture(texUnit, PS_StateInput.texcoord);
}

to rule out any texture issues. Sometimes texture problems lead to a blank black texture. If you get a white colour, you then know its a texture problem and can look there instead.