如何在OpenGL中正确绘制3D轴?

时间:2019-05-21 08:19:52

标签: opengl opengl-compat

我正在尝试绘制3d轴。我还有一个圆锥形旋转,它围绕其顶点旋转,我希望轴从那里开始。

在这里,我具有绘制圆锥的功能,并且在gluLookAt之后编写了绘制轴的功能:

GLfloat xRotated, yRotated, zRotated;
// Cone
GLdouble base=0.5;
GLdouble height=1.3;
GLint slices =20;
GLint stacks =20;
std::vector<std::array<GLfloat, 3>> data;


    void displayCone(void)
    {
        // set matrix mode
        glMatrixMode(GL_MODELVIEW);
        // clear model view matrix
        glLoadIdentity();
        // multiply view matrix to current matrix
        gluLookAt(0,2.,0.,0.,0.,-4.5,0,1,0); // <----------------------- add

        // ******
        glPushMatrix();

        glLoadIdentity();

        glTranslatef(0.0, 0.0, -4.5);

        glBegin(GL_LINES);

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

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

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

        glPopMatrix();

// ******
        // clear the drawing buffer.
        glClear(GL_COLOR_BUFFER_BIT);

        // traslate the draw by z = -4.0
        // Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
        glTranslatef(0.0,0.0,-4.5);
        // Red color used to draw.
        glColor3f(0.8, 0.2, 0.1);
        // changing in transformation matrix.
        // rotation about X axis
        glRotatef(xRotated,1.0,0.0,0.0);
        // rotation about Y axis
        glRotatef(yRotated,0.0,1.0,0.0);
        // rotation about Z axis
        glRotatef(zRotated,0.0,0.0,1.0);

        // scaling transfomation
        glScalef(1.0,1.0,1.0);
        // built-in (glut library) function , draw you a Cone.

        // move the peak of the cone to the origin
        glTranslatef(0.0, 0.0, -height);

        glutSolidCone(base,height,slices,stacks);
        // Flush buffers to screen
        // gluLookAt(3,3,3,0,0,-4.5,0,1,0); <----------------------- delete



        glFlush();
        // sawp buffers called because we are using double buffering
        // glutSwapBuffers();
    }

我本想以这种方式绘制3d轴,但我犯了一些错误。 我想念什么?

1 个答案:

答案 0 :(得分:1)

谎言在被glClear吸引后立即被清除。在glClear(GL_COLOR_BUFFER_BIT);的开头执行displayCone

如果要在视图空间中绘制线条,则必须设置视图矩阵。绘制线条之前,请先删除glLoadIdentity

例如

void displayCone(void)
{
    // clear the drawing buffer.
    glClear(GL_COLOR_BUFFER_BIT);  // <---- add

    // set matrix mode
    glMatrixMode(GL_MODELVIEW);
    // clear model view matrix
    glLoadIdentity();
    // multiply view matrix to current matrix
    gluLookAt(3.0, 3.0, 3.0-4.5, 0.0, 0.0,-4.5,0,1,0);

    // ******
    glPushMatrix();

    // glLoadIdentity(); <---- delete

    glTranslatef(0.0, 0.0, -4.5);

    glBegin(GL_LINES);

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

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

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

    glPopMatrix();

    // clear the drawing buffer.
    // glClear(GL_COLOR_BUFFER_BIT);  // <---- delete

    // traslate the draw by z = -4.0
    // Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
    glTranslatef(0.0,0.0,-4.5);
    // Red color used to draw.
    glColor3f(0.8, 0.2, 0.1);
    // changing in transformation matrix.
    // rotation about X axis
    glRotatef(xRotated,1.0,0.0,0.0);
    // rotation about Y axis
    glRotatef(yRotated,0.0,1.0,0.0);
    // rotation about Z axis
    glRotatef(zRotated,0.0,0.0,1.0);

    // scaling transfomation
    glScalef(1.0,1.0,1.0);
    // built-in (glut library) function , draw you a Cone.

    // move the peak of the cone to the origin
    glTranslatef(0.0, 0.0, -height);

    glutSolidCone(base,height,slices,stacks);
    // Flush buffers to screen
    // gluLookAt(3,3,3,0,0,-4.5,0,1,0); <----------------------- delete

    glFlush();
    // sawp buffers called because we are using double buffering
    // glutSwapBuffers();
}