bool IntersectedPolygon(Vector3 vPoly[], Vector3 vLine[], int verticeCount) { Vector3 vNormal; float originDistance = 0; if(!IntersectedPlane(vPoly, vLine,vNormal,originDistance)) return false; Vector3 vIntersection = IntersectionPoint(vNormal, vLine, originDistance);//计算交点 if(InsidePolygon(vIntersection, vPoly, verticeCount))//找到交点,判断是否在多边形内 return true; return false; }
void RenderScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The matrix // Let's set our camera to the left a bit for a better view // Position View Up Vector gluLookAt(-2.5f, 0.5, 0.5f, 0, 0.5f, 0, 0, 1, 0); // This determines where the camera's position and view is // Below we give OpenGL the 3 vertices of our triangle. Once again, we put them // into an array of CVector3 structures so we could dynamically move it around screen. glBegin (GL_TRIANGLES); // This is our BEGIN to draw glColor3ub(255, 0, 0); // Make the Left vertex RED glVertex3f(vTriangle[0].x, vTriangle[0].y, vTriangle[0].z); glColor3ub(0, 255, 0); // Make the Right vertex GREEN glVertex3f(vTriangle[1].x, vTriangle[1].y, vTriangle[1].z); glColor3ub(0, 0, 255); // Make the Top vertex BLUE glVertex3f(vTriangle[2].x, vTriangle[2].y, vTriangle[2].z); glEnd(); // This is the END of drawing // Below we use our function we just wrote to see if the plane of the // triangle and the line intersect. It will return true if that's the case. bool bCollided = IntersectedPlane(vTriangle, vLine); // Below we draw the line that the polygon will be colliding with. // We will check to see if the line collides with the polygons plane, and if it does, // we will turn the line green to show when it is intersecting the plane. glBegin (GL_LINES); // This is our BEGIN to draw if(bCollided) // If we collided, change the color of the line to illustrate this. glColor3ub(0, 255, 0); // Make the line GREEN if we collided with the triangle's plane else glColor3ub(255, 255, 0); // Make the line YELLOW if we didn't collide glVertex3f(vLine[0].x, vLine[0].y, vLine[0].z); // Let's draw the normal centered on the triangle // Draw the normal of the polygon from the center of the polygon to better see it glVertex3f(vLine[1].x, vLine[1].y, vLine[1].z); glEnd(); // This is the END of drawing // That's it, now use the LEFT and RIGHT arrow keys to move it around to see it in action. SwapBuffers(g_hDC); // Swap the backbuffers to the foreground }
bool IntersectedPolygon(CVector3 vPoly[], CVector3 vLine[], int verticeCount) { CVector3 vNormal; float originDistance = 0; // First, make sure our line intersects the plane // Reference // Reference if(!IntersectedPlane(vPoly, vLine, vNormal, originDistance)) return false; // Now that we have our normal and distance passed back from IntersectedPlane(), // we can use it to calculate the intersection point. CVector3 vIntersection = IntersectionPoint(vNormal, vLine, originDistance); // Now that we have the intersection point, we need to test if it's inside the polygon. if(InsidePolygon(vIntersection, vPoly, verticeCount)) return true; // We collided! Return success return false; // There was no collision, so return false }
vec3 EditorToolBar::getGroundPickPos(float elevation) const { glMatrixMode(GL_PROJECTION); glPushMatrix(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); g_Camera.setCamera(); vec3 groundPos; groundPos.zero(); // Get the point on the ground plane that the mouse is hovering over. // Get a ray for the mouse vec3 mouseRay[] = { UnProject(0.0f), UnProject(1.0f), }; // Get the location that the mouse ray intersect the ground plane at. // Check the mouse ray against the ground plane (XZ plane) float originDistance=0.0f; vec3 normal; // A polygon on the intersection plane vec3 a(0,elevation,0); vec3 b(0,elevation,1); vec3 c(1,elevation,0); vec3 vPoly[3] = {a, b, c}; if(IntersectedPlane(vPoly, mouseRay, normal, originDistance) == true) { // Get the intersection point groundPos = IntersectionPoint(normal, mouseRay, (double)originDistance); // Snap to grid if(snapToGrid) { float snap = 0.25f; float snappedX = (snap) * floorf(groundPos.x / snap); float snappedZ = (snap) * floorf(groundPos.z / snap); groundPos.x = snappedX; groundPos.z = snappedZ; } } glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); return groundPos; }