2
$\begingroup$

I have custom exporter which exports data from blender to opengl app.

To convert meshes to opengl space I used:

global_matrix = axis_conversion(to_forward="-Z", to_up="Y").to_4x4()

Then when I export mesh I call mesh.transform(global_matrix) and vertex position is correct.

Each object I export as reference to mesh and transform matrix. To convert transform matrix I use:

matrix_world = global_matrix * obj.matrix_world * global_matrix

If I set axis_conversion forward to Z it works (but model is not facing camera), but -Z have wrong results.

Bellow is scene in blender, there are two triangle shaped objects and three cones.

blender scene

This is wrong image, triangles are placed correctly but cones are upside down (used forward_axis -Z)

enter image description here

This image is shows objects exported with correct positions but wrong axis (used Z instead of -Z)

enter image description here

How can I fix this? Why are the results wrong with -Z?

$\endgroup$
  • $\begingroup$ Shouldn't it be matrix_world = global_matrix.transposed() * obj.matrix_world * global_matrix? $\endgroup$ – maddin45 Mar 5 '15 at 17:35
  • $\begingroup$ You are right! I changed it to matrix_world = global_matrix * obj.matrix_world * global_matrix.transposed() and it is working $\endgroup$ – Bobanini Mar 5 '15 at 18:37
  • $\begingroup$ @maddin45 thanks! If you write suggestion as answer I will select it. $\endgroup$ – Bobanini Mar 5 '15 at 18:50
4
$\begingroup$

I made a mistake in my comment to your question, but here is the correct version:

Also, as you have found out yourself, it is the second global_matrix that needs to be transposed.

The correct formula is:

matrix_world = global_matrix * obj.matrix_world * global_matrix.transpose()

However this only works if global_matrix is just a rotation (which it is in your case). In general what you need to calculate is not the transpose of the matrix, but the inverse. In the case of rotation matrices the inverse is equal to the transpose.

So the general case looks like this:

matrix_world = global_matrix * obj.matrix_world * global_matrix.inverse()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.