c - 正确使用 gluLookAt()?

标签 c opengl glut opengl-compat

我正在尝试使用 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()...

最佳答案

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

关于c - 正确使用 gluLookAt()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56221535/

相关文章:

c - 这个 BitTorrent 客户端的片段验证过程中的错误在哪里?

跨平台编辑器控件

C 数组 char 与数组 int 比较

c++ - OpenGL 输出窗口立即缩小到 0 x 0 窗口

c++ - 包含 glew c++ 后不能使用 glut 函数

c - 抛出未处理的异常 : write access violation

c++ - OpenGL 库

opengl - 了解 GL_ARB_conservative_depth 扩展

c++ - 将函数作为参数传递给方法

opengl - OpenGL 中的背景图像