I load a mesh and draw it up, also rotate it. Now i create a function that can create a ray to detect if cursor on the mesh every time, but it is not working(if it is working a message will show up) what is wrong with my code? can anyone help me?
Buy the way, i refer to this websit
click_here
my project
here
below is my code
void calculateray(LPD3DXMESH &mesh)
{
POINT cursorpos;
//get cursor pos
GetCursorPos(&cursorpos);
//get cursor position within window
ScreenToClient(get_hwndhandler(), &cursorpos);
float dx=0.0f;
float dy=0.0f;
D3DXVECTOR3 origin,objorigin;
D3DXVECTOR3 dir,objdir;
D3DXMATRIX project,view,rview,world,rworld;
// get the matrix of projection
d3dd->GetTransform(D3DTS_PROJECTION, &project);
// transform cursor position to projection
dx=(((2.0f*cursorpos.x)/800)-1.0f)/project(1,1);
dy=-(((2.0f*cursorpos.y)/600)-1.0f)/project(2,2);
//in dir's z axis i set it to 1.0f
origin=D3DXVECTOR3(0.0f,0.0f,0.0f);
dir=D3DXVECTOR3(dx,dy,1.0f);
//get inverse matrix of view
d3dd->GetTransform(D3DTS_VIEW, &view);
D3DXMatrixInverse(&rview, NULL, &view);
//after i get inverse matrix of view, i apply to dir and origin vector.
//mean of this is to translate dir and origin to world space
dir.x=dir.x*rview._11 + dir.y*rview._21 + dir.z*rview._31;
dir.y=dir.y*rview._12 + dir.y*rview._22 + dir.z*rview._32;
dir.z=dir.z*rview._13 + dir.y*rview._23 + dir.z*rview._33;
origin.x=rview._41;
origin.y=rview._42;
origin.z=rview._43;
//get inverse matrix of world
d3dd->GetTransform(D3DTS_WORLD, &world);
D3DXMatrixInverse(&rworld, NULL, &world);
//after i get inverse matrix of world, i translate origin and dir
// to model's space, also normalize dir
//buy the way, objorigin and objdir are new vectors, i use those
// to store the origin and dir in model's space
D3DXVec3TransformCoord(&objorigin, &origin, &rworld);
D3DXVec3TransformNormal(&objdir, &dir, &rworld);
D3DXVec3Normalize(&objdir, &objdir);
//calculate intersection with mesh
BOOL hit;
DWORD fi;
float u,v, dist;
D3DXIntersect(mesh, &origin, &dir, &hit, &fi, &u, &v, &dist, NULL, NULL);
// if hit is true than show message box
if(hit)
MessageBox(0,"hit","hit test", 0);
}
here is the function setup and load the mesh
void setup()
{
//get the d3d device
d3dd=getd3ddevice();
HRESULT hr=0;
// load mesh
hr=D3DXLoadMeshFromX("tiger.x", D3DXMESH_VB_MANAGED, d3dd, &adjbuffer, &materialbuffer, NULL, &numofmaterial, &mesh);
if(hr!=S_OK)
{
MessageBox(0, "load mesh fail", "waring", 0);
meshload=false;
}
if(meshload==true)
{
material=(D3DXMATERIAL*)materialbuffer->GetBufferPointer();
meshmaterial=new D3DMATERIAL9[numofmaterial];
texture=new LPDIRECT3DTEXTURE9[numofmaterial];
for(DWORD i=0; i < numofmaterial; i++)
{
meshmaterial.Ambient=meshmaterial.Diffuse;
meshmaterial=material.MatD3D;
D3DXCreateTextureFromFile(d3dd, material.pTextureFilename, &texture);
}
adjbuffer->Release();
materialbuffer->Release();
}
//position camera
D3DXVECTOR3 eye(0.0f,0.0f,-5.0f);
D3DXVECTOR3 lookat(0.0f,0.0f,0.0f);
D3DXVECTOR3 up(0.0f,1.0f,0.0f);
D3DXMATRIX v;
D3DXMatrixLookAtRH(&v, &eye, &lookat, &up);
d3dd->SetTransform(D3DTS_VIEW, &v);
//projection
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovRH(&proj, -(float)D3DX_PI*0.5f, (float)800/(float)600, 1.0f, 1000.0f);
d3dd->SetTransform(D3DTS_PROJECTION, &proj);
//setup rendering state
d3dd->SetRenderState(D3DRS_LIGHTING,false);
d3dd->SetRenderState(D3DRS_ZENABLE,true);
d3dd->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
}
Buy the way, what are these mean:
meshmaterial
.Ambient=meshmaterial.Diffuse;
meshmaterial=material.MatD3D;
[Edited by - tomneo2004 on June 26, 2009 4:55:41 AM]