在OpenTK中,使用Assimp导入时,多个网格不会转换

时间:2014-03-28 17:19:51

标签: opengl blender opentk collada assimp

我确定网上有答案,但我无法找到答案。 我将具有多个网格的Blender场景导入OpenTK。 我用来导入的库是Assimp-net,文件格式是Collada(.dae)。

我创造了一个有多个部分的宇宙飞船,每个部分都是一个网格。 现在,当我导入和绘制时,对象的几何图形看起来很好,材质按预期工作。但是,不同的部分不会像Blender中出现的那样旋转,缩放或翻译。发生的事情是不同的部分没有连接,有些看起来比它们应该更大/更小,在错误的地方等。

我从Blender导出时是否缺少一个设置,或者是否有一些Assimp成员/函数可用于在渲染之前转换网格?

导入文件:

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();

绘制网格物体(整个" RenderFrame" 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();

已更新以使用建议。

1 个答案:

答案 0 :(得分:1)

c example中,每个节点都有一个转换矩阵......

aiMultiplyMatrix4(trafo,&nd->mTransformation);

检查一下:

如果您不知道如何处理该矩阵,请查看this以了解矩阵堆栈。 (请注意,现代OpenGL建议实现自己的转换矩阵)

Golobaly,你需要以下的渲染步骤(详见c示例):

//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