2014-10-31 277 views
0

我使用glm库在C++中创建碰撞检测系统。glm :: vec3 precision C++

我有定义为std::vector<glm::vec3> vertices顶点的阵列,并且以计算maxX的,Y的函数,Z定义为

GLfloat AssetInstance::maxX() 
{ 
    GLfloat max = vertices.at(0).x; 

    for(glm::vec3 vertex : vertices) 
     if(vertex.x > max) max = vertex.x; 

    return max; 
} 

但如果我运行下面的代码:

std::vector<glm::vec3> testVector; 
testVector.push_back(glm::vec3(3.0500346, 1.0, 1.0)); 
testVector.push_back(glm::vec3(3.0500344, 2.0, 2.0)); 
testVector.push_back(glm::vec3(3.0500343, 3.0, 3.0)); 

std::cout << maxX(testVector) << std::endl; 

输出是3.05003

我以为glm::vec3doubledouble比那更精确? 有没有更好的方法来做到这一点?我的maxX没有返回足够精确的结果。

回答

3

尝试setprecision。默认情况下,它是你得到的6。

#include <iostream> 
#include <iomanip> 
#include <cmath> 
#include <limits> 
int main() 
{ 
    std::cout << "default precision (6): " << 3.0500346 << '\n'; 
    std::cout << "std::precision(10): " << std::setprecision(10) << 3.0500346 << '\n'; 

    return 0; 
} 

输出(Coliru link):

clang++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out 
default precision (6): 3.05003 
std::precision(10): 3.0500346 

在另一方面,你返回GLFloat这是很好的浮动,而不是双,所以无论什么GLM使用,你转换它浮动。所以最终,调用maxX会给你float精度,而不是double。

P.S:看看文档,它看起来像dvec类型,这让我怀疑vec默认使用double。

1

不,他们是单精度float准备插入一个VBO,顺便说一句,是一个GLFoat

即使不打印所有数字,您仍然可以得到正确的索引。