Why does the same code in different formats give different results?

Started by poo2thegeek, June 11, 2013, 16:37:31

Previous topic - Next topic

poo2thegeek

I have 2 piecies of code. 1 is a test piece:
public static void main(String[] args){
		try {
			Display.setDisplayMode(new DisplayMode(800,560));
			Display.create();
		} catch (LWJGLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		glMatrixMode(GL_PROJECTION);
		glOrtho(0, getWidth(), getHeight(), 0, 1, -1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		
		while(!Display.isCloseRequested()){
			glBegin(GL_QUADS);{
				glVertex2f(100,100);
				glVertex2f(200,100);
				glVertex2f(200,200);
				glVertex2f(100,200);
			}glEnd();
			Display.update();
			Display.sync(60);
		}

	
	}


and one is a section of a full program I plan to make:
public static void main(String[] args) throws Exception{
		Config.load("data/config.txt");

		initDisplay();
		initGame();
		start();
	}
	private static void initDisplay(){
		try{
			setDisplayMode(new DisplayMode(Config.getInt("WIDTH"),Config.getInt("HEIGHT")));
			create();
		}catch(Exception e){e.printStackTrace();System.exit(1);}
	}
	
	private static void initGame(){
		
	}
	private static void start(){
		while(!isCloseRequested()){
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			//set3D();
			//draw3D();
			//set2D();
			draw2D();
			update();
			sync(60);
		}
	}
	
	
	private static void set3D(){
		
	}
	private static void draw3D(){
		
	}

	private static void set2D(){
		glMatrixMode(GL_PROJECTION);
		glOrtho(0, getWidth(), getHeight(), 0, 1, -1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

	}
	
	private static void draw2D(){
		glBegin(GL_QUADS);{
			glVertex2f(100,100);
			glVertex2f(200,100);
			glVertex2f(200,200);
			glVertex2f(100,200);
		}glEnd();
	}


All the code is EXACTLY the same, but in the second example it is more spread out. The only differance (and this is what is stopping it from working) is that set2D() is called every game frame, instead of just once infront of the draw code.
I would like to know why this happens and how I can make the code work in the second format (with a "set2D" method I can call every frame)

Thanks!

poo2thegeek

I noticed that, through my own stupidity, that I left out "glLoadIdentity();" between the lines
glMatrixMode(GL_PROJECTION);

and
glOrtho(0, getWidth(), getHeight(), 0, 1, -1);
   

I would still like to know why I must do this.
Thanks

quew8

glOrtho (and pretty much all the matrix commands bar load and loadIdentity) multiply the current matrix by the one the command specifies. ie the current transform gets concatenated with the specified transform. loadIdentity loads the identity (revelation) and the identity is the equivalent of 1 in matrices so that for any matrix M and the identity matrix I, I * M = M.

Essentially, until you know about matrices, just load the identity and don't question.