Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/464517972283756544
added 1907 characters in body
Source Link

THIS IS SOLVED! The code shows how to render a 3D model using only Assimp and XNA with vertex and index buffers. oScene is the Scene object imported using Assimp. This code only draws faces in a single color without textures.

Here is the relevant code:

   private void SetUpVertices()
    {
        mMesh = oScene.Meshes[0];
        vertices = new VertexPositionColor[mMesh.VertexCount];
        indices = new  short[mMesh.FaceCount*3];

        int i=0;
        foreach (Vector3D mVec in mMesh.Vertices)
        {
            vertices[i] = new VertexPositionColor(new Vector3(mVec.X, mVec.Y, mVec.Z), Color.Red);
            i++;
        }

        int f = 0;
        Face mFace;
        for (i = 0 ; i < mMesh.FaceCount*3 ; i=i+3) 
        {
            mFace = mMesh.Faces[f];
            mFacef++;
            indices[i] = mMesh(short)mFace.Faces[f];Indices[0];
            f++;indices[i + 1] = (short)mFace.Indices[1];
            //Console.WriteLine(i.ToStringindices[i + 2] = (short)mFace.Indices[2];

 + " " + mMesh.FaceCount.ToString   }
        
        vertexBuffer = new VertexBuffer()+"device, "+mFaceVertexPositionColor.IndicesVertexDeclaration, vertices.Length, BufferUsage.ToString(WriteOnly);
        vertexBuffer.SetData(vertices);
         
    //if    indexBuffer = new IndexBuffer(mFacedevice, typeof(short), indices.IndexCountLength, ==BufferUsage.None);
 3       indexBuffer.SetData(indices);

    }

    private void SetUpCamera()
    {
        cameraPos = new Vector3(0, 5, 9);
        viewMatrix Console= Matrix.WriteLineCreateLookAt(i.ToStringcameraPos, new Vector3(0, 0, 1), +new "Vector3(0, "1, +0));
 mMesh       projectionMatrix = Matrix.FaceCountCreatePerspectiveFieldOfView(MathHelper.ToStringPiOver4, device.Viewport.AspectRatio, 1.0f, 200.0f);
    }

    protected override void Draw(GameTime gameTime) 
 + " " +{
 mFace       device.IndicesClear(ClearOptions.LengthTarget | ClearOptions.ToString()DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);
        effect = new BasicEffect(GraphicsDevice);
     indices[i]   effect.VertexColorEnabled = (short)mFacetrue;

        effect.Indices[0];View = viewMatrix;
        effect.Projection = projectionMatrix;
      indices[i + 1]effect.World = Matrix.Identity;

        foreach (short)mFaceEffectPass pass in effect.Indices[1];CurrentTechnique.Passes)
        {
        indices[i + 2] = pass.Apply(short)mFace.Indices[2];;
            device.SetVertexBuffer(vertexBuffer);
        //i = i + 3;device.Indices = indexBuffer;
            device.DrawIndexedPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleList, 0, 0, oScene.Meshes[0].VertexCount, 0, mMesh.FaceCount);
        } 

        base.Draw(gameTime);
    }

       

Here is the relevant code:

        for (i = 0 ; i < mMesh.FaceCount*3 ; i=i+3) 
        {
            
            mFace = mMesh.Faces[f];
            f++;
            //Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString()+" "+mFace.Indices.Length.ToString());
            //if (mFace.IndexCount == 3)
            {
                Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString() + " " + mFace.Indices.Length.ToString());
                indices[i] = (short)mFace.Indices[0];
                indices[i + 1] = (short)mFace.Indices[1];
                indices[i + 2] = (short)mFace.Indices[2];
                //i = i + 3;
            }
        }

THIS IS SOLVED! The code shows how to render a 3D model using only Assimp and XNA with vertex and index buffers. oScene is the Scene object imported using Assimp. This code only draws faces in a single color without textures.

