Hello All,
I was mostly using the old glBegin() method for a long time. Now, im learning to use modern ways of specifying vertices. Here is some sample code i found online. I modified a little:
What i tried to do is, in the same VBO, use color values also:
static const char* pVS = "
\
#version 330
\
\
layout (location = 0) in vec3 Position;
\
layout (location = 1) in vec3 Col;
\
out vec3 col_out;
\
\
void main()
\
{
\
gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0);
\
col_out = Col;
\
}";
static const char* pFS = "
\
#version 330
\
\
out vec4 FragColor;
\
in vec3 col_out;
\
\
void main()
\
{
\
FragColor = vec4(col_out, 1.0);
\
}";
typedef struct {
Vector3f vert;
Vector3f col;
} Vertex;
Vertex Vertices[3];
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &(Vertices[0].col));
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glutSwapBuffers();
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
}
static void CreateVertexBuffer()
{
Vertices[0].vert = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[0].col = Vector3f(1.0f, 0.0f, 0.0f);
Vertices[1].vert = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[1].col = Vector3f(1.0f, 0.0f, 0.0f);
Vertices[2].vert = Vector3f(0.0f, 1.0f, 0.0f);
Vertices[2].col = Vector3f(1.0f, 0.0f, 0.0f);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
Here is the essential part. But i get a blank screen. Can anyone please let me know what i’m doing wrong?
Thanks in advance!