AWTGLCanvas & ModelViewMatrix

Started by nbs, March 07, 2011, 11:09:29

Previous topic - Next topic

nbs

Hi!

I started developing an Visualisation Application with LWJGL using AWTGLCanvas. At first I wanted to set up my Projection an coordinate system but get stuck pretty early. Somehow I seem to miss the essence of the Canvas.

First of all I added an MouseWheelListener to the Canvas:

public void mouseWheelMoved(MouseWheelEvent wheelEvent) {
   zTransform += wheelEvent.getUnitsToScroll();
   wheelEvent.consume();
   paintGL();
}


And overwrote the initGL and paintGL methods (I used some static import, all from GL11.*):


@Override
protected void initGL() {
   glClearColor(0f, 0f, 0f, 1f);
   glEnable(GL_DEPTH_TEST);
   glDepthFunc(GL_LEQUAL);
   glShadeModel(GL_SMOOTH);
}
   
@Override
protected void paintGL() {
   try {
      synchronized (GL_LOCK) {
         makeCurrent();
         glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
         glMatrixMode(GL11.GL_MODELVIEW);
         glLoadIdentity();
            
         GL11.glTranslatef(0, 0, zTransform);
         FloatBuffer buffer = ByteBuffer.allocateDirect(16*Double.SIZE/8).asFloatBuffer();
         buffer.rewind();
         GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, buffer);
         System.out.println("ModelView Matrix: ");
         System.out.println(getMatrixAsString(buffer));
            
         buffer.rewind();
         GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, buffer);
         System.out.println("Projection Matrix: ");
         System.out.println(getMatrixAsString(buffer));
         
         drawRectBox(20, 20, 20); // Contains one glBegin..glEnd block drawing some quads
         swapBuffers();
      }
   } catch (LWJGLException e) {
      e.printStackTrace();
   }
      

}
   
private String getMatrixAsString(FloatBuffer matrix) {
   String s = "";
   s += String.format("| %f %f %f %f \n", matrix.get(0), matrix.get(4), matrix.get(8), matrix.get(12));
   s += String.format("| %f %f %f %f \n", matrix.get(1), matrix.get(5), matrix.get(9), matrix.get(13));
   s += String.format("| %f %f %f %f \n", matrix.get(2), matrix.get(6), matrix.get(10), matrix.get(14));
   s += String.format("| %f %f %f %f \n", matrix.get(3), matrix.get(7), matrix.get(11), matrix.get(15));
   
   return s;
}


While zTransform always contains correct int values, both the ModelView and the Projection matrix are echoed all 0. What do I do wrong?

Furthermore, what exactly does swapBuffers? I understand that makeCurrent sets the current Canvas GL Context as global context. Where does swapBuffers come into play?

Thanks in advance,
Nico

Matthias

You allocate your FloatBuffer wrong. The easiest way is to just use BufferUtils from LWJGL:
BufferUtils.createFloatBuffer(16);


Also in Java the size of all data types is fixed in the language spec - a float is always 4 bytes.

nbs

Hi, thanks, works like charm. Actually my way of allocation wasnt overall wrong. If you look what the BufferUtil does:

ByteBuffer.allocateDirect(size << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();

It would be nice if glGet* would check the order of the buffer if it depends on the native order.