Here is the relevant code:

   private void SetUpVertices()
    {
        mMesh = oScene.Meshes[0];
        vertices = new VertexPositionColor[mMesh.VertexCount];
        indices = new  short[mMesh.FaceCount*3];

        int i=0;
        foreach (Vector3D mVec in mMesh.Vertices)
        {
            vertices[i] = new VertexPositionColor(new Vector3(mVec.X, mVec.Y, mVec.Z), Color.Red);
            i++;
        }

        int f = 0;
        Face mFace;
        for (i = 0 ; i < mMesh.FaceCount*3 ; i=i+3) 
        {
            mFace = mMesh.Faces[f];
            f++;
            indices[i] = (short)mFace.Indices[0];
            indices[i + 1] = (short)mFace.Indices[1];
            indices[i + 2] = (short)mFace.Indices[2];

        }
        
        vertexBuffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
        vertexBuffer.SetData(vertices);
         
        indexBuffer = new IndexBuffer(device, typeof(short), indices.Length, BufferUsage.None);
        indexBuffer.SetData(indices);

    }

    private void SetUpCamera()
    {
        cameraPos = new Vector3(0, 5, 9);
        viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 1), new Vector3(0, 1, 0));
        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 200.0f);
    }

    protected override void Draw(GameTime gameTime) 
    {
        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);
        effect = new BasicEffect(GraphicsDevice);
        effect.VertexColorEnabled = true;

        effect.View = viewMatrix;
        effect.Projection = projectionMatrix;
        effect.World = Matrix.Identity;

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            device.SetVertexBuffer(vertexBuffer);
            device.Indices = indexBuffer;
            device.DrawIndexedPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleList, 0, 0, oScene.Meshes[0].VertexCount, 0, mMesh.FaceCount);
        } 

        base.Draw(gameTime);
    }

       
added 24 characters in body
Source Link

I am trying to render the vertices of a scene with a cube I exported as an OBJ from Blender. The 8 vertices become 24 when imported into XNA but when I render it I dont see all faces. This is not an issue of missing meshes because I'm pretty sure this is being stored as the first and only mesh. I suspect this is because of the vectors defined in the Assimp Mesh object but I can't seem to find any help for this.

Here is the relevant code:

        for (i = 0 ; i < mMesh.FaceCountFaceCount*3 ; i=i+3) 
        {
            
            mFace = mMesh.Faces[i];Faces[f];
            f++;
            //Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString()+" "+mFace.Indices.Length.ToString());
            //if (mFace.IndexCount == 3)
            {
                Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString() + " " + mFace.Indices.Length.ToString());
                indices[i] = (short)mFace.Indices[0];
                indices[i + 1] = (short)mFace.Indices[1];
                indices[i + 2] = (short)mFace.Indices[2];
                //i = i + 3;
            }
        }

I am trying to render the vertices of a scene with a cube I exported as an OBJ from Blender. The 8 vertices become 24 when imported into XNA but when I render it I dont see all faces. This is not an issue of missing meshes because I'm pretty sure this is being stored as the first and only mesh. I suspect this is because of the vectors defined in the Assimp Mesh object but I can't seem to find any help for this.

Here is the relevant code:

        for (i = 0 ; i < mMesh.FaceCount ; i=i+3) 
        {
            
            mFace = mMesh.Faces[i];
            //Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString()+" "+mFace.Indices.Length.ToString());
            //if (mFace.IndexCount == 3)
            {
                Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString() + " " + mFace.Indices.Length.ToString());
                indices[i] = (short)mFace.Indices[0];
                indices[i + 1] = (short)mFace.Indices[1];
                indices[i + 2] = (short)mFace.Indices[2];
                //i = i + 3;
            }
        }

I am trying to render the vertices of a scene with a cube I exported as an OBJ from Blender. The 8 vertices become 24 when imported into XNA but when I render it I dont see all faces. This is not an issue of missing meshes because I'm pretty sure this is being stored as the first and only mesh. I suspect this is because of the vectors defined in the Assimp Mesh object but I can't seem to find any help for this.

Here is the relevant code:

        for (i = 0 ; i < mMesh.FaceCount*3 ; i=i+3) 
        {
            
            mFace = mMesh.Faces[f];
            f++;
            //Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString()+" "+mFace.Indices.Length.ToString());
            //if (mFace.IndexCount == 3)
            {
                Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString() + " " + mFace.Indices.Length.ToString());
                indices[i] = (short)mFace.Indices[0];
                indices[i + 1] = (short)mFace.Indices[1];
                indices[i + 2] = (short)mFace.Indices[2];
                //i = i + 3;
            }
        }
