struct Model_t
{
D3DXVECTOR3 Position2D;
D3DXVECTOR3 Position3D;
BOOL bVisible;
FLOAT DepthDistance;
};
std::vector<Model_t*>ModelList;
ULONG CurrentTexture[5];
FLOAT GetDistance(float Xx,float Yy,float xX,float yY)
{
return sqrt((yY-Yy)*(yY-Yy)+(xX-Xx)*(xX-Xx));
}
VOID AddModel(IDirect3DDevice9* Device,UINT NumVertices)
{
D3DVIEWPORT9 Viewport;
D3DXMATRIX ViewProjectionMatrix,LocalToWorld,WorldToLocal;
IDirect3DVertexBuffer9 *pStreamData;
IDirect3DIndexBuffer9 *pIndexData;
Model_t* pModel = new Model_t;
VOID* pIndex,*pData;
UINT OffsetInBytes,Stride;
Device->GetIndices(&pIndexData);
Device->GetViewport(&Viewport);
Device->GetVertexShaderConstantF(0,ViewProjectionMatrix,4);
Device->GetVertexShaderConstantF(230,LocalToWorld,4);
D3DXVECTOR3 VectorMiddle(0,0,0),ScreenMiddle(Viewport.Width/2,Viewport.Height/2,0);
D3DXMatrixIdentity(&WorldToLocal);
D3DXVec3Unproject(&VectorMiddle,&ScreenMiddle,&Viewport,&ViewProjectionMatrix,&LocalToWorld,&WorldToLocal);
if(pIndexData->Lock(0, 0, &pIndex, D3DLOCK_READONLY) == D3D_OK)
{
Device->GetStreamSource(0, &pStreamData, &OffsetInBytes, &Stride);
if(pStreamData->Lock(0, 0,&pData, D3DLOCK_READONLY) == D3D_OK)
{
D3DXVECTOR3 BoxMin,BoxMax;
D3DXComputeBoundingBox((LPD3DXVECTOR3)pData,NumVertices,Stride,&BoxMin,&BoxMax);
pModel->Position3D = BoxMin - ((BoxMin - BoxMax)/2);
}
}pIndexData->Unlock();pIndexData->Release();pStreamData->Unlock();pStreamData->Release();
D3DXVec3Project(&pModel->Position2D,&pModel->Position3D,&Viewport,&ViewProjectionMatrix,&LocalToWorld,&WorldToLocal);
pModel->DepthDistance = GetDistance(VectorMiddle.x,VectorMiddle.y,pModel->Position3D.x,pModel->Position3D.y);
if(pModel->Position2D.x > Viewport.Width || pModel->Position2D.x < 0 || pModel->Position2D.y > Viewport.Height || pModel->Position2D.y < 0 || (float)pModel->Position2D.z > 1.0f || (float)pModel->Position2D.z < 0.9f)
{
pModel->bVisible = D3DZB_FALSE;
}else{
pModel->bVisible = D3DZB_TRUE;
}
ModelList.push_back(pModel);
}
VOID ProcessTargets(IDirect3DDevice9* Device)
{
if(ModelList.size() != NULL)
{
for(int i = 0; i < ModelList.size(); i++)
{
if(ModelList[i]->bVisible)
{
char bEspString[MAX_PATH];
sprintf(bEspString,"-=|%.1f|=-",ModelList[i]->DepthDistance);
iD3DFont.Draw((int)ModelList[i]->Position2D.x - (strlen(bEspString)/2*5),(int)ModelList[i]->Position2D.y,bEspString,D3DCOLOR_XRGB(50,100,255));
}
}
ModelList.clear();
}
}
HRESULT (WINAPI* pEndScene)(LPDIRECT3DDEVICE9);
HRESULT WINAPI myEndScene(LPDIRECT3DDEVICE9 pDevice)
{
if(ResourceLoaded==1)
{
ProcessTargets(pDevice);
}
return pEndScene(pDevice);
}
HRESULT (WINAPI* pSetTexture)(LPDIRECT3DDEVICE9,DWORD,IDirect3DBaseTexture9*);
HRESULT WINAPI mySetTexture(LPDIRECT3DDEVICE9 pDevice,DWORD Stage, IDirect3DBaseTexture9 *pTexture)
{
if(pTexture != NULL && pTexture->GetType() == D3DRTYPE_TEXTURE && pTexture != TextureBlue && pTexture != TextureRed)
{
ULONG* pTex = (ULONG*)pTexture;//modelrec...
CurrentTexture[0] = pTex[8];
CurrentTexture[1] = pTex[9];
CurrentTexture[2] = pTex[35];
}
return pSetTexture(pDevice,Stage, pTexture);
}
HRESULT (WINAPI* pDrawIndexedPrimitive)(LPDIRECT3DDEVICE9,D3DPRIMITIVETYPE,int,int,int,int,int);
HRESULT WINAPI myDrawIndexedPrimitive(LPDIRECT3DDEVICE9 pDevice, D3DPRIMITIVETYPE pType,int BaseVertexIndex,int uiMinIndex,int NumVertices,int StartIndex,int PrimitiveCount)
{
if(myStride == 48)
{
if((CurrentTexture[0]==0x100 && CurrentTexture[1]==0x100 && CurrentTexture[2]==0x15570) || (CurrentTexture[0]==0x1 && CurrentTexture[1]==0x1 && CurrentTexture[2]==0x4))
{
if( NumVertices==784 || NumVertices==800 ||
NumVertices==803 || NumVertices==806 ||
NumVertices==801 || NumVertices==780 ||
NumVertices==708 || NumVertices==750 ||
NumVertices==737 || NumVertices==754 ||
NumVertices==774 || NumVertices==786 ||
NumVertices==783)
{
AddModel(pDevice,NumVertices);//just add heads, if you want another aimpoints get the right nums for them
}
DWORD dwOldZEnable;
pDevice->SetTexture(1,TextureBlue);
pDevice->GetRenderState(D3DRS_ZENABLE,&dwOldZEnable);
pDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);
pDrawIndexedPrimitive(pDevice,pType,BaseVertexIndex,uiMinIndex,NumVertices,StartIndex,PrimitiveCount);
pDevice->SetRenderState(D3DRS_ZENABLE,dwOldZEnable);
pDevice->SetTexture(1,TextureRed);
}
}
return pDrawIndexedPrimitive(pDevice,pType,BaseVertexIndex,uiMinIndex,NumVertices,StartIndex,PrimitiveCount);
}