简体   繁体   中英

glm::dot() doesn't return float/double value?

I am trying to use glm::dot to calculate the dot product of two glm::dvec3

However, my compiler returns this error when I try to do this

#include <glm/glm.hpp>

int main(int argc, char* argv[]){
    glm::dvec3 a = glm::dvec3(1f, 2f, 3f);
    glm::dvec3 b = glm::dvec3(4f, 5f, 6f);
    float weight = glm::dot(a, b)
}

ERROR: Type 'double' and 'glm::dvec3' are not compatible in the last line when I assign weight to the result of glm::dot

I am using (clion, cygwin, gcc) and glm 0.9.8 and c++ 11

EDIT: Add more detailed error location and code context

Maybe there is a better solution, but this is working for me

glm::vec3 a;
glm::vec3 b;
glm::dot<3, float, glm::qualifier::highp>(a, b);

Since GLM has two methods dot

template<typename T>
GLM_FUNC_QUALIFIER T dot(T x, T y)
{
    GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'dot' accepts only floating-point inputs");
    return x * y;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER T dot(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
    GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'dot' accepts only floating-point inputs");
    return detail::compute_dot<vec<L, T, Q>, T, detail::is_aligned<Q>::value>::call(x, y);
}

The compiler is assuming that T is vec3 and for that reason is using the first method

Take a look to this post:

Templates C++

In this specific case where the generic type T is used as a parameter for GetMax the compiler can find out automatically which data type has to instantiate without having to explicitly specify it within angle brackets (like we have done before specifying and ). So we could have written instead: int i,j; GetMax (i,j);

Hope this helps

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