I want to know the feasibility of converting packed homogenous structs (structs with members of same datatype) into arrays by casting them straight to pointers.
I have a packed vec3 struct with members x,y,z that i need to pass as a vec3 uniform to the vertex shader.
typedef struct vec3_t
{
float x;
float y;
float z;
}__attribute__((packed)) vec3;
int main()
{
/*
boilerplate stuff, window init, shader loading etc.
*/
vec3 v;
v.x=0.0f;
v.y=1.0f;
v.z=0.0f;
GLuint location=glGetUniformLocation(shaderId,uniformName);
glUniform3fv(location,1,(float*)&v);
}
Now I do get a
-Waddress-of-packed-member
warning when i compile it but when i looked it up, it was mostly about pointers to a member of the struct, not the struct itself, so I didn't find much info for the usecase I was looking for. I know this is potentially unsafe because what undefined behaviour isn't. However I'm having doubts about how unsafe this is. Should i continue using this or should i move to
float[3]
as 3d vectors if absolutely necessary?
Edit: Referring to the documentation for glUniform3fv, it can be safely assumed that any type with a GL- prefix is a typedef for that same type, eg: GLfloat for float (with some exceptions like GLsizei)