【问题标题】:Returning a glm::vec3 x value returns null返回 glm::vec3 x 值返回 null
【发布时间】:2016-09-23 21:13:00
【问题描述】:

所以我有一个任务,我必须将旋转轴作为垂直于平面的法向量返回。这是我的代码:

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;


}

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

帮助!

【问题讨论】:

    标签: opengl glm-math


    【解决方案1】:

    不能直接对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 类似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 2021-12-08
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      相关资源
      最近更新 更多