Sign in to follow this  
jor1980

[SlimDX]access violation exception on ComputeNormals

Recommended Posts

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


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

            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);
Edited by jor1980

Share this post


Link to post
Share on other sites

@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.

Share this post


Link to post
Share on other sites

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


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this