参考网址:http://www.cnblogs.com/FredCong/archive/2012/10/13/2722893.html
#include <glut.h> #include <math.h> #include <stdio.h> #include <stdlib.h> void myDisplay1(void) { glClear(GL_COLOR_BUFFER_BIT); glRectf(-0.5f, -0.5f, 0.5f, 0.5f); glFlush(); } void myDisplay2() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINES);//GL_POINTS GL_LINE_STRIP,GL_LINE_LOOP,GL_TRIANGLES,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN glVertex2f(-0.5f, -0.5f);//LINE1 glVertex2f(0.5f, 0.5f);//LINE1 glVertex2f(-0.5f, 0.5f);//LINE2 glVertex2f(0.5f, -0.5f);//LINE2 glEnd(); glFlush(); } const int n = 100; const GLfloat R = 0.5f; const GLfloat Pi = 3.1415926f; void myDisplay3() { int i; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); for (i=0; i<n; ++i) { glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i)); } glEnd(); glFlush(); } void myDisplay4(void) { GLfloat a = 1 / (2-2*cos(72*Pi/180)); GLfloat bx = a * cos(18 * Pi/180); GLfloat by = a * sin(18 * Pi/180); GLfloat cy = -a * cos(18 * Pi/180); GLfloat PointA[2] = { 0, a }, PointB[2] = { bx, by }, PointC[2] = { 0.5, cy }, PointD[2] = { -0.5, cy }, PointE[2] = { -bx, by }; glClear(GL_COLOR_BUFFER_BIT); // 按照A->C->E->B->D->A的顺序,可以一笔将五角星画出 glBegin(GL_LINE_LOOP); glVertex2fv(PointA); glVertex2fv(PointC); glVertex2fv(PointE); glVertex2fv(PointB); glVertex2fv(PointD); glEnd(); glFlush(); } void myDisplay5() { glClear(GL_COLOR_BUFFER_BIT); //glLineWidth(5.0f); //glPointSize(2.0f); glEnable(GL_LINE_STIPPLE); glLineStipple(1, 0X0f01);//第一个参数是单位像素点的个数, 第二个参数16位,每位控制了param1 个像素点是否显示 glBegin(GL_LINES); glVertex2f(-1.0f, 0.0f); glVertex2f(1.0f,0.0f); glVertex2f(0.0f,-1.0f); glVertex2f(0.0f,1.0f); glEnd(); glBegin(GL_LINE_STRIP); for (int i=0; i<100; i++) glVertex2f(1.0f/100*i, 0.5*cos(2.0f*Pi/100*i)); glEnd(); glFlush(); } void myDisplay6() { glClear(GL_COLOR_BUFFER_BIT); glPolygonMode(GL_FRONT,GL_FILL);//设置正面的显示模式 填充 glPolygonMode(GL_BACK, GL_LINE);//设置反面的现实模式 线形 glFrontFace(GL_CCW);//设置哪个是正面,这里将逆时针设置为多边形的正面 glBegin(GL_POLYGON); glVertex2f(-0.5f, -0.5f);//逆时针画一个多边形 glVertex2f(0.0f, -0.5f); glVertex2f(0.0f, 0.0f); glVertex2f(-0.5f, 0.0f); glEnd(); glBegin(GL_POLYGON); glVertex2f(0.0f,0.0f);//顺时针刷一个多边形 glVertex2f(0.0f, 0.5f); glVertex2f(0.5f, 0.5f); glVertex2f(0.5f, 0.0f); glEnd(); glFlush(); } void myDisplay7() { static GLubyte Mask[128]; FILE *fp; fp = fopen("C:\\Users\\sunny\\Desktop\\mask.bmp", "rb");//32x32尺寸单色图像,每个像素占用1bit,坐标原点可能是在左下角 if(!fp) exit(0); if(fseek(fp, -(int)sizeof(Mask), SEEK_END))//直接从后128byte开始读取,这样是为了跳过bmp文件头 exit(0); if(!fread(Mask, sizeof(Mask), 1,fp)) exit(0); fclose(fp); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_POLYGON_STIPPLE); glPolygonStipple(Mask); glRectf(-0.5f, -0.5f, 0.0f, 0.0f); glDisable(GL_POLYGON_STIPPLE); glRectf(0.0f, 0.0f, 0.5f, 0.5f); // 在右上方绘制一个无镂空效果的正方形 glFlush(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("我的第一个opengl程序"); glutDisplayFunc(&myDisplay7); glutMainLoop(); return 0; }
myDisplay1~7的效果
mask: