Правильно ли использовать gluLookAt ()? - PullRequest
1 голос
/ 20 мая 2019

Я пытаюсь установить угол обзора с помощью gluLookAt()

Здесь у меня есть код, где я пытался установить камеру без результатов

Здесь функция displaycone():

void displayCone(void)
{

    glMatrixMode(GL_MODELVIEW);
    // clear the drawing buffer.
    glClear(GL_COLOR_BUFFER_BIT);
    // clear the identity matrix.
    glLoadIdentity();
    // 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);

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

С моим основным:

int main (int argc, char **argv)
{
    glutInit(&argc, argv);
    //double buffering used to avoid flickering problem in animation
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    // window size
    glutInitWindowSize(400,350);

    // create the window
    glutCreateWindow("Cone Rotating Animation");
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

    glClearColor(0.0,0.0,0.0,0.0);
    //Assign  the function used in events

    glutDisplayFunc(displayCone);
    glutReshapeFunc(reshapeCone);
    glutIdleFunc(idleCone);
    //Let start glut loop
    glutMainLoop();
    return 0;
}

Функция idlecone вместо этого изменяет значения xRotated, yRotated ... и отображает конус.Любые идеи?

Я почти уверен, что не понял подходящий момент, где использовать gluLookAt() ...

1 Ответ

2 голосов
/ 20 мая 2019

gluLookAt изменяет текущую матрицу, аналогично glTranslatef или glRotatef.
Операция определяет матрицу преобразования и умножает текущую матрицу на новую матрицу преобразования.

gluLookAt должен быть вызван до glutSolidCone, например:

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

    // 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();
}
...