Arguments of gluOrtho2D

Hello!

I have a question that is more OpenGL specific, but I hope someone can give me the answer!

Are the arguments “right” und “top” of gluOrtho2D exclusive? When I use gluOrtho2d like

gluOrtho2D(0, width, height, 0)

to have the origin in the top left corner, I do not see vertices with a y coordinate of 0. When I use it like

gluOrtho2D(0, width, height - 1, -1)

all is ok. Is my assumption right?

Greetings
olli73

This doesn’t appear to be explicitly spelled out in the man pages but I am pretty sure they are exclusive. Think about the case of gluOrtho2D(0, width, 0, height); the leftmost pixel is 0, and the rightmost is width - 1, the bottom is 0, and the topmost is height - 1. I think you’ve figured out the right transform to flip things vertically.

Hi!

Thanks for your answer! I just wanted a confirmation by someone else, because it isnt´t explicit mentioned (has you wrote) in the documentation.

Greetings
olli73

Hi again!

I did some more testing on this issue. There are still some problem when switching the arguments for top and bottom, that I don´t understand.

I wrote this code:


public class CoordinateTest implements GLEventListener
{

	public void init(GLAutoDrawable drawable)
	{
		GL gl = drawable.getGL();
		
		gl.glFrontFace(GL.GL_CW);
		
		gl.glEnable(GL.GL_CULL_FACE);
		gl.glCullFace(GL.GL_BACK);
		
		gl.glEnable(GL.GL_BLEND);
		gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
	}

	public void display(GLAutoDrawable drawable)
	{
		GL gl = drawable.getGL();
		
		gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		
		gl.glColor3f(0.0f, 0.0f, 0.0f);
		gl.glBegin(GL.GL_POINTS);
		gl.glVertex2i(0, 0);
		gl.glEnd();
		
		gl.glBegin(GL.GL_QUADS);
		gl.glVertex2i(10,  0);
		gl.glVertex2i(20,  0);
		gl.glVertex2i(20, 10);
		gl.glVertex2i(10, 10);
		
		gl.glVertex2i( 0, 10);
		gl.glVertex2i(10, 10);
		gl.glVertex2i(10, 20);
		gl.glVertex2i( 0, 20);
		
		gl.glEnd();

	}

	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
	{
		GL gl = drawable.getGL();
		GLU glu = new GLU();
		
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluOrtho2D(0, width, height - 1, -1);
	}

	public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
	{
	}
	
	public static void main(String[] args)
	{
		GLCanvas canvas = new GLCanvas();
		canvas.addGLEventListener(new CoordinateTest());
		
		Frame frame = new Frame("Coordinate Test");
		frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } });
		frame.setSize(100, 100);
		frame.add(canvas);
		frame.setVisible(true);
	}
	
}

The result is strange. It seems that the y coordinate for a single vertex does not mean the same as for vertices of a quad (see attached file).

Can someone help?

olli73

Your test case seems to be working as I would expect it. There is a very small black dot in the upper left corner of the window, which corresponds to the point (0, 0) you’ve drawn.

LINES and POINTS have different renderpaths than TRIANGLES and QUADS

you have to offset the lines and points by (0.375, 0.375, 0.000) IIRC

Thanks for that hint, Riven! With your keywords, I googled the Appendix H of the OpenGL 1.1 Redbook (http://www.rush3d.com/reference/opengl-redbook-1.1/appendixh.html). With the solution descriped there, all my points and quads have correct positions relative to each other.

But now my assumption about the arguments of gluOrtho2D() as no longer correct. gluOrtho2D(0, width, height, 0) renders the primitives in the top left corner. gluOrtho2D(0, width, height - 1, -1) moves all primitives 1 row of pixels down.