OpenGL轴出现不同

时间:2015-09-10 11:32:15

标签: c++ visual-studio opengl perspective axes

当我运行我的程序时,只是查看xyz轴,偶尔透视图似乎会发生变化 它往往看起来像this。 但有时它看起来像this 在第一张图片中,“前景”中的任何内容都要小得多。而在第二张图片中,一切看起来大小相同。我更喜欢第二张图片,但我不知道发生了什么! 这是我的代码:

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
}

我做了什么改变轴的外观?

1 个答案:

答案 0 :(得分:1)

我的代码中没有任何错误。

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

您在X = -8,Y = 0行,并且您将Z坐标命名为zoomInOutWhich is not a zoom but a position。就像你在有外景的电梯里看到建筑物底部广场中间的喷泉一样,你不要放大,你只是看得更近(like that)。

要进行实际缩放,您必须更改视角范围gluPerspective的第一个参数)。

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

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

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

“3D”图像(来自OpenGl& co。)只是视错觉的王者,你写的程序只是画出了一种不好的幻觉。

一个可怜的,但是一个正确的!这是一个好的开始。