PDA

View Full Version : How to fix black screen with GLFW



markydee
05-09-2014, 08:24 AM
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;
}

marshats
05-09-2014, 02:41 PM
In OPENGL_CORE_PROFILE a VertexArrayObject must be explicitly created and bound. Try adding this just before call to
sendDataToOpenGL()



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

reto.koradi
05-10-2014, 02:31 PM
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...