Как правильно нарисовать 3D-ось в OpenGL? - PullRequest
1 голос
/ 21 мая 2019

Я пытаюсь нарисовать ось 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 Ответ

1 голос
/ 21 мая 2019

Ложь очищается сразу после того, как она была нарисована glClear. Do 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();
}
...