简体   繁体   中英

How to fill a glm::vec3 in C++

I've got three vectors which represent each cordinate in a x, y, z system. What I want to do is to create a glm::vec3 in order to join each component of every point like this for example:

x[0] = 1, y[0] = 0, z[0] = 2 -> new_vector[0] = (1, 0, 2)

My code so far is this one:

glm::vec3 transform(std::vector<float> const& test)
{
    std::vector<float> mul_x (test.size());
    std::vector<float> mul_y (test.size());
    std::vector<float> mul_z (test.size());
    glm::vec3 new_vector;

    for (uint32_t i = 0; i < test.size(); ++i)
    {
        mul_x[i] = test[i]*lib::x[i];
        mul_y[i] = test[i]*lib::y[i];   
        mul_z[i] = test[i]*lib::z[i];           
    }

    for (uint32_t i = 0; i < test.size(); ++i)
    {
        new_vector[i].x = mul_x[i]; 
        new_vector[i].y = mul_y[i]; 
        new_vector[i].z = mul_z[i];
    }

return new_vector;

}

Where lib::x , lib::y and lib::z are three pre-defined std::vector<float> .

But compiler returns to me an error message. Particularly this one:

error: request for member 'x' in 'new_vector.glm::tvec3<T, P>::operator[]<float, (glm::precision)0u>(i)', which is of non-class type 'float' new_vector[i].x = mul_x[i];

Any advice?

Your problem is that new_vector must also be an std::vector , otherwise you are trying to pack test.size() vectors into a single one. The reason you might be confused is that glm::vec3 's operator [] actually refers to the x, y, z components, not to individual vectors. Replace your glm::vec3 with std::vector<glm:::vec3> and you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM