Hello,
I’m trying to convert my own vector to a glm:vec3 like this:
MyVec<float> v1;
glm::vec3 v2 = v1;
In my template vector class MyVec I have added this
conversion/cast operator:
template <class Real>
MyVec<Real>::operator glm::vec3() const
{
return glm::vec3((float) Vector[0], (float) Vector[1], (float) Vector[2]);
}
Compiling results in:
MyVec<Real> cannot be converted to glm::detail::tvec3<T>.
What am I doing wrong?
Thanks for help!
When I read you code I thought it should have worked… so I gave it a try and guess what it works!
#include "glm/glm.hpp"
template <class T>
struct vec
{
operator glm::vec2() const;
float x, y;
};
template <class T>
vec<T>::operator glm::vec2() const
{
glm::vec2 v;
v.x = x;
v.y = y;
return v;
}
int main(int argc, char *argv[])
{
vec<float> A;
glm::vec2 B = A;
return 0;
}
Maybe there is something specific in your code that implies this behaviour… I can’t really what. Could you provide your implementation of MyVec?
Hi,
thanks your code compiles fine! I also have
a const cast operator defined in MyVec:
template <class T>
vec<T>::operator const glm::vec3() const
{
return glm::vec3(x, y, z);
}
Adding this to your code example also gives compile errors.
But let me explain my original intention. Basically I want
this code to work:
MyVec<float> v;
glm::scale(v);
Therefor I added the (non-const) cast operator which gave
me compile errors. So my first assumption was that I have
to add a const cast operator to make it work with the
scale function that requires a const argument.
So now I’m stuck with two questions which are maybe more
C++ related than to GLM But maybe you know the answer!
-
Why is your example code working fine UNTIL I add
the const cast operator?
-
How can I make this code work:
MyVec<float> v;
glm::scale(v);
Thanks a lot for you help!
The transformation functions are mapped on the deprecated OpenGL transform functions:
I guess:
glm::mat4 M = glm::scale(glm::mat4(1.0), glm::vec3(v));
Due to the template implementation, I can see some trouble for the compiler to actually perform an implicit cast. By the way, implicit cast are evil!