OpenGL绘制一个点、线、多边形

来源:互联网 发布:python程序设计答案 编辑:程序博客网 时间:2024/04/29 11:34
#include "stdafx.h"#include <windows.h> #include <math.h>// #include <GL\GL.h>// #include <GL\GLU.h>#include <GL\glut.h>const int n = 20;const GLfloat R = 0.5f;const GLfloat Pi = 3.1415926536f;void MyDisplay(void){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();//该函数的功能是重置当前指定的矩阵为单位矩阵.//绘制点glPointSize(5.0);//点的像素大小,默认值为1.0glBegin(GL_POINTS);               glVertex2f(0.0f, 0.0f);glVertex2f(0.5f, 0.5f);glEnd();//绘制直线glLineWidth(2.0f);//直线的宽度。glEnable(GL_LINE_STIPPLE);//绘制虚线glLineStipple(2, 0x0F0F);glBegin(GL_LINES); //绘制实线glVertex2f(0.5f, 0.6f);glVertex2f(0.6f, 0.9f);glEnd();//绘制多边形glColor3f(255.0,0.0,0.0);glBegin(GL_POLYGON);//glBegin(GL_LINE_LOOP);//glBegin(GL_POINTS);for(int i=0; i<n; ++i)glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));glEnd(); glFlush();glutSwapBuffers();//glutInitDisplayMode设置为GLUT_DOUBLE需要交换缓存}int main(int argc, _TCHAR* argv[]){glutInit(&argc, (char**) argv);  glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);  glutInitWindowPosition(400,400);  glutInitWindowSize(520,520);  glutCreateWindow("Hello OpenGL");  glutDisplayFunc(MyDisplay);  glutMainLoop();//enters the GLUT event processing loop.  getchar();return 0;}

0 0