datenwolf / linmath.h

a lean linear math library, aimed at graphics programming. Supports vec3, vec4, mat4x4 and quaternions
Do What The F*ck You Want To Public License
922 stars 134 forks source link

Issue with constructing vectors #35

Closed HugoPeters1024 closed 5 years ago

HugoPeters1024 commented 5 years ago

Hiya, Loving this module so far, but I have an issue constructing a 3 dimension vector. I have a large array of floats that represent 3d vertices. I wish to loop over them on a per vertex basis so i constructed the following piece of code.

for(int i=0; i<n; i++) {
   vec3 v = VEC3(array[i+0], array[i+1], array[i+2]);
}

But this gives me the following compile error (using g++)

linmath.h:148:30: error: array must be initialized with a brace-enclosed initializer
  #define VEC3(x,y,z)   (vec3){x,y,z}
                              ^~~~~~~
vertexbuf.h:76:17: note: in expansion of macro ‘VEC3’
       vec3 p1 = VEC3(grid[i+0], grid[i+1], grid[i+2]);

Am I doing something wrong here?

datenwolf commented 5 years ago

Well, to initialize a struct you can omit the (vec3) and just write

vec3 v = {array[i+0], array[i+1], array[i+2]};

No need for that VEC3 macro.

HugoPeters1024 commented 5 years ago

Thanks! Makes sense. Still wondering what the macro is for then.

datenwolf commented 5 years ago

The macro is for returning vec-s from functions.

vec3 foobar(…)
{
    /* ... */
    return VEC3(a,b,c);
}