gpt4 book ai didi

c++ - 为什么我的有线球体在平移和改变摄像机角度时会变成椭球体?

转载 作者:行者123 更新时间:2023-12-02 10:32:21 25 4
gpt4 key购买 nike

我需要沿 z 轴来回平移我的有线球体,同时还要改变相机角度。每当我的球体被平移时,它就会慢慢变成椭球体。我真的不明白为什么。在这里你可以看到我认为是错误的代码片段。此外,在调整窗口大小时不应该改变形状,只改变它们的大小。

void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);

}


void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glLoadIdentity();
gluLookAt(ex, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glTranslatef(0.0,0.0,tra);
glScalef(0.65, 0.65, 0.65);
glColor3f(1.0f, 0.8f, 1.0f);
glutWireSphere(0.65, 10, 15);
glPopMatrix();

glPushMatrix();
glLoadIdentity();
gluLookAt(ex, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glColor3f(0.1f, 0.8f, 1.0f);
glutWireTorus(0.25, 1.0, 15, 15);
glPopMatrix();
glFlush();

glFlush();
}

void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();


gluPerspective(70.0, (GLfloat)w / (GLfloat)h, 1.0, 80.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

最佳答案

[...] while also changing camera angle. Whenever my sphere gets translated, it slowly turns into ellipsoid.



这是由 Perspective distortion 引起的或广角失真,并朝着 View 边缘增加。可以通过减小视场角来降低效果,但效果永远不会完全取消(平行投影除外)。
另见 How to fix perspective projection distortion? .

Also, shapes shouldn't be changed when resizing the window, only their size."



在透视投影中,对象的大小总是相对于视口(viewport)的大小而不是屏幕的大小。

如果您不希望透视失真并且希望对象的大小必须与屏幕的大小相关(以像素为单位),那么您必须使用正交投影,相对于视口(viewport)的大小.

例如:

void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//gluPerspective(70.0, (GLfloat)w / (GLfloat)h, 1.0, 80.0);

float sx = w / 100.0f;
float sy = h / 100.0f;
glOrtho(-sx/2.0f, sx/2.0f, -sy/2.0f, sy/2.0f, 1.0f, 80.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

关于c++ - 为什么我的有线球体在平移和改变摄像机角度时会变成椭球体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61796907/

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