2016-09-23 84 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 –