PDA

View Full Version : OpenGL failing to draw vertices



AdamVO
12-02-2013, 11:45 PM
Helloooo.
I'm trying to figure out how to use shading pipeline aswell as GLSL itself, and I've encountered a problem with openGL not drawing my vertices. OpenGL drew the vertices without a problem before I added in colour Vertex Array Objects and binded them. I got the idea that you were supposed to have two separate sets of vertex array objects; one storing the coordinates of the vertices, and the other storing colour values. Perhaps this is where I went wrong? Some one please help me with this.
Cheers.

Here is my code:


#include "main.h" //g++ main.cpp -lGLEW -lGL -lglfw -lGLU -lglut -o triangle
#define BUFFER_OFFSET(offset) ((void *)(offset))
using namespace std;


enum VAO_IDs {Triangles, NumVAOs, colours};
enum Buffer_IDs {ArrayBuffer, NumBuffers, cBuffer};
enum Attrib_IDs {vPosition = 0, colorPosition = 1};
GLuint VAOs[NumVAOs];
GLuint ColourObjects[NumVAOs];
GLuint Buffers[NumBuffers];
GLuint colorbuffer[NumBuffers];
const GLuint NumVertices = 36;

//assigning vertex data to buffer objects and preparing to send to
void init() //Vertex Shaders.
{
glewInit();
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);

GLfloat vertices [NumVertices] [3] =
{
{ 0.30, 0.00, -0.30 }, //Triangle 1
{ 0.30, 0.00, 0.30 },
{ -0.30, 0.00, -0.30 },
{ -0.30, 0.00, -0.30 }, //Triangle 2
{ -0.30, 0.00, 0.30 },
{ 0.30, 0.00, -0.30 },
{ -0.30, 0.00, 0.30 }, //Triangle 3
{ 0.30, 0.00, 0.30 },
{ 0.30, 0.60, 0.30 },
{ -0.30, 0.60, 0.30 }, //Triangle 4
{ 0.30, 0.60, 0.30 },
{ -0.30, 0.00, 0.30 },
};

glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


glGenVertexArrays(NumVAOs, ColourObjects);
glBindVertexArray(ColourObjects[colours]);

GLfloat g_color_buffer_data[NumVertices][3] =
{
{ 0.583f, 0.771f, 0.014f },
{ 0.609f, 0.115f, 0.436f },
{ 0.327f, 0.483f, 0.844f },
{ 0.822f, 0.569f, 0.201f },
{ 0.435f, 0.602f, 0.223f },
{ 0.310f, 0.747f, 0.185f },
{ 0.597f, 0.770f, 0.761f },
{ 0.559f, 0.436f, 0.730f },
{ 0.359f, 0.583f, 0.152f },
{ 0.483f, 0.596f, 0.789f },
{ 0.559f, 0.861f, 0.639f },
{ 0.195f, 0.548f, 0.859f },
};
glGenBuffers(1, colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer[cBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);

ShaderInfo shaders[] =
{
{ GL_VERTEX_SHADER, "triangles.vert"},
{ GL_FRAGMENT_SHADER, "triangle.frag"},
{ GL_NONE, NULL}
};

glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition );

glVertexAttribPointer(colorPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(colorPosition);


}

void display()
{
glClearColor(1,0.5f,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]); //?
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}



int main(int argc, char** argv)
{
const int width = 400, height = 400;

glutInit(&argc, argv);
glutInitWindowSize(width, height);
glutInitDisplayMode(GLUT_RGBA);
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}


My Vertex Shader:

#version 330 core

layout(location = 0) in vec4 vPostion;
layout(location = 1) in vec3 vertexColor;
out vec3 fragmentColor;
void main()
{
gl_Position = vPosition;
fragmentColor = vertexColor
}

My Fragment Shader:


#version 330 core
in vec3 fragmentColor;
out vec3 color;

void main()
{
color = fragmentColor
}