I have implemented a simple triangle program but somehow triangle is drawn in a single color which is color of first vertex.
Code is like this
GLfloat positions[] = {
0.0f, 0.0f,0.0f,
1.0f, 1.0f,0.0f,
-1.0f, 1.0f,0.0f,
};
GLfloat colors[] = {
0.0f,1.0f,0.0f, // Color
0.0f,1.0f,0.0f, // Color
0.0f,0.0f,1.0f, // Color
};
glGenBuffers(1,&vertexBufferId);
glBindBuffer(GL_ARRAY_BUFFER,vertexBufferId);
glBufferData(GL_ARRAY_BUFFER,sizeof(positions),positions,GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glGenBuffers(1,&colorBufferId);
glBindBuffer(GL_ARRAY_BUFFER,colorBufferId);
glBufferData(GL_ARRAY_BUFFER,sizeof(colors),colors,GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0);
These are my shaders
#version 330
layout(location = 0)in vec3 vert;
layout(location = 1)in vec3 color;
out vec3 frag_color;
void main()
{
gl_Position = vec4(vert,0.0);
frag_color = color;
}
#version 330
out vec4 fragColor;
in vec3 frag_color;
void main()
{
fragColor = vec4(frag_color,1.0);
}
I just see a triangle in green color. Need some help !!!