配置glut.h
相关文件
添加include
文件
右键项目 exp6
,在弹出的选项中,单击 属性
点击 VC++目录->包含目录 —> 编辑
:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\opengl\include
查询方式:
点击 VC++目录->库目录 —> 编辑
:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\lib\x86
点击 链接器->输入 —> 编辑
:
OPENGL32.LIB glut32.lib glut.lib
配置GLEW
和GLFW
相关文件
添加 include 文件
- 右键项目
exp6
,在弹出的选项中,单击属性
- 点击
C/C++ —> 常规 —> 附加包含目录 —> 编辑
查询方式
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\opengl\glew-2.1.0\include
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\opengl\glfw-3.3.6.bin.WIN32\include
添加lib
文件
- 点击
链接器 —> 常规 —> 附加包含目录 —> 编辑
-
分别添加下载的
glew
和glfw
文件夹下的lib
文件夹。 -
- 当添加
glew
时,当选到lib
文件夹后请继续选择,lib -> Release -> Win32
, 请选择Win32
后点击 “选择文件夹” - 当添加
glfw
时,请选择对应版本,2019 版本请选择lib-vc2019
- 当添加
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\opengl\glfw-3.3.6.bin.WIN32\lib-vc2019
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\opengl\glew-2.1.0\lib\Release\Win32
添加库依赖项
- 点击
链接器 —> 输入 —> 附加依赖项 —> 编辑
测试运行一份代码:
#include <gl/glut.h>
//正方形的位置和大小
GLfloat x1 = 100.0f;
GLfloat y1 = 150.0f;
GLsizei rsize = 50;
//正方形运动变化的步长
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
//窗口的大小
GLfloat windowWidth;
GLfloat windowHeight;
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(x1, y1, x1 + rsize, y1 + rsize);
glutSwapBuffers();//清空命令缓冲区并交换帧缓存
}
void ChangeSize(GLsizei w, GLsizei h)
{
if (h == 0)
{
h = 1;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
{
windowHeight = 250.0f * h / w;
windowWidth = 250.0f;
}
else
{
windowHeight = 250.0f;
windowWidth = 250.0f * w / h;
}
glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void TimerFunction(int value)
{
//处理到达窗口边界的正方形,使之反弹
if (x1 > windowWidth - rsize || x1 < 0)
{
xstep = -xstep;
}
if (y1 > windowHeight - rsize || y1 < 0)
{
ystep = -ystep;
}
if (x1 > windowWidth - rsize)
{
x1 = windowWidth - rsize - 1;
}
if (y1 > windowHeight - rsize)
{
y1 = windowHeight - rsize - 1;
}
//根据步长修改正方形的位置
x1 += xstep;
y1 += ystep;
//用新坐标重新绘图
glutPostRedisplay();
glutTimerFunc(50, TimerFunction, value);
}
void SetupRC()
{
//设置窗口的清除色为蓝色
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
void main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(500, TimerFunction, 1);
SetupRC();
glutMainLoop();
}