View Full Version : Converting own vector to glm:vec3

Schnulla

06-01-2010, 02:01 PM

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!

Groovounet

06-01-2010, 04:51 PM

When I read you code I thought it should have worked... so I gave it a try and guess what it works! :p

#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?

Schnulla

06-01-2010, 07:57 PM

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! :D

1. Why is your example code working fine UNTIL I add

the const cast operator?

2. How can I make this code work:

MyVec<float> v;

glm::scale(v);

Thanks a lot for you help! :)

Groovounet

06-02-2010, 05:47 AM

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! :p

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.