[SlimDX]access violation exception on ComputeNormals

Started by
3 comments, last by jor1980 11 years ago

Hi first of all i want to apollogize because i did this question before on the wrong forum.

My problem is the next, i am creating a mesh with vertex and faceslist, then i call to compute normals and i get an access violation exception, you are trying to read or write in protected memory, i don´t know how to solve it.

the error is send in this line : cloneMesh.ComputeNormals();

This is my code:


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.Managed, SlimDX.Direct3D9.VertexFormat.Position|SlimDX.Direct3D9.VertexFormat.Texture1|SlimDX.Direct3D9.VertexFormat.Normal);

            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);
                vertexBuffer.Write<SlimDX.Vector2>(uvListFifa13);
                vertexBuffer.Seek(12, SeekOrigin.Current);
            }


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

            SlimDX.Direct3D9.Mesh cloneMesh = myMesh.Clone(myDevice, SlimDX.Direct3D9.MeshFlags.Dynamic, SlimDX.Direct3D9.VertexFormat.Position|SlimDX.Direct3D9.VertexFormat.Normal|SlimDX.Direct3D9.VertexFormat.Texture1);
            cloneMesh.ComputeNormals();
            cloneMesh.GenerateAdjacency(0.5f);
            SlimDX.Direct3D9.Mesh meshOptimizada = myMesh.Optimize(SlimDX.Direct3D9.MeshOptimizeFlags.StripReorder);
Advertisement

include your code for ComputeNormals() you might be tring to access a value past the size of an array, which would be incorrect RAM management

@Weeve, ComputeNormals() is actually a SlimDX API call.

As I posted in the other thread you created:

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.

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

Hi, i have unlocked now the vertex and index buffer and i still have the same access violation, Do you have any other clue about this?

Here is my actual code:


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.Managed, SlimDX.Direct3D9.VertexFormat.Position|SlimDX.Direct3D9.VertexFormat.Texture1|SlimDX.Direct3D9.VertexFormat.Normal);

            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);
                vertexBuffer.Write<SlimDX.Vector2>(uvListFifa13);
                vertexBuffer.Seek(12, SeekOrigin.Current);
            }


            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.UnlockVertexBuffer();
            myMesh.UnlockIndexBuffer();

            SlimDX.Direct3D9.Mesh cloneMesh = myMesh.Clone(myDevice, SlimDX.Direct3D9.MeshFlags.Dynamic, SlimDX.Direct3D9.VertexFormat.Position|SlimDX.Direct3D9.VertexFormat.Normal|SlimDX.Direct3D9.VertexFormat.Texture1);
            
            cloneMesh.GenerateAdjacency(0.5f);
            cloneMesh.ComputeNormals();
            
            SlimDX.Direct3D9.Mesh meshOptimizada = myMesh.Optimize(SlimDX.Direct3D9.MeshOptimizeFlags.StripReorder);

Now is solved, the problem was at mi index buffer it had wrog data

This topic is closed to new replies.

Advertisement