如何用GLFW修复黑屏

问题描述:

我使用GLEW和GLFW。我的程序构建成功,但当它运行,我得到的是一个黑色的屏幕。我知道这不是一个问题的着色器代码,因为我测试了installshaders函数在QT和它的工作完美,所以我假设我使用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.

#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;
}


3.3核心配置文件在您的启动代码( glfwWindowHint 调用)。您的代码与核心配置文件不兼容。不推荐使用顶点缓冲区和不使用顶点数组对象(VAO)的绘图。在3.3规范的E.2.2 Removed Features部分下:

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:


默认的顶点数组对象当没有绑定缓冲区对象或没有顶点数组对象时,调用VertexAttribPointer将产生一个INVALID_OPERATION错误,当没有顶点数组对象绑定时,将调用任何数组绘制命令。

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.

当你没有输出时,我总是会做的第一件事是在绘图结束时添加一个 glGetError()调用。在这种情况下,您应该看到一个错误返回,然后可以添加额外的 glGetError()调用本地化哪个OpenGL调用触发错误。

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.

要验证这是否是代码中唯一的问题,您可以暂时移除 GLFW_OPENGL_CORE_PROFILE 标志。

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