freeglut是opengl跨平台实用工具库,用于做窗口界面,封装各个平台鼠标键盘事件等等。用于替代早期的glut库(1998年后就不在更新维护)。
【我这里使用的是编译好的包,可以直接跳到第3步,配置。】
安装及使用
环境:win10 VS2019
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++ —> 附加包含目录 —> D:\Soft\freeglut-3.0.0\include
b. 项目属性 ----> 链接器 —> 常规 —> 附加库目录 —> D:\Soft\freeglut-3.0.0\Build-x64\lib
c. 项目属性 ----> 链接器 —> 输入 —> 附加依赖项 —>
opencv_world3411d.lib
opencv_world3411.lib
#上面两个是opencv的#下面这行我加了绝对路径,不然找不到freeglutd.lib
#至于上面opencv的附加依赖项没有绝对路径,应该是opencv配置了环境变量
#而freeglutd没有添加环境变量
D:\Soft\freeglut-3.0.0\Build-x64\lib\Debug\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();
}