(以下信息取自维基百科,www.opengl.org,以及各个前辈博客)
OpenGL入门
OpenGL(Open Graphics Library),开发图形库,是个定义了一个跨编程语言,跨平台的应用程序接口(API)的规范,它用于生成二维,三维图像。
结合课内所学习的Computer Graphics图形学基础,最近开始学习OpenGL,感觉还是蛮有趣的。
下面是入门程序。
#include <gl\glut.h>
#include <stdlib.h>
void init(void)
{
//clear values for the color buffers
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//select flat or smooth shading.GL_FLAT or GL_SMOOTH
glShadeModel(GL_SMOOTH);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//不规则图形
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(500.0f, 200.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(600.0f, 450.0f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(700.0f, 200.0f, 0.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(450.0f, 350.0f, 0.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(750.0f, 350.0f, 0.0f);
glEnd();
//正方体
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(100.0f, 300.0f, 0.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(300.0f, 300.0f, 0.0f);
glColor3f(0.1f, 0.1f, 0.1f);
glVertex3f(300.0f, 100.0f, 0.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(100.0f, 100.0f, 0.0f);
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(100.0f, 300.0f, 0.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(300.0f, 300.0f, 0.0f);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(400.0f, 350.0f, 0.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(200.0f, 350.0f, 0.0f);
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(300.0f, 100.0f, 0.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(300.0f, 300.0f, 0.0f);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(400.0f, 350.0f, 0.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(400.0f, 150.0f, 0.0f);
glEnd();
glFlush();
}
void reshape(int width, int height)
{
//glViewport specifies the affine transformationof x and y from normalized device coordinates to window coordinates.
glViewport(0, 0, width, height);
//glMatrixMode sets the current matrix mode. GL_PROJECTION为投影,GL_MODELVIEW模型视图,GL_TEXTURE纹理
glMatrixMode(GL_PROJECTION);
//replace the current matrix with the identity matrix
glLoadIdentity();
//set up a two-dimensional orthographic viewing region.
gluOrtho2D(0.0, width, 0.0, height);
}
void keyboard(unsigned char key, int x, int y){
switch (key) {
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
//glutInit will initialize the GLUT library and negotiate a session with window system
glutInit(&argc, argv);
//to sets the initial display mode.GLUT_SINGLE is to select a single bufferd window
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//to set the intial window size and position
glutInitWindowSize(1000, 600);
glutInitWindowPosition(0, 0);
//create a top-level window titled Computer graphics
glutCreateWindow("Computer Graphics");
init();
//display callback for the current window
glutDisplayFunc(display);
//reshape callback for the current window
glutReshapeFunc(reshape);
//keyboard callback for the current window
glutKeyboardFunc(keyboard);
/*glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will
never return. It will call as necessary any callbacks that have been registered.*/
glutMainLoop();
return 0;
}