2011-07-05 165 views
0

我需要在OpenGL中显示类似于VTK(vtkaxesactor类)中的x-y-z轴,它位于场景的左下角。OpenGL中的vtkaxesactor类型轴

我正在寻找前景中的3(x,y,z)着色轴,它在旋转时给出方向感,但对于缩放仍然不可知。

有没有这样做的GLUT类?如果不是如何实现这一点?

编辑以下datenwolf的评论:

我跟踪X,Y,Z平移和翻译的程度。 (我用pyopengl)

def renderAxis(self): 

     glViewport(0,0,self.width()/8,self.height()/8) 
     self.translate([-1*self.xpan,-1*self.ypan,-1*self.zpan]) 

     glMatrixMode(GL_MODELVIEW) 
     glLineWidth(2) 
     glBegin(GL_LINES) 
     glColor(1, 0, 0) #Xaxis, Red color 
     glVertex3f(0, 0, 0) 
     glVertex3f(0.15, 0, 0) 
     glColor(0, 1, 0) #Yaxis, Green color 
     glVertex3f(0, 0, 0) 
     glVertex3f(0, 0.15, 0) 
     glColor(0, 0, 1) #Zaxis, Blue color 
     glVertex3f(0, 0, 0) 
     glVertex3f(0, 0, 0.15) 
     glEnd() 

     glutInit() 
     glColor(1,0,0) 
     glRasterPos3f(0.16, 0.0, 0.0) 
     glutBitmapCharacter(GLUT_BITMAP_8_BY_13,88)#ascii x 
     glColor(0,1,0) 
     glRasterPos3f(0.0, 0.16, 0.0) 
     glutBitmapCharacter(GLUT_BITMAP_8_BY_13,89)#ascii y 
     glColor(0,0,1) 
     glRasterPos3f(0.0, 0.0, 0.16) 
     glutBitmapCharacter(GLUT_BITMAP_8_BY_13,90)#ascii z 

     self.translate([self.xpan,self.ypan,self.zpan]) 
     glViewport(0,0,self.width(),self.height()) 

    def translate(self, _trans): 
     self.makeCurrent() 
     glMatrixMode(GL_MODELVIEW) 
     glLoadIdentity() 
     glTranslated(_trans[0], _trans[1], _trans[2]) 
     glMultMatrixd(self.modelview_matrix_) 
     self.modelview_matrix_ = glGetDoublev(GL_MODELVIEW_MATRIX) 
     self.translate_vector_[0] = self.modelview_matrix_[3][0] 
     self.translate_vector_[1] = self.modelview_matrix_[3][1] 
     self.translate_vector_[2] = self.modelview_matrix_[3][2] 

这样做的工作,但我怀疑如果这是最好的方式去。

回答

1

GLUT class?你知道GLUT不使用类吗?此外,为什么这么复杂,只需绘制这些轴。在您的显示功能结束时添加以下内容:

void display(void) 
{ 
    /* ... */ 
    glMatrixMode(GL_MODELVIEW); 
    GLfloat modelview[16]; 
    glGetFloatfv(GL_MODELVIEW_MATRIX, modelview); 
    modelview[12] = modelview[13] = modelview[14] = 0.; 
    modelview[15] = 1.; 
    glLoadMatrixf(modelview); 

    glViewport(0, 0, mini_axis_width, mini_axis_height); 

    GLfloat miniaxis[] = { 
      0., 0., 0., 1., 0., 0., 
      1., 0., 0., 1., 0., 0., 

      0., 0., 0., 0., 1., 0., 
      0., 1., 0., 0., 1., 0., 

      0., 0., 0., 0., 0., 1., 
      0., 0., 1., 0., 0., 1., 
    }; 

    glEnableClientState(GL_VERTEX_ARRAY); 
    glEnableClientState(GL_COLOR_ARRAY); 
    glVertexPointer(3, GL_FLOAT, 6*sizeof(GLfloat), miniaxis); 
    glColorPointer(3, GL_FLOAT, 6*sizeof(GLfloat), miniaxis+3); 
    glDrawArrays(GL_LINES, 0, 6); 

    SwapBuffers(); 
}