SparkyNZ
SparkyNZ

Reputation: 6676

OpenGL with Shaders: Square Looks Like Rectangle

I found a few articles that provide the following code to correct the appearance of squares:

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

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

I'm using simple shaders to draw a square (which currently looks like a rectangle).

What is the correct way to do this with modern OpenGL? I'm only playing around with MVP matrices and rotation now - thats all very new to me. Does the above correction need to be performed in that code (e.g. see below)

  // 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 ) );

Upvotes: 0

Views: 902

Answers (1)

ratchet freak
ratchet freak

Reputation: 48186

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

Indeed, this won't have any effect on the result of the vertex shader.

The second parameter of glm::perspective is the aspect ratio:

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

Upvotes: 2

Related Questions