OGRECave / blender2ogre

Blender exporter for the OGRE 3D engine
GNU Lesser General Public License v2.1
199 stars 69 forks source link

Exported lights point in the "wrong" direction #231

Open sercero opened 1 month ago

sercero commented 1 month ago

Hello, @paroj.

While developing the ability to export rectangular lights, it came to my attention an issue regarding the exporting of lights.

Suppose we have a scene with a cube and a directional light of type "Sun". image

By default in Blender a light with identity transform will have the light pointing downwards.

If we export the scene to OGREs coordinate system, and since blender2ogre does a conversion where x->x, y->z, z->-y, then the result is that the cube now is in position (1, 0, -1) and the light is pointing towards -Z. image

This is because since the light had identity transform in Blender its transform will not change in OGRE. But the thing is that in OGRE the light will point towards -Z because that is what happens with a light that has no rotation.

I changed the code in scene.py so that the lights are the only objects transformed by a -90° rotation on the X axis.

What do you think?

paroj commented 1 month ago

This is because since the light had identity transform in Blender its transform will not change in OGRE.

can you link the code for this?

I would assume that issue this is not limited to lights. Try exporting the monkey mesh, instead of the symmetrical cube.

paroj commented 1 month ago

sidenote: if you are exporting the whole scene, you should not need to flip the coordinate axes. those are defined by the scene, which you are in full control of

sercero commented 1 month ago

This is the function where each node in the scene is processed in terms of "axis swapping": https://github.com/OGRECave/blender2ogre/blob/25a87890a4d21a39326ede171cc548d579a0b117/io_ogre/ogre/scene.py#L350-L393

And in util.py you have the function that does the swapping for most of bleder2ogre: https://github.com/OGRECave/blender2ogre/blob/25a87890a4d21a39326ede171cc548d579a0b117/io_ogre/util.py#L424-L437

paroj commented 1 month ago

ah.. ok.. I thought there is some explicit check for identity transform somewhere.

frankly I think that swap function is just broken for rotations and being needlessly slow due to all the ifs.

The correct approach would be just to construct a 3x3 matrix to transform both rotations and positions. Note, that a Quaternion is not sufficient as the matrix would contain mirroring and not pure rotation.

sercero commented 1 month ago

Yes, you are right.

When I discovered this, I spent 3 days in a deep confusion 😂.

I'll see how to do it with matrices.

Although let me tell you that despite all that... it works.

If you export a scene it looks correct even with rotations.

sercero commented 1 month ago

So, really my question is this: If you create a light in OGRE and the transform is identity, then the light should point towards -Z? (I know it can be tested fairly easily but I want to know how it is supposed to work.)

If that is true, then there needs to be a correction.

paroj commented 1 month ago

If you create a light in OGRE and the transform is identity, then the light should point towards -Z?

yes. with identity transform lights and cameras in ogre point towards Z-

Although let me tell you that despite all that... it works.

actually, for meshes the rotation is taken care of by swapping the vertices (position) and lights are broken. So I still assume that it does not work :P

sercero commented 1 month ago

If you create a light in OGRE and the transform is identity, then the light should point towards -Z?

yes. with identity transform lights and cameras in ogre point towards Z-

Although let me tell you that despite all that... it works.

actually, for meshes the rotation is taken care of by swapping the vertices (position) and lights are broken. So I still assume that it does not work :P

I mean, it works for everything except lights.

That is in fact everything is properly converted.

The issue is that in Blender lights with identity transform point downwards (-Y in OGRE) and in OGRE they point towards the "back" (-Z in OGRE), that is why lights need special handling.

paroj commented 1 month ago

That is in fact everything is properly converted.

see #240