void invSPDMatrix(double *Matrix, int nrow){ double tmp, det; int i; if (nrow == 1){ Matrix[0] = 1.0/Matrix[0]; } else if (nrow == 2){ det = Matrix[0]*Matrix[3] - Matrix[1]*Matrix[2]; tmp = Matrix[0]; Matrix[0] = Matrix[3]; Matrix[3] = tmp; Matrix[1] = -Matrix[1]; Matrix[2] = -Matrix[2]; for (i=0; i<nrow*nrow; i++) Matrix[i] /= det; } else if (nrow > 2){ double *L = calloc(nrow*nrow, sizeof(double)); double *tL = calloc(nrow*nrow, sizeof(double)); cholesky(Matrix, nrow, L, 0); invMatrix(L, nrow); for (i=0; i<nrow*nrow; i++) tL[i] = L[i]; tr(tL, nrow, nrow); prodMatrix(tL, L, Matrix, nrow, nrow, nrow, nrow); free(L); free(tL); } }
bool MeshEntity::checkCollision(const Ray& ray, FaceSample& collisionPoint) const { PR_GUARD_PROFILE(); // Local space Ray local = ray; local.setStartPosition(PM::pm_Multiply(invMatrix(), ray.startPosition())); local.setDirection(PM::pm_Normalize3D(PM::pm_Multiply(invDirectionMatrix(), ray.direction()))); if (mMesh->checkCollision(local, collisionPoint)) { collisionPoint.P = PM::pm_Multiply(matrix(), collisionPoint.P); collisionPoint.Ng = PM::pm_Normalize3D(PM::pm_Multiply(directionMatrix(), collisionPoint.Ng)); Projection::tangent_frame(collisionPoint.Ng, collisionPoint.Nx, collisionPoint.Ny); if(mMaterialOverride) collisionPoint.Material = mMaterialOverride; return true; } else { return false; } }
dVector dSceneRender::ScreenToGlobal (const dVector& screen) const { dFloat width = dFloat (GetViewPortWidth()); dFloat height = dFloat (GetViewPortHeight()); dVector p0 (2.0f * screen.m_x / width - 1.0f, 2.0f * (height - screen.m_y) / height - 1.0f, 2.0f * screen.m_z - 1.0f, 1.0f); dMatrix modelview (GetModelViewMatrix()); dMatrix projection (GetProjectionMatrix()); dMatrix matrix (modelview * projection); dMatrix invMatrix (matrix.Inverse4x4()); dVector p1 (invMatrix.RotateVector4x4(p0)); p1.m_w = 1.0f / p1.m_w; p1 = p1.Scale(p1.m_w); p1.m_w = 1.0f; return p1; }
void ViewSphereRender::paintGL() { m_camera.setupModelMatrix(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); float ps; glGetFloatv(GL_POINT_SIZE, &ps); vector<ViewPoint>& vp = m_viewSphere.vViewPos; glPointSize(5); /* // 绘制顶点 glColor3ub(255, 0, 0); glPointSize(5); glBegin(GL_POINTS); glVertex3dv(vp[0].coord); vector<ViewPoint>::iterator iter = vp.begin(); for (; iter != vp.end(); ++iter) { glVertex3dv(iter->coord); } glEnd(); */ Vector4d p0(vp[0].coord[0], vp[0].coord[1], vp[0].coord[2], 1.0); double matrix[16]; glGetDoublev(GL_MODELVIEW_MATRIX, matrix); // GLint viewport[4]; // GLdouble projection[16]; // double win_coord[3]; // win_coord[0] = width() / 2; // win_coord[1] = height() / 2; // glReadPixels( int(win_coord[0]), int(win_coord[1]), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &win_coord[2] ); // glGetDoublev( GL_PROJECTION_MATRIX, projection ); // glGetIntegerv( GL_VIEWPORT, viewport ); // double pos[3]; // gluUnProject( win_coord[0], win_coord[1], win_coord[2], matrix, projection, viewport, &pos[0], &pos[1], &pos[2]); // glPushMatrix(); // glMatrixMode(GL_MODELVIEW); // glLoadIdentity(); // glPopMatrix(); p0.set(0, 0, 1.0, 1.0); invMatrix(matrix, matrix); p0 = multiply(p0, matrix); long ipix; vec2pix_ring(m_nside, (double*)&p0, &ipix); glColor3ub(255, 128, 0); glBegin(GL_POINTS); glVertex3dv(vp[ipix].coord); glEnd(); m_currentPos = &(vp[ipix]); /* glColor3ub(255, 255, 255); // 绘制顶点连线 glBegin(GL_LINES); vector< pair<int, int> >::iterator itLink = m_viewSphere.links.begin(); for(; itLink != m_viewSphere.links.end(); ++ itLink) { glVertex3dv(vp[itLink->first].coord); glVertex3dv(vp[itLink->second].coord); } glEnd(); */ // 绘制球面 vector<Vector3i>& vTris = m_viewSphere.vTris; vector<Vector3i>::iterator itTris = vTris.begin(); glBegin(GL_TRIANGLES); for (; itTris != vTris.end(); ++itTris) { glColor3ub(128, 0, 0); glVertex3dv(vp[itTris->x].coord); glColor3ub(0, 128, 0); glVertex3dv(vp[itTris->y].coord); glColor3ub(0, 0, 128); glVertex3dv(vp[itTris->z].coord); } glEnd(); // 绘制视点变化曲线 vector<const ViewPoint*> vPoint = m_recorder->all(); if (vPoint.size() > 0) { glColor3ub(255, 255, 0); glBegin(GL_POINTS); glVertex3dv(vPoint.front()->coord); glEnd(); if (vPoint.size() > 1) { glColor3ub(255, 102, 0); glBegin(GL_LINES); glVertex3dv(vPoint.front()->coord); for(int i = 0; i < m_recorder->current(); ++i) { glVertex3dv(vPoint[i]->coord); glVertex3dv(vPoint[i]->coord); } glVertex3dv(vPoint[m_recorder->current()]->coord); glEnd(); glColor3ub(0, 255, 0); glBegin(GL_POINTS); glVertex3dv(vPoint.back()->coord); glEnd(); } } //glDisable(GL_POLYGON_OFFSET_FILL); ////////////////////////////////////////////////////////////////////////// glDisable(GL_DEPTH_TEST); //drawCoord(); m_camera.setOrthoProjection(); m_camera.drawArc(); glEnable(GL_DEPTH_TEST); glPointSize(ps); }