------------------------
Pick. h
------------------------
# Pragma once
# Include <vector>
Using STD: vector;
struct pickresult
{< br> DWORD dwface; // mesh face that was intersected
float fbary1, fbary2; // Barycentric coords of intersection
float fdist; // distance from Ray origin to intersection
float tu, TV; // texture coords of intersection
renderobject * object;
float3 hitpoint;
};
Struct d3dvertex
{
Float3 P;
Float3 normal;
Float tu, TV;
Static const DWORD fvf; // flexible vertex format
};
Class pick
{
Public:
Pick (void );
~ Pick (void );
Vector <pickresult> excute (INT mousex, int Mousey, Camera * camer );
Void setmesh (vector <renderobject *> objects );
PRIVATE:
Vector <renderobject *> normalobjects;
Hresult createray (INT mousex, int Mousey );
Vector <pickresult> m_intersectionarray;
Float3 vpickraydir; // The direction vector of the ray.
Float3 vpickrayorig; // the starting point of the ray.
Camera * m_pcamera;
};
----------------------
Pick. cpp
----------------------
# Include "stdafx. H"
# Include "pick. H"
# Define max_intersections 16
Pick: Pick (void)
{
}
Pick ::~ Pick (void)
{
}
Void pick: setmesh (vector <renderobject *> objects)
{
Normalobjects = objects;
}
Hresult pick: createray (INT mousex, int Mousey)
{
// Obtain the Projection Matrix
Float4x4 matproj = m_pcamera-> getmatproj ();
// Calculates the coordinates from the screen coordinate to the projection matrix coordinate.
Float3 V;
Float width = d3dsystem: getinstance ()-> getdevicewidth ();
Float Height = d3dsystem: getinstance ()-> getdeviceheight ();
V. x = (2.0f * mousex)/width)-1)/matproj. _ 11;
V. Y =-(2.0f * Mousey)/height)-1)/matproj. _ 22;
V. z = 1.0f;
// Obtain the inverse matrix of the view Matrix
Float4x4 matview = m_pcamera-> getmatview ();
Float4x4 m;
D3dxmatrixinverse (& M, null, & matview); // calculate the inverse matrix
D3dxvec3transformnormal (& vpickraydir, & V, & M );
D3dxvec3normalize (& vpickraydir, & vpickraydir );
Float3 o = float3 (0, 0 );
D3dxvec3transformcoord (& vpickrayorig, & O, & M); // (X, Y, Z, 1): W = 1
Return s_ OK;
}
Bool sortfunction (const pickresult & r0, const pickresult & R1)
{
Return r0.fdist <r1.fdist? True: false;
}
Vector <pickresult> pick: excute (INT mousex, int Mousey, Camera * camer)
{
M_pcamera = camer;
M_intersectionarray.clear (); // clear the pick-up record
Createray (mousex, Mousey );
Hresult hR = s_ OK;
DWORD * pindices;
Float U, V, fdist;
For (INT I = 0; I <normalobjects. Size (); I ++)
{
Objectframe * frame = normalobjects [I]-> getobjectframe ();
Int numvertex = frame-> numvertex;
Int numindex = frame-> numindex;
DWORD dwnumfaces = frame-> dwnumfaces;
If (1> numindex)
{
Continue;
}
Pindices = New DWORD [numindex];
Int maxword = 1 <16;
If (numvertex <maxword)
{
For (INT I = 0; I <numindex; I ++)
{
Pindices [I] = (word *) (frame-> pindex) [I];
}
}
Else
{
For (INT I = 0; I <numindex; I ++)
{
Pindices [I] = frame-> pindex [I];
}
}
For (DWORD f = 0; F <dwnumfaces; F ++)
{
Float3 vert [3];
For (Int J = 0; j <3; j ++)
{
// Use a matrix to transform a three-dimensional vector outv srcv matri
Float3 V0 = frame-> pvertex [pindices [3 * F + J]. P;
D3dxvec3transformcoord (& vert [J], & v0, & normalobjects [I]-> gettransform ());
}
If (d3dxintersecttri (& vert [0], & vert [1], & vert [2], & vpickrayorig, & vpickraydir, & U, & V, & fdist) & fdist> = 0)
{
Pickresult intersection;
Intersection. dwface = F;
Intersection. fbary1 = u;
Intersection. fbary2 = V;
Intersection. fdist = fdist;
Intersection. Object = normalobjects [I];
Intersection. hitpoint = vert [0] + (VERT [1]-Vert [0]) * u + (VERT [2]-Vert [0]) * V;
M_intersectionarray.push_back (intersection );
}
}
Safe_delete_array (pindices );
}
STD: Sort (m_intersectionarray.begin (), m_intersectionarray.end (), sortfunction );
Return m_intersectionarray;
}