Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<CR_VECTOR4> CMathHelper::CreateOccluderPlanes(const CR_VECTOR3 &pCameraPos, const CR_VECTOR3 *pCorners, const CR_VECTOR3 &pCenter, std::vector<CR_VECTOR3> &pPlanesSigns, std::vector<CR_VECTOR3> &pDebugPoints)
- {
- assert(pCorners != nullptr);
- pPlanesSigns.clear();
- std::vector<int> cornerIds = { 0, 1, 5, 4, 4, 5, 7, 6, 6, 7, 3, 2, 2, 3, 1, 0, 1, 3, 7, 5 , 0, 4, 6, 2 }; // 6*4 = 24
- std::set<CR_VECTOR2I> visibleEdges;
- std::vector<CR_VECTOR4> planes;
- // find quads facing the camera (and use for 'near' planes) - planar, so 3 verts per quad is enough
- for(size_t verts=0;verts<cornerIds.size();verts+=4)
- {
- CR_VECTOR3 faceNormal;
- if(TriangleFacingPoint(pCorners[cornerIds[verts+1]], pCorners[cornerIds[verts+2]], pCorners[cornerIds[verts+3]], pCameraPos, faceNormal))
- {
- faceNormal *= -1.0f; // flip normal, as it was pointing towards camera
- faceNormal.Normalize();
- float d = -DotProductVec3(pCorners[cornerIds[verts+1]], faceNormal);
- planes.emplace_back(CR_VECTOR4(faceNormal.x, faceNormal.y, faceNormal.z, d));
- pPlanesSigns.emplace_back(GetSigns(faceNormal));
- std::vector<CR_VECTOR2I> newEdges;
- newEdges.emplace_back(CR_VECTOR2I(cornerIds[verts], cornerIds[verts+1]));
- newEdges.emplace_back(CR_VECTOR2I(cornerIds[verts+1], cornerIds[verts+2]));
- newEdges.emplace_back(CR_VECTOR2I(cornerIds[verts+2], cornerIds[verts+3]));
- newEdges.emplace_back(CR_VECTOR2I(cornerIds[verts+3], cornerIds[verts]));
- for(auto && edge : newEdges)
- {
- edge.LowToHigh();
- auto it = visibleEdges.find(edge);
- if(it != visibleEdges.end())
- {
- visibleEdges.erase(it);
- }
- else
- {
- visibleEdges.insert(edge);
- }
- }
- }
- }
- // continue with 'sides' of the volume, based on the edges with duplicates
- for(auto && tEdge : visibleEdges)
- {
- CR_VECTOR3 faceNormal = CalculateFaceNormal(pCameraPos, pCorners[tEdge.x], pCorners[tEdge.y]);
- if(!TriangleFacingPoint(faceNormal, pCameraPos, pCenter))
- {
- faceNormal *= -1.0f;
- }
- faceNormal.Normalize();
- float d = -DotProductVec3(pCameraPos, faceNormal);
- planes.emplace_back(CR_VECTOR4(faceNormal.x, faceNormal.y, faceNormal.z, d));
- pPlanesSigns.emplace_back(GetSigns(faceNormal));
- }
- return planes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement