glLoadMatrix doesnt take my FloatBuffer

Started by rokkun, July 28, 2005, 03:15:01

Previous topic - Next topic

rokkun

Hey there,

I am working on a quaternion class, and i have checked all my data... everything seems to be working fine up until i load the matrix into opengl.  i make a float array and feed it into the float buffer using this code:


ByteBuffer bb = ByteBuffer.allocateDirect(64);
		// GetModelMatrix()
		 FloatBuffer m2 = bb.asFloatBuffer();
		 for(int a =0; a < 16; a++){
			// System.out.println(matrix[a]);
			 m2.put(a,matrix[a]);
		 }
		 System.out.println("MATRIX");
		 for(int a =0; a < 16; a++){
		 	System.out.println(m2.get(a));
		 }
		 GL11.glLoadMatrix(m2);

when i load it into opengl using the glLoadMatrix method, everything seems to revert to zeros.  Am i loading the data the wrong way?  did i do something wrong when loading the FloatBuffer?

Thanks a lot for your time,


Chris

Matzon

try to do this instead:
ByteBuffer fb = BufferUtils.createFloatBuffer(16);
for(int a=0; a<16; a++){
  fb.put(matrix[a]);
}

// now that we have stuffed it with data, we need to flip it so that position is placed at the beginning of data and end limit is set to current position
fb.flip();

GL11.glLoadMatrix(fb);

rokkun

wow... that made it come up.  now all i have to do is figure out why its not being displayed correctly.  roll looks like it works but the other two rotations dont like to come out right... the model skews itself.

thanks again for your help



--
EDIT:  http://img.photobucket.com/albums/v479/rokkun/problem.jpg

there is a screenshot showing before and after rotation.  it skews and flattens itself... i think its a matrix problem but i can't be sure

rokkun

i guess i might as well show my entire quaternion class... here it goes:

I might as well send my entire quaternion class... here it goes...


package spacesim;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

public class Quaternion {
	float x,y,z,w;
	public Quaternion(){
		set(1,0,0,0);
	}
	void set(float W, float X, float Y, float Z){
		x=X;
		y=Y;
		z=Z;
		w=W;
	}
	void setRotation(float angle, float xAngle, float yAngle, float zAngle){
		this.w  = (float)Math.cos( angle/2);
		this.x = (float)(xAngle * Math.sin( angle/2 ));
		this.y = (float)(yAngle * Math.sin( angle/2 ));
		this.z = (float)(zAngle * Math.sin( angle/2 ));
	}
	void applyRotation(float angle, float xAngle, float yAngle, float zAngle){
		Quaternion c = new Quaternion();
		c.setRotation(angle,xAngle,yAngle,zAngle);
		c = multiply(this, c);
		set(c.w, c.x, c.y, c.z);
	}
	void normalize(){
		float mag = (float)Math.sqrt(Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2));
		x/=mag;
		y/=mag;
		z/=mag;
		w/=mag;
	}
	void getMatrix(){
		float[] matrix = new float[16];
		
		matrix[0] = 1 - (float)(2*Math.pow(y, 2)) - (float)(2*Math.pow(z,2));
		matrix[1] = (2 * x * y) - (2 * w * z);
		matrix[2] = (2 * x * z) + (2 * w * y);
		matrix[3] = 0;
		
		matrix[4] = (2 * x * y) - (2 * w * z);
		matrix[5] = 1 - (float)(2*Math.pow(x, 2)) - (float)(2*Math.pow(z,2));
		matrix[6] = (2 * y * z) + (2 * w * z);
		matrix[7] = 0;
		
		matrix[8] = (2 * x * z) - (2 * w * y);
		matrix[9] = (2 * y * z) - (2 * w * z);
		matrix[10] = 1 - (float)(2*Math.pow(x, 2)) - (float)(2*Math.pow(y,2));
		matrix[11] = 0;
		
		matrix[12] = 0;
		matrix[13] = 0;
		matrix[14] = 0;
		matrix[15] = 1;

		 FloatBuffer m2 = bb.asFloatBuffer();
		 for(int a =0; a < 16; a++){
			 m2.put(a,matrix[a]);
		 }
		 System.out.println("MATRIX");

		 
		 GL11.glLoadMatrix(m2);
		 GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, m2);
		 for(int a =0; a < 16; a++){
		 	System.out.println(m2.get(a));
		 }
		FloatBuffer fb = BufferUtils.createFloatBuffer(16);
		for(int a=0; a<16; a++){
		  fb.put(matrix[a]);
		}

//		 now that we have stuffed it with data, we need to flip it so that position is placed at the beginning of data and end limit is set to current position
		fb.flip();

		GL11.glLoadMatrix(fb);
		//return matrix;
	}
	Quaternion multiply(Quaternion a, Quaternion b){
		Quaternion c = new Quaternion();
		c.w = (a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z);
		c.x = (a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y);
		c.y = (a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x);
		c.z = (a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w);
		return c;
	}
}

Matzon

do consider if you cant just use:
fb.put(matrix).flip();

and perhaps your problem is caused by your matrix being row-column ordered, whereas OpenGL is column-row ordered ? (just an idea)

Matzon

have you considered using:
org.lwjgl.util.vector.Quaternion ?

rokkun

oh my..

heh, i spent a few days converting C++ quaternion classes into java readable ones, all turning out just as bad.  now i find that they had a class all along?

*sigh*

well ill look through the class then

----
EDIT:

Well, thats a time saver i guess... but once i have a quaternion set up, how do i apply it to the model matrix?  it doesnt have anything like that implemented. (or so i see so far)

well, i will keep on looking around.  any help would be appreciated

rokkun

well... i got my quaternion code to work (some...how)

the ship doesnt distort at all, which is making me really happy!  there were a few +'s and -'s that were misplaced.

Thanks for all your help, everyone!