Jump to content
  • Advertisement
Sign in to follow this  
majc

Loop through vertices

This topic is 4991 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

I made this code to normalize my mesh but when i run it gives a memory problem. I dont know why. Thx in advance!! .cpp file: #include "my_mesh.h" my_mesh::my_mesh() { } my_mesh::~my_mesh() { } void my_mesh::normalize_mesh() { vertex *vertices; float minX; float minY; float minZ; float maxX; float maxY; float maxZ; float middle_x; float middle_y; float middle_z; minX = 1e10; minY = 1e10; minZ = 1e10; maxX = -1e10; maxY = -1e10; maxZ = -1e10; // lock the vertex buffer hr=pMesh->LockVertexBuffer(D3DLOCK_READONLY, (BYTE**)&vertices); ULONG numVerts=pMesh->GetNumVertices(); pMesh->GetFVF(); // loop through the vertices for (DWORD i=0;i<numVerts;i++) { if(vertices.x < minX) { minX=vertices.x; } if(vertices.y < minY) { minY=vertices.y; } if(vertices.z < minZ) { minZ=vertices.z; } if(vertices.x > maxX) { maxX=vertices.x; } if(vertices.y > maxY) { maxY=vertices.y; } if(vertices.z > maxZ) { maxZ=vertices.z; } } middle_x=(minX + maxX)/2; middle_y=(minY + maxY)/2; middle_z=(minZ + maxZ)/2; m_Object.Move(middle_x-middle_x,middle_y-middle_y,middle_z-middle_z); pMesh->UnlockVertexBuffer(); } .h file: #ifndef my_mesh_h #define my_mesh_h #include "Core_Global.h"// used because of cMesh and cObject class my_mesh { private: BYTE *ptr; HRESULT hr; D3DXVECTOR3 vOff; ID3DXMesh *pMesh; D3DXVECTOR3 *offset; cMesh m_Mesh; cObject m_Object; struct vertex { FLOAT x, y, z; FLOAT nx,ny,nz; DWORD colour; }; public: my_mesh(); ~my_mesh(); void normalize_mesh(); }; #endif

Share this post


Link to post
Share on other sites
Advertisement
most probably has something to do with this line:


// lock the vertex buffer
//hr=pMesh->LockVertexBuffer(D3DLOCK_READONLY, (BYTE**)&vertices);


you've commentated it out so the pointer verticies is probably initialized with some random address or 0.

Share this post


Link to post
Share on other sites
i only commented after the problem to see whats happening.
i think the problem is in:
hr=pMesh->LockVertexBuffer(D3DLOCK_READONLY, (BYTE**)&vertices);
and pMesh->UnlockVertexBuffer();

Share this post


Link to post
Share on other sites
i don't use direct3d, but i looked-up d3d documentation, you should check if LockVertexBuffer returns D3D_OK or D3DERR_INVALIDCALL before doing your access :


if(pMesh->LockVertexBuffer(D3DLOCK_READONLY, &vertices) == D3DERR_INVALIDCALL)
//error handling code here


also LockVertexBuffer takes a pointer to void pointer, in c++ there is no need to cast to void pointers.

Share this post


Link to post
Share on other sites
also looking at the code given carefully pMesh is also not initialized properly and either refers to some random location in memory or is zero.

Share this post


Link to post
Share on other sites
I don't know anything about direct3d but refering to MSDN docs i found this:

Quote:

To obtain the ID3DXMesh interface, call either the D3DXCreateMesh or D3DXCreateMeshFVF function.


find out more from here

when using pointers before you can actually use it must have some correct real address and the memory it refers to to be or still be there.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!