废话不多说,直接开始
下列是会使用到的函数简单介绍:
- 初始化背景 void Initial(void)
- 改变窗口大小 void ChangeSize(int w,int h)
- 菜单响应函数 chooseWay(int value)
- 清除当前已经画的图形 void clear(void)
- 画图函数 void Display(void)
- 鼠标点击事件响应函数
void MousePlot(GLint button,GLint action,GLint xMouse,GLint yMouse) - 鼠标跟踪函数 void PassiveMouseMove(GLint xMouse,GLint yMouse)
首先,让我们看看主函数
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
//创建窗口,并设置大小为长400宽300
glutInitWindowSize(400,300);
glutInitWindowPosition(100,100);
glutCreateWindow("橡皮筋技术");
//向鼠标右键注册菜单-选择画折线还是矩形
glutCreateMenu(chooseWay); //菜单函数
glutAddMenuEntry("BrokenLine", 1); //折线选项
glutAddMenuEntry("Actangle", 2); //矩形选项
glutAddMenuEntry("Clear", 0); //清空选项
glutAttachMenu(GLUT_RIGHT_BUTTON); //将该菜单绑定在右键
Initial(); //初始化函数
glutReshapeFunc(ChangeSize); //调用改变窗口函数
glutDisplayFunc(Display); //调用画图函数
glutMouseFunc(MousePlot); //调用鼠标点击事件函数
glutPassiveMotionFunc(PassiveMouseMove);//调用鼠标追踪事件函数
glutMainLoop();
return 0;
}
初始化函数,glClearColor函数向缓存写入颜色值
//初始化函数
void Initial(void)
{
glClearColor(0.5f,0.5f,0.75f,1.0f); //设置背景颜色
}
//改变窗口大小函数
void ChangeSize(int w,int h)
{
winWidth=w,winHeight=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //调用单位矩阵,去除以前投影参数设置
gluOrtho2D(0.0,winWidth,0.0,winHeight);
}
//清除当前已经画的图形
void clear(void)
{
int flag = iPointNum;
iPointNum = 0;
for (int i = 0; i < flag; ++i)
{
x[i] = 0;
y[i] = 0;
}
glutPostRedisplay(); //清除后重画
}
菜单选择函数
void chooseWay(int value)
{
switch (value)
{
case 0:clear(); break;
case 1:choose=1; break; //画折线
case 2:choose=2; break; //画矩形
default:
choose = 1;break; //默认画折线
}
}
鼠标点击响应函数
void MousePlot(GLint button,GLint action,GLint xMouse,GLint yMouse)
{
if (choose == 1) //画折线
{
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
//左键按下判定
{
if (iPointNum == 0)
//已经点击的点数
{
x[iPointNum] = xMouse;
y[iPointNum] = winHeight - yMouse; //坐标转换
iPointNum += 1;
}
else
{
x[iPointNum] = xMouse;
y[iPointNum] = winHeight - yMouse;
iPointNum += 1;
glutPostRedisplay();
}
}
}
if (choose == 2) //画矩形
{
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
if (iPointNum== 0||iPointNum==2)
{
x[0] = xMouse;
y[0] = winHeight - yMouse;
iPointNum = 1;
}
else
{
x[1] = xMouse;
y[1] = winHeight - yMouse;
iPointNum = 2;
glutPostRedisplay();
}
}
}
}
鼠标跟踪函数
void PassiveMouseMove(GLint xMouse,GLint yMouse)
{
if (choose == 1 && iPointNum >=1)
{
x[iPointNum] = xMouse;
y[iPointNum] = winHeight - yMouse;
glutPostRedisplay();
}
if(choose==2&&iPointNum==1)
{
x[iPointNum]=xMouse;
y[iPointNum]=winHeight-yMouse;
glutPostRedisplay();
}
}
画图函数
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
if(choose==1)
{
if (iPointNum >= 1)
{
glColor3f(1.0f, 0.0f, 0.0f);
int flag = iPointNum;
glBegin(GL_LINE_STRIP);
while (flag)
{
glVertex2f(x[flag], y[flag]);
glVertex2f(x[flag - 1], y[flag - 1]);
flag--;
}
glEnd();
}
}
//画矩形
if (choose == 2)
{
if (iPointNum >=1)
{
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(x[0], y[0]);
glVertex2f(x[0], y[1]);
glVertex2f(x[1], y[1]);
glVertex2f(x[1], y[0]);
glEnd();
}
}
glutSwapBuffers();
}
把上述拼接起来就可以完整运行,希望对你有所帮助!