   [Help] need help on axis aligned bounding box
  First I load a X.file and then set it's position (0.0f, 0.0f, 0.0f) into world space.Second i create a function "void aabb(LPD3DXMESH &mesh)" to calculate bounding box's min and max and then call the function  "void renderbox(LPDIRECT3DVERTEXBUFFER9 &vertexbuffer, LPDIRECT3DINDEXBUFFER9 &indexbuffer, LPD3DXMESH &mesh)"  to draw axis aligned bounding box. the z axis is fit on mesh but both X axis and Y axis just not fit on the mesh the picture like this picture1picture2 i couldn't figure it out where is the problem for many dayscan anyone help me function code is under belowthis is the vertex structurestruct ver{float _x,_y,_z;ver(float x, float y, float z){_x=x;_y=y;_z=z;}};/////////////////////////////////////////////////////////////////////////////this function is to present the scene//////////////////////////////////////function renderpoint()/////////////////void renderpoint(){//rotation and moveD3DXMATRIX world;D3DXMatrixIdentity(&world);D3DXMATRIX rx,ry,movey;degree+=0.01f;unit+=0.1f;D3DXMatrixRotationX(&rx,180*(D3DX_PI/180));D3DXMatrixRotationY(&ry,degree);D3DXMatrixTranslation(&movey, 0.0f, 0.0f, 0.0f);world=rx*ry*movey;d3dd->SetTransform(D3DTS_WORLD, &world);//clear the scened3dd->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0,0,0,0), 1.0f, 0);//start renderingd3dd->BeginScene();//draw meshfor(DWORD i=0; i<numofmaterial; i++){d3dd->SetTexture(0, texture);d3dd->SetMaterial(&meshmaterial);mesh->DrawSubset(i);}   //call aabb function to calculate bounding box and draw it upaabb(mesh);//end renderingd3dd->EndScene();//present the scene (show backbuffer)d3dd->Present(NULL, NULL, NULL, NULL);}///////////////////////////////////////////////////////////////////////////////////////////////////////function aabb/////////////////////////////////////void aabb(LPD3DXMESH &mesh){D3DXVECTOR3 minvertex(0.0f, 0.0f, 0.0f);D3DXVECTOR3 maxvertex(0.0f, 0.0f, 0.0f);LPDIRECT3DVERTEXBUFFER9 vertexbuffer=0;LPDIRECT3DINDEXBUFFER9 indexbuffer=0;DWORD numofvertex;//number of vertices in mesh   d3dd->CreateVertexBuffer(8*sizeof(D3DXVECTOR3), 0,D3DFVF_XYZ, D3DPOOL_DEFAULT, &vertexbuffer, NULL); d3dd->CreateIndexBuffer(24*sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexbuffer, NULL);numofvertex=mesh->GetNumVertices();D3DXVECTOR3 *newvertex=new D3DXVECTOR3[numofvertex];   //extract each vertices from mesh then store in newvertex arrayD3DXVECTOR3* ptr=NULL;mesh->LockVertexBuffer(0, (void**)&ptr);memcpy(newvertex, ptr, sizeof(*ptr)*numofvertex);   //find the min and max vertexfor(DWORD i=0; i<numofvertex; i++){minvertex.x=min(minvertex.x, newvertex.x);minvertex.y=min(minvertex.y, newvertex.y);minvertex.z=min(minvertex.z, newvertex.z);maxvertex.x=max(maxvertex.x, newvertex.x);maxvertex.y=max(maxvertex.y, newvertex.y);maxvertex.z=max(maxvertex.z, newvertex.z);}mesh->UnlockVertexBuffer();   //setup 8 vertices of bounding box ver* vertex;vertexbuffer->Lock(0, 0, (void**)&vertex, 0);vertex[0]=ver(minvertex.x, minvertex.y, minvertex.z);vertex[1]=ver(minvertex.x, maxvertex.y, minvertex.z);vertex[2]=ver(minvertex.x, maxvertex.y, maxvertex.z);vertex[3]=ver(minvertex.x, minvertex.y, maxvertex.z);vertex[4]=ver(maxvertex.x, maxvertex.y, maxvertex.z);vertex[5]=ver(maxvertex.x, maxvertex.y, minvertex.z);vertex[6]=ver(maxvertex.x, minvertex.y, minvertex.z);vertex[7]=ver(maxvertex.x, minvertex.y, maxvertex.z);vertexbuffer->Unlock();//setup indexWORD* index;indexbuffer->Lock(0, 0, (void**)&index, 0);//side of minindex[0]=0; index[1]=1;index[2]=1; index[3]=2;index[4]=2; index[5]=3;index[6]=0; index[7]=3;//side of maxindex[8]=4; index[9]=5;index[10]=5; index[11]=6;index[12]=6; index[13]=7;index[14]=7; index[15]=4;index[16]=0; index[17]=6;index[18]=1; index[19]=5;index[20]=4; index[21]=2;index[22]=7; index[23]=3;indexbuffer->Unlock();   //call render box functionrenderbox(vertexbuffer, indexbuffer, mesh);indexbuffer->Release();vertexbuffer->Release();}/////////////////////////////////////////////////////////////////////////////this function only draw vertices on backbuffer not present yet////////////////////////////////function renderbox()////////////////////////void renderbox(LPDIRECT3DVERTEXBUFFER9 &vertexbuffer, LPDIRECT3DINDEXBUFFER9 &indexbuffer, LPD3DXMESH &mesh){d3dd->SetStreamSource(0, vertexbuffer, 0, sizeof(ver));d3dd->SetFVF(mesh->GetFVF());d3dd->SetIndices(indexbuffer);d3dd->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 8, 0, 12);} /////////////////////////////////////////////////////////////////////////////[Edited by - tomneo2004 on June 17, 2009 4:49:54 AM]
  First please read this.Second, I didn't thoroughly read your code but there's one part that might cause problems:You initialize min and max with (0,0,0).Now, what happens with minvertex.x=min(minvertex.x, newvertex.x); if all vertices of the model have x > 0? minvertex.x will still be 0.The same is true for the other coordinates as well as for maxvertex (the other way round).You should better initialize them either to the highest/lowest possible values (e.g. D3DXVECTOR3 minvertex(<max float>, <max float>, <max float>); and D3DXVECTOR3 maxvertex(<min float>, <min float>, <min float>);) or to the values of the first vertex in your mesh.;Quote:Original post by Lord_EvilFirst please read this.Second, I didn't thoroughly read your code but there's one part that might cause problems:You initialize min and max with (0,0,0).Now, what happens with minvertex.x=min(minvertex.x, newvertex.x); if all vertices of the model have x > 0? minvertex.x will still be 0.The same is true for the other coordinates as well as for maxvertex (the other way round).You should better initialize them either to the highest/lowest possible values (e.g. D3DXVECTOR3 minvertex(<max float>, <max float>, <max float>); and D3DXVECTOR3 maxvertex(<min float>, <min float>, <min float>);) or to the values of the first vertex in your mesh.thank you for advicei set the minvertex and maxvertex to the first point of coordinate in the mesh but the result is still as same as the picture i posted.i also trace value on minvertex and maxvertex and i found the result of value  after comparesionminvertex.x=-1.5041499minvertex.y-1.5184798minvertex.z=-1.5102599maxvertex.x=1.5184798maxvertex.y=1.5136799maxvertex.z=1.5050999the mesh is the tiger i get it from media folder under Directx sdk 2009. The bounding box still not fit on mesh, i mean the shape of bounding box is suppose to be rectangular which precise cover each side of mesh not the square like picture. is there any logical error in my code?   ;  Im not a DX Expert but mesh->LockVertexBuffer(0, (void**)&ptr); seems to return a pointer to the entire vertex data of the mesh, not just the positions. It might include interleaved normals, colors and texture coordinates.So you probably copy part of the other data into your newvertex array and miss some if not most of the real positions. Simple Example:If the vertex data is stored as pos1, normal1, pos2, normal2, ...copying sizeof(D3DXVECTOR3) * 3 bytes into a D3DXVECTOR3 array would end up in the array being: pos1, normal1, pos2 instead of pos1, pos2, pos3And it gets worse if non-3-component data is included, e.g. 2d texture coordinates or 4-component colors. Then some data might be a mix of, say texture coords and normals.Example:Vertex buffer: pos1 (3d), tex1 (2d), normal1 (3d), ...Reading 3d vectors ends up in:pos1, ax mixture of tex1 and normal (i.e. tex1.x/tex1.y/normal1.x)....   ;Quote:Original post by Lord_EvilIm not a DX Expert but mesh->LockVertexBuffer(0, (void**)&ptr); seems to return a pointer to the entire vertex data of the mesh, not just the positions. It might include interleaved normals, colors and texture coordinates.So you probably copy part of the other data into your newvertex array and miss some if not most of the real positions. Simple Example:If the vertex data is stored as pos1, normal1, pos2, normal2, ...copying sizeof(D3DXVECTOR3) * 3 bytes into a D3DXVECTOR3 array would end up in the array being: pos1, normal1, pos2 instead of pos1, pos2, pos3And it gets worse if non-3-component data is included, e.g. 2d texture coordinates or 4-component colors. Then some data might be a mix of, say texture coords and normals.Example:Vertex buffer: pos1 (3d), tex1 (2d), normal1 (3d), ...Reading 3d vectors ends up in:pos1, ax mixture of tex1 and normal (i.e. tex1.x/tex1.y/normal1.x)....Are there any way that i can get the each vertices position from mesh without normal and color information? i open the mesh file and look through it, the mesh file seem have color, normal and texture coordinate and so on. i guess i used wrong way to get each vertex position. Does DirectX provide any function to get the vertex position from X.file?   ;  You might create a vertex structure whose format matches the vertexbuffer's layout and read the whole data into that structure. Then loop over each vertex and extract the position only.As said I'm no DX Expert since I switched over to OpenGL years ago so I don't really know the API in depth. You might be able to read the positions one by one from the vertex buffer but I doubt it will result in a decent performance.   ;Quote:Original post by Lord_EvilYou might create a vertex structure whose format matches the vertexbuffer's layout and read the whole data into that structure. Then loop over each vertex and extract the position only.As said I'm no DX Expert since I switched over to OpenGL years ago so I don't really know the API in depth. You might be able to read the positions one by one from the vertex buffer but I doubt it will result in a decent performance.thank you very much the problem has solved. Now the bounding box is fit on the mesh. the problem it couldn't fit on the mesh is when vertex data loaded from mesh is x,y,z,u,v but i use structure x,y,z so it goes totly wrong after that  i rewrite the structure then it is ok now but thank you.   ;  Bu yong Xie [smile]   ;Quote:Original post by Lord_EvilBu yong Xie [smile]what is that but Danke