Search code examples
openglglutsfmlvertex-shader

openGL migration from SFML to glut, vertices arrays or display lists are not displayed


Due to using quad buffered stereo 3D (which i have not included yet), i need to migrate my openGL program from a SFML window to a glut window. With SFML my vertices and display list were properly displayed, now with glut my window is blank white (or another color depending on the way i clear it).

Here is the code to initialise the window :

    int type;
    int stereoMode = 0;
    if ( stereoMode == 0 )
        type = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
    else
        type = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STEREO;
    glutInitDisplayMode(type);
    int argc = 0;
    char *argv = "";
    glewExperimental = GL_TRUE; 
    glutInit(&argc, &argv);

    bool fullscreen = false;
    glutInitWindowSize(width,height);
    int win = glutCreateWindow(title.c_str());
    glutSetWindow(win);
    assert(win != 0);
    if ( fullscreen ) {
        glutFullScreen();
        width = glutGet(GLUT_SCREEN_WIDTH);
        height = glutGet(GLUT_SCREEN_HEIGHT);
    }


    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    }

    glutDisplayFunc(loop_function);

This is the only code i had to change for now, but here is the code i used with sfml and displayed my objects in the loop, if i change the value of glClearColor, the window's background does change color so the opengl context seems to be working :

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(255.0f, 255.0f, 255.0f, 0.0f);

glLoadIdentity();
sf::Time elapsed_time = clock.getElapsedTime();
clock.restart();

camera->animate(elapsed_time.asMilliseconds());
camera->look();

for (auto i = objects->cbegin(); i != objects->cend(); ++i)
        (*i)->draw(camera);

glutSwapBuffers();

Is there any other changes i should have done switching to glut ? that would be great if someone could enlighten me on the subject.

In addition to that, i found out that adding too many objects (that were well handled before with SFML), openGL gives error 1285: out of memory. Maybe this is related.

EDIT : Here is the code i use to draw each object, maybe it is the problem :

GLuint LightID = glGetUniformLocation(this->shaderProgram, "LightPosition_worldspace");
if(LightID ==-1)
    cout << "LightID not found ..." << endl;
GLuint MaterialAmbientID = glGetUniformLocation(this->shaderProgram, "MaterialAmbient");
if(LightID ==-1)
    cout << "LightID not found ..." << endl;
GLuint MaterialSpecularID = glGetUniformLocation(this->shaderProgram, "MaterialSpecular");
if(LightID ==-1)
    cout << "LightID not found ..." << endl;

glm::vec3 lightPos = glm::vec3(0,150,150);
glUniform3f(LightID, lightPos.x, lightPos.y, lightPos.z);
glUniform3f(MaterialAmbientID, MaterialAmbient.x, MaterialAmbient.y, MaterialAmbient.z);
glUniform3f(MaterialSpecularID, MaterialSpecular.x, MaterialSpecular.y, MaterialSpecular.z);


// Get a handle for our "myTextureSampler" uniform
    GLuint TextureID  = glGetUniformLocation(shaderProgram, "myTextureSampler");
    if(!TextureID)
        cout << "TextureID not found ..." << endl;
    glActiveTexture(GL_TEXTURE0);
    sf::Texture::bind(texture);
    glUniform1i(TextureID, 0);
// 2nd attribute buffer : UV
    GLuint vertexUVID = glGetAttribLocation(shaderProgram, "color");
    if(vertexUVID==-1)
        cout << "vertexUVID not found ..." << endl;
    glEnableVertexAttribArray(vertexUVID);
    glBindBuffer(GL_ARRAY_BUFFER, color_array_buffer);
    glVertexAttribPointer(vertexUVID, 2, GL_FLOAT, GL_FALSE, 0, 0);  


GLuint vertexNormal_modelspaceID = glGetAttribLocation(shaderProgram, "normal");
if(!vertexNormal_modelspaceID)
    cout << "vertexNormal_modelspaceID not found ..." << endl;
glEnableVertexAttribArray(vertexNormal_modelspaceID);
glBindBuffer(GL_ARRAY_BUFFER, normal_array_buffer);
glVertexAttribPointer(vertexNormal_modelspaceID, 3, GL_FLOAT, GL_FALSE, 0, 0 );


GLint posAttrib;
posAttrib = glGetAttribLocation(shaderProgram, "position");
if(!posAttrib)
    cout << "posAttrib not found ..." << endl;

glEnableVertexAttribArray(posAttrib);
glBindBuffer(GL_ARRAY_BUFFER, position_array_buffer);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements_array_buffer);

glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
GLuint error;
while ((error = glGetError()) != GL_NO_ERROR) {
    cerr << "OpenGL error: " << error << endl;
}

disableShaders();

Solution

  • The code is fine, migrating from SFML to glut doesn't need a lot of changes but you will have to change the textures if you used SFML texture object. The only way you are not seeing anything else than your background changing color is simply because your camera is not looking at your object. I advise you check the code of your view and or post it.