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

nsc4cvqm  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(297)

这是我用C++编写的代码,应该可以生成两个三角形,但是我却得到了一个空白的屏幕。是不是有什么东西我遗漏了?


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

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

41ik7eoe

41ik7eoe1#

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

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);
bnl4lu3b

bnl4lu3b2#

代码不应该生成 * 两个 * 三角形,因为它被要求渲染一个 * 三角形带 *。前三个顶点定义一个三角形,每个后续顶点添加另一个三角形,总共4个三角形。
您得到的空白屏幕是顶点着色器发送2D位置并将其解释为4D位置的结果,并且不存在任何颜色信息。
要解决这些问题,请执行以下操作:
1.顶点着色器应接收位置的vec2,因为这些位置是以2D形式给出的:

layout(location = 0) in vec2 position;

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

gl_Position = vec4(position, 0.0f, 1.0f);

1.缺少颜色定义、设置相应缓冲区和上传颜色数据。例如,添加:

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);

结果是:

相关问题