openGL学习笔记四: freeglut库及使用

  freeglut是opengl跨平台实用工具库,用于做窗口界面,封装各个平台鼠标键盘事件等等。用于替代早期的glut库(1998年后就不在更新维护)。

安装及使用

环境:win7 VS2013
1. 下载freeglut:

  地址:https://sourceforge.net/projects/freeglut/

在这里插入图片描述

2. cmake打开生成VS工程:

a. build目录:为VS工程生成目录。
b. FREEGLUT_BUILD_SHARED_LIBS、FREEGLUT_BUILD_STATIC_LIBS:分别为共享动态库项目和静态库项目这里都选择。
c. 点击Configure(配置),再点击Generate(生成)。

在这里插入图片描述
生成freeglut工程后,选择freeglut项目进行编译得到freeglutd.lib、freeglutd.dll 库文件

3. opengl项目配置:

a. 项目属性 ----> C/C++ —> 附加包含目录 —> your_path\freeglut-3.2.1\include
b. 项目属性 ----> 链接器 —> 常规 —> 附加库目录 —> your_path\lib
c. 项目属性 ----> 链接器 —> 输入 —> 附加依赖项 —> freeglutd.lib
d. freeglutd.dll 库文件放到运行程序exe目录下

4. 代码:

来自OpenGL Code Samples


#include <iostream>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32) || defined(WIN32)
#include <windows.h> 
#endif

#include <GL/glut.h>


GLenum doubleBuffer;
GLint thing1, thing2;

static void Init(void) {

	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClearAccum(0.0, 0.0, 0.0, 0.0);

	thing1 = glGenLists(1);
	glNewList(thing1, GL_COMPILE);
	glColor3f(1.0, 0.0, 0.0);
	glRectf(-1.0, -1.0, 1.0, 0.0);
	glEndList();

	thing2 = glGenLists(1);
	glNewList(thing2, GL_COMPILE);
	glColor3f(0.0, 1.0, 0.0);
	glRectf(0.0, -1.0, 1.0, 1.0);
	glEndList();
}

static void Reshape(int width, int height) {

	glViewport(0, 0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

static void Key(unsigned char key, int x, int y) {

	switch(key) {
		case '1':
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
			glutPostRedisplay();
			break;
		case '2':
			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
			glutPostRedisplay();
			break;
		case 27:
			exit(0);
	}
}

static void Draw(void) {

	glPushMatrix();

	glScalef(0.8, 0.8, 1.0);

	glClear(GL_COLOR_BUFFER_BIT);
	glCallList(thing1);
	glAccum(GL_LOAD, 0.5);

	glClear(GL_COLOR_BUFFER_BIT);
	glCallList(thing2);
	glAccum(GL_ACCUM, 0.5);

	glAccum(GL_RETURN, 1.0);

	glPopMatrix();

	if(doubleBuffer) {
		glutSwapBuffers();
	}
	else {
		glFlush();
	}
}

static void Args(int argc, char **argv) {
	GLint i;

	doubleBuffer = GL_FALSE;

	for(i = 1; i < argc; i++) {
		if(strcmp(argv[i], "-sb") == 0) {
			doubleBuffer = GL_FALSE;
		}
		else if(strcmp(argv[i], "-db") == 0) {
			doubleBuffer = GL_TRUE;
		}
	}
}

static void showGlutInfo() {

	const GLubyte* name = glGetString(GL_VENDOR);
	const GLubyte* biaoshifu = glGetString(GL_RENDERER);
	const GLubyte* OpenGLVersion = glGetString(GL_VERSION);
	const GLubyte* gluVersion = gluGetString(GLU_VERSION);

	std::cout << "OpenGL实现厂商的名字:" << name << std::endl;
	std::cout << "渲染器标识符:" << biaoshifu << std::endl;
	std::cout << "OpenGL实现的版本号:" << OpenGLVersion << std::endl;
	std::cout << "OGLU工具库版本:" << gluVersion << std::endl;
}

int main(int argc, char **argv) {
	GLenum type;

	glutInit(&argc, argv);
	Args(argc, argv);

	type = GLUT_RGB | GLUT_ACCUM;
	type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
	glutInitDisplayMode(type);
	glutInitWindowSize(300, 300);
	glutCreateWindow("Accum Test");

	Init();

	showGlutInfo();

	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Key);
	glutDisplayFunc(Draw);
	glutMainLoop();
}
5. 运行结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值