1. 程式人生 > >在Visual Studio 2017上配置並使用OpenGL

在Visual Studio 2017上配置並使用OpenGL

技術 splay lock res mar ces under isp .org

在Visual Studio 2017上配置並使用OpenGL

作者:凱魯嘎吉 - 博客園 http://www.cnblogs.com/kailugaji/

首先在Windows下安裝Visual Studio 2017,參考Visual Studio 2017的安裝與使用。

1. 下載glut庫文件

在GLUT - The OpenGL Utility Toolkit:https://www.opengl.org/resources/libraries/glut/glut_downloads.php#windows中點擊“GLUT for Microsoft Windows 95 & NT users”,選擇“If you want just the GLUT header file, the .LIB, and .DLL files all pre-compiled for Intel platforms, you can simply download the glutdlls37beta.zip file (149 kilobytes).”並點擊下載。

技術分享圖片

技術分享圖片

下載完並解壓之後,會出現如下幾個文件:

技術分享圖片

2. 配置glut庫文件

找到vs2017的安裝目錄,我的安裝路徑為D:\VS2017\VC\Tools\MSVC\14.16.27023\include\,創建一個名為gl的文件夾,並將解壓的glut.h文件復制其中。

技術分享圖片

再找到路徑為 D:\VS2017\VC\Tools\MSVC\14.16.27023\lib\x86,將解壓到的glut.lib,glut32.lib復制其中。

技術分享圖片

最後把解壓到的glut.dll和glut32.dll復制到C:\Windows\System32文件夾內(32位系統)或C:\Windows\SysWOW64(64位系統)。

技術分享圖片

3. 安裝Nuget程序包

新建項目“Windows桌面向導”,選擇“空項目”。

技術分享圖片

技術分享圖片

“項目”->“管理Nuget程序包”

技術分享圖片

“瀏覽”->“搜索nupengl”

技術分享圖片

將“nupengl.core.redist”與“nupengl.core”下載下來。

4. 運行程序

“項目”->“添加新項”->“C++文件”,新建一個“.cpp”文件。

技術分享圖片

編寫第一個OpenGL程序,並按“F5”鍵運行。

#include <GL/glut.h>
void myDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
	glFlush();
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("第一個OpenGL程序");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}

顯示出一個黑色背景上的白色矩形圖。

技術分享圖片

註意:每次新建一個項目時,都應重新安裝“nupengl.core.redist”與“nupengl.core”這兩個文件。

在Visual Studio 2017上配置並使用OpenGL