OpenGL學習--開發環境


1. VS2017 Professional安裝

1.1. 下載

mu_visual_studio_professional_2017_x86_x64_10049787.exe

1.2. 雙擊開始安裝

雙擊mu_visual_studio_professional_2017_x86_x64_10049787.exe開始安裝

clip_image001[1]

clip_image003[1]

clip_image005[1]

安裝完成后重啟計算機

1.3. 啟動

點擊菜單中的【Visual Studio 2017】啟動

clip_image006[1]

clip_image007[1]

1.4. 注冊

clip_image009[1]

輸入以下序列號

Visual Studio 2017 Key
企業版:Enterprise:
NJVYC-BMHX2-G77MM-4XJMR-6Q8QF
專業版:Professional
KBJFW-NXHK6-W4WJM-CRMQB-G3CDH

clip_image011[1]

2. OpenGL環境配置

https://www.opengl.org/sdk/libs/

1.5. GLEW(OpenGL Extension Wrangler Library)

GLEW is an open-source cross-platform extension loading library with thread-safe support for multiple rendering contexts and automatic code generation capability. GLEW provides easy-to-use and efficient methods for checking OpenGL extensions and core functionality.

GLEW是一個跨平台的C++擴展庫,基於OpenGL圖形接口。使用OpenGL的朋友都知道,window目前只支持OpenGL1.1的函數,但 OpenGL現在都發展到2.0以上了,要使用這些OpenGL的高級特性,就必須下載最新的擴展,另外,不同的顯卡公司,也會發布一些只有自家顯卡才支持的擴展函數,你要想用這數函數,不得不去尋找最新的glext.h,有了GLEW擴展庫,你就再也不用為找不到函數的接口而煩惱,因為GLEW能自動識別你的平台所支持的全部OpenGL高級擴展函數。也就是說,只要包含一個glew.h頭文件,你就能使用gl,glu,glext,wgl,glx的全部函數。GLEW支持目前流行的各種操作系統(including Windows, Linux, Mac OS X, FreeBSD, Irix, and Solaris)。

1.5.1. 下載

https://sourceforge.net/projects/glew/?source=typ_redirect

1.5.2. 配置
1.5.2.1. glew.dll

bin/glew32.dll

    to   

%SystemRoot%/system32

1.

glew-2.0.0\bin\Release\Win32\glew32.dll

glew-2.0.0\bin\Release\Win32\glewinfo.exe

glew-2.0.0\bin\Release\Win32\visualinfo.exe

復制到

C:\Windows\System32

目錄下

2.

glew-2.0.0\bin\Release\x64\glew32.dll

glew-2.0.0\bin\Release\x64\glewinfo.exe

glew-2.0.0\bin\Release\x64\visualinfo.exe

復制到

C:\Windows\SysWOW64

目錄下

1.5.2.2. glew32.lib

lib/glew32.lib    to    {VC Root}/Lib

1.將

glew-2.0.0-win32\glew-2.0.0\lib\Release\Win32\glew32.lib

glew-2.0.0-win32\glew-2.0.0\lib\Release\Win32\glew32s.lib

復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\lib\x86

目錄下

2.將

glew-2.0.0-win32\glew-2.0.0\lib\Release\x64\glew32.lib

glew-2.0.0-win32\glew-2.0.0\lib\Release\x64\glew32s.lib

復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\lib\x64

目錄下

1.5.2.3. glew.h和wglew.h

include/GL/glew.h    to    {VC Root}/Include/GL

include/GL/wglew.h    to    {VC Root}/Include/GL

glew-2.0.0-win32\glew-2.0.0\include\GL

復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\include\GL

目錄下

1.6. GLFW

GLFW is a free, Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan application development. It provides a simple, platform-independent API for creating windows, contexts and surfaces, reading input, handling events, etc.

1.6.1. 下載

http://www.glfw.org/

1.6.2. 配置
1.6.2.1. glfw3.dll

bin/glfw3.dll

    to   

%SystemRoot%/system32

1.

glfw-3.2.1.bin.WIN32\lib-vc2015\glfw3.dll

復制到

C:\Windows\System32

目錄下

2.

glfw-3.2.1.bin.WIN64\lib-vc2015\glfw3.dll

復制到

C:\Windows\SysWOW64

目錄下

1.6.2.2. glfw3.lib

1.將

glfw-3.2.1.bin.WIN32\lib-vc2015\glfw3.lib

glfw-3.2.1.bin.WIN32\lib-vc2015\glfw3dll.lib

復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\lib\x86

目錄下

2.將

glfw-3.2.1.bin.WIN64\lib-vc2015\glfw3.lib

glfw-3.2.1.bin.WIN64\lib-vc2015\glfw3dll.lib

復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\lib\x64

目錄下

1.6.2.3. glfw3.h

D:\glfw-3.2.1\include

glfw-3.2.1.bin.WIN64\include\GLFW

復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\include\GLFW

目錄下

1.7. GLM

OpenGL Mathematics (GLM) is a header only C++ mathematics library for graphics software based on the OpenGL Shading Language (GLSL) specifications.

