[fixed] Error whith MD2 normals loading

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
cyberwan
Posts: 40
Joined: Mon May 14, 2007 12:18 pm
Location: Rennes, France

[fixed] Error whith MD2 normals loading

Post by cyberwan »

Hi everyone !
I'm loading animated meshes through MD2 format, and I've got problems with the normals. A picture will explain better than any words.
(If the link is not working, my computer is probably offline.. wait a few hours and try again :wink: )
As you can see, the light is positioned just above the model, but its face is lighted ! Actually, it looks as the normals Z and Y are switched..
I saw on a very old topic that it has already been discussed, but no answer has been given.. I could fix the code, but I think if there was a problem, it would have been fixed already, but where is my actual problem coming from then? I'v tried a lot of things, to ensure that my file was not bad : exporting from Misfit3D (good alternative to MilkShape !), importing to and re-exporting from MilkShape, importing in Blender, and viewing with MD2 Viewer. The results are always clean, except when importing to irrlicht.. Has someone really tested MD2 format, and encountered such a problem ?
Thanks for answer
Virion
Competition winner
Posts: 2149
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

I think you've posted at the wrong subforum?
My company: https://kloena.com
My profile: https://zhieng.com
My co-working space: https://deskspace.info
My game engine: https://kemena3d.com
cyberwan
Posts: 40
Joined: Mon May 14, 2007 12:18 pm
Location: Rennes, France

Post by cyberwan »

Sorry, maybe I did.. Is there a way to move the topic to the appropriate category ?
cyberwan
Posts: 40
Joined: Mon May 14, 2007 12:18 pm
Location: Rennes, France

Post by cyberwan »

My problem can be fixed with the following patch :

Code: Select all

--- irrlicht-1.3/source/Irrlicht/CAnimatedMeshMD2.cpp   2007-03-10 09:51:30.000000000 +0100
+++ irrlicht-1.3-mod/source/Irrlicht/CAnimatedMeshMD2.cpp       2007-05-16 10:53:44.000000000 +0200
@@ -645,8 +645,8 @@ bool CAnimatedMeshMD2::loadFile(io::IRea
                        if (normalidx > 0 && normalidx < Q2_VERTEX_NORMAL_TABLE_SIZE)
                        {
                                v.X = Q2_VERTEX_NORMAL_TABLE[normalidx][0];
-                               v.Y = Q2_VERTEX_NORMAL_TABLE[normalidx][1];
-                               v.Z = Q2_VERTEX_NORMAL_TABLE[normalidx][2];
+                               v.Z = Q2_VERTEX_NORMAL_TABLE[normalidx][1];
+                               v.Y = Q2_VERTEX_NORMAL_TABLE[normalidx][2];
                        }
 
                        normals[i].push_back(v);
It may be applied and be useful to everyone, but it has to be made sure that it is really needed, and that I'm not doing something wrong...
white tiger
Posts: 269
Joined: Tue Oct 31, 2006 3:24 pm
Contact:

Post by white tiger »

i have a similar problem.

i have a md2 model and a light. in some camera orientation the model is rendered correctly, and in others camera orietation the model changes drastically to totally black or totally white in the parts where it would be lighted

my models is scaled, but i have called
setMaterialFlag(EMF_NORMALIZE_NORMALS,true) on the node

it happen also (but change less drastically) on unscaled models

screens:

here are my model and the yellow point is the light:

Image

here are the same scene. i have only rotate the camera on the y axis a bit:

Image
cyberwan
Posts: 40
Joined: Mon May 14, 2007 12:18 pm
Location: Rennes, France

Post by cyberwan »

Very strange indeed.. Your first screenshot looks as the lighting is deactivated on your model.
Did you try to apply the patch I provided on irrlicht ? It doesn't seem to be the same problem, but maybe..
tuXXX
Posts: 6
Joined: Fri May 11, 2007 3:22 pm

Post by tuXXX »

I extracted a md2 (soldier) model from quake2 : this model has correct normals!
Now I setup a scene in Irrlicht with the model and one light at (0, 100, 0), above the model.

This is what I get with the original irrlicht 1.3 library :
ImageImage

And now with the patch above :
ImageImage

We can see that normal are wrong with the original code and that this patch is needed.


Another proof is the code itself :
In CAnimatedMeshMD2.cpp, line 638 :

Code: Select all

            v.X = frame->vertices[j].vertex[0] * frame->scale[0] + frame->translate[0];
            v.Z = frame->vertices[j].vertex[1] * frame->scale[1] + frame->translate[1];
            v.Y = frame->vertices[j].vertex[2] * frame->scale[2] + frame->translate[2];
So it's : X ->0 , Z ->1 , Y ->2
But at line 647, without the patch we have : X ->0 , Y ->1 , Z ->2

The patch just set the same order than the code before.

Note : I also modified the normal debug code because it's useless in the original 1.3 library, maybe I should post it? This is visible on the four pictures above.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Ok, the normals fix is comitted. What about the normal debug mode?
tuXXX
Posts: 6
Joined: Fri May 11, 2007 3:22 pm

Post by tuXXX »

hybrid wrote:Ok, the normals fix is comitted. What about the normal debug mode?
This is how it looks like currently in irrlicht 1.3 (one solid cube and ont smoothed cube) :
ImageImage

And now with the patch :
ImageImage

(See the four previous pictures too with a more complex model)

I think that it's really better to understand what is going on.

The patch is like this :

Code: Select all

--- irrlicht-1.3/source/Irrlicht/CAnimatedMeshSceneNode.cpp     2007-03-10 10:32:08.000000000 +0100
+++ irrlicht-1.3-mod/source/Irrlicht/CAnimatedMeshSceneNode.cpp 2007-05-16 19:43:53.000000000 +0200
@@ -378,12 +378,7 @@ void CAnimatedMeshSceneNode::render()
                                const video::S3DVertex* v = ( const video::S3DVertex*)mb->getVertices();
                                for ( i = 0; i != mb->getVertexCount(); ++i )
                                {
-                                       AlignToUpVector ( m2, v->Normal );
-                                       AbsoluteTransformation.transformVect ( m2.pointer(), v->Pos );
-
-                                       driver->setTransform(video::ETS_WORLD, m2 );
-                                       for ( u32 a = 0; a != mesh->getMeshBufferCount(); ++a )
-                                               driver->drawMeshBuffer ( mesh->getMeshBuffer ( a ) );
+                                       driver->draw3DLine(v->Pos, v->Pos + v->Normal );
 
                                        v = (const video::S3DVertex*) ( (u8*) v + vSize );
                                }
Yes it's really simple, in fact I don't know exactly what the current code is supposed to show.
Post Reply