带有着色器的 OpenGL:方形看起来像矩形

OpenGL with Shaders: Square Looks Like Rectangle

我发现几篇文章提供了以下代码来更正方块的外观:

  glViewport( 0, 0, nWinWidth, nWinHeight );
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  double aspectRatio = (double) nWinWidth / (double) nWinHeight;

  glOrtho( aspectRatio, -aspectRatio, -1.0f, 1.0f, 1.0f, -1.0f );
  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();

这个旧代码是否只适用于立即模式 OpenGL?

我正在使用简单的着色器绘制一个正方形(目前看起来像一个矩形)。

使用现代 OpenGL 执行此操作的正确方法是什么?我现在只玩 MVP 矩阵和轮换——这对我来说都是全新的。是否需要在该代码中执行上述更正(例如,见下文)

  // PROJECTION - should aspect ration be corrected in here??
  glm::mat4 Projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);

  // VIEW
  glm::mat4 View = glm::mat4(1.0);

  dist = -5.0f;

  View = glm::translate( View, glm::vec3( 0.0f, 0.0f, dist ) );

  // MODEL
  glm::mat4 Model = glm::mat4(1.0);

  // Scale by factor 0.5
  //Model = glm::scale(glm::mat4(1.0f),glm::vec3(0.5f));

  if( GetTickCount() - dwLastTicks > 10 )
  {
    dwLastTicks = GetTickCount();
    rot += 0.01f;

    if( rot >= 360.0f )
      rot = 0;
  }

  Model = glm::rotate( Model, rot, glm::vec3( 0.0f, 1.0f, 1.0f ) ); // where x, y, z is axis of rotation (e.g. 0 1 0)

  glm::mat4 MVP = Projection * View * Model;

  GLuint transformLoc = glGetUniformLocation( g_ShaderProgram, "transform" );
  glUniformMatrix4fv( transformLoc, 1, GL_FALSE, glm::value_ptr( MVP ) );

Is this old code that only applies to immediate mode OpenGL?

确实,这不会对顶点着色器的结果产生任何影响。

glm::perspective的第二个参数是纵横比:

glm::mat4 Projection = glm::perspective(45.0f, aspectRatio, 0.1f, 100.0f);