- #include <stdio.h>
- #include <windows.h>
- #include <gl/gl.h>
- #include <gl/glu.h>
- #include <gl/glut.h>
- void display(void);
- void reshape(int width,int height);
- void myidelefunc(void);
- void mouseDo(int button,int state,int x,int );
- int jiaodu=0;
- GLdouble vertexs[]=
- {
- 0.0,0.0,
- 0,0.4,
- 0.6,0.4,
- 0.6,0.0
- };
- int main(int argc,char **argv)
- {
- /**
- 初始化
- */
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
- glutInitWindowSize(400,400);
- glutInitWindowPosition(100,100);
- glutCreateWindow("window");
- glutDisplayFunc(display);
- // glutIdleFunc(myidelefunc);
- glutMouseFunc(mouseDo);
- glutReshapeFunc(reshape);
- glLineWidth(3);
- glutMainLoop();
- return 0;
- }
- /**
- 界面可现实的时候的回调函数
- */
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.1f,0.2f,0.8f);
- glEnableClientState(GL_VERTEX_ARRAY);//指定需要激活点数组
- glVertexPointer(2,GL_DOUBLE,0,vertexs);//指定点数组的数据
- glPushMatrix();//复制栈里面最上的矩阵并压如栈
- glColor3f(0.9f,0.2f,0.8f);//改变颜色
- glTranslatef(0.3f,0.2f,0.0f);//移动
- glRotatef(jiaodu,0,0,1);//旋转
- glTranslatef(-0.3f,-0.2f,0.0f);
- //画一个矩形
- glBegin(GL_QUADS);
- glArrayElement(0);
- glArrayElement(1);
- glArrayElement(2);
- glArrayElement(3);
- glEnd();
- glPopMatrix();//把矩阵踢出栈
- glColor3f(1,1,1);//改变画笔的颜色
- glEnable(GL_LINE_STIPPLE);//激活点画线
- glLineStipple(1,145);//设置点画线的模式 145对应的二进制是10010001所以现这种模式的虚线
- //画一个x轴线和y
- glBegin(GL_LINES);
- glVertex2f(-1,0);
- glVertex2f(1,0);
- glVertex2f(0,1);
- glVertex2f(0,-1);
- glEnd();
- glDisable(GL_LINE_STIPPLE);//关闭点画线的功能
- glutSwapBuffers();//因为使用的是双缓存,所以这里必须要交换缓存才会显示
- }
- /**
- 界面大小改变的时候回调函数
- */
- void reshape(int width,int height)
- {
- printf("is reshape");
- glViewport(0,0,width,height);
- glLoadIdentity();
- }
- void mouseDo(int button,int state,int x,int y)
- {
- switch(state)
- {
- case GLUT_DOWN:
- jiaodu=jiaodu%360;
- jiaodu+=10;
- glutPostRedisplay();
- break;
- }
- }
- /**
- 空闲的时候的回调函数
- */
- void myidelefunc(void)
- {
- }
- //该片段来自于http://www.codesnippet.cn/detail/0708201410162.html
来源: http://www.codesnippet.cn/detail/0708201410162.html