Search code examples
openglglslfragment-shadervertex-shader

OpenGL Shader Program Weird Result


I have created a basic shader program to brush up on my openGL GLSL.

on the cpu side i have properly created my shader program .vp and .fp linked properly and error checked

my result when i render is always a black Triangle

Right before i link my program but after i attach both shaders i do this

glBindAttribLocation( program, 0, "vVertexPos" );
glBindAttribLocation( program, 1, "vColor" );

both my position variables and my color variable in the shaders

all of this is just a quick run through so im not worried about ugly code besides the openGL calls and shader setup

struct PosColVertex
{
    float pos[3];
    float color[4];
};

PosColVertex verts[3];
float vPos1[3] = { 0.5f, 0.0f, -1.0f };
float vPos2[3] = { 0.0f, 1.0f, -1.0f };
float vPos3[3] = { -0.5f, 0.0f, -1.0f };
memcpy( verts[0].pos, vPos1, sizeof(float)*3 );
memcpy( verts[1].pos, vPos2, sizeof(float)*3 );
memcpy( verts[2].pos, vPos3, sizeof(float)*3 );

float vColor1[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
float vColor2[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
float vColor3[4] = { 0.0f, 0.0f, 1.0f, 1.0f };
memcpy( verts[0].color, vColor1, sizeof(float)*4 );
memcpy( verts[1].color, vColor2, sizeof(float)*4 );
memcpy( verts[2].color, vColor3, sizeof(float)*4 );

glGenBuffers( 1, &vboHandle );
glBindBuffer( GL_ARRAY_BUFFER, vboHandle );
glBufferData( GL_ARRAY_BUFFER, sizeof(PosColVertex)*3, verts, GL_DYNAMIC_READ );

for my rendering this is what i do

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

//use our shader program
glUseProgram( program );

//set which vertices we will be using
glBindBuffer( GL_ARRAY_BUFFER, vboHandle );

glEnableVertexAttribArray( 0 );
glEnableVertexAttribArray( 1 );

//specify our vertex attribute
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof( PosColVertex ), (void*)(0) );

//specify our texture attribute
glVertexAttribPointer( 1, 4, GL_FLOAT, GL_FALSE, sizeof( PosColVertex ), (void*)(12) );

glPushMatrix();

//draw our rectangle
glDrawArrays( GL_TRIANGLES, 0, 3 );

glPopMatrix();

glDisableVertexAttribArray( 0 );
glDisableVertexAttribArray( 1 );

did i do something wrong with the glVertexAttribPointer calls? i've checked my shader it works and it does change the values since i hard coded values in there before to test it. but im assuming im not telling openGL on the CPU side how to read my verts properly. any help?

tri.vp

#version 330

in vec3 vVertexPos;

void main(void) 
{ 
    gl_Position = vec4( vVertexPos.x, vVertexPos.y, vVertexPos.z, 1 );
}

tri.fp

#version 330

out vec4 vFragColor;
in vec4 vColor;

void main(void)
{ 
   vFragColor = vColor;
}

Solution

  • You can't access attributes inside of fragment shaders, only inside of vertex shaders. This makes sense because you are specifying a color for each vertex, and not for each fragment. So, I'd recommend changing your code to read in the color in the vertex shader and smoothly output it to your fragment shader:

    Vertex shader:

    in vec3 vVertexPos;
    in vec4 vColor;
    
    smooth out vec4 fColor;
    
    void main(void) 
    { 
        gl_Position = vec4( vVertexPos.x, vVertexPos.y, vVertexPos.z, 1 );
        fColor = vColor;
    }
    

    Fragment shader:

    smooth in vec4 fColor;
    out vec4 vFragColor;
    
    void main(void)
    { 
       vFragColor = fColor;
    }