0
votes

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;
}
1
This is not your problem, but why would you use GL_DYNAMIC_READ for your buffer object usage? Are you planning to read the memory back to the CPU? Do you plan to update the memory? If not, use GL_STATIC_DRAW.Nicol Bolas
More importantly, where is your shader?Nicol Bolas
i debugged my shader it works :D.but if you want to see it i'll add it. also i use GL_DYNAMIC_READ since i have an actual project im working on and i made this sample project to just debug stuff. so yes i do plan on messing with the data there for i kept it GL_DYNAMIC_READFranky Rivera
Sorry, have to give -1 for posting a completely broken shader and claiming that it 'works'. I don't know how you debugged that, but there's no way that that shader can possibly function correctly. (see Xymo's answer below)Tim

1 Answers

3
votes

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;
}