I want to have an enum wich contains glm::vec3 "constants".

Currently i have an enum called "Axis", wich containts X, Y and Z. and everytime i use them i have to map them with a switch case into a glm::vec3 with corresponding values.

This is my Axis enum:

enum Axis 
{
    X,
    Y,
    Z
};

This is how i currently convert it to glm::vec3:

glm::vec3 angleVector;

switch (axis)
{
case X:
    angleVector = glm::vec3(1.0f, 0.0f, 0.0f);
    break;
case Y:
    angleVector = glm::vec3(0.0f, 1.0f, 0.0f);
    break;
case Z:
    angleVector = glm::vec3(0.0f, 0.0f, 1.0f);
    break;
}

And this is how i want to use it instead:

enum Axis 
{
    X = glm::vec3(1.0f, 0.0f, 0.0f),
    Y = glm::vec3(0.0f, 1.0f, 0.0f),
    Z = glm::vec3(0.0f, 0.0f, 1.0f)
};

I tried much things to get the glm::vec3(1.0f, 0.0f, 0.0f) as a constant wich my enum does accept, but nothing worked.

(

like this:

static const glm::vec3 vec3x = glm::vec3(1.0f, 0.0f, 0.0f);

and then:

enum Axis 
{
    X = vec3x
};

)

Hope you can help me there. Thanks

If it might be a repost, sorry, i searched but havent found anything.

2 Answers

2
Ted Klein Bergman On Best Solutions

You could use a namespace with static variables instead. Then you'll be able to use it roughly as you would with an enum.

namespace Axis
{
    static const glm::vec3 X = glm::vec3(1.0f, 0.0f, 0.0f);
    static const glm::vec3 Y = glm::vec3(0.0f, 1.0f, 0.0f);
    static const glm::vec3 Z = glm::vec3(0.0f, 0.0f, 1.0f);
};

If you want to pass it to a function, you could create a class. However, then you'd have to instantiate an object when passing it in.

class Axis
{
public:
    static const glm::vec3 X = glm::vec3(1.0f, 0.0f, 0.0f);
    static const glm::vec3 Y = glm::vec3(0.0f, 1.0f, 0.0f);
    static const glm::vec3 Z = glm::vec3(0.0f, 0.0f, 1.0f);
} euclidean_space_axis;

void DoStuff(Axis axis)
{
    std::cout << "X-axis: " << axis.X.x << ", " << axis.X.y << ", " << axis.X.z << "\n" <<
    std::cout << "Y-axis: " << axis.Y.x << ", " << axis.Y.y << ", " << axis.Y.z << "\n" <<
    std::cout << "Z-axis: " << axis.Z.x << ", " << axis.Z.y << ", " << axis.Z.z << std::endl;
}

int main()
{
    DoStuff(euclidean_space_axis);
}
1
molbdnilo On

You can use a table instead of switching:

enum Axis { X, Y, Z };
const glm::vec3 axes[] = { glm::vec3(1.0f, 0.0f, 0.0f), 
                           glm::vec3(0.0f, 1.0f, 0.0f), 
                           glm::vec3(0.0f, 0.0f, 1.0f)};

and then

Axis axis = ... whatever ...;
glm::vec3 angleVector = axes[axis];