主要用來記錄一下學習代碼,每次新建一個項目還要配置太麻煩啦
配置網址:https://blog.csdn.net/qq_19003345/article/details/76098781
學習的是可編輯管線,不過順便也配置了一下舊版本的,這樣可以偶爾運行一下別人的代碼
題外話:新版OpenGL比較少,一不小心就找到舊的了。而且和舊版相比,新版需要理解的東西太多了
學習網址:https://learnopengl-cn.github.io/01%20Getting%20started/03%20Hello%20Window/
這個代碼全部都是原來教程上的,不過加了一點自己的備註
建議跟着教程閱讀,直接看代碼很累
update:如果只是爲了寫出東西的話,這一塊初始化內容完全可以不要去管它
#include <glad/glad.h>
#include <glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height); //窗口回調函數
void processInput(GLFWwindow *window); //輸入控制
// settings 初始化設置
const unsigned int SCR_WIDTH = 800; //初始寬度
const unsigned int SCR_HEIGHT = 600; //初始高度
int main()
{
// glfw: initialize and configure 不需要做任何改動
glfwInit(); //初始化GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //主版本號爲3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //次版本號爲3
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //核心模式
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 對於OS X
#endif
// glfw 窗口對象 不需要改動
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); //窗口:寬、高、名稱。
if (window == NULL) //確保正確創建窗口
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); //設置窗口上下文爲當前線程
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //告訴glfw窗口大小會根據改變
// glad: load all OpenGL function pointers 加載系統相關的OpenGL函數指針地址的函數 不需要改動
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// render loop 渲染循環,這樣可以保持一直運行
while (!glfwWindowShouldClose(window))
{
// 輸入
processInput(window);
//渲染指令
glClearColor(0.8f, 0.3f, 0.3f, 1.0f); //設置清空屏幕所用的顏色,每次循環重新渲染,因此需要清空,也因此這是屏幕的背景色。RGB色
glClear(GL_COLOR_BUFFER_BIT); //清楚顏色緩衝
// 檢查並調用事件、交換緩衝
glfwSwapBuffers(window); //交換顏色緩衝
glfwPollEvents(); //檢查事件觸發
}
// glfw: terminate, clearing all previously allocated GLFW resources.
glfwTerminate(); //終止glfw
return 0;
}
// 輸入控制
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) //判斷是否按下Esc
glfwSetWindowShouldClose(window, true); //如果時Esc,那麼glfw需要關閉窗口
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes 根據窗口大小改變顯示大小
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height); //窗口左下角的座標x、y
}