如何修复OpenGL 3.3中的黑屏输出

时间:2019-04-13 21:43:19

标签: c++ opengl glsl glfw

我在使用OpenGL和C ++的项目中遇到黑屏。对于在屏幕上呈现红色三角形的地方,我需要帮助。

我尝试检查顶点和片段着色器中的错误。

#include <GL\glew.h>
#include <GL\GL.h>
#include <GLFW\glfw3.h>
#include <iostream>
#include <string>

using namespace std;

unsigned int CreateShader(int type, const string shaderSource) {
    unsigned int shader = glCreateShader(type);
    const char *src = shaderSource.c_str();
    glShaderSource(shader, 1, &src, NULL);
    glCompileShader(shader);

    int result;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE) {
        int length;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
        char *message = (char*)alloca(length * sizeof(char));
        glGetShaderInfoLog(shader, length, &length, message);
        cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex shader " : "fragment shader ") << endl;
        cout << message << endl;
    }


    return shader;
}

unsigned int CreateProgram(const string vertexShader, const string fragmentShader) {
    unsigned int program = glCreateProgram();

    unsigned int vs = CreateShader(GL_VERTEX_SHADER, vertexShader);
    unsigned int fs = CreateShader(GL_FRAGMENT_SHADER, fragmentShader);

    glAttachShader(program, vs);
    glAttachShader(program, fs);

    glDeleteShader(vs);
    glDeleteShader(fs);

    glLinkProgram(program);
    glValidateProgram(program);

    return program;

}

int main() {

    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow *window = glfwCreateWindow(800, 600, "window", NULL, NULL);

    glfwMakeContextCurrent(window);

    glewInit();
    if (glewInit() != GLEW_OK) {
        cout << "Glew initialization Failed! " << endl;
        glfwTerminate();
        return -1;
    }

    float positions[] = {
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f,
        0.0f, 0.5f, 0.0f,
    };

    cout << "Made By Rial Seebran" << endl;
    cout << glfwGetVersionString() << endl;


    unsigned int VAO;
    unsigned int VBO;

    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    const string vertexShader =
        "#version 330 core\n"
        "layout (location = 0) in vec3 position;\n"
        "void main() {\n"
        "gl_Position = vec4(position, 0.0f);\n"
        "}\n";

    const string fragmentShader =
        "#version 330 core\n"
        "layout (location = 0) out vec4 color;\n"
        "void main() {\n"
        "color = vec4(1.0f, 0.0f, 0.0f, 1.0f);\n"
        "}\n";

    unsigned int program = CreateProgram(vertexShader, fragmentShader);


    while (!glfwWindowShouldClose(window)) {
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(program);
        glBindVertexArray(VAO);
        glDrawArrays(GL_ARRAY_BUFFER, 0, 3);
        glBindVertexArray(0);

        glfwPollEvents();
        glfwSwapBuffers(window);
    }
    glfwTerminate();
    return 0;
}

我期望在带有蓝色背景的指定坐标处绘制一个红色三角形。

1 个答案:

答案 0 :(得分:4)

glDrawArrays的第一个参数必须是原始类型。
它必须是GL_TRIANGLES而不是GL_ARRAY_BUFFER

glDrawArrays(GL_ARRAY_BUFFER, 0, 3);
glDrawArrays(GL_TRIANGLES, 0, 3);

OpenGL枚举器常量GL_ARRAY_BUFFER不是该参数的可接受值,并且会导致GL_INVALID_ENUM错误(该错误可以通过glGetError得到)。


将剪辑空间坐标转换为规范化的设备空间时,xyz组件将除以w组件。 这意味着必须将w的{​​{1}}组件设置为1.0,而不是0.0:

gl_Position
gl_Position = vec4(position, 0.0f);


Glew可以通过gl_Position = vec4(position, 1.0);启用其他扩展名。参见GLEW documentation,其中指出:

  

GLEW从图形驱动程序获取有关支持的扩展的信息。但是,实验或预发行版驱动程序可能不会通过标准机制报告所有可用扩展,在这种情况下,GLEW将报告不支持的扩展。为了避免这种情况,可以通过在调用glewExperimental = GL_TRUE;之前将glewExperimental全局开关设置为GL_TRUE来打开它,以确保所有带有有效入口点的扩展都被暴露。

glewInit()