Sign in to follow this  

[SlimDX] Optimize mesh

This topic is 1763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i]);
               
            }


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

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

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

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

 

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this