【问题标题】:Using gluLookAt() correctly?正确使用 gluLookAt()?
【发布时间】:2019-05-20 12:59:56
【问题描述】:

我正在尝试使用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 改为更改xRotatedyRotated... 的值并显示圆锥。有什么想法吗?

我很确定我不知道在哪里使用 gluLookAt()...

【问题讨论】:

    标签: c opengl glut opengl-compat


    【解决方案1】:

    gluLookAt 改变当前矩阵,类似于glTranslatefglRotatef
    该操作定义了一个变换矩阵,并将当前矩阵乘以新的变换矩阵。

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

    【讨论】:

    • 我没有得到我想要的东西。是否可以使用 gluLookAt 改变眼睛的位置?因为在我的代码中,我只能从顶部看到圆锥体,并且我想设置一个与土壤成 45 度的视角。 @Rabbid76
    • @Jatos 你想达到什么目的? glLookAt的前3个参数是眼睛(相机)位置的坐标。
    • 我尝试将参数更改为函数,但似乎没有任何变化@Rabbid76
    • @Jatos 前 3 个参数是相机位置。参数 4 到 6 是目标。请注意,必须相对于目标设置相机位置。可能gluLookAt(3, 3, 3-4.5, 0, 0, -4.5, 0, 1, 0); 做你想做的事。由于旋转,很难看到变化。出于调试原因跳过glRotations 以调查glLookAt的效果
    • gluLookAt后是否也可以画X、Y、Z轴? @Rabbid76
    猜你喜欢
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多