首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在opengl中获取arcball的正确屏幕映射

在opengl中获取arcball的正确屏幕映射
EN

Stack Overflow用户
提问于 2010-12-15 04:26:04
回答 1查看 739关注 0票数 1

我正在尝试实现一个arcball接口,似乎在模型旋转90度后,模型停止在该特定方向上旋转,我怀疑将屏幕上的点击映射到arcball存在问题,但它可能是错误的数学和/或错误的转换累积,任何帮助都将不胜感激,这里是问题的相关代码,当对向量进行操作时,^运算符表示叉积,*运算符表示内积。

代码语言:javascript
复制
    void mouseButton(int button,int state,int x,int y){
          if(state==GLUT_DOWN){
            GLdouble xx,yy,zz;
            GLdouble modelMatrix[16];
            glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
            GLdouble projMatrix[16];
            glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
            int viewport[4];
            glGetIntegerv(GL_VIEWPORT,viewport);
            gluUnProject(x,height-y-1,0.755
                 ,modelMatrix,projMatrix,viewport,&xx,&yy,&zz);
            arcBall_begin(xx,yy);
          }

        }

        void mouseMotion(int x,int y){
          GLdouble xx,yy,zz;
          GLdouble modelMatrix[16];
          glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
          GLdouble projMatrix[16];
          glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
          int viewport[4];
          glGetIntegerv(GL_VIEWPORT,viewport);
          gluUnProject(x,height-y-1,0.755
                   ,modelMatrix,projMatrix,viewport,&xx,&yy,&zz);

          arcBall_drag(xx,yy);  

    }

void arcBall_begin(GLdouble x,GLdouble y){
  if(sqrt((x*x)+(y*y))>radius)
    begin = vec(x,y,0);
  else
    begin = vec(x,y,sqrt((radius*radius)-(x*x)-(y*y)));
  begin = begin.unit(); 
 glGetDoublev(GL_MODELVIEW_MATRIX,mm);    
}


void arcBall_drag(GLdouble x,GLdouble y){
   if(sqrt((x*x)+(y*y))>radius)
    end = vec(x,y,0);
  else
    end = vec(x,y,sqrt((radius*radius)-(x*x)-(y*y)));
  end = end.unit(); 

  rotationAxis = begin^end;
  rotationAxis = rotationAxis.unit();

  angle =  -2*acos(begin*end);
  angle = angle * (float(180)/float(PI));
}

float arcBall_rotate(){

  if(angle!=0.0){
    glLoadMatrixd(mm);
    glRotatef(angle,rotationAxis.x,rotationAxis.y,rotationAxis.z);
    angle = 0.0;
  }
  return angle;      
}
EN

回答 1

Stack Overflow用户

发布于 2012-03-06 16:31:15

请记住,在OpenGL中,像素坐标是(0,0)在屏幕的左上角,所以当您想要映射时,您必须反转y轴以将坐标映射到球体。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4443856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档