gpt4 book ai didi

c++ - 在 glPushMatrix 内部和外部实现 glulookat?

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:02 25 4
gpt4 key购买 nike

我知道在 opengl 中使用 gluLookAt(...) 、glPushMatrix 和其他基本转换工具的目的是什么。我被困在这些代码中。在为 opengl 设置适当的要求后,当我在 glPushMatrix() 中实现 glulookat(.....) 时。代码工作正常,在按键上,立方体以适当的旋转呈现,但是当我在 glPushMatrix() 和 glPopMatrix() 之外实现 gluLookAt(....) 时,事情变得疯狂了。立方体显示异常行为,最后得到从屏幕上消失。

gluLookAt(0.0f, 0.0f, 400.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);          
glPushMatrix();

//gluLookAt(0.0f, 0.0f, 400.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);


glBegin(GL_QUADS);

// Front Face
// White
glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)255);
glVertex3f(50.0f,50.0f,50.0f);

// Yellow
glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)0);
glVertex3f(50.0f,-50.0f,50.0f);

// Red
glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)0);
glVertex3f(-50.0f,-50.0f,50.0f);

// Magenta
glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)255);
glVertex3f(-50.0f,50.0f,50.0f);


// Back Face
// Cyan
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);

// Green
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);

// Black
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);

// Blue
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);

// Top Face
// Cyan
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);

// White
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,50.0f);

// Magenta
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,50.0f);

// Blue
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);

// Bottom Face
// Green
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);

// Yellow
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,50.0f);

// Red
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,50.0f);

// Black
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);

// Left face
// White
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,50.0f);

// Cyan
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f,50.0f,-50.0f);

// Green
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,-50.0f);

// Yellow
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(50.0f,-50.0f,50.0f);

// Right face
// Magenta
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,50.0f);

// Blue
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f,50.0f,-50.0f);

// Black
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,-50.0f);

// Red
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-50.0f,-50.0f,50.0f);
glEnd();

glPopMatrix();

最佳答案

gluLookAt 将事件矩阵堆栈的当前最顶层元素与观察矩阵相乘,并以此替换最顶层元素。

Push 和 Pop 是标准的堆栈操作。 Push 创建最顶层元素的拷贝并将其压入栈顶,pop 将其移除。

因此,您在 push-pop block 中所做的任何更改都会通过 pop 操作恢复。但是在堆栈框架(push-pop)之外,更改将累积。如果你在 push-pop 之外的 gluLookAt 之前放置一个 glLoadIdentity,它也会工作,但那是因为你将矩阵重置为一个正常的值而不是在之前渲染的基础上工作。

关于c++ - 在 glPushMatrix 内部和外部实现 glulookat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21730632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com