要使用GLUT库,要将glut32.lib放到VC98\Lib下,将glut.h放到VC98\Include\GL下,并在程序中包含进glut.h。
#include <GL\glut.h>
#include <stdlib.h>
GLfloat point1[] = {0.25,0.25,0.0};
GLfloat point2[] = {0.75,0.25,0.0};
GLfloat point3[] = {0.75,0.75,0.0};
GLfloat point4[] = {0.25,0.75,0.0};
GLfloat point5[] = {0.3,0.45,0.0};
GLfloat point6[] = {0.5f,0.5f,0.0f};
void init(void)
{//初始化OpenGL参数
glClearColor(0.0,0.0,0.0,0.0);//设置清空颜色
glShadeModel(GL_FLAT);//设置阴影模式
glLineWidth(2.0);
glLineStipple(1,0xCCFC);
glEnable(GL_LINE_STIPPLE);
}
void DrawPoint(void)
{//画点
glColor3f(0.0f,0.0f,1.0f);
glPointSize(10.0);
glBegin(GL_POINTS);
glVertex3fv(point6);
glEnd();
}
void DrawPolyGon(void)
{//画正方形
glColor3f(0.3f,0.2f,0.0f);//设置绘制颜色
glBegin(GL_POLYGON);//绘制正方形
glVertex3fv(point1);
glVertex3fv(point2);
glVertex3fv(point3);
glVertex3fv(point4);
glEnd();
}
void DrawLines(void)
{//画直线
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINES);
glVertex3fv(point1);
glVertex3fv(point3);
glVertex3fv(point2);
glVertex3fv(point4);
glEnd();
}
void DrawTriangle(void)
{//画三角形
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_TRIANGLES);
glVertex3fv(point1);
glVertex3fv(point5);
glVertex3fv(point6);
glEnd();
}
void DrawLinesStrip(void)
{
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_LINE_STRIP);
glVertex3fv(point1);
glVertex3fv(point3);
glVertex3fv(point5);
glVertex3fv(point2);
glVertex3fv(point4);
glEnd();
}
void DrawLineLoop(void)
{
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_LINE_LOOP);
glVertex3fv(point1);
glVertex3fv(point3);
glVertex3fv(point5);
glVertex3fv(point2);
glVertex3fv(point4);
glEnd();
}
void display(void)
{//绘制函数
glClear(GL_COLOR_BUFFER_BIT);//清空缓存
DrawPolyGon();
DrawPoint();
DrawTriangle();
DrawLines();
DrawLinesStrip();
DrawLineLoop();
glutSwapBuffers();//交换缓存
}
void reshape(int w,int h)
{//重画函数,初次绘制,移动窗口或改变窗口大小时调用
glViewport(0,0,(GLsizei)w,(GLsizei)h);//视口变换
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);//投影变换
gluOrtho2D(0.0f,1.0f,0.0f,1.0f);
glMatrixMode(GL_MODELVIEW);//开始模型视图变换
glLoadIdentity();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//设置显示模式为"双缓冲,RGB"
glutInitWindowPosition(100,100);//窗口左上角在(100,100);
glutInitWindowSize(500,400);//窗口大小为宽500,高400
glutCreateWindow("Hello OpenGL");//创建窗口,标题为"Hello OpenGL"
init();//初始化OpenGL参数
glutDisplayFunc(display);//注册显示回调函数
glutReshapeFunc(reshape);//注册重画函数
glutMainLoop();
return 0;
}
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/04/26/728849.html,如需转载请自行联系原作者