首页 > 代码库 > OpenGL用法暨第一个OpenGL程序

OpenGL用法暨第一个OpenGL程序

//OpenGL的书写规范 暨我的第一个OpenGL程序

//有了框架,其他就什么都好说了

#include "..\H\glut.h" 

#pragma comment(lib, "glut.lib")

void init(void)
{
glClearColor(1.0, 1.0, 0.0, 0.0);//设置主窗口的背景颜色
glColor3f(1.0f,0.0f,0.0f);  // 设置所绘图形的颜色
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);

}


void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);// 将缓存清除为预先的设置值
/*------------------------------------------------------------------------*/
//此处添加需要绘制的图形
glBegin(GL_POLYGON);
glVertex3f(200.0,200.0,0.0);
glVertex3f(200.0,300.0,0.0);
glVertex3f(300.0,300.0,0.0);
glVertex3f(300.0,200.0,0.0);
glEnd();
//
glBegin(GL_POINTS);
glVertex2i(60,60);
glVertex2i(80,80);
glVertex2i(30,50);
glEnd();
//
glBegin(GL_POINTS);
for(int i=50;i<400;i=i+10)
glVertex2i(i,50);
glEnd();
//
glBegin(GL_LINES);//两条线段
glVertex2i(85,80);
glVertex2i(100,100);
glVertex2i(110,90);
glVertex2i(130,150);
glVertex2i(100,205);//不会画出
glEnd();
//
glBegin(GL_LINE_STRIP);//依次链接的折现
glVertex2i(200,100);
glVertex2i(290,180);
glVertex2i(250,90);
glVertex2i(230,150);
glVertex2i(270,200);
glEnd();
//
glColor3f(0.0f,0.0f,1.0f);// 下方颜色
glBegin(GL_LINE_LOOP);//依次链接 直至 封闭
glVertex2i(250,300);
glVertex2i(300,280);
glVertex2i(190,300);
glVertex2i(330,330);
glVertex2i(300,350);
glEnd();
/*------------------------------------------------------------------------*/
glFlush();//强制OpenGL函数在有限时间内运行

}


int main(int argc, char ** argv)
{
glutInit(&argc, argv);//初始化GLUT库
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB|GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);//单缓冲、RGB模式|其他模式
glutInitWindowSize(500,500);//窗口尺寸大小
glutInitWindowPosition(200, 200);//距离屏幕边框距离
glutCreateWindow("OpenGL_Window");//窗口标题
init();
glutDisplayFunc(myDisplay);//显示需要绘制的图形
glutMainLoop();//循环
return 0;

}


技术分享

OpenGL用法暨第一个OpenGL程序