C ++ Cast float * to glm :: vec3

How can I create an array of floating point numbers in the form of float*to glm::vec3? I thought I did this before, but I lost my hdd. I tried several C-style and static_cast, but I can not get it to work.

+3
source share
3 answers

glm documentation tells you how to distinguish from vec3to float*.

#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
glm::vec3 aVector(3);
glm::mat4 someMatrix(1.0);
glUniform3fv(uniformLoc, 1, glm::value_ptr(aVector));
glUniformMatrix4fv(uniformMatrixLoc,
       1, GL_FALSE, glm::value_ptr(someMatrix));

You are using glm::value_ptr.

The documentation does not say this explicitly, but it seems obvious that glm intends these types to be "layout compatible" with arrays, so they can be used directly with OpenGL functions. If so, you can do from an array in vec3 using the following cast:

float arr[] = { 1.f, 0.f, 0.f };
vec3<float> *v = reinterpret_cast<vec3<float>*>(arr);

, , .

+7

float* vec3:

float data[] = { 1, 2, 3 };
glm::vec3 vec = glm::make_vec3(data);

vec3 float*:

glm::vec3 vec(1, 2, 3);
float* data = glm::value_ptr(vec);

#include <glm/gtc/type_ptr.hpp>.

+12
glm::vec3 construct_vec3(float *value)
{
    return glm::vec3(value[0], value[1], value[2]);
}
+1
source

All Articles