Deriving classes from GLM clasees?

Hi all -

I was trying to create a class based on a GLM class, for example:

#include <glm/glm.hpp>
class Vec3 : public glm::vec3
{
public:
glm::vec3 GetVec() { return (glm::vec3)*this; }
};

but when I compile this simple example (g++ -std=c++11 -c testfile.cpp) I get errors such as:

In file included from /usr/include/glm/./core/type_vec3.hpp:341:0,
from /usr/include/glm/./core/type.hpp:40,
from /usr/include/glm/glm.hpp:97,
from glmTest.cpp:1:
/usr/include/glm/./core/type_vec3.inl: In instantiation of ‘glm::detail::tvec3<T>::tvec3(const U&) [with U = Vec3; T = float]’:
glmTest.cpp:6:43: required from here
/usr/include/glm/./core/type_vec3.inl:163:18: error: invalid cast from type ‘const Vec3’ to type ‘glm::detail::tvec3<float>::value_type {aka float}’
/usr/include/glm/./core/type_vec3.inl:163:18: error: invalid cast from type ‘const Vec3’ to type ‘glm::detail::tvec3<float>::value_type {aka float}’
/usr/include/glm/./core/type_vec3.inl:163:18: error: invalid cast from type ‘const Vec3’ to type ‘glm::detail::tvec3<float>::value_type {aka float}’

which means that I am obviously doing it wrong. My question is (since I am not incredibly familiar with templates ) can I do what I am trying to do? If so, could someone point me to a simple example? Googling doesn’t bring up much - the only examples I see are how to use glm::vec3, etc. directly. Or, is it better to wrap glm classes like:

#include <glm/glm.hpp>

class Vec3
{
private:
glm::vec3 m_vec;
public:
…member funtions that manipulate m_vec…
};

The reason I ask is I am trying to port code from DirectX to OpenGL - just to see if I can :slight_smile:
Many of the classes are formed like:

class Vec3 : public D3DXVECTOR3
{
public:
inline float Length() { return D3DXVec3Length(this); }
inline Vec3 *Normalize() { return static_cast<Vec3 *>(D3DXVec3Normalize(this, this)); }
inline float Dot(const Vec3 &b) { return D3DXVec3Dot(this, &b); }
inline Vec3 Cross(const Vec3 &b) const;

Vec3(D3DXVECTOR3 &v3) { x = v3.x; y = v3.y; z = v3.z; }
Vec3() : D3DXVECTOR3() { x = 0; y = 0; z = 0; }
Vec3(const float _x, const float _y, const float _z) { x=_x; y=_y; z=_z; }
Vec3(const double _x, const double _y, const double _z) { x = (float)_x; y = (float)_y; z = (float)_z; }
inline Vec3(const class Vec4 &v4);

};

if I can change the class to inherit from glm::vec3, alter the members to use glm to manipulate it, and wrap the code in some #ifdef/#else/#endif statements, I essentially can keep it like an interface. My second option (wrap the class around a private member variable) works also, but I do have to either make the glm::(types) public, recode pieces to use get/set, or let the class have alot of friends, which feels kind of hacky to me, but may be the correct way to do it - not sure.

Any suggestions/simple examples to get me started would be much appreciated!

Thanks!

try this


class Vec3 : public glm::vec3
{
public:
const glm::vec3& GetVec() { return (const glm::vec3&)*this; }
};

void fnc()
{
  Vec3 v;
  glm::vec3 x = v.GetVec();
}

this might ne of more use


class Vec3 : public glm::vec3
{
  float q;

public:
  Vec3(){};
  const Vec3& operator=(const glm::vec3& v)
  {
    x = v.x;
    y = v.y;
    z = v.z;
    q = 0;
    return (const Vec3&)*this;
  }

  void inc_q()
  {
    q++;
  }
};

void fnc()
{
  Vec3 v, v1;
  glm::vec3 x = v;
  v1 = v = x;
  v1.inc_q();
}