[SlimDX] Optimize mesh

Started by
0 comments, last by Postie 11 years, 1 month ago

Hi, i am trying to optimize my mesh with this code, but i don´t know why i have an access violation exception whe i call mesh.GenerateAdjacency,

i don´t know if this error is because the flags used creating the mesh or the flags creating my device or i don´t know, Anyone could guide me?


SlimDX.Direct3D9.Direct3D myDirect3D= new SlimDX.Direct3D9.Direct3D();
            SlimDX.Direct3D9.PresentParameters myPresentParamters= new SlimDX.Direct3D9.PresentParameters();
            myPresentParamters.Windowed=true;
            myPresentParamters.SwapEffect=SlimDX.Direct3D9.SwapEffect.Discard;
            SlimDX.Direct3D9.Device myDevice= new SlimDX.Direct3D9.Device(myDirect3D,0,SlimDX.Direct3D9.DeviceType.Hardware,this.Handle,SlimDX.Direct3D9.CreateFlags.SoftwareVertexProcessing,myPresentParamters);
            
            int faceCount=triangleList.Count;
            int vertexCount=vertexList.Count;
            SlimDX.Direct3D9.Mesh myMesh = new SlimDX.Direct3D9.Mesh(myDevice, faceCount, vertexCount, SlimDX.Direct3D9.MeshFlags.SystemMemory, SlimDX.Direct3D9.VertexFormat.Position);

            SlimDX.DataStream indexBuffer = myMesh.LockIndexBuffer(SlimDX.Direct3D9.LockFlags.None);
            SlimDX.DataStream vertexBuffer = myMesh.LockVertexBuffer(SlimDX.Direct3D9.LockFlags.None);

            for (int i = 0; i < myMesh.VertexCount; i++)
            {
                vertexBuffer.Write<SlimDX.Vector3>(vertexList);
               
            }


            for (int i = 0; i < myMesh.FaceCount; i++)
            {
                ushort index;
                index = (ushort)triangleList.X;
                indexBuffer.Write<ushort>(index);

                index = (ushort)triangleList.Y;
                indexBuffer.Write<ushort>(index);

                index = (ushort)triangleList.Z;
                indexBuffer.Write<ushort>(index);
            }

            
            myMesh.GenerateAdjacency(0.5f);
            SlimDX.Direct3D9.Mesh meshOptimizada = myMesh.Optimize(SlimDX.Direct3D9.MeshOptimizeFlags.StripReorder);

Advertisement

I've seen access violations like this occur when the index buffer contains data that is out of the range of your vertices. Since you haven't unlocked your buffers after writing data to them, I'm guessing the data hasn't actually been committed yet. You'll need to call Unlock() on both of your buffers before you call any of those other functions.

Also, if your vertex and index buffer formats match the data in your lists, you should be able to use vertexBuffer.WriteRange(vertexList) and indexbuffer.WriteRange(trianglelist) instead of having to do each item individually in a loop.

Finally, its good practise to test your return values, rather than just blindly use the result that's passed back.

[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler

This topic is closed to new replies.

Advertisement