Jump to content
  • Advertisement
Sign in to follow this  
glopen

Aligning your coordinate system with another. What am I doing wrong? [OpenGL]

This topic is 3753 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

I have three orthogonal vectors to represent the local axes of an object, but before I render it I want to align the system with these vectors. This is what I'm doing:
[SOURCE]
//This is how the up/right/forward vectors are initialized
//Will rotate/transform over time
float lxyz[3][3]={1,0,0,// X
		  0,1,0,// Y
		  0,0,1 // Z
		  };
...

//This is the code I have to do the alignment
float xx[3]={1,0,0};
float yy[3]={0,1,0};

float dp1 = dotP(lxyz[0],xx);   // dot product of current X and Forward vector
float ang1= acos(dp1)*180/PI;   // determine angle between them
float* ax1= crossP(lxyz[0],xx); // determine rotation axis
norm(ax1);                      // normalize

glRotatef(ang1,ax1[0],ax1[1],ax1[2]); //Align the two X axes (this is ok, right?)

float dp2 = dotP(lxyz[1],yy);   // align current Y with Up vector
float ang2= acos(dp2)*180/PI;

glRotatef(ang2,1,0,0);          // rotate around X to finally align
[/SOURCE]
The last rotation is fine right, because by the time the first rotation is done, the current X is the forward vector. I'm getting some weird results and it looks like it's rotating about the wrong vectors, which are doing just fine. Am I doing anything fundamentally wrong?

Share this post


Link to post
Share on other sites
Advertisement
I can't quite tell what you're doing there, but it kind of looks like you just want to render an object using the orientation specified by lxyz.

If that is the case, just upload the matrix directly using glMultMatrixf(). (You'll have to convert your 3x3 matrix into a 4x4 matrix first.)

Share this post


Link to post
Share on other sites
Yeah I sort of tried and retracted out of confusion. Should I just make it a 4x4 matrix by adding a few 0s and a 1 or do I have to make it lxyz[16]? I thought this is the format OpenGL uses and theres also a bit of confusion with it being column-major and everything. Which is the right way?

Share this post


Link to post
Share on other sites
Quote:
Original post by glopen
Yeah I sort of tried and retracted out of confusion. Should I just make it a 4x4 matrix by adding a few 0s and a 1 or do I have to make it lxyz[16]? I thought this is the format OpenGL uses and theres also a bit of confusion with it being column-major and everything. Which is the right way?
Based on what you've posted, I think you'll want to perform the conversion like this:
float m[16];

m[ 0] = xyz[0][0];
m[ 1] = xyz[0][1];
m[ 2] = xyz[0][2];
m[ 3] = 0.f;
m[ 4] = xyz[1][0];
m[ 5] = xyz[1][1];
m[ 6] = xyz[1][2];
m[ 7] = 0.f;
m[ 8] = xyz[2][0];
m[ 9] = xyz[2][1];
m[10] = xyz[2][2];
m[11] = 0.f;
m[12] = 0.f;
m[13] = 0.f;
m[14] = 0.f;
m[15] = 1.f0;
Although this should work, I would recommend instead using a proper matrix class that hides these sorts of details from the user.

(Actually, you could probably just upload you matrix to OpenGL directly, since it is already laid out as OpenGL expects.)

Share this post


Link to post
Share on other sites
Thanks. I've just quickly mashed up this (I don't really know about the overhead associated with initializations):

float mat[16] = {lxyz[0][0],lxyz[1][0],lxyz[2][0],lxyz[3][0],
lxyz[0][1],lxyz[1][1],lxyz[2][1],lxyz[3][1],
lxyz[0][2],lxyz[1][2],lxyz[2][2],lxyz[3][2],
lxyz[0][3],lxyz[1][3],lxyz[2][3],lxyz[3][3],
};


I'll clean this up to what you just posted but I'm getting better but strange results. There could be something wrong with the XYZ order, but when I render the vectors separately and compare with the object the rotations seem to be happening around different axes. Any known reason for this? Thanks again, you've saved a lot of work :)

Share this post


Link to post
Share on other sites
Quote:
Original post by glopen
Thanks. I've just quickly mashed up this (I don't really know about the overhead associated with initializations):

float mat[16] = {lxyz[0][0],lxyz[1][0],lxyz[2][0],lxyz[3][0],
lxyz[0][1],lxyz[1][1],lxyz[2][1],lxyz[3][1],
lxyz[0][2],lxyz[1][2],lxyz[2][2],lxyz[3][2],
lxyz[0][3],lxyz[1][3],lxyz[2][3],lxyz[3][3],
};


I'll clean this up to what you just posted but I'm getting better but strange results. There could be something wrong with the XYZ order, but when I render the vectors separately and compare with the object the rotations seem to be happening around different axes. Any known reason for this? Thanks again, you've saved a lot of work :)
Don't worry about overhead at this point. As for your initialization, it's the transpose of what I suggested, so you might try it the other way around.

Also, I'm assuming you've changed lxyz to a 4x4 matrix...

Share this post


Link to post
Share on other sites
Of course! *punches himself*

I just tried it with the transpose and it works like a charm! No wonder it was looking odd earlier. It's exactly like you wrote and I was just caught up with the column-major idea. This is probably what DirectX would use.

Yes I already turned lxyz into a 4x4. Thanks a LOT!

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!