// 渲染
void CmfcView::RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0,0.0f,-10.0f); // Move Right 1.5 Units And Into The Screen 7.0
//glRotatef(45,0.0f,1.0f,0.0f); // Rotate The Quad On The X axis ( NEW )
//glRotatef(180,0.0f,0.0f,1.0f); // Rotate The Quad On The X axis ( NEW )
glBegin(GL_QUADS); // Draw A Quad
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f(0, 0, 2); // Top Right Of The Quad (Top)
glVertex3f(1, 0, 2); // Top Left Of The Quad (Top)
glVertex3f(1, 1, 2); // Bottom Left Of The Quad (Top)
glVertex3f(0, 1, 2); // Bottom Right Of The Quad (Top)
glEnd();
}
/****************************************/
/* 功能:计算直线和平面交点
/* param[in]: planePoint--平面上的一个点
/* param[in]: planeVector--平面的法向量
/* param[in]: linePoint1--直线的点1
/* param[in]: linePoint2--直线的点2
/* return: 交点坐标
/***************************************/
float* CmfcView::CalPlaneLineIntersectPoint(float* planePoint, float* planeVector, float* linePoint1, float* linePoint2)
{
float *res = new float[3]; // 交点坐标
//float planeVector[3] = {0, 0, 1};
float lineVector[3]; // 直线向量
lineVector[0] = linePoint2[0] - linePoint1[0];
lineVector[1] = linePoint2[1] - linePoint1[1];
lineVector[2] = linePoint2[2] - linePoint1[2];
float vp1, vp2, vp3, n1, n2, n3, v1, v2, v3, m1, m2, m3, t,vpt;
vp1 = planeVector[0];
vp2 = planeVector[1];
vp3 = planeVector[2];
n1 = planePoint[0];
n2 = planePoint[1];
n3 = planePoint[2];
v1 = lineVector[0];
v2 = lineVector[1];
v3 = lineVector[2];
m1 = linePoint1[0];
m2 = linePoint1[1];
m3 = linePoint1[2];
vpt = v1 * vp1 + v2 * vp2 + v3 * vp3;
//首先判断直线是否与平面平行
if (vpt == 0)
{
res = NULL;
}
else
{
t = ((n1 - m1) * vp1 + (n2 - m2) * vp2 + (n3 - m3) * vp3) / vpt;
res[0] = m1 + v1 * t;
res[1] = (m2 + v2 * t);
res[2] = m3 + v3 * t;
}
return res;
}
float* CmfcView::CrossPoint(CPoint pt)
{
float *crsPt;
GLdouble x1, y1, z1;
GLdouble x2, y2, z2;
GLdouble modelview[16];
GLdouble projection[16];
GLint viewport[4];
glGetDoublev (GL_MODELVIEW_MATRIX, modelview);
glGetDoublev (GL_PROJECTION_MATRIX, projection);
glGetIntegerv (GL_VIEWPORT, viewport);
GLdouble world_x1, world_y1, world_z1;
GLdouble world_x2, world_y2, world_z2;
// 获取近裁剪面上的交点
gluUnProject( (GLdouble) pt.x, (GLdouble)(viewport[3] - pt.y - 1), 0.0,
modelview, projection, viewport,
&world_x1, &world_y1, &world_z1);
// 获取远裁剪面上的交点
gluUnProject( (GLdouble) pt.x, (GLdouble)(viewport[3] - pt.y - 1), 1.0,
modelview, projection, viewport,
&world_x2, &world_y2, &world_z2);
float planePoint[3] = {10, 13, 2}; // 平面上一点
float linePoint1[3];
float linePoint2[3];
linePoint1[0] = world_x1;
linePoint1[1] = world_y1;
linePoint1[2] = world_z1;
linePoint2[0] = world_x2;
linePoint2[1] = world_y2;
linePoint2[2] = world_z2;
float planeVector[3] = {0, 0, 1}; // 平面向量(根据实际情况计算)
crsPt = CalPlaneLineIntersectPoint(planePoint, planeVector, linePoint1, linePoint2);
return crsPt;
}
void CmfcView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
float *crossPoint;
crossPoint = CrossPoint(point);
CView::OnLButtonDown(nFlags, point);
}