943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3982
  • C++ RSS
Jan 29th, 2008
0

DirectX and C++ DrawIndexedPrimitive question

Expand Post »
Here is my code of the function in questiion:

C++ Syntax (Toggle Plain Text)
  1. void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device, LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer, LPDIRECT3DINDEXBUFFER9 p_dx_IndexBuffer)
  2. {
  3. p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,60), 1.0f, 0);
  4. p_dx_Device->BeginScene();
  5.  
  6. p_dx_Device->SetStreamSource(0, p_dx_VertexBuffer, 0, sizeof(OURCUSTOMVERTEX));
  7. p_dx_Device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
  8. p_dx_Device->SetIndices(p_dx_IndexBuffer);
  9.  
  10. D3DXMATRIX m_Rotation;
  11. D3DXMatrixRotationZ(&m_Rotation, flt_Angle);
  12. D3DXMATRIX m_Translation;
  13. D3DXMatrixTranslation(&m_Translation, 32, -32, 0);
  14.  
  15. D3DXMATRIX m_World;
  16. D3DXMatrixMultiply(&m_World, &m_Translation, &m_Rotation);
  17. p_dx_Device->SetTransform(D3DTS_WORLD, &m_World);
  18.  
  19. p_dx_Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, int_VertCount, 0, int_PrimCount);
  20.  
  21. p_dx_Device->EndScene();
  22. p_dx_Device->Present(NULL, NULL, NULL, NULL);
  23. }

the following line is the error line:

C++ Syntax (Toggle Plain Text)
  1. p_dx_Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, int_VertCount, 0, int_PrimCount);

when i build it, no errors occur, however upon running i get:

C++ Syntax (Toggle Plain Text)
  1. Unhandled exception at 0x4ff4f522 in testdx.exe: 0xC0000005: Access violation reading location 0x00231ad8.

now i have done a little debugging already. i have found that none of my variables or pointers to variables in my entire program are set at memory location 0x00231ad8, or any where close for that matter. I bring up the memory listing for that area, and there are "?" all over.... the closest variable that the function DrawIndexedPrimitive should use (and that my program includes) is at 0x002197c0, and none of the relevant variables would indicate any entries out of the range of that vector.

my question is more along the lines of "why is it trying to access this memory location if none of my pointers or variables indicate values needed are there? and how do i solve this annoying little error?" i am at wits end.

Any Help would be much appreciated.
Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
loushou is offline Offline
11 posts
since Jan 2008
Jan 29th, 2008
0

Re: DirectX and C++ DrawIndexedPrimitive question

The functions you call are probably attempting to access that memory location because of bad arguments you send it. Check the value of pointers such as p_dx_Device to make sure they are valid and the value of the parameters sent to make sure they are correct. for example what is the value of int_VertCount ? Is it a valid value ?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jan 29th, 2008
0

Re: DirectX and C++ DrawIndexedPrimitive question

The functions you call are probably attempting to access that memory location because of bad arguments you send it. Check the value of pointers such as p_dx_Device to make sure they are valid and the value of the parameters sent to make sure they are correct. for example what is the value of int_VertCount ? Is it a valid value ?
int_vertCount = 5
int_PrimCount = 3

p_dx_Device is a object created by the DirectX function Direct3DCreate9::CreateDevice and is populated properly as well as passed properly.

the DrawIndexedPrimitive function syntax is:

C++ Syntax (Toggle Plain Text)
  1. DrawIndexedPrimitive(
  2. D3DPRIMITIVETYPE Type,
  3. INT BaseVertexIndex,
  4. UINT MinIndex,
  5. UINT NumVertices,
  6. UINT StartIndex,
  7. UINT PrimitiveCount
  8. );

according to the SDK.

Below is the code where the values are stored for the Vertexs and Indices:

C++ Syntax (Toggle Plain Text)
  1. int LoadIndicesAndVerts(HWND han_Window, LPDIRECT3DDEVICE9 p_dx_Device, char *s_fName, LPDIRECT3DVERTEXBUFFER9 &p_VertBuf, LPDIRECT3DINDEXBUFFER9 &p_IndBuf)
  2. {
  3. OCVArray p_verts;
  4. SArray p_Inds;
  5.  
  6. int i_errorRetruned = LoadVerts(s_fName, p_verts, p_Inds, int_VertCount, int_TotalInd);
  7. int_PrimCount = int_TotalInd/3;
  8.  
  9. // create the vertex buffer variable
  10. LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;
  11.  
  12. // try to initialize the vertexbuffer otherwise pop error
  13. if (FAILED(p_dx_Device->CreateVertexBuffer(int_VertCount*sizeof(OURCUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL)))
  14. {
  15. MessageBox(han_Window, "Error While Creating VertexBuffer.", "FillVertices()", MB_OK);
  16. }
  17.  
  18. // declare pointer for pointer in memory where vertices will be stored for d3d to find them
  19. // declare pointer for pointer in memory where vertices will be stored for d3d to find them
  20. VOID* p_Vertices;
  21.  
  22. // try to lock the memory location so other progs cannot access while copying vertices to it or pop error
  23. if(FAILED(p_dx_VertexBuffer->Lock(0, int_VertCount*sizeof(OURCUSTOMVERTEX), (void**)&p_Vertices, 0)))
  24. {
  25. MessageBox(han_Window, "Error while trying to lock memory for Verts.", "FillVertices()", MB_OK);
  26. }
  27. else
  28. {
  29. // copy verts from array to memory and return pointer to location in memory to p_Vertices
  30. memcpy(p_Vertices, &p_verts, int_VertCount*sizeof(OURCUSTOMVERTEX));
  31. // unlock allocated memory area
  32. p_dx_VertexBuffer->Unlock();
  33. }
  34.  
  35. p_VertBuf = p_dx_VertexBuffer;
  36.  
  37. // create the index buffer and if it does not work pop an error
  38. LPDIRECT3DINDEXBUFFER9 p_dx_IndexBuffer;
  39. if (FAILED(p_dx_Device->CreateIndexBuffer(int_TotalInd*sizeof(short), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &p_dx_IndexBuffer, NULL)))
  40. {
  41. MessageBox(han_Window, "Error creating the IndexBuffer.", "FillIndices()", MB_OK);
  42. }
  43.  
  44. // store index buffer in dx managed memory by locking, storing, unlocking for safe copy
  45. VOID* p_Indices;
  46. if(FAILED(p_dx_IndexBuffer->Lock(0, int_TotalInd*sizeof(short), (void**)&p_Indices, 0)))
  47. {
  48. MessageBox(han_Window, "Error while trying to Lock memory for copy.", "FillIndices()", MB_OK);
  49. }
  50. else
  51. {
  52. memcpy(p_Indices, &p_Inds, int_TotalInd*sizeof(short));
  53. p_dx_IndexBuffer->Unlock();
  54. }
  55.  
  56. p_IndBuf = p_dx_IndexBuffer;
  57.  
  58. return 0;
  59. }

the LoadVerts function is a function solely for the purpose of extracting the vertex information from my custom file format (which is working properly). The file being loaded is a simple 3 triangle file for testing.... I am befuddled.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
loushou is offline Offline
11 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Applet calling C++ Code
Next Thread in C++ Forum Timeline: postfix/prefix notations





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC