opengl 3.3中的黑屏输出

时间:2019-03-23 15:37:26

标签: c++ opengl gpu glfw glew

我正在尝试在OpenGL屏幕上绘制一个红色三角形。 glClearColor(x, x, x, 1)可以正常工作并更改背景颜色。但是,没有三角形出现,也没有错误出现。

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

using namespace std;

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

static unsigned int CompileShader(unsigned int type, const string &source) {
    unsigned int id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);

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

    return id;
}

static unsigned int CreateShader(const string &vertexShader, const string &fragmentShader) {
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
    unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);

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

    glDeleteShader(vs);
    glDeleteShader(fs);

    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(SCR_WIDTH, SCR_HEIGHT, "window", NULL, NULL);

    if (window == NULL) {
        cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    cout << glfwGetVersionString() << endl;

    glfwMakeContextCurrent(window);

    glewInit();
    if (glewInit()) {
        cout << "Glew initialization failed! " << endl;
        return -1;
    }


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

    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
    glEnableVertexAttribArray(0);

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

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

    unsigned int shader = CreateShader(vertexShader, fragmentShader);
    glUseProgram(shader);

    while (!glfwWindowShouldClose(window)) {

        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.0f, 0.0f, 0.0f, 1);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
        glfwPollEvents();

    }

    glfwTerminate();
    return 0;
}

2 个答案:

答案 0 :(得分:4)

您需要使用Vertex Array Object。因此,将程序更改为:

varchar

然后,在渲染时:

(...)
unsigned int vao;                                                           
glGenVertexArrays(1, &vao);                                                 
glBindVertexArray(vao);                                                     

unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
glEnableVertexAttribArray(0);

glBindVertexArray(0);
(...)

答案 1 :(得分:2)

您缺少包含这些VertexAttrib设置的Vertex数组对象。同样,您的顶点缓冲区格式设置为vec2点,但是顶点着色器期望vec4

正确的着色器:

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

创建和绑定VAO:

unsigned int buffer;
unsigned int VAO;

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &buffer);

您初始化了两次毛刺,不确定是否重要。

glewExperimental = GL_TRUE;//Recommended for compatibility
    if (glewInit()!= GLEW_OK) {
        cout << "Glew initialization failed! " << endl;
        return -1;
    }