PDA

View Full Version : Problems drawing a triangle



dzzank
05-28-2014, 05:58 AM
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();
}

Agent D
05-28-2014, 07:47 AM
What OpenGL(R) version and profile are you using? I don't see any shader loading and binding in your code at all.

dzzank
05-28-2014, 01:11 PM
It's been just a couple of days since I've started to learn OpenGL so I'm not using yet shaders.
I'm using OpenGL 4.3.

dzzank
05-28-2014, 01:17 PM
I finally made it. I put

glewExperimental = GL_TRUE
glewInit()

before the GL functions.

rodrigoloc
06-01-2014, 09:16 PM
Hmm... How did it work if you didn't use shaders with OpenGL 4.3? glEnableVertexAttribArray(0) without shaders won't do anything (in both, core and compatibility profile).