【问题标题】:access violation when i use my index buffer to draw objects [duplicate]当我使用索引缓冲区绘制对象时访问冲突[重复]
【发布时间】:2021-12-21 03:31:33
【问题描述】:

我正在学习 OpenGL,我正在尝试抽象它以方便我使用它。 但是当我在渲染时使用我的 IndexBuffer 类时,我遇到了访问冲突。

这是我的 IndexBuffer.h 代码

class IndexBuffer
{
public:
    IndexBuffer(void* data, int count);
    IndexBuffer(int count);

    IndexBuffer Bind();
    IndexBuffer UnBind();

    void AddData(void* data);

    ~IndexBuffer();
private:
    int count;
    size_t size;
    unsigned int id;
};

这是我的 IndexBuffer.cpp 代码

#include "IndexBuffer.h"
#include "glew/glew.h"

IndexBuffer::IndexBuffer( void* data, int count): id(0), count(count), size(sizeof(unsigned int)* count)
{
    glGenBuffers(1, &id);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * count, data, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

IndexBuffer::IndexBuffer( int count) : id(0), count(count), size(sizeof(unsigned int)* count)
{
    glGenBuffers(1, &id);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * count, nullptr, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

IndexBuffer IndexBuffer::Bind()
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
    return *this;
}

IndexBuffer IndexBuffer::UnBind()
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    return *this;
}

void IndexBuffer::AddData(void* data)
{
    Bind();
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(unsigned int) * count, data);
    UnBind();
}

IndexBuffer::~IndexBuffer()
{
    glDeleteBuffers(1, &id);
}

我以同样的方式编写了我的 VertexBuffer,它工作正常,但我的索引缓冲区不起作用。

这是我的 main.cpp

#include <iostream>
#include "glew/glew.h"
#include "glfw/glfw3.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"

struct Vertex
{
    float aPos[2];
};

int main() {

    GLFWwindow* window;

    if (glfwInit() == GLFW_FALSE)
    {
        return -1;
    }

    //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
    window = glfwCreateWindow(600, 600, "Hello world", nullptr, nullptr);

    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK)
    {
        return -2;
    }

    unsigned int index[3] = {
        0, 1, 2
    };

    Vertex data[] = {
        Vertex({-0.5f, -0.5f}),
        Vertex({ 0.5f, -0.5f}),
        Vertex({ 0.0f,  0.5f})
    };

    unsigned int VertexArrayObject;

    glGenVertexArrays(1, &VertexArrayObject);
    VertexBuffer buffer = VertexBuffer(sizeof(Vertex) * 3);
    IndexBuffer Ibuffer = IndexBuffer( 3);

    buffer.Bind();

    //glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, data, GL_STATIC_DRAW);
    buffer.AddData(data);

    glBindVertexArray(VertexArrayObject);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, aPos));

    //glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    buffer.UnBind();
    
    Ibuffer.Bind();

    Ibuffer.AddData(index);

    Ibuffer.UnBind();

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        buffer.Bind();
        Ibuffer.Bind();
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glDeleteVertexArrays(1, &VertexArrayObject);

    glfwDestroyWindow(window);
    return 0;
}

谁能帮帮我?

【问题讨论】:

  • 请发布您遇到的错误。您是否尝试过使用调试器单步执行代码?

标签: c++ opengl opengl-3


【解决方案1】:

简而言之,你有未定义的行为。

您的课程不支持深拷贝。 当Bind函数按值返回对象(即自身)时,如:

IndexBuffer IndexBuffer::Bind()

IndexBuffer的析构函数被调用,它删除了之前分配的缓冲区,所以缓冲区的id是悬空的。

所有Bind 都应该返回对实例的引用:

IndexBuffer& IndexBuffer::Bind()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多