为什么我的 OpenGL 代码会产生黑屏?

Why is my OpenGL code producing a black screen?

提问人:Elen Mouradian 提问时间:10/24/2022 最后编辑:YunElen Mouradian 更新时间:10/24/2022 访问量:753

问:

这是我用 C++ 编写的代码,应该产生 2 个三角形,但是我得到一个空白屏幕。我错过了什么吗?

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#include <glm/vec3.hpp> // glm::vec3
#include <glm/vec4.hpp> // glm::vec4
#include <glm/mat4x4.hpp> // glm::mat4
#include <glm/gtc/matrix_transform.hpp> // glm::translate, glm::rotate, glm::scale, glm::perspective

using namespace std;

static string ParseShader(string filepath) {
    ifstream stream(filepath);
    string line;
    stringstream stringStream;

    while (getline(stream, line))
    {
        stringStream << line << '\n';
    }

    return stringStream.str();
}

static unsigned int CompileShader(unsigned int type, const string& source) {
    unsigned int id = glCreateShader(type);
    const char* src = source.c_str(); // this returns a pointer to data inside the string, the first character
    glShaderSource(id, 1, &src, nullptr); // shader id, count of source codes, a pointer to the array that holds the strings
    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 << type << endl;
        cout << message << endl;
        glDeleteShader(id);
        return 0;
    }
    return id;
}

// takes the shader codes as a string parameters 
static unsigned int CreateShader(const string& vertexShader, const string& fragmentShader)
{
    GLuint 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); // validate if the program is valid and can be run in the current state of opengl

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;
}

int main(void)
{
    GLFWwindow* window;

    float Angle = 0;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    // call glewInit after creating the context...
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    }


    GLfloat coordinates[12] = {
        -0.5f,  0.5f,
         0.0f,  0.5f, 
         0.5f,  0.5f, 

        -0.5f,  -0.5f,
         0.0f,  -0.5f,
         0.5f,  -0.5f
    };

    GLuint indices[6] = { 0, 3, 1, 4, 2, 5 };

    GLuint position_buffer;
    glGenBuffers(1, &position_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), coordinates, GL_STATIC_DRAW);

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


    GLuint index_buffer;
    glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLuint), indices, GL_STATIC_DRAW);


    string vertexSource = ParseShader("vertex.shader");
    string fragmentSource = ParseShader("fragment.shader");

    unsigned int program = CreateShader(vertexSource, fragmentSource);
    glUseProgram(program);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        // Render here 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_INT, nullptr);

         //Swap front and back buffers 
        glfwSwapBuffers(window);

        // Poll for and process events 
        glfwPollEvents();
    }

    glDeleteProgram(program);

    glfwTerminate();
    return 0;
}

这是我的顶点着色器。

#version 330 core 

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;

out vec4 var_color;

void main()
{
    var_color = color;
    gl_Position =  position;
};

这是我的片段着色器。

#version 330 core 

out vec4 color;
in vec4 var_color;


void main()
{
    //color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
    color = var_color;
    //discard;
};

结果我得到了一个黑屏,我怀疑这是我的机器的问题,因为其他代码片段在它上面工作。

C++ glfw opengl-3

评论

0赞 vandench 10/24/2022
您的着色器需要 vec4,但它正在接收 vec2。
0赞 Elen Mouradian 10/24/2022
究竟在哪里?它接收 VEC4
0赞 vandench 10/24/2022
究竟在哪里? 它接收 VEC4 不,它没有,它需要 VEC4 并且只接收 VEC2。如果您想要 vec4,则需要将输入坐标数据更改为 4 个浮点数,并将输入布局更改为 4 个浮点数。
1赞 Gilles-Philippe Paillé 10/24/2022
没有向着色器提供任何颜色。在我的计算机上,您的代码显示一个红色方块,但我没有看到任何将红色指定为显示颜色的代码。我想未初始化的数据对你来说是黑色的。

答:

3赞 Gilles-Philippe Paillé 10/24/2022 #1

提供的代码中未指定颜色。着色器很可能正在绘制黑色方块,因为执行程序时内存中已有该值。若要添加颜色,请使用以下代码:

GLfloat colors[24] = { // Random colors
    1.0f,  1.0f, 1.0f, 1.0f,
    1.0f,  0.0f, 1.0f, 1.0f,
    1.0f,  1.0f, 0.0f, 1.0f,
    0.0f,  1.0f, 0.0f, 1.0f,
    1.0f,  1.0f, 1.0f, 1.0f,
    0.0f,  0.0f, 1.0f, 1.0f,
};

GLuint color_buffer;
glGenBuffers(1, &color_buffer);
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * 4 * sizeof(float), colors, GL_STATIC_DRAW);

glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0); //vertex colors
glEnableVertexAttribArray(1);
2赞 Yun 10/24/2022 #2

代码不应生成两个三角形,因为它被要求呈现一个三角形条带。前三个顶点定义一个三角形,每个后续顶点添加另一个三角形,总共 4 个三角形。

您得到的空白屏幕是顶点着色器被发送 2D 位置并将其解释为 4D 位置的结果,并且不存在任何颜色信息。

要解决这些问题,请执行以下操作:

  1. 顶点着色器应接收位置的 a,因为这些位置是以 2D 形式给出的:vec2

    layout(location = 0) in vec2 position;
    

    然后将这些转换为 4D 用于:gl_Position

    gl_Position = vec4(position, 0.0f, 1.0f);
    
  2. 缺少颜色的定义、设置相应的缓冲区和上传颜色数据。例如,添加:

    GLfloat colors[24] = {
         1.0f,  0.0f, 0.0f, 1.0f,
         0.0f,  1.0f, 0.0f, 1.0f,
         0.0f,  0.0f, 1.0f, 1.0f,
         1.0f,  1.0f, 0.0f, 1.0f,
         0.0f,  1.0f, 1.0f, 1.0f,
         1.0f,  1.0f, 1.0f, 1.0f,
    };
    
    GLuint color_buffer;
    glGenBuffers(1, &color_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * 4 * sizeof(float), colors, GL_STATIC_DRAW);
    
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
    glEnableVertexAttribArray(1);
    

结果:

corresponding_output