我自己写了数学实用程序,它可以完成我编程的简单图形所需的一切。但是,我不知道如何让他们有资格直接传递给OpenGL。这可以用GLM,如来完成:glm数据类型如何通过OpenGL缓冲区直接传递给GPU?
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);
我希望能够用我自己的VEC 3类,数学:: 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;
};
它是另一个运算符重载函数,我必须添加,或者完全不同的东西?
您需要2个'operator []'(一个非const和一个const)参见std :: vector签名。他们应该为非const和const版本返回'_type&'和'_type const&'。 –
添加这样的功能对结果没有任何影响。 (当我用math :: vec3f替换glm :: vec3的所有出现时) – Anickyan