OpenGL-学习之路-OpenGL入门

(以下信息取自维基百科,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;
}

效果图:

我也不知道右边那是什么鬼,画得自己都嫌弃Orz。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值