我正在使用GLEW和GLFW。我的程序成功构建,但是当它运行时,我得到的只是一个黑屏。我知道这不是着色器代码的问题,因为我已经在QT中测试了installShaders函数并且它工作得很好,所以我假设我错误地使用了GLFW。
#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;
}
答案 0 :(得分:3)
您在启动代码(glfwWindowHint
调用)中指定了OpenGL 3.3核心配置文件。您的代码与核心配置文件不兼容。不推荐使用顶点缓冲区和不使用顶点数组对象(VAO)绘图。在3.3规范的“E.2.2删除的功能”部分下:
也不推荐使用默认的顶点数组对象(名称为零)。在没有绑定缓冲区对象或没有绑定顶点数组对象时调用VertexAttribPointer将生成INVALID_OPERATION错误,在没有绑定顶点数组对象时调用任何数组绘制命令。
当你没有输出时我会经常做的第一件事是在图纸的末尾添加一个glGetError()
调用。在这种情况下,您应该看到错误返回,然后可以添加其他glGetError()
调用来本地化哪个OpenGL调用触发了错误。
要验证这是否是代码中的唯一问题,您可以暂时删除GLFW_OPENGL_CORE_PROFILE
标记。