本文內容:
配置OPENGL
學習地址:
https://learnopengl-cn.github.io
學習成果:

1.閱讀配置教程
https://blog.csdn.net/qq_37338983/article/details/78997179
感謝 Bruce_wjh 博主的配置教程,比官方的好很多.(可惜還有些缺陷)
2.補充教程的不足
(1) GLFW 的安裝是可以在VS中NuGet的完成的(如圖)
(2) GLAD得自己安裝,不過看上面的配置教程是很快的.
(3) 用到的GLSL語言代碼高亮需要自己去裝個插件.(如圖)


GLSL 高亮插件
工具-獲取工具與功能

問題1:

直接往項目中添加glad.c 會報錯:
----glad.c在查找預編譯頭遇到意外的文件結尾,是否忘記向源中添加#include "stdafx.h" ?

解決方法:
右擊glad.c -> 屬性 -> C/C++ -> 預編譯頭 -> 不使用預編譯頭

問題2: OpenGL窗口白屏並且未響應

解決方案:
1.檢查是否忘記使用緩存 glfwSwapBuffers(window); (一般原因都是緩沖忘記交換)
2.是否 glfwWindowShouldClose(window) 打錯
glfwWindowShouldClose
(ps: 第二種是可以通過編譯的.....這里坑了好久)
附上代碼:
// 頭文件位置不一定都一樣
#include "stdafx.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Oh!I see you!", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create the windows" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
while (!glfwWindowShouldClose(window)) {
//輸入處理
processInput(window);
//渲染指令
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* windows, int width, int height) {
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
運行結果:

