四元对象未渲染到屏幕上

问题描述 投票:0回答:1
#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColour;"
"out vec3 vertexColour;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"vertexColour = aColour;\n"
"}\0";

const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 vertexColour;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(vertexColour,1.0f);\n"
"}\n\0";

// renderable classes 
class Render {

};

class Quad : public Render {
    public:
        unsigned int VAO;
        unsigned int VBO;
        unsigned int IBO;
    
        float vertices[24] = {
            -0.5f, -0.5f, 0.0f,  0.0f, 0.0f, 0.0f,               // bottom left   index -- 0
         0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 0.0f,               // bottom right   index -- 1
         -0.5f,  0.5f, 0.0f,  0.0f, 0.0f, 0.0f,               // top left      index -- 2
         0.5f, 0.5f, 0.0f,    0.0f, 0.0f, 0.0f,               // top right     index -- 3
        };
        unsigned int indices[6] = {
            0,1,2,
            1,3,2
        };

        void constructVAO() {
            glGenVertexArrays(1, &VAO);
            glBindVertexArray(0);

            constructVBO();  // Bind the VBO
            constructIBO();  // Bind the IBO
            // position VAO
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
            glEnableVertexAttribArray(0);
            // colour VAO
            glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
            glEnableVertexAttribArray(1);
        };

        
        void constructVBO() {
            glGenBuffers(1, &VBO);
            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
        };
        void constructIBO() {
            glGenBuffers(1, &IBO);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
        }

        void quadForge() {
            constructVBO();
            constructIBO();
            constructVAO();
        }
};

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(1920, 1080, "test", NULL, NULL);
    glfwMakeContextCurrent(window);
    gladLoadGL();
    glViewport(0, 0, 1920, 1080);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    unsigned int vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER); 
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    unsigned int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    unsigned int shaderProgram;
    shaderProgram = glCreateProgram();

    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);


    Quad quad1;
    quad1.quadForge();

    glfwSwapBuffers(window);
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(shaderProgram);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);


        glfwSwapBuffers(window); 

    }


    glfwDestroyWindow(window);
    glfwTerminate();

}

我创建了一个四边形类并初始化了一个要渲染的对象。然而,它只是在将其放入对象之前而不是在屏幕上渲染任何内容

我确实有这些警告

Severity    Code    Description Project File    Line    Suppression State
Warning C26495  Variable 'Quad::IBO' is uninitialized. Always initialize a member variable (type.6).    OpenGLProject   C:\\Users\\intel\\source\\repos\\OpenGLProject\\main.cpp    79  
Warning C26495  Variable 'Quad::VAO' is uninitialized. Always initialize a member variable (type.6).    OpenGLProject   C:\\Users\\intel\\source\\repos\\OpenGLProject\\main.cpp    79  
Warning C26495  Variable 'Quad::VBO' is uninitialized. Always initialize a member variable (type.6).    OpenGLProject   C:\\Users\\intel\\source\\repos\\OpenGLProject\\main.cpp    79  

(虽然变量已经初始化)

我尝试首先从函数中实际返回 VAO,然后分配它们,但这些都没有返回任何结果

c++ opengl glfw glm-math
1个回答
0
投票
  1. 警告

由于使用了未初始化的变量,编译器会给出适当的警告。编译器不知道这些函数实际上会初始化它们。

  1. 编译和链接状态

您不检查着色器是否已编译,也不检查程序的链接是否成功。我不是 100% 确定这是否值得关注,但顶点着色器源代码中第 3 行末尾缺少

\n

如果编译或链接不成功,着色器信息日志或程序信息日志会告诉您出了什么问题。

同样重要的是:

glDebugMessageCallback
(OpenGL 4.3+)

  1. 多个函数调用

quadForge
被调用,它调用
constructVBO
constructIBO
constructVAO
,但后者还调用前面的函数(vbo、ibo)。看起来很多余,不是吗?

  1. zero
    顶点数组对象

在函数

constructVAO
中,您生成一个顶点数组对象名称,但不使用生成的顶点数组对象,而是使用名为
zero
的特殊顶点数组对象,该对象通常用于打破现有绑定。

所以,不要使用

glBindVertexArray(0)
,而是使用
glBindVertexArray(VAO)

© www.soinside.com 2019 - 2024. All rights reserved.