使用默认构造函数调用的网格类不起作用 OpenGL

Mesh class called with default constructor not working OpenGL C++(使用默认构造函数调用的网格类不起作用 OpenGL C++)

本文介绍了使用默认构造函数调用的网格类不起作用 OpenGL C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 OpenGL 3.3 创建了一个 Mesh 类,当我使用非默认构造函数创建类时,当我创建对象时创建顶点时,它可以正常工作.
但是,我现在想要通过将它们放入向量中来动态创建多个对象,因此我必须添加一个默认构造函数,我使用与其他构造函数相同的函数来设置缓冲区数据......但是它不起作用.据我所知,不是因为它在向量中,而是与构造函数有关,或者与稍后创建缓冲区数据的事实有关.我真的不太确定.

I created a Mesh class for OpenGL 3.3, it works fine when I create the class with a non-default constructor, when I create the vertices when I create the object.
However, I now want to have multiple objects that I can create dynamically by putting them in a vector, so I had to add in a default constructor I use the same functions for setting up the buffer data as with the other constructor... but it doesn't work. It's as far as I can tell not because of the fact it's in the vector but it's something to do with the constructor or something with the fact the buffer data is created later. I'm really not quite sure.

这是我的课程.(当我创建一个有效的网格时,我调用带参数的构造函数,当它不起作用时,我构建一个没有参数的网格并调用changeMes​​h"函数)

Here are my classes. ( When I create a mesh that works I call the constructor with parameters and when it doesn't work I construct a mesh with no parameters and call the "changeMesh" function)

mesh.h

#ifndef MESH_H
#define MESH_H

#include <iostream>
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

class mesh
{
    public:
        mesh();
        mesh(std::vector<GLfloat> vertices, std::vector<GLuint> triangles, GLuint shaderProgram);
        ~mesh();
        void changeMesh(std::vector<GLfloat> vertices, std::vector<GLuint> triangles, GLuint shaderProgram);
        void render();
        void Translate(glm::vec3 addVector);
        void Rotate(glm::vec3 rotVector, GLfloat angle);
    protected:
    private:
        GLuint vertexArrayObject, vertexBuffer, elementBuffer, shaderProgram;
        std::vector<GLfloat> vertices;
        std::vector<GLuint> indices;
        glm::mat4 transform;
        void setUpMesh();
        void bindVertices();
};

#endif // MESH_H

mesh.cpp

    #include "../include/mesh.h"

mesh::mesh(std::vector<GLfloat> vertices, std::vector<GLuint> indices, GLuint shaderProgram)
{
    this->shaderProgram = shaderProgram;
    this->vertices = vertices;
    this->indices = indices;
    setUpMesh();
}

mesh::mesh(){
    glGenVertexArrays(1, &vertexArrayObject);
    glBindVertexArray(vertexArrayObject);

    glGenBuffers(1, &vertexBuffer);
    glGenBuffers(1, &elementBuffer);
}

mesh::~mesh()
{
    glDeleteBuffers(1, &elementBuffer);
    glDeleteBuffers(1, &vertexBuffer);

    glDeleteVertexArrays(1, &vertexArrayObject);
}

void mesh::changeMesh(std::vector<GLfloat> vertices, std::vector<GLuint> triangles, GLuint shaderProgram){
    this->shaderProgram = shaderProgram;
    this->vertices = vertices;
    this->indices = indices;
    bindVertices();

}

void mesh::setUpMesh(){
    glGenVertexArrays(1, &vertexArrayObject);
    glBindVertexArray(vertexArrayObject);

    glGenBuffers(1, &vertexBuffer);
    glGenBuffers(1, &elementBuffer);

    bindVertices();
    glBindVertexArray(0);

}

void mesh::bindVertices(){
    glBindVertexArray(vertexArrayObject);

    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(GLfloat), this->vertices.data(), GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), this->indices.data(), GL_STATIC_DRAW);


    GLint amountDataPerVert = 5;

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, amountDataPerVert*sizeof(GLfloat), 0);

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, amountDataPerVert*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));


    glBindVertexArray(0);

}
void mesh::render(){
    glBindVertexArray(vertexArrayObject);
    glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "transform"), 1, GL_FALSE, glm::value_ptr(transform));

    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

void mesh::Translate(glm::vec3 addVector){
    transform = glm::translate(transform, addVector);
}

void mesh::Rotate(glm::vec3 rotVector, GLfloat angle){
    transform = glm::rotate(transform, glm::radians(angle), rotVector);
}

推荐答案

虽然您认为问题与将对象存储在向量中无关,但我有一种强烈的感觉,它可能会发生.您在 C++ 包装器中封装 OpenGL 对象的方式是一种痛苦的方法,您可能会像之前的许多人一样发现.

While you believe that the problem has nothing to do with storing the objects in a vector, I have a strong feeling that it probably does. The way you are encapsulating OpenGL objects in C++ wrappers is a recipe for pain, and you're probably finding out like many did before you.

典型的问题是由复制和销毁对象时发生的组合引起的.C++包装器拥有的OpenGL对象在析构函数中被删除:

The typical problems are caused by the combination of what happens when objects are copied and destructed. The OpenGL objects owned by the C++ wrapper are deleted in the destructor:

mesh::~mesh()
{
    glDeleteBuffers(1, &elementBuffer);
    glDeleteBuffers(1, &vertexBuffer);

    glDeleteVertexArrays(1, &vertexArrayObject);
}

