Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: How to fix black screen with GLFW

  1. #1
    Newbie Newbie
    Join Date
    May 2014
    Posts
    1

    How to fix black screen with GLFW

    I'm using GLEW and GLFW. My program builds successfully but when it runs, all I get is a black screen. I know it's not a problem with the shader code because I have tested the installShaders function in QT and it worked perfectly so I am assuming I am using GLFW incorrectly.

    Code :
    #include <stdio.h>
    #include <stdlib.h>
    #include <GL\glew.h>
    #include <GLFW\glfw3.h>
     
    GLFWwindow* window;
     
    extern const char* vertexShaderCode;
    extern const char* fragmentShaderCode;
     
    void sendDataToOpenGL()
    {
        GLfloat verts[] = 
    {
        +0.0f, +1.0f,
        +1.0f, +0.0f, +0.0f, 
        -1.0f, -1.0f,
        +1.0f, +0.0f, +0.0f,
        +1.0f, -1.0f,
        +1.0f, +0.0f, +0.0f,
    };
    GLuint vertexBufferID;
    glGenBuffers(1, &vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (char*)(sizeof(float) * 2));
     
    GLushort indices[] = { 0,1,2 };
    GLuint indexBufferID;
    glGenBuffers(1, &indexBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    }
     
    void installShaders()
    {
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
     
    const char* adapter[1]; 
    adapter[0] = vertexShaderCode;
    glShaderSource(vertexShaderID, 1, adapter, 0);
    adapter[0] = fragmentShaderCode;
    glShaderSource(fragmentShaderID, 1, adapter, 0);
     
    glCompileShader(vertexShaderID);
    glCompileShader(fragmentShaderID);
     
    GLuint programID = glCreateProgram();
    glAttachShader(programID, vertexShaderID);
    glAttachShader(programID, fragmentShaderID);
    glLinkProgram(programID);
     
    glUseProgram(programID);
    }
     
    int main( void )
    {
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }
     
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
     
    window = glfwCreateWindow( 1024, 768, "OpenGL Graphics", NULL, NULL);
    if( window == NULL ){
        fprintf( stderr, "Failed to open GLFW window.\n" );
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
     
    glewExperimental = true; 
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
     
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
     
    sendDataToOpenGL();
    installShaders();
     
    do{
        glClear( GL_COLOR_BUFFER_BIT );
     
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
     
        glfwSwapBuffers(window);
        glfwPollEvents();
     
    } 
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window) == 0 );
     
    glfwTerminate();
     
    return 0;
    }

  2. #2
    Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408
    In OPENGL_CORE_PROFILE a VertexArrayObject must be explicitly created and bound. Try adding this just before call to
    sendDataToOpenGL()

    Code :
    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray( VertexArrayID );

  3. #3
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    This is like a deja vu all over again if you visit multiple we sites. I answered the same question from the same poster on a different board yesterday. At least we gave him the same answer...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •