【问题标题】:c++ with openGl can't render带有openGl的c ++无法渲染
【发布时间】:2021-12-09 06:02:36
【问题描述】:

我尝试通过在某些类中提取整个主 Proramm 来使代码更简洁。所以我有一个顶点缓冲区、索引缓冲区和一个着色器类。只有数组缓冲区在主缓冲区中并且它起作用了。

现在我创建了一个布局类和一个顶点数组类来将不同的布局放在顶点数组中,并且我的 main 中的渲染函数停止工作。

这是我的代码:

vertexBuffer.cpp

#include "../headerData/VertexBuffer.h"

VertexBuffer::VertexBuffer(const void* data, unsigned int size) {

    glGenBuffers(1, &bufferId);
    glBindBuffer(GL_ARRAY_BUFFER, bufferId);
    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);


}
VertexBuffer::~VertexBuffer() {

    glDeleteBuffers(1, &bufferId);
}

void VertexBuffer::bind() const {
    glBindBuffer(GL_ARRAY_BUFFER, bufferId);
}

void VertexBuffer::unbind() const {
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

顶点索引cpp:

#include "../headerData/IndexBuffer.h"

IndexBuffer::IndexBuffer(const void* data, unsigned int count) {

    glGenBuffers(1, &bufferId);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, count, data, GL_STATIC_DRAW);


}
IndexBuffer::~IndexBuffer() {

    glDeleteBuffers(1, &bufferId);
}

void IndexBuffer::bind() const {
    glBindBuffer(GL_ARRAY_BUFFER, bufferId);
}

void IndexBuffer::unbind() const {
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

VertexLayout.h

#pragma once
#include <vector>


//Stubides Layout da vieles vorgegeben wird
struct ElementLayout {
    unsigned int size;          //anzahl an koordinaten die zählen
    unsigned int stride;        //Die größe der 
};

class VertexLayout {

private:
    std::vector<ElementLayout> elements;
public:
    VertexLayout();
    ~VertexLayout();

    void layoutPush(unsigned int size);
    std::vector<ElementLayout> getElements() const;
};

vertexLayout.cpp:

#include"../headerData/VertexLayout.h"


VertexLayout::VertexLayout() {}
VertexLayout::~VertexLayout() {}

void VertexLayout::layoutPush(unsigned int size)
{
    elements.push_back({ size, size * sizeof(float) });
}

std::vector<ElementLayout> VertexLayout::getElements() const {
    return elements;
}

VertexArray.cpp

#include "../headerData/VertexArray.h"
#include <iostream>

VertexArray::VertexArray() {
    glGenVertexArrays(1, &vertexArrayID);

}
VertexArray::~VertexArray() {
    glDeleteVertexArrays(1, &vertexArrayID);
}

void VertexArray::bind() const {
    glBindVertexArray(vertexArrayID);
}
void VertexArray::unbind() const {
    glBindVertexArray(0);
}

//Vom Buffer soll auch das zugehörige Layout übergeben werden
void VertexArray::addBuffer(VertexBuffer& vb, VertexLayout& layout) {
    bind();
    vb.bind();
    std::vector<ElementLayout> element = layout.getElements();
    unsigned int offset = 0;
    for (int i = 0; i < element.size(); i++) {

        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, element[i].size, GL_FLOAT, GL_FALSE, element[i].stride, (void*)0);
        offset += element[i].size * sizeof(float);
        std::cout << GL_NO_ERROR;
    }

    std::cout << glGetError();

}

main.cpp:

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include "headerData/VertexBuffer.h"
#include "headerData/IndexBuffer.h"
#include "headerData/Shader.h"
#include "headerData/VertexLayout.h"
#include "headerData/VertexArray.h"


void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    Shader shader;
    shader.bind();
    // set up vertex data (and buffer(s)) and configure vertex attributes
    // ------------------------------------------------------------------
    float vertices[] = {
         0.5f,  0.5f, 0.0f,  // top right
         0.5f, -0.5f, 0.0f,  // bottom right
        -0.5f, -0.5f, 0.0f,  // bottom left
        -0.5f,  0.5f, 0.0f,   // top left
        -1.0f, -1.0f, 0.0f  //zum Testen
        //dreieck

    };
    unsigned int indices[] = {  // note that we start from 0!
        0, 1, 3,   // first triangle
        1, 2, 3    // second triangle
    };

    float vertices2[] = {
    -1.0f, -1.0f, 0.0f,
     1.0f, -1.0f, 0.0f,
     0.0f,  0.0f, 0.0f
    };
    unsigned int test;
    glGenVertexArrays(1, &test);

    // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
    glBindVertexArray(test);

    //glBindBuffer(GL_ARRAY_BUFFER, VBO);
    //glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    VertexBuffer vb(vertices, 5 * 3 * sizeof(float));
    IndexBuffer ib(indices, 6 * sizeof(unsigned int));
    VertexLayout layout;
    layout.layoutPush(3);
    VertexArray va;
    //va.addBuffer(vb, layout);


    //va.addBuffer(vb, layout); when this is uncomment the render function doesn't work

    //Parameter
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(0));
    //glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(0);
    //glEnableVertexAttribArray(1);


    // uncomment this call to draw in wireframe polygons.
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        shader.bind();
        //glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(unsigned int) * 3));
        //glDrawArrays(GL_TRIANGLES, 0, 3);
        //glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(unsigned int) * 3));
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        // glBindVertexArray(0); // no need to unbind it every time 

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // optional: de-allocate all resources once they've outlived their purpose:
    // ------------------------------------------------------------------------
    glDeleteVertexArrays(1, &test);


    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

使用 glGetError() 我得到数字 1281 =>无效值但是在哪里。

【问题讨论】:

  • 之后需要拨打glGetError。每一个。 gl* 打电话。或者,设置 OpenGL 调试回调。
  • 问题解决了吗?

标签: c++ class opengl glfw vertex-array


【解决方案1】:

index buffer (ELEMENT_ARRAY_BUFFER) 绑定存储在Vertex Array Object 中。当glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId) 被调用时,对元素缓冲区对象的引用存储在当前绑定的顶点数组对象中。因此VAO必须在元素缓冲区之前绑定glBindVertexArray(vertexArrayID)

VertexArray va;
va.bind();
IndexBuffer ib(indices, 6 * sizeof(unsigned int));

【讨论】:

  • 非常感谢 :) 它有效
  • @Reazelruss 不客气。
  • @Reazelruss 如果答案有效,请务必将其标记为正确答案,以便在其他有相同问题的人中脱颖而出(:
猜你喜欢
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 2021-06-28
  • 2016-04-30
  • 2021-06-13
相关资源
最近更新 更多