Why does my wired sphere turn into ellipsoid when translating and changing camera angle?

不想你离开。 提交于 2021-02-05 07:56:11

问题


I need to translate my wired sphere along z-axis back and forth while also changing camera angle. Whenever my sphere gets translated, it slowly turns into ellipsoid. I really don't understand why. Here you can see pieces of code where I believe is a mistake. Also, shapes shouldn't be changed when resizing the window, only their size.

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

回答1:


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

That's caused by the Perspective distortion or wide-angle distortion and increases towards the edge of the view. The effect can be decreased by reducing the field of view angle, but the effect will never be canceled completely (except parallel projection).
See als How to fix perspective projection distortion?.

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

At perspective projection the size of the objects is always relative to the size of the viewport rather than the size of your screen.

If you don't want perspective distortion and if you want that the size of the objects has to be relative to the size of the screen (measured in pixel), then you have to use an orthographic projection, relative to the size of the viewport.

For instance:

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


来源:https://stackoverflow.com/questions/61796907/why-does-my-wired-sphere-turn-into-ellipsoid-when-translating-and-changing-camer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!