Sign in to follow this  
Revelation60

OpenGL Applying a transformation matrix in OpenGL

Recommended Posts

Hi, I am creating a 3ds loader and I am having trouble finding the correct way to express the translation matrix of block 4160. The information about this block is:
Quote:
4160 - Translation Matrix This structure describes a matrix for the object. It is stored as a 3 X 4 matrix because it is assumed that the right most column is 0,0,0,1
What command must I use to translate the scene? I've tried, maybe in the wrong way, glMultMatrixf. Thanks!

Share this post


Link to post
Share on other sites
Quote:
4160 - Translation Matrix

This structure describes a matrix for the object.
It is stored as a 3 X 4 matrix because it is assumed that
the right most column is 0,0,0,1
That doesn't seem right - if it's a 3x4 affine transform matrix, it should be the bottom row (not the right-most column) that's assumed to be identity.

Are you sure the docs didn't say '4x3'?

Share this post


Link to post
Share on other sites
It seems to be correct this way. Another doc says the following:

Quote:

Local axis info.
The three first blocks of three floats are the definition
(in the absolute axis) of the local axis X Y Z of the object.
And the last block of three floats is the local center of the object.



But how can I apply such a matrix in OpenGL?

Share this post


Link to post
Share on other sites
Quote:
But how can I apply such a matrix in OpenGL?
I'm guessing something like the following will work:
float glm[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };

// 'm' is the 3ds matrix

glm[0 ] = m[0 ];
glm[1 ] = m[1 ];
glm[2 ] = m[2 ];

glm[4 ] = m[3 ];
glm[5 ] = m[4 ];
glm[6 ] = m[5 ];

glm[8 ] = m[6 ];
glm[9 ] = m[7 ];
glm[10] = m[8 ];

glm[12] = m[9 ];
glm[13] = m[10];
glm[14] = m[11];

glMultMatrixf(glm); // Or whatever...

Share this post


Link to post
Share on other sites
Quote:
Original post by Revelation60
Unfortunately, that doens't work. All the objects are shattered around the room :(
Well, that could mean:

1. Your implementation is incorrect
2. The information you have about the 3ds matrix format is wrong
3. My example was wrong

I would bet on no. 1 being the most likely, followed by no. 2, and then no. 3. If you're not sure about no. 1, you might post some of your code so that we can see what you're doing.

Share this post


Link to post
Share on other sites
Well,

This is the code I use to load the information:

Quote:

for (int i=0;i<4;i++)
for (int j=0;j<3;j++)
{
pPreviousChunk->bytesRead += fread(&(pObject->localMatrix[i][j]), 1, sizeof (float), m_FilePointer);
}

pObject->localMatrix [0][3]=0;
pObject->localMatrix [1][3]=0;
pObject->localMatrix [2][3]=0;
pObject->localMatrix [3][3]=1;



And this is the code to multiply the matrix:

Quote:

glMultMatrixf(&m3DModel.pObject[i].localMatrix[0][0]);


I am trying to search code that does about the same, and I saw in one example that the localMatrix got inverted. I tried to do that (note that I don't know a lot abot matrices), but that didn't give good results. :(


Also leaving the pObject->localMatrix [0][3]=0; part out didn't help.

Share this post


Link to post
Share on other sites
Hmmm......you shouldn't be using a 2-dimensional matrix to store the object's position. OpenGL assumes that each of the 16 elements are perfectly adjacent in memory, and a 2-dimensional matrix is not. Try changing your code to use a 1-dimensional matrix and make sure to reference the indexes correctly when loading the values. Also make sure that the last value in your matrix, m[15], is 1.

Share this post


Link to post
Share on other sites
What happens when you only place a single object in the scene and export to 3ds and try to load it in your app? Is that object simply reflected across the z-axis?

Also keep in mind that 3dsmax uses the z-axis for up and down, and the y-axis for into and out of the viewport. So you will need to perform a 90 degree rotation around the x-axis.

Share this post


Link to post
Share on other sites
Quote:
Original post by Revelation60
Making it a 1D array gave no other results. Are you sure I don't need to change the matrix before handing it to OpenGL?
I'm curious: did you try the code that I posted?

Share this post


Link to post
Share on other sites
All the objects alone load great, but for the rotation of course.

I've uploaded my project:
http://home.versatel.nl/benruyl/test.rar

In 3ds.cpp you can find ReadTranslation, that's where the matrix is loaded.
In main.cpp I rotate the scene -90 degs in the DrawGLScene procedure.
In 3dsloader.cpp you can see my matrix multiplied.

I've included the binary to give you an example. Furthermore, the white spot in the middle is just the crosshair with the texture missing ;)

I have a feeling that says I shouldn't be multiplying at all, but like I said before, I am new to matrices :P


@jyk, I've tried your suggestion. But it didn't work :(


edit: of course, the model is in the file too

[Edited by - Revelation60 on July 10, 2007 7:35:47 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Revelation60
All the objects alone load great, but for the rotation of course.

I've uploaded my project:
http://home.versatel.nl/benruyl/test.rar

In 3ds.cpp you can find ReadTranslation, that's where the matrix is loaded.
In main.cpp I rotate the scene -90 degs in the DrawGLScene procedure.
In 3dsloader.cpp you can see my matrix multiplied.

I've included the binary to give you an example. Furthermore, the white spot in the middle is just the crosshair with the texture missing ;)

I have a feeling that says I shouldn't be multiplying at all, but like I said before, I am new to matrices :P


@jyk, I've tried your suggestion. But it didn't work :(


edit: of course, the model is in the file too
You might consider posting some code for those of those who may not have the time or inclination to download and examine your project.

As for having tried my suggestion and it not working, I'm not sure I buy it (I'm guessing you either messed up the implementation, or that your information about the model format is incorrect). Anyway, again, I'd recommend just posting the relevant files (3ds.cpp, 3dsloader.cpp, main.cpp) to this thread (remember to use [ source ] tags for formatting) so that we can take a look at them.

Share this post


Link to post
Share on other sites
I already posted that code and I guessed something else was the problem. Anyway, I found another code package that does successfully load the .3ds file. In that code I saw them translation with the negative of some node pivots.
But what are pivots?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this