为了说明这个问题,让我们看一个典型的序列.假设您有一个网格对象向量,以及一种向该向量添加新网格的方法(注释点以供以后参考):

To illustrate the problem with this, let's look at a typical sequence. Let's say you have a vector of mesh objects, and a method to add a new mesh to this vector (points annotated for later reference):

std::vector<mesh> m_meshes;

void createMesh(...) {
    mesh newMesh;  // point 1
    newMesh.changeMesh(...);
    m_meshes.push_back(newMesh);  // point 2
}  // point 3

看起来无害?根本不是.这里发生了不好的事情:

Looks harmless? It's not at all. Bad things happened here:

  • 第 1 点:创建了新对象.构造函数创建 OpenGL 对象,并将它们的名称存储在成员变量中.
  • 第 2 点:将网格对象的副本添加到向量中,其中使用默认复制构造函数创建副本.这意味着将复制包含 OpenGL 对象名称的成员变量.
  • 第 3 点:网格对象超出范围.调用析构函数,删除 OpenGL 对象.
  • Point 1: New object is created. The constructor creates the OpenGL objects, and stores their names in member variables.
  • Point 2: A copy of the mesh object is added to the vector, where the copy is created with the default copy constructor. This means that the member variables, which contain the OpenGL object names, are copied.
  • Point 3: The mesh object goes out of scope. The destructor is invoked, which deletes the OpenGL objects.

毕竟你拥有的是一个存储在向量中的网格对象,OpenGL 对象名称存储在其成员变量中,而实际的 OpenGL 对象已被删除.这意味着存储在此网格对象中的对象名称现在无效.

What you have after all of this is a mesh object stored in the vector, with OpenGL object names stored in its member variables, while the actual OpenGL objects have been deleted. This means that the object names stored in this mesh object are now invalid.

根本问题是您的类没有正确的复制构造函数和赋值运算符.不幸的是,在成员变量中存储 OpenGL 对象名称,以及在构造函数/析构函数中生成/删除对象名称时,实现它们并不容易.

The root problem is that your class does not have proper copy constructors and assignment operators. And unfortunately, it is not easily possible to implement them when storing OpenGL object names in member variables, and generating/deleting the object names in constructor/destructor.

有很多方法可以解决这个问题.它们都不是完美的:

There are a number of ways to handle this. None of them are perfectly pretty:

  1. 不要在构造函数/析构函数中生成/删除 OpenGL 对象.相反,使用您显式调用的某种形式的 init()/cleanup() 方法.缺点是您必须小心正确地调用这些方法.例如,如果您有一个对象向量,并且想要删除该向量,则必须手动对向量的所有成员调用 cleanup().

  1. Do not generate/delete the OpenGL objects in constructor/destructor. Instead, use some form of init()/cleanup() methods that you invoke explicitly. The downside is that you have to be careful to invoke these methods correctly. For example, if you have a vector of objects, and want to delete the vector, you have to invoke cleanup() on all members of the vector manually.

总是用指针来引用对象.使用网格对象指针向量代替网格对象向量.这样,对象就不会被复制.您还必须小心正确地管理对象的生命周期,而不是泄漏它们.如果您使用某种形式的智能指针而不是裸指针,这是最简单的.

Always reference the objects with pointers. Instead of having a vector of mesh objects, use a vector of mesh object pointers. This way, the objects are not copied. You also have to be careful to manage the lifetime of the objects correctly, and not leak them. This is easiest if you use some form of smart pointer instead of naked pointers.

使用某种形式的混合,您仍然使用实际的 C++ 对象,但它们将底层 OpenGL 对象的名称存储在引用计数的嵌套对象中.这样,他们就可以实现正确的复制/分配语义.

Use some form of hybrid, where you still use actual C++ objects, but they store the names of the underlying OpenGL objects in a nested object that is reference counted. This way, they can implement proper copy/assign semantics.

我认为最简单、最干净的方法是使用智能指针的选项 2.较新版本的 C++ 在标准库中具有智能指针,因此您无需实现任何内容.例如,在 C++11 中,您可以使用类型 std::shared_ptr 来引用您的网格对象.上面的代码片段将如下所示:

I think the easiest and cleanest approach is option 2 with using smart pointers. Newer versions of C++ have smart pointers in the standard library, so there isn't anything you need to implement. For example in C++11, you can use the type std::shared_ptr<mesh> to reference your mesh objects. The code fragment above would then look like this:

std::vector<std::shared_ptr<mesh> > m_meshes;

void createMesh(...) {
    std::shared_ptr<mesh> newMesh = std::make_shared<mesh>();
    newMesh->changeMesh(...);
    m_meshes.push_back(newMesh);
}

为了确保无论如何都不会意外复制对象,为类声明未实现的(私有)复制构造函数和赋值运算符也是一个好主意.本主题解释了如何在 C++11 中做到最好:在 C++11 中使用显式删除的成员函数,是否仍然值得从不可复制的基类继承?.

To be sure that you don't accidentally copy the objects anyway, it's also a good idea to declare unimplemented (private) copy constructors and assignment operators for the class. This topic explains how to do that best in C++11: With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class?.

这篇关于使用默认构造函数调用的网格类不起作用 OpenGL C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用默认构造函数调用的网格类不起作用 OpenGL

基础教程推荐