Hi. I’m trying to draw a triangle on the screen but the following code is not working.
#include <glew.h>
#include <glfw3.h>
#include <stdexcept>
int main(int argc, char** argv)
{
if(!glfwInit())
{
throw std::runtime_error("GLFW could not initialize.");
}
GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL / GLFW", NULL, NULL);
if(!window)
{
throw std::runtime_error("GLFWwindow could not be created.");
}
glewInit();
glfwMakeContextCurrent(window);
const float vertices[] =
{
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
while(!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
}