OpenGL轴出现不同

OpenGL Axis Appearing Different

本文关键字:OpenGL      更新时间:2023-10-16

当我运行程序时,只是查看xyz轴,偶尔透视图似乎发生了变化。
通常情况下是这样的。但有时它看起来像这样。
在第一张图片中,"前景"中的任何东西都要小得多。而在第二张图中,所有的东西看起来都差不多大。我更喜欢第二张图,但我不知道发生了什么!
下面是我的代码:

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)     // Resize And Initialize The GL Window
{
  if (height == 0)                                      // Prevent A Divide By Zero By
  {
    height = 1;                                     // Making Height Equal One
  } 
  glViewport(0, 0, width, height);                      // Reset The Current Viewport
  aspectRatio = (GLfloat)width / (GLfloat)height;
  screenHeight = (GLfloat)height;
  screenWidth = (GLfloat)width;
}
int InitGL(GLvoid)                                      // All Setup For OpenGL Goes Here
{
  glShadeModel(GL_SMOOTH);                          // Enable Smooth Shading
  glClearColor(1.0f, 1.0f, 1.0f, 0.5f);             // Black Background
  glClearDepth(1.0f);                                   // Depth Buffer Setup
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Really Nice Perspective Calculations
  return TRUE;                                      // Initialization Went OK
}
int DrawGLScene(GLvoid)                                 // Here's Where We Do All The Drawing
{
  //3D setup
  glDepthMask(GL_TRUE);
  glEnable(GL_DEPTH_TEST);                          // Enables Depth Testing
  glDepthFunc(GL_LEQUAL);                               // The Type Of Depth Testing To Do
  glMatrixMode(GL_PROJECTION);                      // Select The Projection Matrix
  glLoadIdentity();                                 // Reset The Projection Matrix
  gluPerspective(45.0f, aspectRatio, 0.1f, 500.0f);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Clear Screen And Depth Buffer
  //3D stuff
  glTranslatef(-8.0f*aspectRatio, 0.0f, -zoomInOut);                // Move and zoom axes
  glRotatef(rotateLeftRight, 0.0f, 1.0f, 0.0f);                 // Rotations
  glRotatef(rotateUpDown, 1.0f, 0.0f, 0.0f);
  //axes
  glLineWidth(1.0);
  glColor3f(0.0, 0.0, 1.0);
  glBegin(GL_LINES);//x axis
  glVertex3f(-20.0, 0.0, 0.0);
  glVertex3f(20.0, 0, 0);
  glEnd();
  glBegin(GL_LINES);//y axis
  glVertex3f(0.0, 20.0, 0.0);
  glVertex3f(0.0, -20.0, 0);
  glEnd();
  glBegin(GL_LINES);//z axis
  glVertex3f(0.0, 0.0, 20.0);
  glVertex3f(0.0, 0.0, -20.0);
  glEnd();
  //2D setup
  glDepthMask(GL_FALSE);
  glDisable(GL_DEPTH_TEST);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity(); 
  gluOrtho2D(0, screenWidth, 0, screenHeight); //left,right,bottom,top
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  //2D overlay goes here
  return TRUE;                                      // Keep Going
}

我做了什么改变了坐标轴的样子?

我看不出你的代码有什么问题。

你的翻译代码有点奇怪glTranslatef(-8.0f*aspectRatio, 0.0f, -zoomInOut);

你在X=-8, Y=0线上,你命名你的Z坐标为zoomInOut。这不是缩放,而是位置。这就像你在电梯里,从外面看,你看着大楼底部广场中央的喷泉,你不放大,你只是看得更近(像这样)。

要做一个真正的变焦,你必须改变视角 (gluPerspective的第一个参数)。

你在X, Y和Z轴上从-20到20画3条线。

在你的屏幕截图中,我们几乎可以从一端看到另一端。没有奇怪的钳夹(你的Z限制从gluPerspective是好的[0.1f, 500.0f])。

事实上,你的大脑有时没有看到它应该看到的视角,这不是一个错误,这只是因为你正在看一个只有两种颜色的平面图像(没有阴影,雾…)。

"3D"图像(来自OpenGl &Co .)只是一个光学错觉之王,你写的程序只是画一个糟糕的错觉。

一个可怜的,但正确的!