6. 处理 OnSize()
void CTestView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
VERIFY(wglMakeCurrent(m_pDC->m_hDC,m_hRC));//确认RC与当前DC关联
w=cx;
h=cy;
VERIFY(wglMakeCurrent(NULL,NULL));//确认DC释放RC
}
7 处理 OnLButtonDown()
void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CView::OnLButtonDown(nFlags, point);
if(bezier_curve.m_N>MAX-1)
{
MessageBox("顶点个数超过了最大数MAX=50");
return;
}
//以下为坐标变换作准备
GetClientRect(&m_ClientRect);//获取视口区域大小
w=m_ClientRect.right-m_ClientRect.left;//视口宽度 w
h=m_ClientRect.bottom-m_ClientRect.top;//视口高度 h
//w,h 是CTestView的成员变量
centerx=(m_ClientRect.left+m_ClientRect.right)/2;//中心位置,
centery=(m_ClientRect.top+m_ClientRect.bottom)/2;//取之作原点
//centerx,centery 是 CTestView 的成员变量
GLdouble tmpx,tmpy;
tmpx=scrx2glx(point.x);//屏幕上点坐标转化为OpenGL画图的规范坐标
tmpy=scry2gly(point.y);
bezier_curve.m_Vertex[bezier_curve.m_N].x=tmpx;//加一个顶点
bezier_curve.m_Vertex[bezier_curve.m_N].y=tmpy;
bezier_curve.m_N++;//顶点数加一
InvalidateRect(NULL,TRUE);//发送刷新重绘消息
}
double CTestView::scrx2glx(int scrx)
{
return (double)(scrx-centerx)/double(h);
}
double CTestView::scry2gly(int scry)
{
}
附录:
1.CBezierCurve 的声明: (BezierCurve.h)
class CBezierCurve
{
public:
myPOINT2D m_Vertex[MAX];//控制顶点,以数组存储
//myPOINT2D 是一个存二维点的结构
//成员为Gldouble x,y
int m_N; //控制顶点的个数
public:
CBezierCurve();
virtual ~CBezierCurve();
void bezier_generation(myPOINT2D P[MAX],int level);
//算法的具体实现
void myDraw();//画曲线函数
void myPolygon(); //画控制多边形
};
2. CBezierCurve 的实现: (BezierCurve.cpp)
CBezierCurve::CBezierCurve()
{
m_N=4;
m_Vertex[0].x=-0.5f;
m_Vertex[0].y=-0.5f;
m_Vertex[1].x=-0.5f;
m_Vertex[1].y=0.5f;
m_Vertex[2].x=0.5f;
m_Vertex[2].y=0.5f;
m_Vertex[3].x=0.5f;
m_Vertex[3].y=-0.5f;
}
CBezierCurve::~CBezierCurve()
{
}
void CBezierCurve::myDraw()
{
bezier_generation(m_Vertex,LEVEL);
}
void CBezierCurve::bezier_generation(myPOINT2D P[MAX], int level)
{ //算法的具体描述,请参考相关书本
int i,j;
level--;
if(level<0)return;
if(level==0)
{
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_LINES); //画出线段
glVertex2d(P[0].x,P[0].y);
glVertex2d(P[m_N-1].x,P[m_N-1].y);
glEnd();//结束画线段
return; //递归到了最底层,跳出递归
}
myPOINT2D Q[MAX],R[MAX];
for(i=0;i {
Q[i].x=P[i].x;
Q[i].y=P[i].y;
}
for(i=1;i<m_N;i++)
{
R[m_N-i].x=Q[m_N-1].x;
R[m_N-i].y=Q[m_N-1].y;
for(j=m_N-1;j>=i;j--)
{
Q[j].x=(Q[j-1].x+Q[j].x)/double(2);
Q[j].y=(Q[j-1].y+Q[j].y)/double(2);
}
}
R[0].x=Q[m_N-1].x;
R[0].y=Q[m_N-1].y;
bezier_generation(Q,level);
bezier_generation(R,level);
}
void CBezierCurve::myPolygon()
{
glBegin(GL_LINE_STRIP); //画出连线段
glColor3f(0.2f,0.4f,0.4f);
for(int i=0;i<m_N;i++)
{
glVertex2d(m_Vertex[i].x,m_Vertex[i].y);
}
glEnd();//结束画连线段
}
查看本文来源