這個博客已經(jīng)寫的非常好了:OpenGL 開發(fā)環(huán)境配置:Visual Studio 2017 + GLFW + GLEW
我把上面的鏈接內(nèi)容摘要一下:
-
相關(guān)下載鏈接:
1.CMake下載地址:https://cmake.org/download/
2.GLFW下載地址 :http://www.glfw.org/download.html
3.GLEW下載地址 :http://glew.sourceforge.net/
-
使用CMake生成vs解決方案(*.sln)
如果沒有下載預(yù)編譯文件蛋欣,那么需要使用CMake來生成對應(yīng).sln并在vs2017中編譯生成相應(yīng)靜態(tài)鏈接庫.lib船老。
執(zhí)行過程:打開CMake -> source code一欄里填包含CMakeList.txt的目標(biāo)文件夾 -> 填寫build目錄 -> 點Config -> 點Generate -> 打開對應(yīng).sln -> 設(shè)置為生成靜態(tài)鏈接庫編譯生成.lib亲配。
-
最終需要用到的文件
1.頭文件:將GLFW和GLEW中include內(nèi)的文件(夾)拷貝到D:\Program Files\OpenGL\headers缘眶;
2.lib文件:將GLFW和GLEW編譯生成的靜態(tài)鏈接庫拷貝到D:\Program Files\OpenGL\libs;
-
新建空工程并配置
1.VC++目錄->包含目錄:添加 D:\Program Files\OpenGL\headers淹遵;
2.VC++目錄->庫目錄:添加D:\Program Files\OpenGL\libs咽安;
3.鏈接器->輸入:添加opengl32.lib,glfw3.lib和glew32d.lib衔掸。
-
以上都搞定后烫幕,新建測試代碼即可:
#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);
}