1

我正在编写一个关于在 3D 空间中旋转的锥体的 OpenGl 程序,并且我有一个 txt,其中包含发生旋转的角度和轴的数据。问题是OpenGL需要总旋转角度,而在文件中我有每个时间步的角度增量。

这是我的代码:

GLdouble matrice1 []  {1.,0,0,0,0,1.,0,0,0,0,1.,0,0,0,0,1.};
GLdouble * matrice = matrice1;


void displayCone(void){

    // clear the drawing buffer.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  //

    // set matrix mode
    glMatrixMode(GL_MODELVIEW);
    // clear model view matrix
    glLoadIdentity();
    

    //gluLookAt(3.0, 3.0, 3.0-4.5, 0.0, 0.0,-4.5,0,0,1);
    gluLookAt(3.0, 3.0, 3.0, 0.0, 0.0,0.0,0,0,1);




    glBegin(GL_LINES);

    glColor3f (1.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(2.0, 0.0, 0.0);

    glColor3f (0.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 2.0, 0.0);

    glColor3f (0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 2.0);
    glEnd();

 

    
    //glLoadMatrixd(matrice);

    glColor3f(0.8, 0.2, 0.1);

glMultMatrixd(matrice);  // <------ ** me trying to implement the previous rotation before //rotating of the increment angle xRotated 
    glRotated(xRotated,a1,a2,a3);    // <----- **here the rotation**
      
      glGetDoublev(GL_MODELVIEW, matrice); // <----- ** me trying to save the total rotation //matrix ** 

    glTranslatef(0.0, 0.0, height);
    glutSolidCone(base,height,slices,stacks);
glPushMatrix();
  glTranslatef(0.0, 0.0, height-0.1);
    glutSolidCylinder(0.07,height_c,slices,stacks);
    glPopMatrix();
glPushMatrix();
    glScalef(1.0,1.0,-1.0);
//glTranslatef(0.0, 0.0, 2*height);

//glPopMatrix();



//glPushMatrix();
  glutSolidCone(base,height,slices,stacks);

    // Flush buffers to screen
glPopMatrix();

    glFlush();


} 

我在这里尝试做的glGetDoublevglMultMatrixd保存先前的旋转角度,但这不起作用。有谁知道如何保存先例旋转矩阵以获得总旋转而不是增量角度的 RotationAngle ?

4

1 回答 1

1

一方面,您拥有的代码使用的是旧的和破坏的固定功能,即已经过时超过 15 年的遗留 API。

更重要的是,您可以使用适当的 3D 图形数学库(GLM、Eigen、linmath.h)来管理旋转矩阵并将其应用于 OpenGL 转换矩阵堆栈,glMultMatrix而不是使用多次调用glRotate. 或者自己管理整个转换矩阵,然后用glLoadMatrix.

当您进行自己的矩阵管理时,放弃固定函数管道并使用着色器只是一小步,您可以在其中将矩阵作为所谓的统一值传递。

于 2020-10-12T17:04:07.837 回答