Modelview Matrix outside GLEventListener Always 0 ?

Hi there,

I have a Scene and want to draw its elements to the display. What I am doing is, I call the scene draw methon in the
GLEventlisteners display method and give it the gl object as parameter like this:


public void display(GLAutoDrawable drawable) {
             this.gl = drawable.getGL();
	     GLController.getInstance().getScene().draw(gl);
	     gl.glFlush();
	}

this is my drawing setup in the scene draw method:


public void draw(GL gl){
 		
 	gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
        gl.glLoadIdentity();

        gl.glTranslatef(0.0f, 0.0f, -25.0f);
        
        gl.glColor3f(1.0f,0.0f,0.0f);
        
        gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modMatrix, 0);
        gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projMatrix, 0);
        gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);

...

the the drawing method, I just want to draw some cubes on different positions. SO I change the values for gl.glTranslatef in two loops
and draw the cube object in the next step:


gl.glBegin(GL.GL_TRIANGLES);
 	
 		for(int col = 0; col < (int)(floorDimension[0]+2); col++){
 			for(int row = 0; row < (int)(floorDimension[1]+2) ; row++){
 			
 				gl.glPushMatrix();
 				
 				
 				
 				// start drawing in the one row and one column under the gl screen (like a safety buffer zone)
 				// then add one floor tile after another till top corner is reached one row above gl screen
 				gl.glTranslatef(((0.0f - floorTileSize[0])+(float)(col*floorTileSize[0])),	// x
 								((0.0f - floorTileSize[1])+(float)(row*floorTileSize[1])),	// y
 								0.0f);														// z
 				gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modMatrix, 0);
 				System.out.println("mod x = " + modMatrix[12] + "mod y = " + modMatrix[13] );
 			
 				floor.draw(gl);
 				
 				gl.glPopMatrix();
 			}
 				
 		}
 		
 		gl.glEnd();

Since I was wandering, why the cubes are always drawn in the center of the window, I loaded the modMatrix and printed the x,y coordinates, which are always 0.

Why is it not possible to load them outside the event listener?

When I have to draw everything in the event listener, the code is getting messy.

Or have I done a mistake?

I haven’t noticed anything wrong, but you haven’t posted all of your setup. You should make sure you’re setting up the projection matrix correctly (and on the GL_PROJECTION mode) and then make sure you’ve switched back to GL_MODELVIEW when you do the rest of your drawing.

Hi again,
this is my init and reshape method, respectively:

public void init(GLAutoDrawable drawable) {
		gl = drawable.getGL();
		
		 gl.glEnable(GL.GL_DEPTH_TEST);      // Enable Depth Testing
		 // Set Perspective Calculations To Most Accurate
	        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);  
		 
		 gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
	    gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
		
	    gl.glEnable(GL.GL_LIGHTING);
	    gl.glEnable(GL.GL_LIGHT0);
	    gl.glEnable(GL.GL_COLOR_MATERIAL);

	 // Create light components
	 float ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	 float diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
	 float specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
	 float position[] = { -1.5f, 1.0f, -4.0f, 1.0f };

	 // Assign created components to GL_LIGHT0
	 gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, ambientLight, 0);
	 gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, diffuseLight, 0);
	 gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, specularLight, 0);
	 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position, 0);
	 		
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		GL gl = drawable.getGL();
        GLU glu = new GLU();

        if (height <= 0) { // avoid a divide by zero error!
        
            height = 1;
        }
        final float h = (float) width / (float) height;
       
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        
        gl.glLoadIdentity();
        
        glu.gluPerspective(45.0f, h, 1.0, 50.0);
        
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        
        
        gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
        GLController.getInstance().setViewport(viewport);
		
	}

I also added a System.out to show some values in the scene.draw() function to show, that the TileSize has values:

System.out.println("pos x = "+((0.0f - floorTileSize[0])+(float)(col*floorTileSize[0]))+"pos y = "+((0.0f - floorTileSize[1])+(float)(row*floorTileSize[1])));
 				
 				// start drawing in the one row and one column under the gl screen (like a safety buffer zone)
 				// then add one floor tile after another till top corner is reached one row above gl screen
 				gl.glTranslatef(((0.0f - floorTileSize[0])+(float)(col*floorTileSize[0])),	// x
 								((0.0f - floorTileSize[1])+(float)(row*floorTileSize[1])),	// y
 								0.0f);														// z

This is some of the output:

pos x = 0.0pos y = 0.0
mod x = 0.0mod y = 0.0
pos x = 0.0pos y = 54.018032
mod x = 0.0mod y = 0.0
pos x = 0.0pos y = 108.03606
mod x = 0.0mod y = 0.0
pos x = 0.0pos y = 162.0541

mod x = 0.0mod y = 0.0 -> is still the not changing modelview matrix

And to make sure, that the distances are maybe not just too big I testet the gl.Translatef with the following values:

	
// just a test check 
//gl.glTranslatef((0.0f + (float)(col*2.0f)),0.0f,0.0f);


So I commented it out, after seeing that the result is the same and every cube is drawn to the center of the window.

Could that be a windows vista 64 problem? But what the hell, Its just running in eclipse, and I put the gl directly during the display function of the gleventlistener in the scene draw method and in the end the drawing is finished be gl.glflush() in the display method it self.

Actually, I had the same problem with another programm, that the modmatrix projmatrix and the viewport is only filled correctly in the GLEvent Listener methods, thus I had to change the hole drawing structur.

Is it me or is it JOGL?

With kind regards,

Knut

Of course it shall work with a valid and current context,
either in init, display or the event-listeners.

Sure the matrix mode is MODELVIEW when you do your translation ?
You might want to add a ‘gl.glMatrixMode(GL.GL_MODELVIEW);’ before it …

If this is not the bug and turns out to be a JOGL bug,
please refer to http://blog.jausoft.com/2009/06/30/jogl-bugreports/
and send such a reproducible one … thank you.

Perhaps you could try glGetFloatv instead of glGetDoublev, I don’t know it’s a long shot.

Also, are projMatrix and modMatrix length 16? and have you called glGetError() to check for errors? Another option is to wrap the GLAutoDrawable’s GL with a DebugGL, which would help if there were errors.