loading identity matrix via glLoadMatrix

Started by Morin, May 08, 2010, 17:00:07

Previous topic - Next topic

Morin

Hi everyone,

I am currently trying to make glLoadMatrix work, but so far without success. What I am doing in this example is ONLY a test to understand glLoadMatrix, regardless of whether it is the best tool for the job. The example is a modified version of the example available from the Eclipse homepage.

What I am doing is to fill a buffer with values for a 4x4 identity matrix, and load it via glLoadMatrix() (float version). So far, it seems as loading an identity matrix this way has a different effect than calling glLoadIdentity(), which seems wrong to me.

Could you have a look at the code and tell me if you have any idea what is going wrong? Thanks in advance.

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

/*
 * SWT OpenGL snippet: use LWJGL to draw to an SWT GLCanvas
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.util.glu.GLU;

/**
 * 
 */
public class MatrixTest {

	/**
	 * @param args ...
	 */
	public static void main(String[] args) {
		
		/** SWT setup stuff **/
		final Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		Composite comp = new Composite(shell, SWT.NONE);
		comp.setLayout(new FillLayout());
		GLData data = new GLData();
		data.doubleBuffer = true;
		final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

		/** create buffer **/
		final ByteBuffer matrixByteBuffer = ByteBuffer.allocateDirect(16 * 4);
		matrixByteBuffer.clear();
		final FloatBuffer matrixBuffer = matrixByteBuffer.asFloatBuffer();
		matrixBuffer.clear();

		/** fill buffer with the identity matrix **/
		matrixBuffer.put(1.0f);
		matrixBuffer.put(0.0f);
		matrixBuffer.put(0.0f);
		matrixBuffer.put(0.0f);

		matrixBuffer.put(0.0f);
		matrixBuffer.put(1.0f);
		matrixBuffer.put(0.0f);
		matrixBuffer.put(0.0f);

		matrixBuffer.put(0.0f);
		matrixBuffer.put(0.0f);
		matrixBuffer.put(1.0f);
		matrixBuffer.put(0.0f);

		matrixBuffer.put(0.0f);
		matrixBuffer.put(0.0f);
		matrixBuffer.put(0.0f);
		matrixBuffer.put(1.0f);

		/** add a paint listener **/
		canvas.addListener(SWT.Paint, new Listener() {
			public void handleEvent(Event event) {
				
				/** make GL context the current context **/
				canvas.setCurrent();
				try {
					GLContext.useContext(canvas);
				} catch (LWJGLException e) {
					e.printStackTrace();
				}

				/** clear to background color **/
				GL11.glClearColor(.3f, .5f, .8f, 1.0f);
				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
				
				/** setup transformations **/
				Rectangle bounds = canvas.getBounds();
				GL11.glViewport(0, 0, bounds.width, bounds.height);
				
				/*
				 * !!! This is where it happens !!!
				 */
				matrixBuffer.rewind();
				for (int i=0; i<16; i++) {
					System.out.println("dump: " + matrixBuffer.get());
				}
				// ---
				GL11.glMatrixMode(GL11.GL_MODELVIEW);
				matrixBuffer.rewind();
				GL11.glLoadMatrix(matrixBuffer);
//				GL11.glLoadIdentity();
				
				GL11.glMatrixMode(GL11.GL_PROJECTION);
				GL11.glLoadIdentity();

				/** draw a rectangle **/
				GL11.glBegin(GL11.GL_QUADS);
				GL11.glColor3f(1.0f, 0.0f, 0.0f);
				GL11.glVertex2f(-0.5f, -0.5f);
				GL11.glColor3f(1.0f, 1.0f, 0.0f);
				GL11.glVertex2f(-0.5f, 0.5f);
				GL11.glColor3f(0.0f, 1.0f, 0.0f);
				GL11.glVertex2f(0.5f, 0.5f);
				GL11.glColor3f(0.0f, 0.0f, 1.0f);
				GL11.glVertex2f(0.5f, -0.5f);
				GL11.glEnd();
				
				/** swap double buffers **/
				canvas.swapBuffers();

			}
		});

		/** open SWT shell **/
		shell.setText("SWT/LWJGL Example");
		shell.setSize(640, 480);
		shell.open();

		/** main event loop **/
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
		/** shutdown **/
		display.dispose();
		System.exit(0);
		
	}
	
}


Morin

Sorry to reply to myself, but I found the problem. The byte buffer that is used to back the float buffer must be manually set to the byte order that LWJGL expects.