轉載:http://blog.csdn.net/u011926277/article/details/50912638
最近發現了一個非常好的學習OpenGL基礎的網站,點選打開連結。由于當中配置環境部分說得不是很詳細,經過許多天的掙紮後,終于成功地配置出了開發環境,特此記錄。
1. 下載下傳glfw。網址為:點選打開連結。由于還得翻牆,是以使用的github下載下傳的。
2. 下載下傳cMake。網址為:點選打開連結。下載下傳的版本為本網站作者使用的版本,即win32 Installer。
3. 下載下傳glew。網址為:點選打開連結。下載下傳的zip包,因為此處使用的方法是用本機編譯。
1>工具準備完畢後,在某一目錄建立一個檔案夾用于放置源代碼和庫,本機建立了OpenGL檔案夾,其下又建立有include和libs檔案夾,分别用于放置源代碼和庫,截圖如下。
2> 由于glew不用使用cMake,是以先編譯glew以獲得所需的庫。
a. 解壓glew的壓縮包,進入build目錄,用對應版本的VS打開對應的項目(選擇vs12目錄下的工程),本機用VS2013。
b. 用VS2013或者更高版本打開glew.sln,編譯此項目(右鍵->生成解決方案)。
c. 依次打開glew-2.0.0\lib\Debug\Win32,此檔案夾下有glew32sd.lib,将其拷貝到OpenGL/libs檔案夾下。
d. 将glew-2.0.0\include檔案夾下的GL檔案夾整體拷貝到OpenGL/include檔案夾下。
3> 準備glfw的庫,下面使用cMake工具首先生成項目,然後編譯,流程如下。
a. 安裝好cMake以後,打開CMake (cmake-gui),點選Browse Source将目錄設定到glfw目錄。
b. 點選Browse Build,将目錄設定到glfw/build檔案夾。(此處的build檔案夾需自己建立)如下。
c. 點選左下角Configure,選擇目标平台,由于本機為vs2013,是以選擇Visio Studio 12 2013,點選确定。
d. 然後再次點選Configure,在點選Generate生成項目,選擇狀态如下。
e. 關閉cMake,剛才建立的build目錄下已經生成了glfw的項目,用vs2013打開後,編譯,成功後生成glfw3.lib。
f. build\src\Debug目錄下有剛才編譯項目生成的glfw3.lib庫,将此庫拷貝到OpenGL/libs目錄下。
g. 将glfw-master\include下的GLFW檔案夾拷貝到OpenGL/include檔案夾下。
h. 最後生成的OpenGL檔案夾下的檔案如下:
4> 到這一步基本環境就配置完成了,下一步就是在建立的空項目中将庫和源代碼連結,并手動導入一些庫。具體流程如下。
a. 建立空項目。(c++項目)
b. 右擊項目->屬性->配置屬性->vc++目錄->包含目錄,添加路徑到OpenGL/include。
c. 右擊項目->屬性->配置屬性->vc++目錄->庫目錄,添加路徑到OpenGL/libs,截圖如下。
d. 右擊項目->屬性->配置屬性->連結器->輸入->附加依賴項,在其中添加
opengl32.Lib
glfw3.lib
glew32sd.lib
,截圖如下。
e. 依次将配置儲存後,即可在main類中輸入代碼,進行測試,如果未報錯,即環境配置成功,測試代碼可以從開頭的網站上或者,或者複制下面的代碼進行測試。
#include <iostream>
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
cop
f. 編譯運作後,出現如下視窗即代表環境配置成功。
到這一步,環境配置就結束了。
再次感謝大神的分享,環境總算是配好了!!!