Question

I'm sure there is an answer to this on the web but I can't find it. I'm importing a scene from Blender that has multiple meshes, into OpenTK. The library I'm using to import is Assimp-net, and the file format is Collada (.dae).

I have created a spaceship with multiple parts, each part being a mesh. Now when I import and draw, the geometry of the objects looks fine and materials work as expected. However, the different parts are not rotated, scaled, or translated as they appear in Blender. What happens is the different parts are not connected, and some appear larger/smaller than they should, in the wrong place etc.

Is there a setting I'm missing when I export from Blender, or is there some Assimp member/function I can use to transform the meshes before I render them?

Importing the file:

string filename = @"C:\Path\ship.dae";
Scene ship;

//Create a new importer
AssimpImporter importer = new AssimpImporter();

//This is how we add a configuration (each config is its own class)
NormalSmoothingAngleConfig config = new NormalSmoothingAngleConfig(66.0f);
importer.SetConfig(config);

//Import the model
ship = importer.ImportFile(filename, PostProcessPreset.TargetRealTimeMaximumQuality);

//End of example
importer.Dispose();

Drawing the meshes(entire "RenderFrame" event handler in OpenTK):

// Clear color/depth buffers
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

    // Define world space
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();
    GL.Ortho(-15.0, 15.0, -15.0, 15.0, 15.0, -15.0);

    // Rotate around X and Y axes for better viewing
    rotateX(xrot);
    rotateY(yrot);

    GL.Enable(EnableCap.ColorMaterial);

    var rootnode = wes10.RootNode;
    foreach (Node node in rootnode.Children)
    {
        //for each node, do
        GL.MatrixMode(MatrixMode.Modelview);  //ensure your current matrix is the model matrix.
        GL.PushMatrix();              //save current model matrix so you can undo next transformations;

        var meshIndices = node.MeshIndices;

        if (meshIndices == null)
            continue;
        else
        {
            Matrix4d convertedTransform = new Matrix4d();
            getConvertedMatrix(node.Transform, ref convertedTransform);
            GL.MultMatrix(ref convertedTransform);

            GL.Begin(BeginMode.Triangles);
            foreach (uint i in meshIndices)
            {
                Mesh mesh = wes10.Meshes[i];

                Material mat = wes10.Materials[mesh.MaterialIndex];

                // Material setup                        
                var spec_color = mat.ColorSpecular;
                var amb_color = mat.ColorAmbient;
                var diff_color = mat.ColorDiffuse;


                float[] mat_specular = { spec_color.R, spec_color.G, spec_color.B, spec_color.A };
                float[] mat_ambient = { amb_color.R, amb_color.G, amb_color.B, amb_color.A };
                float[] mat_diffuse = { diff_color.R, diff_color.G, diff_color.B, diff_color.A };

                float[] mat_shininess = { 0.0f };

                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, mat_specular);
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Ambient, mat_ambient);
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Diffuse, mat_diffuse);
                GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, mat_shininess);

                foreach (Face face in mesh.Faces)
                {
                    foreach (uint indice in face.Indices)
                    {
                        var normal = mesh.Normals[indice];
                        var pos = mesh.Vertices[indice];
                        //var tex = mesh.GetTextureCoords(0)[v];
                        //GL.TexCoord2(tex.X, tex.Y);
                        GL.Normal3(normal.X, normal.Y, normal.Z);
                        GL.Vertex3(pos.X, pos.Y, pos.Z);
                    }
                }
            }                            
        }

        GL.PopMatrix();
    }


    GL.End();                    

    game.SwapBuffers();

Updated to use suggestions.

Was it helpful?

Solution

In the c example, there is a transformation matrix per node...

aiMultiplyMatrix4(trafo,&nd->mTransformation);

Check this:

If you don't know what to do with that matrix, check this to learn about matrix stack. (Be aware that modern OpenGL recommand to implement your own transformation matrix)

Golobaly, you need the folowing steps for rendering (read the c example for details):

//for each node, do
glMatrixMode (GL_MODELVIEW);  //ensure your current matrix is the model matrix. 
glPushMatrix ();              //save current model matrix so you can undo next transformations;
glMultMatrixf(Transformation);//apply your node matrix

//render your node, in your example it's surely a mesh

glPopMatrix ();               //restore model matrix
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top