0

I was drawing a square with coordinates (-.5, -.5), (-.5, .5), (.5, .5), (.5, -.5) and I noticed that it appeared squashed in my 800 x 600 window (which seems entirely logical).

I was trying to fix it so that the square appeared square, not rectangular. My approach was to call glOrtho() with values of left = -ar, right = ar, bottom = -1, top = 1 where ar was my aspect ratio (800/600). What I found was that any call I made to glOrtho() had no effect, including the one I was already making (I could remove it and nothing would change).

My understanding of glOrtho() is that it maps the corners of the OpenGL context to the values supplied, and stretches everything between those points to fit. Is that incorrect, or am I doing something that's preventing my call to glOrtho() from taking effect?

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import java.nio.FloatBuffer;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

public class GLOrthoTestDriver {
  public static void main(String[] args) {
    try {
      Display.setDisplayMode(new DisplayMode(800, 600));
      Display.setTitle("glOrtho Test");
      Display.create();
    } catch (LWJGLException e) {
      e.printStackTrace();

      Display.destroy();
      System.exit(1);
    }

    // Initialization code for OpenGL
    glMatrixMode(GL_PROJECTION);
    glOrtho(-1, 1, -1, 1, -1, 1);
    glLoadIdentity();

    // Scene setup

    int vertexBufferHandle;
    int colorBufferHandle;

    FloatBuffer vertexData = BufferUtils.createFloatBuffer(8);
    vertexData.put(new float[]
        {
          -.5f, -.5f,
          -.5f,  .5f,
           .5f,  .5f,
           .5f, -.5f
        }
    );
    vertexData.flip();

    FloatBuffer colorData = BufferUtils.createFloatBuffer(12);
    colorData.put(new float[]
        {
          1, 1, 1,
          1, 1, 1,
          1, 1, 1,
          1, 1, 1
        }
    );
    colorData.flip();

    vertexBufferHandle = glGenBuffers();
    colorBufferHandle = glGenBuffers();

    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferHandle);
    glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
    glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    while (!Display.isCloseRequested()) {
      glClear(GL_COLOR_BUFFER_BIT);

      glBindBuffer(GL_ARRAY_BUFFER, vertexBufferHandle);
      glVertexPointer(2, GL_FLOAT, 0, 0);

      glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
      glColorPointer(3, GL_FLOAT, 0, 0);

      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);
      glDrawArrays(GL_QUADS, 0, 4);
      glDisableClientState(GL_COLOR_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);

      Display.update();
      Display.sync(60);
    }

    glDeleteBuffers(vertexBufferHandle);
    glDeleteBuffers(colorBufferHandle);

    Display.destroy();
  }
}
2
  • Well your code shows glOrtho being called with (-1,1,-1,1...) Also, if you really are using 800/600 as your aspect ratio, have you ensured that you're using floating point division? (800.0/600.0) If you are using integer division, you'll just round to ar=1.
    – Gretchen
    Oct 24, 2014 at 17:06
  • @Anna yes, that was the original call before I tried to de-squash the square. I found that I could change it in any way, or remove it entirely and nothing would change. Oct 24, 2014 at 17:07

1 Answer 1

4

Ah - your problem is that you're calling glLoadIdentity after calling glOrtho. glLoadIdentity replaces the matrix in the current mode (GL_PROJECTION) with the identity matrix, thus wiping out any previous call to glOrtho.

Try calling glLoadIdentity before glOrtho.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.