int main(int argc, char **argv) { DEBUG_START; if (argc < 3) { printUsage(argv[0]); DEBUG_END; return EXIT_FAILURE; } const char *pathPolyhedron = argv[1]; const char *pathEdgesData = argv[2]; PolyhedronPtr p(new Polyhedron()); if (!p->fscan_default_1_2(pathPolyhedron)) { printf("Failed to read polyhedron data!\n"); printUsage(argv[0]); DEBUG_END; return EXIT_FAILURE; } std::cout << "Number of vertices (custom): " << p->numVertices << std::endl; globalPCLDumper.setNameBase(pathPolyhedron); globalPCLDumper.enableVerboseMode(); Polyhedron_3 initP(p); globalPCLDumper(PCL_DUMPER_LEVEL_DEBUG, "init-polyhedron.ply") << initP; std::cout << "Successfully read the polyhedron..." << std::endl; std::vector<EdgeInfo> data; if (!readEdgeInfoFile(pathEdgesData, data)) { printf("Failed to read edge info data!\n"); printUsage(argv[0]); DEBUG_END; return EXIT_FAILURE; } if (!getInitialPosition(initP, data)) { printf("Failed to initialize edges!\n"); DEBUG_END; return EXIT_FAILURE; } std::cout << "Successfully read the edge info data..." << std::endl; std::vector<Plane_3> planes; for (auto I = initP.planes_begin(), E = initP.planes_end(); I != E; ++I) planes.push_back(normalizePlane(*I)); std::vector<Plane_3> resultingPlanes = correctPlanes(planes, data); dumpResult(planes, resultingPlanes, getCenter(initP)); DEBUG_END; return EXIT_SUCCESS; }
void Frustum::buildPlane( Plane& _plane, const ngl::Matrix& _mat, int _row, int _sign ) { _plane.a = _mat.m_m[0][3] + _sign * _mat.m_m[0][_row]; _plane.b = _mat.m_m[1][3] + _sign * _mat.m_m[1][_row]; _plane.c = _mat.m_m[2][3] + _sign * _mat.m_m[2][_row]; _plane.d = _mat.m_m[3][3] + _sign * _mat.m_m[3][_row]; normalizePlane( _plane ); }
void Frustum::calculate(const TMath::TMatrix<float>& matrix) { TMath_assert((matrix.rows() == 4) && (matrix.cols() == 4)); //matrix = matrix.inverted(); // Нам нужно получить стороны frustum'а. Чтобы сделать это, // возьмём обрезающие плоскости, полученные из matrix (матрица проекции умноженная на мировую), и из них получим стороны. // получаем ПРАВУЮ сторону frustum'а _frustum[RIGHT][A] = matrix(3, 0) - matrix(0, 0); _frustum[RIGHT][B] = matrix(3, 1) - matrix(0, 1); _frustum[RIGHT][C] = matrix(3, 2) - matrix(0, 2); _frustum[RIGHT][D] = matrix(3, 3) - matrix(0, 3); // Теперь, имея нормаль (A,B,C) и расстояние (D) к плоскости, // нам нужно нормализовать нормаль и дистанцию // Нормализуем правую сторону normalizePlane(RIGHT); // получаем ЛЕВУЮ сторону frustum'а _frustum[LEFT][A] = matrix(3, 0) + matrix(0, 0); _frustum[LEFT][B] = matrix(3, 1) + matrix(0, 1); _frustum[LEFT][C] = matrix(3, 2) + matrix(0, 2); _frustum[LEFT][D] = matrix(3, 3) + matrix(0, 3); normalizePlane(LEFT); // получаем нижнюю сторону frustum'а _frustum[BOTTOM][A] = matrix(3, 0) - matrix(1, 0); _frustum[BOTTOM][B] = matrix(3, 1) - matrix(1, 1); _frustum[BOTTOM][C] = matrix(3, 2) - matrix(1, 2); _frustum[BOTTOM][D] = matrix(3, 3) - matrix(1, 3); normalizePlane(BOTTOM); // получаем верхнюю сторону frustum'а _frustum[TOP][A] = matrix(3, 0) + matrix(1, 0); _frustum[TOP][B] = matrix(3, 1) + matrix(1, 1); _frustum[TOP][C] = matrix(3, 2) + matrix(1, 2); _frustum[TOP][D] = matrix(3, 3) + matrix(1, 3); normalizePlane(TOP); // получаем заднюю сторону frustum'а _frustum[BACK][A] = matrix(3, 0) - matrix(2, 0); _frustum[BACK][B] = matrix(3, 1) - matrix(2, 1); _frustum[BACK][C] = matrix(3, 2) - matrix(2, 2); _frustum[BACK][D] = matrix(3, 3) - matrix(2, 3); normalizePlane(BACK); // получаем переднюю сторону frustum'а _frustum[FRONT][A] = matrix(3, 0) + matrix(2, 0); _frustum[FRONT][B] = matrix(3, 1) + matrix(2, 1); _frustum[FRONT][C] = matrix(3, 2) + matrix(2, 2); _frustum[FRONT][D] = matrix(3, 3) + matrix(2, 3); normalizePlane(FRONT); }
//get the planes of the view frustrum void getPlanes(){ float projMatrix[16]; float modelMatrix[16]; glGetFloatv( GL_PROJECTION_MATRIX, projMatrix ); glGetFloatv( GL_MODELVIEW_MATRIX, modelMatrix ); mat4TransposeFloat(projMatrix); mat4TransposeFloat(modelMatrix); float combinedMatrix[16]; mat4MultMat4Float(projMatrix, modelMatrix, combinedMatrix); leftPlane[0] = combinedMatrix[12] + combinedMatrix[0]; leftPlane[1] = combinedMatrix[13] + combinedMatrix[1]; leftPlane[2] = combinedMatrix[14] + combinedMatrix[2]; leftPlane[3] = combinedMatrix[15] + combinedMatrix[3]; rightPlane[0] = combinedMatrix[12] - combinedMatrix[0]; rightPlane[1] = combinedMatrix[13] - combinedMatrix[1]; rightPlane[2] = combinedMatrix[14] - combinedMatrix[2]; rightPlane[3] = combinedMatrix[15] - combinedMatrix[3]; topPlane[0] = combinedMatrix[12] - combinedMatrix[4]; topPlane[1] = combinedMatrix[13] - combinedMatrix[5]; topPlane[2] = combinedMatrix[14] - combinedMatrix[6]; topPlane[3] = combinedMatrix[15] - combinedMatrix[7]; bottomPlane[0] = combinedMatrix[12] + combinedMatrix[4]; bottomPlane[1] = combinedMatrix[13] + combinedMatrix[5]; bottomPlane[2] = combinedMatrix[14] + combinedMatrix[6]; bottomPlane[3] = combinedMatrix[15] + combinedMatrix[7]; nearPlane[0] = combinedMatrix[12] + combinedMatrix[8]; nearPlane[1] = combinedMatrix[13] + combinedMatrix[9]; nearPlane[2] = combinedMatrix[14] + combinedMatrix[10]; nearPlane[3] = combinedMatrix[15] + combinedMatrix[11]; farPlane[0] = combinedMatrix[12] - combinedMatrix[8]; farPlane[1] = combinedMatrix[13] - combinedMatrix[9]; farPlane[2] = combinedMatrix[14] - combinedMatrix[10]; farPlane[3] = combinedMatrix[15] - combinedMatrix[11]; normalizePlane(topPlane); normalizePlane(bottomPlane); normalizePlane(leftPlane); normalizePlane(rightPlane); normalizePlane(nearPlane); normalizePlane(farPlane); }
void GL_FrustumCulling::calculateFrustum( const float* proj, const float* modl ) { float clip[16]; #define D_SET_CLIP(X) \ clip[X * 4 + 0] = modl[X * 4]*proj[ 0] + modl[X * 4 + 1]*proj[ 4] + modl[X * 4 + 2]*proj[ 8] + modl[X * 4 + 3]*proj[12]; \ clip[X * 4 + 1] = modl[X * 4]*proj[ 1] + modl[X * 4 + 1]*proj[ 5] + modl[X * 4 + 2]*proj[ 9] + modl[X * 4 + 3]*proj[13]; \ clip[X * 4 + 2] = modl[X * 4]*proj[ 2] + modl[X * 4 + 1]*proj[ 6] + modl[X * 4 + 2]*proj[10] + modl[X * 4 + 3]*proj[14]; \ clip[X * 4 + 3] = modl[X * 4]*proj[ 3] + modl[X * 4 + 1]*proj[ 7] + modl[X * 4 + 2]*proj[11] + modl[X * 4 + 3]*proj[15]; D_SET_CLIP(0); D_SET_CLIP(1); D_SET_CLIP(2); D_SET_CLIP(3); #undef D_SET_CLIP // clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12]; // clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13]; // clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14]; // clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15]; // clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12]; // clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13]; // clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14]; // clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15]; // clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12]; // clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13]; // clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14]; // clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15]; // clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12]; // clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13]; // clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14]; // clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15]; _Frustum[eRight][eA] = clip[ 3] - clip[ 0]; _Frustum[eRight][eB] = clip[ 7] - clip[ 4]; _Frustum[eRight][eC] = clip[11] - clip[ 8]; _Frustum[eRight][eD] = clip[15] - clip[12]; normalizePlane(eRight); _Frustum[eLeft][eA] = clip[ 3] + clip[ 0]; _Frustum[eLeft][eB] = clip[ 7] + clip[ 4]; _Frustum[eLeft][eC] = clip[11] + clip[ 8]; _Frustum[eLeft][eD] = clip[15] + clip[12]; normalizePlane(eLeft); _Frustum[eBottom][eA] = clip[ 3] + clip[ 1]; _Frustum[eBottom][eB] = clip[ 7] + clip[ 5]; _Frustum[eBottom][eC] = clip[11] + clip[ 9]; _Frustum[eBottom][eD] = clip[15] + clip[13]; normalizePlane(eBottom); _Frustum[eTop][eA] = clip[ 3] - clip[ 1]; _Frustum[eTop][eB] = clip[ 7] - clip[ 5]; _Frustum[eTop][eC] = clip[11] - clip[ 9]; _Frustum[eTop][eD] = clip[15] - clip[13]; normalizePlane(eTop); _Frustum[eBack][eA] = clip[ 3] - clip[ 2]; _Frustum[eBack][eB] = clip[ 7] - clip[ 6]; _Frustum[eBack][eC] = clip[11] - clip[10]; _Frustum[eBack][eD] = clip[15] - clip[14]; normalizePlane(eBack); _Frustum[eFront][eA] = clip[ 3] + clip[ 2]; _Frustum[eFront][eB] = clip[ 7] + clip[ 6]; _Frustum[eFront][eC] = clip[11] + clip[10]; _Frustum[eFront][eD] = clip[15] + clip[14]; normalizePlane(eFront); }