Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I can't create a shader when trying to use shaders with sfml. The function glCreateShader(GL_VERTEX_SHADER); causes a segmentation fault. At first I googled it and found that it does that when the program does not have an opengl context. I tried SDL first but the poor documentation and "look at the header to know what to do" made me go for sfml

the code that causes the seg fault is bellow

    sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");

// Set color and depth clear value
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);

// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);

    //...

I'm including glew, gl.h, sfml-window, sfml-system, using opengl 2.1 on gcc linux.

What is missing?

share|improve this question

1 Answer 1

up vote 9 down vote accepted

You're not checking OpenGL extensions, version, or etc.

GLenum err = glewInit();
if (err != GLEW_OK)
  exit(1); // or handle the error in a nicer way
if (!GLEW_VERSION_2_1)  // check that the machine supports the 2.1 API.
  exit(1); // or handle the error in a nicer way

This code needs to happen after creating the OpenGL context, but before using any potentially-not-existing functions. More details on the GLEW web page

share|improve this answer
    
glewInit returns 1 and glewGetString(err) returns "1.6.0" –  ibrabeicker Jan 23 '12 at 0:51
    
that '1' return is GLEW_ERROR_NO_GL_VERSION. It means that you don't have an OpenGL context. Don't pass 'err' into glewGetString() -- pass it into glewGetErrorString() to get the printable version of the error message. –  Trevor Powell Jan 23 '12 at 1:26
    
shouldn't the context be created when I call the constructor to sf::Window? –  ibrabeicker Jan 23 '12 at 11:08
    
I don't know SFML, so don't know what you need to do to make the OpenGL context in that library (maybe someone else will be able to post another answer, filling in that knowledge?) But I do use GLEW a lot, and the error value you posted means that at the time when you called glewInit(), no OpenGL context had been created. So presumably there's something else you need to do? –  Trevor Powell Jan 23 '12 at 13:08
    
I followed SFML tutorial on their website. I'll accept your answer but how you create an opengl context? I'm having no luck with SDL as well –  ibrabeicker Jan 23 '12 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.