deleted 1487 characters in body
Source Link
    protected override void LoadContent()
    {
          device = GraphicsDevice;

         for imp(i = new0 AssimpImporter();
          oScenei =< impmMesh.ImportFile("C:\\Users\\Anoop\\Documents\\Visual StudioFaceCount 2010\\Projects\\Project1\\cyl.obj");

        SetUpVertices(i=i+3); SetUpCamera();
    }
    private void SetUpVertices(){
    {
        Mesh 
 mMesh = oScene.Meshes[0];
        VertexPositionColor[] verticesmFace = new VertexPositionColor[mMeshmMesh.VertexCount];
Faces[i];
        int i=0;
        foreach //Console.WriteLine(Vector3D mVec in mMeshi.VerticesToString()
        {
            
           + vertices[i]" =" new+ VertexPositionColormMesh.FaceCount.ToString(new)+" Vector3(mVec"+mFace.X, mVecIndices.Y, mVecLength.ZToString(), Color.Red);
            i++;
        }

        
        vertexBuffer = new//if VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsagemFace.WriteOnly);
      IndexCount == vertexBuffer.SetData(vertices3);
        
    }

    private void SetUpCamera()
    {
        cameraPos = new Vector3(0, 5, 19);
        viewMatrix = MatrixConsole.CreateLookAtWriteLine(cameraPos, new Vector3i.ToString(0, 0, 1), new Vector3(0, 1, 0));
        projectionMatrix+ =" Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4," device.Viewport.AspectRatio,+ 1mMesh.0f, 200FaceCount.0f);
    }


    protected override void DrawToString(GameTime gameTime)
    {
      + " device.Clear(ClearOptions.Target" |+ ClearOptionsmFace.DepthBuffer, ColorIndices.DarkSlateBlue, 1Length.0f, 0);
        effect = new BasicEffectToString(GraphicsDevice));
        effect.VertexColorEnabled = true;

        effect.Viewindices[i] = viewMatrix;(short)mFace.Indices[0];
        effect.Projection = projectionMatrix;
      indices[i + effect.World1] = Matrix.Identity;

        foreach (EffectPass pass in effect.CurrentTechnique.Passesshort)mFace.Indices[1];
        {
        indices[i + 2] = pass.Apply(short);mFace.Indices[2];
            device.SetVertexBuffer(vertexBuffer);
      //i = i + 3;
  device.DrawPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleStrip, 0, oScene.Mesh[0].VertexCount);
        }
 
        base.Draw(gameTime);
    }

This is what I'm seeing:

http://www.4shared.com/photo/XP21eHBjba/cube.html

    protected override void LoadContent()
    {
          device = GraphicsDevice;

          imp = new AssimpImporter();
          oScene = imp.ImportFile("C:\\Users\\Anoop\\Documents\\Visual Studio 2010\\Projects\\Project1\\cyl.obj");

        SetUpVertices(); SetUpCamera();
    }
    private void SetUpVertices()
    {
        Mesh mMesh = oScene.Meshes[0];
        VertexPositionColor[] vertices = new VertexPositionColor[mMesh.VertexCount];

        int i=0;
        foreach (Vector3D mVec in mMesh.Vertices)
        {
            
            vertices[i] = new VertexPositionColor(new Vector3(mVec.X, mVec.Y, mVec.Z), Color.Red);
            i++;
        }

        
        vertexBuffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
        vertexBuffer.SetData(vertices);
        
    }

    private void SetUpCamera()
    {
        cameraPos = new Vector3(0, 5, 19);
        viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 1), new Vector3(0, 1, 0));
        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 200.0f);
    }


    protected override void Draw(GameTime gameTime)
    {
        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);
        effect = new BasicEffect(GraphicsDevice);
        effect.VertexColorEnabled = true;

        effect.View = viewMatrix;
        effect.Projection = projectionMatrix;
        effect.World = Matrix.Identity;

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            device.SetVertexBuffer(vertexBuffer);
            device.DrawPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleStrip, 0, oScene.Mesh[0].VertexCount);
        }
 
        base.Draw(gameTime);
    }

This is what I'm seeing:

http://www.4shared.com/photo/XP21eHBjba/cube.html

        for (i = 0 ; i < mMesh.FaceCount ; i=i+3) 
        {
             
            mFace = mMesh.Faces[i];
            //Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString()+" "+mFace.Indices.Length.ToString());
            //if (mFace.IndexCount == 3)
            {
                Console.WriteLine(i.ToString() + " " + mMesh.FaceCount.ToString() + " " + mFace.Indices.Length.ToString());
                indices[i] = (short)mFace.Indices[0];
                indices[i + 1] = (short)mFace.Indices[1];
                indices[i + 2] = (short)mFace.Indices[2];
                //i = i + 3;
            }
        }
Source Link
Loading