如何通过 OpenGL 缓冲区将 glm 数据类型直接传递给 GPU



我自己编写了数学实用程序,它可以完成我正在编程的简单图形所需的一切。但是,我不知道如何使它们有资格直接传递给OpenGL。这可以通过 glm 来完成,例如:

std::vector<glm::vec3> locations;
[...]

glGenBuffers(NUM_BUFFERS, _vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo[POSITION_VBO]);
// throw data in vbo[0]
glBufferData(GL_ARRAY_BUFFER, _num_vertices * sizeof(locations[0]), &locations[0], GL_STATIC_DRAW);

我希望能够使用我自己的 vec3 类型 math::vec3f 来做到这一点,因为我不想"浪费"我自己的时间来编写这些实用程序。它的实现可以在这里看到:

namespace math
{
    template<typename _type> class vec2;
    template<typename _type>
    class vec3
    {
        private:
            _type _gl_a[3];
        public:
            _type x, y, z;
            vec3() {};
            vec3(_type _x, _type _y, _type _z)
            {
                _gl_a[0] = x = _x;
                _gl_a[1] = y = _y;
                _gl_a[2] = z = _z;
            }
            vec3(vec2<_type> v, _type w)
            {
                _gl_a[0] = x = v.x;
                _gl_a[1] = y = v.y;
                _gl_a[2] = z = w;
            }
            inline vec3<_type> operator=(vec2<_type> &v)
            {
                _gl_a[0] = x = v.x;
                _gl_a[1] = y = v.y;
                _gl_a[2] = z = 0;
            }
            inline vec3<_type> operator+(_type other)        { return vec3<_type>(x + other,   y + other,   z + other); }
            inline vec3<_type> operator-(_type other)        { return vec3<_type>(x - other,   y - other,   z - other); }
            inline vec3<_type> operator*(_type other)        { return vec3<_type>(x * other,   y * other,   z * other); }
            inline vec3<_type> operator/(_type other)        { return vec3<_type>(x / other,   y / other,   z / other); }
            inline vec3<_type> operator+(vec3<_type> &other) { return vec3<_type>(x + other.x, y + other.y, z + other.z); }
            inline vec3<_type> operator-(vec3<_type> &other) { return vec3<_type>(x - other.x, y - other.y, z - other.z); }
            inline vec3<_type> operator*(vec3<_type> &other) { return vec3<_type>(x * other.x, y * other.y, z * other.z); }
            inline vec3<_type> operator/(vec3<_type> &other) { return vec3<_type>(x / other.x, y / other.y, z / other.z); }
            inline _type operator[](int i)
            {
                if(i < 0 || i >= 3)
                    return 0;
                _gl_a[0] = x;
                _gl_a[1] = y;
                _gl_a[2] = z;
                return _gl_a[i];
            }
            inline double magnitude()
            {
                return sqrt(x * x + y * y + z * z);
            }
            inline vec3<_type> normal()
            {
                double m = this->magnitude();
                return vec3<_type>(x / m, y / m, z / m);
            }
            inline _type dot(vec3<_type> other)
            {
                return x * other.x + y * other.y + z * other.z;
            }
            inline vec3<_type> cross(vec3<_type> other)
            {
                return vec3<_type>(y * other.z - other.y * z,
                                   z * other.x - other.z * x,
                                   x * other.y - other.x * y);
            }
    };
    typedef vec3<float>             vec3f;
    typedef vec3<double>            vec3d;
    typedef vec3<int>               vec3i;
    typedef vec3<unsigned int>      vec3ui;
    typedef vec3<short>             vec3s;
    typedef vec3<unsigned short>    vec3us;
};

是我必须添加的另一个运算符重载功能,还是完全不同的功能?

glBufferData 需要一个void指针。这正是您使用带有 glm::vec3 的代码段所做的。但是,您可以通过不同的方式执行此操作。

由于GLM将其数据存储在union中,您可以通过多种方式访问向量的元素:通过operator[]或坐标xyz。由于元素是值,而不是指针,因此需要&运算符取消引用它们。所以&myvec[0]&myvec.x具有相同的效果。

您的代码片段采用 std::vector 中第一个 glm::vec3 的指针。每个 glm::vec3 的内存地址是其第一个元素的内存地址。通过这种方式,您将指针传递给浮点数数组(向量的元素,如果它们是紧密打包的 - 在 GLM 中它们是(:

std::vector<glm::vec3> locations;
glBufferData(GL_ARRAY_BUFFER, _num_vertices * sizeof(locations[0]), &locations[0], GL_STATIC_DRAW);
// same as above
//glBufferData(GL_ARRAY_BUFFER, _num_vertices * sizeof(locations[0]), &locations[0][0], GL_STATIC_DRAW);
// same as above
//glBufferData(GL_ARRAY_BUFFER, _num_vertices * sizeof(locations[0]), &locations[0].x, GL_STATIC_DRAW);

我建议您熟悉C++中指针、引用和联合的概念,因为我的以下部分答案依赖于它们。


您的math::vec3实现存在多个问题。

1(正如其他人在评论中已经指出的那样,您需要两者

_type operator[](int i);
_type operator[](int i) const;

以支持const

2( 返回引用 (_type& ( 而不是值 ( _type (。目前,您按 _type operator[](int i); 返回值,因此您的元素是只读的。使用引用,您可以读取和写入它们。

_type& operator[](int i);
_type& operator[](int i) const;

3( 由于不能有负索引,因此不应使用带符号int进行索引。使用无符号类型:unsigned int可以完成这项工作,但size_t更好。

&_type operator[](size_t i);
&_type operator[](size_t i) const;

4(无需针对if (i < 0 || i >= 3)进行测试。它只会使您的元素在关键循环中访问 LOT 变慢(当您多次访问元素时(。使用 size_t 的值不能小于 0,并且在正确的代码中,绝不应传递大于向量实际大小的索引。

5(您存储数据两次:一次在_gl_a[3],一次在xyz。这是对内存的巨大浪费。相反,您应该使用union以多种方式访问相同的数据。

union // anonymous union
{
   _gl_a[3];
   struct { x, y, z, }; // anonymous struct
};

一旦你有了正确的math::vec3实现,你将能够以与glm类型相同的方式使用它。

glBufferData()需要void*,所以它不在乎你传递给它什么类型。该函数仅查看原始内存。

但是,您需要以其他方式告诉OpenGL如何解释该数据(例如 glVertexAttribPointer() (。如果你告诉它期望一个浮点 x3 数组,那么你需要给它传递一个浮点 x3 数组,否则你会得到损坏的输出。

虽然glm::vec3包含 3 个浮点数,但您的包含 6 个(假设_type浮点数(。您似乎无缘无故地复制了组件。要么删除重复项,要么告诉 opengl 期望您的格式,最好是前者。

最新更新