Page 1 of 1

[Ogre 1.11] Use Manual Object to load Obj file into Ogre3d

Posted: Tue Aug 27, 2019 8:42 am
by randomcode
Ogre Version: 1.11
Operating System: Windows 7
Render System: OpenGL

My Model:

Code: Select all

# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 27.08.2019 15:18:26

mtllib box.mtl

#
# object Box01
#

v  20.9207 -18.5411 -27.7879
v  20.9207 -18.5411 27.7879
v  -20.9207 -18.5411 27.7879
v  -20.9207 -18.5411 -27.7879
v  20.9207 18.5411 -27.7879
v  -20.9207 18.5411 -27.7879
v  -20.9207 18.5411 27.7879
v  20.9207 18.5411 27.7879
# 8 vertices

vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 0.0000 1.0000
vn 1.0000 0.0000 -0.0000
# 4 vertex normals

vt 0.9685 0.8555 0.0000
vt 0.0466 0.8539 0.0000
vt 0.0479 0.1598 0.0000
vt 0.9698 0.1615 0.0000
vt 0.9674 0.1754 0.0000
vt 0.9686 0.8695 0.0000
vt 0.0468 0.8711 0.0000
vt 0.0455 0.1771 0.0000
vt 0.8343 0.8470 0.0000
vt 0.1402 0.8450 0.0000
vt 0.1420 0.2299 0.0000
vt 0.8361 0.2319 0.0000
vt 0.9485 0.8362 0.0000
vt 0.0267 0.8362 0.0000
vt 0.0267 0.2211 0.0000
vt 0.9485 0.2211 0.0000
# 16 texture coords

g Box01
usemtl Material__25
s 2
f 1/1/1 2/2/1 3/3/1 4/4/1 
s 4
f 5/5/2 6/6/2 7/7/2 8/8/2 
s 8
f 8/9/3 7/10/3 3/11/3 2/12/3 
s 16
f 5/13/4 8/14/4 2/15/4 1/16/4 
# 4 polygons
My Code:

Code: Select all

            ManualObject manualObject = sceneManager.CreateManualObject();
            manualObject.Begin(materialName, RenderOperation.OperationTypes.OT_TRIANGLE_LIST);
            {
                for (int i = 0; i < objModel.Faces.Length; i++)
                {
                    var face = objModel.Faces[i];

                    int vindex1 = face.Tuples[0].VertexIndex - 1;
                    int vnindex1 = face.Tuples[0].NormalIndex - 1;
                    int vtindex1 = face.Tuples[0].TextureIndex - 1;

                    int vindex2 = face.Tuples[1].VertexIndex - 1;
                    int vnindex2 = face.Tuples[1].NormalIndex - 1;
                    int vtindex2 = face.Tuples[1].TextureIndex - 1;

                    int vindex3 = face.Tuples[2].VertexIndex - 1;
                    int vnindex3 = face.Tuples[2].NormalIndex - 1;
                    int vtindex3 = face.Tuples[2].TextureIndex - 1;
                    if (face.IsQuad)
                    {
                        int vindex4 = face.Tuples[3].VertexIndex - 1;
                        int vnindex4 = face.Tuples[3].NormalIndex - 1;
                        int vtindex4 = face.Tuples[3].TextureIndex - 1;

                        var vex1 = objModel.Vertics[vindex1];
                        var vn1 = objModel.VertexNormals[vnindex1];
                        var vt1 = objModel.VertexTextures[vtindex1];
                        manualObject.Position(vex1);
                        manualObject.Normal(vn1);
                        manualObject.TextureCoord(vt1);

                        var vex2 = objModel.Vertics[vindex2];
                        var vn2 = objModel.VertexNormals[vnindex2];
                        var vt2 = objModel.VertexTextures[vtindex2];
                        manualObject.Position(vex2);
                        manualObject.Normal(vn2);
                        manualObject.TextureCoord(vt2);

                        var vex3 = objModel.Vertics[vindex3];
                        var vn3 = objModel.VertexNormals[vnindex3];
                        var vt3 = objModel.VertexTextures[vtindex3];
                        manualObject.Position(vex3);
                        manualObject.Normal(vn3);
                        manualObject.TextureCoord(vt3);

                        var vex4 = objModel.Vertics[vindex4];
                        var vn4 = objModel.VertexNormals[vnindex4];
                        var vt4 = objModel.VertexTextures[vtindex4];
                        manualObject.Position(vex4);
                        manualObject.Normal(vn4);
                        manualObject.TextureCoord(vt4);
                    }
                    else
                    {
                        var vex1 = objModel.Vertics[vindex1];
                        var vn1 = objModel.VertexNormals[vnindex1];
                        var vt1 = objModel.VertexTextures[vtindex1];
                        manualObject.Position(vex1);
                        manualObject.Normal(vn1);
                        manualObject.TextureCoord(vt1);

                        var vex2 = objModel.Vertics[vindex2];
                        var vn2 = objModel.VertexNormals[vnindex2];
                        var vt2 = objModel.VertexTextures[vtindex2];
                        manualObject.Position(vex2);
                        manualObject.Normal(vn2);
                        manualObject.TextureCoord(vt2);

                        var vex3 = objModel.Vertics[vindex3];
                        var vn3 = objModel.VertexNormals[vnindex3];
                        var vt3 = objModel.VertexTextures[vtindex3];
                        manualObject.Position(vex3);
                        manualObject.Normal(vn3);
                        manualObject.TextureCoord(vt3);
                    }
                }
                for (int i = 0; i < objModel.Faces.Length; i++)
                {
                    var face = objModel.Faces[i];

                    int vindex1 = face.Tuples[0].VertexIndex - 1;
                    int vindex2 = face.Tuples[1].VertexIndex - 1;
                    int vindex3 = face.Tuples[2].VertexIndex - 1;
                    if (face.IsQuad)
                    {
                        int vindex4 = face.Tuples[3].VertexIndex - 1;

                        manualObject.Quad(
                            (uint)vindex1,
                            (uint)vindex2,
                            (uint)vindex3,
                            (uint)vindex4);
                    }
                    else
                    {
                        manualObject.Triangle(
                            (uint)vindex1,
                            (uint)vindex2,
                            (uint)vindex3);
                    }
                }
            }
            manualObject.End();

            manualObject.ConvertToMesh(meshName);
            Entity ent = sceneManager.CreateEntity(meshName, meshName);
            ent.SetMaterialName(materialName);
            return ent;
Result:
result.png
As you can see, the result looks dark and texture seems uncorrect

Any idea?

Re: [Ogre 1.11] Load Obj file into Ogre3d

Posted: Sat Aug 31, 2019 5:56 am
by randomcode
@spacegaier

Re: [Ogre 1.11] Load Obj file into Ogre3d

Posted: Mon Sep 02, 2019 1:43 am
by randomcode
Does anyone know how to do that?

Re: [Ogre 1.11] Load Obj file into Ogre3d

Posted: Mon Sep 02, 2019 10:39 am
by paroj

Re: [Ogre 1.11] Load Obj file into Ogre3d

Posted: Thu Sep 05, 2019 12:26 am
by randomcode
paroj wrote:
Mon Sep 02, 2019 10:39 am
you could try https://github.com/OGRECave/ogre-assimp
I know this project, but I hope I can use manual object to do such thing

Re: [Ogre 1.11] Use Manual Object to load Obj file into Ogre3d

Posted: Sat Sep 07, 2019 11:37 pm
by randomcode
Didn't OGRE support manual object import obj file?