Newbie question: clipping volume/viewport

Started by Brush, October 25, 2004, 17:34:36

Previous topic - Next topic

Brush

[note: english is not my first language, so please excuse me if I'm not using the correct terms for certain concepts]

I'm trying to learn openGL using LWJGL and an old book I managed to pick up cheap. Now if I understood correctly the clipping volume determins your bounderies of your coordinate system( min-X, max-X, etc.) and the viewport maps those coordinates to screencoordinates.

In the book they touch on the subject of simple animation using openGL.
Now when I use the following code, it won't draw anything on the screen, yet if i remove the call to glOrtho, I can see the animation just fine.
These are the relevant methods:
private int xpos=0;
	private int ypos=0;
	private int xStep=1;
	private int yStep=1;
	private int size=100;
	public void mainLoop()
	{
		if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
			done=true;
		if(Display.isCloseRequested())
			done=true;
			
		if(xpos>640-size||xpos<0)
			xStep=-xStep;
		if(ypos>480-size||ypos<0)
			yStep=-yStep;
		xpos+=xStep;
		ypos+=yStep;
	}

	private int viewPortX=640;
	private int viewPortY=480;
	public void initGL()
	{
	    GL11.glClearColor(0.0f, 0.0f, 1.0f, 1.0f); 
	    
	    GL11.glViewport(0,0,640,480);
	     
	    //reset coordinatesystem
	    GL11.glLoadIdentity();
	    //			min-X, max-X,  min-Y,max-Y, Z-min,Z-max 
	    //set the clippnig volume
	    
	    GL11.glOrtho(0,640,480,0,-1.0,1.0);
	    
	}
	
	public boolean render()
	{
	    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
	    //				R   G   B
	    GL11.glColor3f(1.0f,0.0f,0.0f);
	    //			upperleft			lowerright
	    GL11.glRectf(xpos,ypos, xpos-size,ypos-size);
	    GL11.glFlush();
	    return true;
	}


I tried with different viewport sizes, but it doesn't work even if I pick the same size for the clipping volume and the viewport.
I realise LWJGL is set up for 2D drawing by default, but I just want to know why this doesn't work, when it does (or is supposed to) work in the book. It's probably some fundamental viewport/clipping concept that I'm misunderstanding.

Orangy Tang

A random guesstimate - I don't see you chosing your active matrix anywhere before you set up your ortho projection. If I remember right, it defaults to the modelview matrix, so LWJGL defaults your projection matrix for 2d, *then* you set the modelview up for 2d. I have no idea what the combined result is likely to do but it sure ain't gonna be what you want. ;)

Try adding a GL11.glMatrixMode(GL11.gl_PROJECTION) in front of your ortho call.

Brush

Thank you for your speedy reply, but that didn't solve it.
Translating this program from the redbook to LWJGL, has the same problem: it simply doesn't draw anything to the screen.
#include <whateverYouNeed.h>

main() {

   OpenAWindowPlease();

   glClearColor(0.0, 0.0, 0.0, 0.0);
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(1.0, 1.0, 1.0);
   glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 
   glBegin(GL_POLYGON);
      glVertex2f(-0.5, -0.5);
      glVertex2f(-0.5, 0.5);
      glVertex2f(0.5, 0.5);
      glVertex2f(0.5, -0.5);
   glEnd();
   glFlush();

   KeepTheWindowOnTheScreenForAWhile();
}


Is LWJGL doing something funky behind my (our) back(s)?

[edit]
It seems that putting the GL11.glMatrixMode(GL11.GL_PROJECTION); before the loadIdentity() call, does make it work

cfmdobbie

Yeah, the problem as you've doubtless now discovered is that you're trying to manipulate two different matrix stacks here.

The glOrtho call should be called on the GL_PROJECTION matrix (usually on an identity matrix), while your drawing commands should be called on the GL_MODELVIEW matrix.

// Setup code - only call this once!
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( ... );
glMatrixMode(GL_MODELVIEW); // Remember to change matrix back!

// Drawing code - call once per frame
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glTranslatef( ... );
glBegin(GL_QUADS);
// etc


Note that the glLoadIdentity() call in the drawing code will only affect the current matrix - GL_MODELVIEW in this instance.  The GL_PROJECTION matrix can be initialised once and then left, and most applications will do exactly this.


A simple rule - when it comes to system initialisation, LWJGL does a few funky things to put the system in as usuable a state as possible.  It sets up a simple 1:1 world-to-pixel coordinates projection, sets a few state flags etc.  However, when the program is in full swing, LWJGL stays right out of the way and leaves you to it.
ellomynameis Charlie Dobbie.