Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an OpenGL 3.3 context in GLFW 3

Tags:

opengl

glfw

I've written a simple OpenGL 3.3 program which is supposed to render a triangle, based off of this tutorial, except I'm using GLFW to create the window and context, instead of doing it from scratch. Also, I'm using Ubuntu.

The triangle doesn't render though, I just get a black screen. Functions like glClearColor() and glClear() seem to be working exactly as they should, but the code to render the triangle doesn't. Here are the relevant bits of it:

#define GLFW_INCLUDE_GL_3
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main ()
{
  glfwInit();

  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  GLFWwindow* window = glfwCreateWindow(800, 600, "GLFW test", NULL, NULL);

  glfwMakeContextCurrent(window);

  glewExperimental = GL_TRUE;
  glewInit();

  float vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, -0.5f};

  GLuint VBOid[1];

  glClear(GL_COLOR_BUFFER_BIT);

  glGenBuffers(1, VBOid);
  glBindBuffer(GL_ARRAY_BUFFER, VBOid[0]);
  glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), vertices, GL_STATIC_DRAW);

  glEnableVertexAttribArray(0);

  glBindBuffer(GL_ARRAY_BUFFER, VBOid[0]);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  glDrawArrays(GL_TRIANGLES, 0, 3);

  ...
}

What am I missing?

like image 620
10 revs Avatar asked Aug 07 '13 20:08

10 revs


1 Answers

In core profile OpenGL 3.3, you need a shader in order to render. So you need to compile and link a program that contains a vertex and fragment shader.

like image 86
Nicol Bolas Avatar answered Sep 25 '22 14:09

Nicol Bolas