2016-09-23 32 views
0

所以我有一个分配,我必须将一个旋转轴作为垂直于该平面的法向量返回。这里是我的代码:返回一个glm :: vec3 x值返回null

glm::vec3 Skeleton::returnAxis() 
{ 
GLdouble firstPoint[3]; 
GLdouble secondPoint[3]; 
GLdouble thirdPoint[3]; 

GLint viewport[4]; 
GLdouble modelview[16]; 
GLdouble projection[16]; 

glGetDoublev(GL_MODELVIEW_MATRIX, modelview); 
glGetDoublev(GL_PROJECTION_MATRIX, projection); 
glGetIntegerv(GL_VIEWPORT, viewport); 

gluUnProject(0,0,0, modelview, projection, viewport, &firstPoint[0], &firstPoint[1], &firstPoint[2]); 
gluUnProject(viewport[2], 0 ,0, modelview, projection, viewport, &secondPoint[0], &secondPoint[1], &secondPoint[2]); 
gluUnProject(0,viewport[3],0, modelview, projection, viewport, &thirdPoint[0], &thirdPoint[1], &thirdPoint[2]); 


glm::vec3 point1 = glm::vec3(secondPoint - firstPoint); 
glm::vec3 point2 = glm::vec3(thirdPoint - firstPoint); 
glm::vec3 axis = glm::normalize(glm::cross(point1, point2)); 
return axis; 
std::cout << axis.x; 


} 

问题是,它没有返回任何数字!只是空白。即使在我做了一个类型转换(在上面的代码中没有显示)之后。

帮助!

回答

2

无法直接对GLdouble[]数组执行矢量运算。

glm::vec3 point1 = glm::vec3(secondPoint - firstPoint); 

这里的减法实际上是在数组的指针(地址)上执行的,并且不返回数学运算。而是计算一个新的地址,然后再将其作为GLdouble[]来初始化vec3。另外,由于创建了一个临时对象,因此在这里创建矢量也不是一个好主意。

你想要做什么可以正确的代码:

glm::vec3 point1(secondPoint[0] - firstPoint[0], 
       secondPoint[1] - firstPoint[1], 
       secondPoint[2] - firstPoint[2]); 

类似的point2

+0

谢谢!!!!工作:D –