0

I have written a simple program with the glfw3. It compiles perfectly but when I run it, nothing is rendered. Just a blank screen pops up.

#include </usr/include/GL/glew.h>
#include </usr/local/include/GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
static void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
    GLFWwindow* window;
    glfwSetErrorCallback(error_callback);
    if (!glfwInit())
        exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    glfwMakeContextCurrent(window);
    glfwSetKeyCallback(window, key_callback);

    //printf("OpenGL Context: %d.%d\n", glfwGetWindowParam(GLFW_CONTEXT_VERSION_MAJOR), glfwGetWindowParam(GLFW_CONTEXT_VERSION_MINOR));
    while (!glfwWindowShouldClose(window))
    {
        float ratio;
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        ratio = width / (float) height;
        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
        glBegin(GL_TRIANGLES);
        glColor3f(1.f, 0.f, 0.f);
        glVertex3f(-0.6f, -0.4f, 0.f);
        glColor3f(0.f, 1.f, 0.f);
        glVertex3f(0.6f, -0.4f, 0.f);
        glColor3f(0.f, 0.f, 1.f);
        glVertex3f(0.f, 0.6f, 0.f);
        glEnd();
        glfwSwapBuffers(window);
        //glfwPollEvents();
        glfwWaitEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

I am running it on an Early 2011 Macbook Pro with AMD Radeon HD 6490M OpenGL Engine and have Mavericks installed on it.

New Code that I ran with after removing the deprecated functions. I am getting segmentation fault 11 when I run this code.

#ifdef __APPLE__
#include </usr/include/GL/glew.h>
#else
#include <GL/glew.h>
#endif
#ifdef __APPLE__
#include </usr/local/include/GLFW/glfw3.h>
#else
#include <GL/glfw.h>
#endif
#include <stdio.h>


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

const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"void main () {"
"  gl_Position = vec4 (vp, 1.0);"
"}";

const char* fragment_shader =
"#version 400\n"
"out vec4 frag_colour;"
"void main () {"
"  frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";

int main () {
  // start GL context and O/S window using the GLFW helper library
  if (!glfwInit ()) {
    fprintf (stderr, "ERROR: could not start GLFW3\n");
    return 1;
  } 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);


  GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
  if (!window) {
    fprintf (stderr, "ERROR: could not open window with GLFW3\n");
    glfwTerminate();
    return 1;
  }
  glfwMakeContextCurrent (window);

  // start GLEW extension handler
  glewInit ();

  // get version info
  const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
  const GLubyte* version = glGetString (GL_VERSION); // version as a string
  printf ("Renderer: %s\n", renderer);
  printf ("OpenGL version supported %s\n", version);


  // tell GL to only draw onto a pixel if the shape is closer to the viewer
  glEnable (GL_DEPTH_TEST); // enable depth-testing
  glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

unsigned int vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);

unsigned int vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);

unsigned int vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertex_shader, NULL);
glCompileShader (vs);
unsigned int fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);
unsigned int shader_programme = glCreateProgram ();
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);
glLinkProgram (shader_programme);

while (!glfwWindowShouldClose (window)) {
  // wipe the drawing surface clear
  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glUseProgram (shader_programme);
  glBindVertexArray (vao);
  // draw points 0-3 from the currently bound VAO with current in-use shader
  glDrawArrays (GL_TRIANGLES, 0, 3);
  // update other events like input handling 
  glfwPollEvents ();
  // put the stuff we've been drawing onto the display
  glfwSwapBuffers (window);
}
  /* OTHER STUFF GOES HERE NEXT */

  // close GL context and any other GLFW resources
  glfwTerminate();
  return 0;
}
derhass
  • 43,833
  • 2
  • 57
  • 78
noddy
  • 1,010
  • 4
  • 14
  • 27
  • Does MacOS X support old style immediate mode OpenGL? And you are asking for the core profile OpenGL 3.2 here, not sure if it has deprecated functions... – Pavel Beliy Nov 20 '13 at 16:15
  • I just wrote an answer to this question that might help http://stackoverflow.com/questions/19865463/opengl-4-1-under-mavericks/20981827#20981827 – Martin Jan 07 '14 at 21:23

1 Answers1

4

You are requesting a GL 3.2 core profile (although your title talks about 4.1). Almost all of the GL commands you are using are not available in core contexts. The matrix stack is gone, the fixed function pipeline is gone, immediate mode is gone. All of your GL calls besides glViewport() and glClear() are invalid and should just generate GL errors.

You have to completely rewrite this to make it work with modern GL. Note that on OSX, there are no compatibility profiles. So you either get the old stuff with an 2.x context, or you get GL >=3.x with the old stuff removed.

derhass
  • 43,833
  • 2
  • 57
  • 78
  • I have changed the code. I have a new code which I have added above. I am getting segmentation fault 11 with it – noddy Nov 20 '13 at 16:35
  • I was able to make it work by adding a line glewExperimental=GL_TRUE; just before the glfwMakeContextCurrent (window); – noddy Nov 20 '13 at 16:41
  • @noddy: as a side note: you should check the return value of `glewInit()`. – derhass Nov 20 '13 at 16:46