bool MapBlock::GetPatchIntersect(int index, const Ray& ray, float& tMin) const { int ox = (index / 16) * 4; int oy = (index % 16) * 4; Vector3 root = mTransform.GetTranslate() + Vector3(2.5f * ox, -2.5f * oy, 0.0f); Vector3 trans = root; float t; bool ret = false; for(unsigned int x = 0; x < 5; ++x){ for(unsigned int y = 0; y < 5; ++y){ int ix = ox + x; int iy = oy + y; Vector3 v1(trans.x, trans.y, trans.z + mHeightmap->GetHeight(ix, iy)); Vector3 v2(trans.x + 2.5f, trans.y, trans.z + mHeightmap->GetHeight(ix+1,iy)); Vector3 v3(trans.x, trans.y - 2.5f, trans.z + mHeightmap->GetHeight(ix, iy+1)); if(!ray.IntersectTriangle(v1, v3, v2, t)){ Vector3 v4(trans.x + 2.5f, trans.y - 2.5f, trans.z + mHeightmap->GetHeight(ix+1,iy+1)); if(!ray.IntersectTriangle(v2, v3, v4, t)){ trans.y -= 2.5f; continue; } } if(t < tMin) tMin = t; ret = true; } trans.x += 2.5f; trans.y = root.y; } return ret; }
bool MapBlock::CastRayDown(const Ray& ray, float& tMin) const { Vector3 mpos = mTransform.GetTranslate(); mpos.x = ray.mPoint.x - mpos.x; mpos.y = mpos.y - ray.mPoint.y; int ix = int(mpos.x / 2.5f) % 64; int iy = int(mpos.y / 2.5f) % 64; float tX = mpos.x - float(ix)*2.5f; float tY = mpos.y - float(iy)*2.5f; Vector3 v1(0.0f, 0.0f, mHeightmap->GetHeight(ix, iy)); Vector3 v2(2.5f, 0.0f, mHeightmap->GetHeight(ix+1,iy)); Vector3 v3(0.0f, 2.5f, mHeightmap->GetHeight(ix, iy+1)); Vector3 rPoint(tX, tY, ray.mPoint.z); Ray nRay = Ray(rPoint, ray.mDirection); if(!nRay.IntersectTriangle(v1, v2, v3, tMin)){ Vector3 v4(2.5f, 2.5f, mHeightmap->GetHeight(ix+1,iy+1)); if(!nRay.IntersectTriangle(v2, v4, v3, tMin)) return false; } return true; }
bool RayCallback( vector< KDTreeNode * > &nodes) { mp_Intersected = false; Vec3 v0, v1, v2; if( nodes.size() > 0 && //node->pntidx >= 0 && mp_MeshP ) //leaf node { //test against triangle index static double su, st, sv; double temp = 999999; int numNodes = nodes.size(); for( int i = 0; i < numNodes; i++ ) { KDTreeNode * node = nodes[ i ]; int size = node->objectList.size(); INDEXLIST::iterator iter = node->objectList.begin(); for( ; iter != node->objectList.end(); ++iter ) { int TriIndex =(*iter); //index refers to vertices, 3 verts per tri TriangleFace &tri = (*mp_MeshP->m_Faces)[ TriIndex ]; v0 = (*mp_MeshP->m_CollapsedMesh)[ tri.index[0] ].originalPosition; v1 = (*mp_MeshP->m_CollapsedMesh)[ tri.index[1] ].originalPosition; v2 = (*mp_MeshP->m_CollapsedMesh)[ tri.index[2] ].originalPosition; bool value = mp_Ray.IntersectTriangle( v0, v1, v2, &st, &su, &sv ); if( value && st >= 0 && temp > st )//distance of this intersection is minimum, make min intersection this one { temp = st; mp_T = st; mp_U = su; mp_V = sv; mp_Intersected = true; mp_TriIndex = TriIndex; } } } return mp_Intersected; } return false; }