示例#1
0
void MOUSE::CalculateMappos(TERRAIN &terrain)
{
	//Get Mouse Ray
	D3DXMATRIX world;
	D3DXMatrixIdentity(&world);
	m_pDevice->SetTransform(D3DTS_WORLD, &world);
	RAY mRay = GetRay();

	float minDistance = 10000.0f;
	for(int i=0;i<(int)terrain.m_patches.size();i++)
	{
		if(mRay.Intersect(terrain.m_patches[i]->m_BBox) > 0.0f)
		{
			// Collect only the closest intersection
			BOOL hit;
			DWORD dwFace;
			float hitU, hitV, dist;
			D3DXIntersect(terrain.m_patches[i]->m_pMesh, &mRay.org, &mRay.dir, &hit, &dwFace, &hitU, &hitV, &dist, NULL, NULL);

			if(hit && dist < minDistance)
			{
				minDistance = dist;
				int tiles = dwFace / 2;		//Two faces to each map tile
				int tilesPerRow = terrain.m_patches[i]->m_mapRect.right - terrain.m_patches[i]->m_mapRect.left;
				int y = tiles / tilesPerRow, x = tiles - y * tilesPerRow;

				if(dwFace % 2 == 0)		//Hit upper left face
				{
					if(hitU > 0.5f)x++;
					else if(hitV > 0.5f)y++;
				}
				else					//Hit lower right face
				{
					if(hitU + hitV < 0.5f)y++;
					else if(hitU > 0.5f)x++;
					else {x++;y++;}
				}

				m_mappos.Set(terrain.m_patches[i]->m_mapRect.left + x, terrain.m_patches[i]->m_mapRect.top + y);
				m_ballPos = terrain.GetWorldPos(m_mappos);
			}			
		}
	}	
}
示例#2
0
文件: app.cpp 项目: Alriightyman/RTS
HRESULT APPLICATION::Render()
{
    // Clear the viewport
    m_pDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L );
	
    // Begin the scene 
    if(SUCCEEDED(m_pDevice->BeginScene()))
    {
		for(int i=0;i<(int)m_objects.size();i++)
			m_objects[i].Render();
		
		//Find intersecting object closest to the camera
		D3DXMATRIX identity;
		D3DXMatrixIdentity(&identity);

		int object = -1;
		float bestDist = 100000.0f;
		for(int i=0;i<(int)m_objects.size() - 1;i++)
		{
			float dist = -1.0f; 
			
			//Do intersection tests
			if(m_intersectType == 0)
			{
				m_pDevice->SetTransform(D3DTS_WORLD, &m_objects[i].m_meshInstance.GetWorldMatrix());
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_meshInstance);
			}
			else if(m_intersectType == 1)
			{				
				m_pDevice->SetTransform(D3DTS_WORLD, &identity);
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_BBox);
			}
			else if(m_intersectType == 2)
			{
				m_pDevice->SetTransform(D3DTS_WORLD, &identity);
				RAY ray = m_mouse.GetRay();
				dist = ray.Intersect(m_objects[i].m_BSphere);
			}

			m_objects[i].RenderBoundingVolume(m_intersectType);

			if(dist >= 0.0f && dist < bestDist)
			{
				object = i;
				bestDist = dist;
			}
		}

		//Write m_mouse text
		if(object != -1)
		{
			RECT mr[] = {{m_mouse.x + 2, m_mouse.y + 24, 0, 0}, {m_mouse.x, m_mouse.y + 22, 0, 0}};
			m_pFontMouse->DrawText(NULL, m_objects[object].m_name.c_str(), -1, &mr[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
			m_pFontMouse->DrawText(NULL, m_objects[object].m_name.c_str(), -1, &mr[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffff0000);
		}

		RECT r[] = {{10, 10, 0, 0}, {10, 30, 0, 0}, {10, 50, 0, 0}};
		m_pFont->DrawText(NULL, "Mouse Wheel: Change Camera Radius", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		m_pFont->DrawText(NULL, "Arrows: Change Camera Angle", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		m_pFont->DrawText(NULL, "Space: Change Bounding Volume", -1, &r[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);

		RECT rc = {500, 10, 0, 0};
		if(m_intersectType == 0)
			m_pFont->DrawText(NULL, "Mesh Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		else if(m_intersectType == 1)
			m_pFont->DrawText(NULL, "Box Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		else if(m_intersectType == 2)
			m_pFont->DrawText(NULL, "Sphere Intersection Test", -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
		
		//Draw m_mouse
		m_mouse.Paint();

        // End the scene.
		m_pDevice->EndScene();
		m_pDevice->Present(0, 0, 0, 0);
    }

	return S_OK;
}