Question

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.

#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;
}
Was it helpful?

Solution

You're specifying an OpenGL 3.3 Core Profile in your startup code (glfwWindowHint calls). Your code is not compatible with the core profile. The use of vertex buffers and drawing without a Vertex Array Object (VAO) is deprecated. Under section "E.2.2 Removed Features" of the 3.3 spec:

The default vertex array object (the name zero) is also deprecated. Calling VertexAttribPointer when no buffer object or no vertex array object is bound will generate an INVALID_OPERATION error, as will calling any array drawing command when no vertex array object is bound.

The first thing I would always do when you get no output is add a glGetError() call at the end of the drawing. In this case, you should see an error return, and can then add additional glGetError() calls to localize which OpenGL call triggered the error.

To verify if this is the only problem in your code, you could temporarily remove the GLFW_OPENGL_CORE_PROFILE flag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top