Issue with glLoadMatrix and own Matrix class

Started by Finalspace, May 22, 2012, 11:19:06

Previous topic - Next topic

Finalspace

Hi there,

i am currently trying to use my own matrix class, but i cant glLoadMatrix to get working.
Here is the code for reproducing this.

package game;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class LWJGLTest {
	public static void main(String[] args) {
		Thread gameThread = new Thread(new Runnable() {
			void setMVP(final Matrix4f matrix, ByteBuffer byteBuffer) {
				FloatBuffer buf = byteBuffer.asFloatBuffer();
				int idx = 0;
				for (int i = 0; i < 4; i++) {
					for (int j = 0; j < 4; j++) {
						buf.put(idx, matrix.m[i][j]);
						idx++;
					}
				}
				buf.position(0);
				GL11.glLoadMatrix(buf);
			}

			@Override
			public void run() {
				try {
					Display.setDisplayMode(new DisplayMode(800, 600));
					Display.create();
				} catch (LWJGLException e) {
					e.printStackTrace();
					return;
				}

				GL11.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
				GL11.glDisable(GL11.GL_CULL_FACE);
				
				ByteBuffer tempMatrixBuffer = ByteBuffer.allocateDirect(16 * (Float.SIZE / 8));

				while (!Display.isCloseRequested()) {
					Display.sync(60);
					
					// Set viewport
					GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
					
					// Clear frame
					GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
					
					// Calculate ortho projection and load it into opengl
					Matrix4f mvp = Matrix4f.createOrtho(0.0f, (float)Display.getWidth(), (float)Display.getHeight(), 0.0f, -1.0f, 1.0f);
					mvp = Matrix4f.mul(mvp, Matrix4f.createTranslation(0.0f, 0.0f, 0.0f));
					setMVP(mvp, tempMatrixBuffer);
					
					// Draw a red quad from pos 0x0 size 100x100
					GL11.glColor3f(0.0f, 1.0f, 0.0f);
					GL11.glBegin(GL11.GL_QUADS);
					GL11.glVertex2i(0, 0);
					GL11.glVertex2i(100, 0);
					GL11.glVertex2i(100, 100);
					GL11.glVertex2i(0, 100);
					GL11.glEnd();
					
					// Swap buffers
					Display.update();
				}

				Display.destroy();
			}
		});
		gameThread.start();
	}

}


There will be now exception thrown, but it simply does not work... the quad is not seen.
I also checked the matrix values and it was exactly the same as my native c++ application.
Therefore is nothing wrong with my matrix implementation.
Of course i checked the FloatBuffer values and the values seems to be uploaded fine...

What do i am wrong?

Regards,
Final

Fool Running

Make sure you are setting the byte order correctly on the ByteBuffer. Try calling this instead:
ByteBuffer.allocateDirect(16 * (Float.SIZE / 8)).order(ByteOrder.nativeOrder())
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Finalspace

Quote from: Fool Running on May 22, 2012, 12:34:25
Make sure you are setting the byte order correctly on the ByteBuffer. Try calling this instead:
ByteBuffer.allocateDirect(16 * (Float.SIZE / 8)).order(ByteOrder.nativeOrder())


Damn, i knew it... it has something to do with this. Java uses big endian by default...
But anyway, Thanks... it works now.