GLM provides classes and functions designed and implemented with the same naming conventions and functionality than GLSL so that anyone who knows GLSL, can use GLM as well in C++.

This project isn't limited to GLSL features. An extension system, based on the GLSL extension conventions, provides extended capabilities: matrix transformations, quaternions, data packing, random numbers, noise, etc...

1.7.1. 下載

https://github.com/g-truc/glm

1.7.2. 配置

將glm-0.9.8.4\glm目錄復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\include\

目錄下

clip_image013[1]

1.8. SOIL

SOIL is a tiny C library used primarily for uploading textures into OpenGL. It is based on stb_image version 1.16, the public domain code from Sean Barrett (found here). I have extended it to load TGA and DDS files, and to perform common functions needed in loading OpenGL textures. SOIL can also be used to save and load images in a variety of formats (useful for loading height maps, non-OpenGL applications, etc.)

1.8.1. 下載

http://www.lonesock.net/soil.html

1.8.2. 配置
1.8.2.1. libSOIL.a

1.將

soil\Simple OpenGL Image Library\lib\libSOIL.a

復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\lib\x86

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\lib\x64

目錄下

2.將libSOIL.a重命名為libSOIL.lib

1.8.2.2. SOIL.h

soil\Simple OpenGL Image Library\src\SOIL.h

復制到

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.10.25017\include

目錄下

3. 項目測試

3.1. 項目環境配置

clip_image015[1]

注意:對於GLEW和SOIL采用的是用靜態鏈接庫的方式

3.2. 測試代碼

#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;

// Shaders

const GLchar* vertexShaderSource = "#version 330 core\n"

"layout (location = 0) in vec3 position;\n"

"void main()\n"

"{\n"

"gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"

"}\0";

const GLchar* fragmentShaderSource = "#version 330 core\n"

"out vec4 color;\n"

"void main()\n"

"{\n"

"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"

"}\n\0";

// 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);

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

glewInit();

// Define the viewport dimensions

int width, height;

glfwGetFramebufferSize(window, &width, &height);

glViewport(0, 0, width, height);

// Build and compile our shader program

// Vertex shader

GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

glCompileShader(vertexShader);

// Check for compile time errors

GLint success;

GLchar infoLog[512];

glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

if (!success)

{

glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);

std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;

}

// Fragment shader

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);

glCompileShader(fragmentShader);

// Check for compile time errors

glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);

if (!success)

{

glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);

std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;

}

// Link shaders

GLuint shaderProgram = glCreateProgram();

glAttachShader(shaderProgram, vertexShader);

glAttachShader(shaderProgram, fragmentShader);

glLinkProgram(shaderProgram);

// Check for linking errors

glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);

if (!success) {

glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);

std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;

}

glDeleteShader(vertexShader);

glDeleteShader(fragmentShader);

// Set up vertex data (and buffer(s)) and attribute pointers

//GLfloat vertices[] = {

// // First triangle

// 0.5f, 0.5f, // Top Right

// 0.5f, -0.5f, // Bottom Right

// -0.5f, 0.5f, // Top Left

// // Second triangle

// 0.5f, -0.5f, // Bottom Right

// -0.5f, -0.5f, // Bottom Left

// -0.5f, 0.5f // Top Left

//};

GLfloat vertices[] = {

0.5f, 0.5f, 0.0f, // Top Right

0.5f, -0.5f, 0.0f, // Bottom Right

-0.5f, -0.5f, 0.0f, // Bottom Left

-0.5f, 0.5f, 0.0f // Top Left

};

GLuint indices[] = { // Note that we start from 0!

0, 1, 3, // First Triangle

1, 2, 3 // Second Triangle

};

GLuint VBO, VAO, EBO;

glGenVertexArrays(1, &VAO);

glGenBuffers(1, &VBO);

glGenBuffers(1, &EBO);

// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).

glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO

// Uncommenting this call will result in wireframe polygons.

//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

// 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);

// Draw our first triangle

glUseProgram(shaderProgram);

glBindVertexArray(VAO);

//glDrawArrays(GL_TRIANGLES, 0, 6);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glBindVertexArray(0);

// Swap the screen buffers

glfwSwapBuffers(window);

}

// Properly de-allocate all resources once they've outlived their purpose

glDeleteVertexArrays(1, &VAO);

glDeleteBuffers(1, &VBO);

glDeleteBuffers(1, &EBO);

// 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)

{

if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)

glfwSetWindowShouldClose(window, GL_TRUE);

}

3.3. 測試結果

clip_image017

 

 

------------------------------------------------------------------------------------------------------------------------------------------------

 

glm OpenGL的數學庫,定義了很多關於向量、矩陣的操作,簡潔迅速。
glfw 跨平台的對窗口資源的一個抽象庫。
glad 針對不同的顯卡驅動有不同的OpenGL實現,glad幫助我們隱藏這些差別歡快的使用OpenGL。
stb_image 輕量級的讀取圖片的庫
assimp 輕量級讀取模型的庫

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM