PDA

View Full Version : Segmentation fault on glClearBuffer*



mkosler
09-06-2013, 11:35 AM
I'm trying to go through the OpenGL SuperBible 6th ed., but was having a bit of trouble using the provided sb6.h framework for setting up windows and whatnot. I have done some legacy OpenGL using GLFW for windowing, so I figured I could just wrap the code in GLFW rather than what the book provides.

However, I am segfaulting whenever I reach glClearBufferfv:


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

#include <stdlib.h>
#include <stdio.h>

static void errorCallback(int error, const char *description)
{
fputs(description, stderr);
}

static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}

static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };

int main(void)
{
glewExperimental = true;
glewInit();

GLFWwindow *window;

glfwSetErrorCallback(errorCallback);

if (!glfwInit()) {
return EXIT_FAILURE;
}

window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window) {
glfwTerminate();
return EXIT_FAILURE;
}

glfwMakeContextCurrent(window);

glfwSetKeyCallback(window, keyCallback);

int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);

int maxBuffers = 0;
glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxBuffers);

printf("GL_MAX_DRAW_BUFFERS = %d\n", maxBuffers);

while (!glfwWindowShouldClose(window)) {
glClearBufferfv(GL_COLOR, (GLint) 0, red);

glfwSwapBuffers(window);
glfwPollEvents();
}

glfwDestroyWindow(window);

glfwTerminate();

return EXIT_SUCCESS;
}

According to my glewinfo, my hardware supports up to GL_VERSION_4_0, but I'm guessing I'm missing something else. According to the OpenGL reference, glGetError() should be flagged, but when I set up a sigaction to caught the segfault, I got GL_NO_ERROR. Any help would be appreciated.

carsten neumann
09-06-2013, 11:53 AM
You are initializing GLEW before creating an OpenGL context (AFAIK GLFW does that as part of glfwCreateWindow). Since GLEW needs to query the OpenGL context for (extension) functions you must initialize GLEW after creating the OpenGL context and making it current.

mkosler
09-06-2013, 01:56 PM
That was it. Thank you!