Jump to content
  • Advertisement
Sign in to follow this  
Pirosan

OpenGL glLoadMatrix doesnt work

This topic is 4829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

WARNING: this code is actually java, not C++. i am using the lwjgl library, but it has ported opengl correctly. i have a quaternion class that outputs the right numbers and everything. i have a SetMatrix method that sets a float array, then pushes it into a FloatBuffer and gives it to the glLoadMatrix method, but when it does that, for some reason the matrix in opengl is set to all 0s... here is the 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); 
I dont know what i am doing wrong... did i set the floatbuffer incorrectly? Cheers

Share this post


Link to post
Share on other sites
Advertisement
There are a number of different matrix modes in OpenGL. Could you have accidently tried to load that matrix into the wrong mode?

Share this post


Link to post
Share on other sites
what mode should i use?

i notice, come render time, the mode is set as so:

GL11.glMatrixMode(GL11.GL_MODELVIEW);

Share this post


Link to post
Share on other sites
Quote:
Original post by Pirosan
what mode should i use?

i notice, come render time, the mode is set as so:

GL11.glMatrixMode(GL11.GL_MODELVIEW);

If you're getting information from a quaternion, then that's probably the right matrix to use. I don't know Java very well, so I can't point out problems with the syntax.

OpenGL is column-major, is it possible you're trying to use row-major? Just a guess...

Share this post


Link to post
Share on other sites
hmmmm... i dont get this. as soon as i load the matrix i take it back out of opengl using the getFloat() method, it seems right... so why is it not rendering the object correctly?

during render time... this is what happens: (all the methods like renderAsteroids() only rotate, translate and render polygons, nothing complicated.)



changeCamera();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
renderStars();

rotation += .5f;
setTextureTile(3,3);
GL11.glPushMatrix();

updateShipPos();
renderAsteroids();

GL11.glLoadIdentity();

GL11.glScalef(.01f,.01f,.01f);

/////This is where i set the matrix, the code is in my first post////
shipRot.getMatrix();
/////////////////////

renderMesh(ship,sunTextureHandle); //This does not come up at all

GL11.glPopMatrix();

checkKeys();



oh, and also here is the output of the quaternion (without being messed with... i just leave it as is in the beginning)

{1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0}

this translates to this following matrix:

[1.0,0.0,0.0,0.0]
[0.0,1.0,0.0,0.0]
[0.0,0.0,1.0,0.0]
[0.0,0.0,0.0,1.0]

Share this post


Link to post
Share on other sites
You can't tell you don't show enough of your code. Maybe you didn't set up your projection correctly. Maybe you didn't setup the initial states correctly. Maybe you are drawing them all with black on a black screen so you can't see them.

Your camera code doesn't do anything either. When you do it you can use gluLookAt to compute the modelview for you from the camera instead of manually building the matrix for the view.

like this:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( ... )



Try putting glColor3f(1,1,1) also.

Share this post


Link to post
Share on other sites
i got a responce from someone suggesting to post the following code:


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);


that made it come up, but it isnt displayed correctly. roll looks like it works but the other two rotations dont like to come out right... the model skews itself.

Share this post


Link to post
Share on other sites

here is an example of what happens. also, this is my changeCamera method for those who wanted to know:

public static void changeCamera(float x, float y, float z)
{
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
/* fovy,aspect,zNear,zFar */
GLU.gluPerspective(30f, aspectRatio, 1f, 5000f);
//GLU.gluLookAt(0f, 0f, 20f, 0f, 0f, 0f, 0f, 1f, 0f);
cameraPosition[0] = x;
cameraPosition[1] = y;
cameraPosition[2] = z;
GLU.gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2], cameraLookAt[0], cameraLookAt[1], cameraLookAt[2], cameraUp[0], cameraUp[1], cameraUp[2]);
}

Share this post


Link to post
Share on other sites
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;
}
}


Share this post


Link to post
Share on other sites
You camera code is wrong.

You are loading it into the projection matrix. Probably not what you want to do.

Camera goes into the MODELVIEW (that is why it is called MODELVIEW.)

you need to do this:

glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(..)

I didn't look at your quat code I am not particularly interested in figuring out if you coded it correctly. Sorry.. I don't like to debug other people's math code I hate debugging my own.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!