OpenGL入门笔记(十)

本文介绍了一个使用OpenGL进行基本图形绘制的示例程序,包括点、线、多边形等元素的绘制过程。通过该示例,读者可以了解如何在Windows环境下配置OpenGL环境,并掌握基本的OpenGL函数使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要使用GLUT库,要将glut32.lib放到VC98/Lib下,将glut.h放到VC98/Include/GL下,并在程序中包含进glut.h
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif #include < GL/glut.h >
None.gif#include
< stdlib.h >
None.gif
ExpandedBlockStart.gifContractedBlock.gifGLfloatpoint1[]
= dot.gif {0.25,0.25,0.0} ;
ExpandedBlockStart.gifContractedBlock.gifGLfloatpoint2[]
= dot.gif {0.75,0.25,0.0} ;
ExpandedBlockStart.gifContractedBlock.gifGLfloatpoint3[]
= dot.gif {0.75,0.75,0.0} ;
ExpandedBlockStart.gifContractedBlock.gifGLfloatpoint4[]
= dot.gif {0.25,0.75,0.0} ;
ExpandedBlockStart.gifContractedBlock.gifGLfloatpoint5[]
= dot.gif {0.3,0.45,0.0} ;
ExpandedBlockStart.gifContractedBlock.gifGLfloatpoint6[]
= dot.gif {0.5f,0.5f,0.0f} ;
None.gif
None.gif
void init( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//初始化OpenGL参数
InBlock.gif
glClearColor(0.0,0.0,0.0,0.0);//设置清空颜色
InBlock.gif
glShadeModel(GL_FLAT);//设置阴影模式
InBlock.gif
glLineWidth(2.0);
InBlock.gifglLineStipple(
1,0xCCFC);
InBlock.gifglEnable(GL_LINE_STIPPLE);
ExpandedBlockEnd.gif}

None.gif
None.gif
void DrawPoint( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//画点
InBlock.gif
glColor3f(0.0f,0.0f,1.0f);
InBlock.gifglPointSize(
10.0);
InBlock.gifglBegin(GL_POINTS);
InBlock.gifglVertex3fv(point6);
InBlock.gifglEnd();
ExpandedBlockEnd.gif}

None.gif
None.gif
void DrawPolyGon( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//画正方形
InBlock.gif

InBlock.gifglColor3f(
0.3f,0.2f,0.0f);//设置绘制颜色
InBlock.gif

InBlock.gifglBegin(GL_POLYGON);
//绘制正方形
InBlock.gif
glVertex3fv(point1);
InBlock.gifglVertex3fv(point2);
InBlock.gifglVertex3fv(point3);
InBlock.gifglVertex3fv(point4);
InBlock.gifglEnd();
ExpandedBlockEnd.gif}

None.gif
None.gif
void DrawLines( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//画直线
InBlock.gif
glColor3f(0.0f,1.0f,0.0f);
InBlock.gif
InBlock.gif
InBlock.gifglBegin(GL_LINES);
InBlock.gifglVertex3fv(point1);
InBlock.gifglVertex3fv(point3);
InBlock.gifglVertex3fv(point2);
InBlock.gifglVertex3fv(point4);
InBlock.gifglEnd();
ExpandedBlockEnd.gif}

None.gif
None.gif
void DrawTriangle( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//画三角形
InBlock.gif
glColor3f(0.0f,1.0f,0.0f);
InBlock.gif
InBlock.gifglBegin(GL_TRIANGLES);
InBlock.gifglVertex3fv(point1);
InBlock.gifglVertex3fv(point5);
InBlock.gifglVertex3fv(point6);
InBlock.gifglEnd();
ExpandedBlockEnd.gif}

None.gif
None.gif
void DrawLinesStrip( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifglColor3f(
1.0f,1.0f,1.0f);
InBlock.gif
InBlock.gif
InBlock.gifglBegin(GL_LINE_STRIP);
InBlock.gifglVertex3fv(point1);
InBlock.gifglVertex3fv(point3);
InBlock.gifglVertex3fv(point5);
InBlock.gifglVertex3fv(point2);
InBlock.gifglVertex3fv(point4);
InBlock.gif
InBlock.gifglEnd();
ExpandedBlockEnd.gif}

None.gif
None.gif
void DrawLineLoop( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifglColor3f(
1.0f,1.0f,1.0f);
InBlock.gif
InBlock.gifglBegin(GL_LINE_LOOP);
InBlock.gifglVertex3fv(point1);
InBlock.gifglVertex3fv(point3);
InBlock.gifglVertex3fv(point5);
InBlock.gifglVertex3fv(point2);
InBlock.gifglVertex3fv(point4);
InBlock.gif
InBlock.gifglEnd();
ExpandedBlockEnd.gif}

None.gif
None.gif
void display( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//绘制函数
InBlock.gif
glClear(GL_COLOR_BUFFER_BIT);//清空缓存
InBlock.gif
DrawPolyGon();
InBlock.gifDrawPoint();
InBlock.gifDrawTriangle();
InBlock.gifDrawLines();
InBlock.gifDrawLinesStrip();
InBlock.gifDrawLineLoop();
InBlock.gifglutSwapBuffers();
//交换缓存
ExpandedBlockEnd.gif
}

None.gif
None.gif
void reshape( int w, int h)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//重画函数,初次绘制,移动窗口或改变窗口大小时调用
InBlock.gif
glViewport(0,0,(GLsizei)w,(GLsizei)h);//视口变换
InBlock.gif
glMatrixMode(GL_PROJECTION);
InBlock.gifglLoadIdentity();
InBlock.gif
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);//投影变换
InBlock.gif
gluOrtho2D(0.0f,1.0f,0.0f,1.0f);
InBlock.gifglMatrixMode(GL_MODELVIEW);
//开始模型视图变换
InBlock.gif
glLoadIdentity();
ExpandedBlockEnd.gif}

None.gif
None.gif
int main( int argc, char ** argv)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifglutInit(
&argc,argv);
InBlock.gifglutInitDisplayMode(GLUT_DOUBLE
|GLUT_RGB);//设置显示模式为"双缓冲,RGB"
InBlock.gif
glutInitWindowPosition(100,100);//窗口左上角在(100,100);
InBlock.gif
glutInitWindowSize(500,400);//窗口大小为宽500,高400
InBlock.gif
glutCreateWindow("HelloOpenGL");//创建窗口,标题为"HelloOpenGL"
InBlock.gif
init();//初始化OpenGL参数
InBlock.gif
glutDisplayFunc(display);//注册显示回调函数
InBlock.gif
glutReshapeFunc(reshape);//注册重画函数
InBlock.gif
glutMainLoop();
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif

200742605.JPG
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值