#include <gl/glut.h>
static GLsizei iMode = 1;
static GLfloat sx = 1.0;
static GLfloat sy = 1.0;
void Initial(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //设置窗口背景颜色为白色
glMatrixMode(GL_PROJECTION); //设置投影参数
gluOrtho2D(-30.0,30.0,-20.0,20.0);//居中显示
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT); //用当前背景色填充窗口
glColor3f(1.0f, .0f, 0.0f); //设置当前的绘图颜色为红色
switch(iMode)
{
case 1:
glColor3f(.0f, 0.0f, 0.0f); //黑
break;
case 2:
glColor3f(1.0f, .0f, .0f); //红
break;
case 3:
glColor3f(0.0f, .0f, 1.0f); //蓝
break;
case 4:
glColor3f(.0f, 1.0f, 0.0f); //绿
break;
case 5:
glColor3f(1.0f, 1.0f, 1.0f); //白
break;
case 6:
glDisable(GL_LINE_STIPPLE);
break;
case 7:
glEnable(GL_LINE_STIPPLE);
glLineStipple(1,255);
break;
default :
break;
}
//多边形的绘制
glBegin(GL_LINE_LOOP);
glVertex2f(-10,-10);
glVertex2f(10,-10);
glVertex2f(20,0);
glVertex2f(0,10);
glVertex2f(-20,0);
glEnd();
glFlush(); //处理所有的OpenGL程序
}
void ProcessMenu(int value)
{
iMode = value;
glutPostRedisplay();
}
//键盘交互调用函数
void Key(unsigned char key,int x,int y)
{
switch(key)
{
case 'q':
exit(0);
break;
case 'Q':
exit(0);
break;
default:
break;
}
}
//鼠标交互调用函数
void MousePlot(GLint button,GLint action,GLint xMouse,GLint yMouse)
{
if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
glScalef(sx+0.1,sy+0.1,0.0f);//左键每一次点击扩大0.1
}
if(button ==GLUT_RIGHT_BUTTON && action == GLUT_DOWN)
{
glScalef(sx-0.1,sy-0.1,0.0f);//右键每一次点击扩大0.1
}
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //初始化窗口的显示模式
glutInitWindowSize(800,800); //设置窗口的尺寸
glutInitWindowPosition(600,120); //设置窗口的位置
glutCreateWindow("矩形"); //创建一个名为矩形的窗口
//创建菜单
int color= glutCreateMenu(ProcessMenu);
glutAddMenuEntry("黑色",1);
glutAddMenuEntry("红色",2);
glutAddMenuEntry("蓝色",3);
glutAddMenuEntry("绿色",4);
glutAddMenuEntry("白色",5);
int lineStrpple = glutCreateMenu(ProcessMenu);
glutAddMenuEntry("直线",6);
glutAddMenuEntry("虚线",7);
int nMainMenu = glutCreateMenu(ProcessMenu);
glutAddSubMenu("颜色",color);
glutAddSubMenu("线型",lineStrpple);
glutAttachMenu(GLUT_MIDDLE_BUTTON);//与鼠标中键绑定
glutKeyboardFunc(Key); //普通键输入注册函数
glutMouseFunc(MousePlot); //注册鼠标响应函数
glutDisplayFunc(Display); //设置当前窗口的显示回调函数
Initial(); //完成窗口初始化
glutMainLoop(); //启动主GLUT事件处理循环
return 0;
}
OpenGl实验2
最新推荐文章于 2023-08-21 09:48:46 发布