程式碼main.cpp:
#include <iostream> #include <glad/glad.h> #include <glfw3.h> #include "Shader.h" #define STB_IMAGE_IMPLEMENTATION #include <stb_image.h> #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> #include <chrono> #include <thread> using namespace std; const unsigned int screenWidth = 800; const unsigned int screenHeight = 600; // 回撥函式,每當視窗改變大小,視口大小也跟隨改變 void framebuffer_size_callback(GLFWwindow* window,int width,int height) { glViewport(0,0,width,height); } // 輸入 在GLFW中實現一些輸入控制 void processInput(GLFWwindow *window) { if(glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS) // 是否按下了返回鍵(Esc) { glfwSetWindowShouldClose(window,true); // 把WindowShouldClose屬性設定為 true來關閉GLFW } } // ************** opengl 具體實現 ************ // 頂點資料 float vertices[] = { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f }; // glm::vec3陣列中定義10個立方體位置: glm::vec3 cubePositions[] = { glm::vec3( 0.0f, 0.0f, 0.0f), glm::vec3( 2.0f, 5.0f, -15.0f), glm::vec3(-1.5f, -2.2f, -2.5f), glm::vec3(-3.8f, -2.0f, -12.3f), glm::vec3( 2.4f, -0.4f, -3.5f), glm::vec3(-1.7f, 3.0f, -7.5f), glm::vec3( 1.3f, -2.0f, -2.5f), glm::vec3( 1.5f, 2.0f, -2.5f), glm::vec3( 1.5f, 0.2f, -1.5f), glm::vec3(-1.3f, 1.0f, -1.5f) }; //****************************************** int main() { cout << "紋理" << endl; // GLFW (視窗) glfwInit(); // 初始化GLFW glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3); // 主版本號 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3); // 次版本號 glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); // 使用核心模式 //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); Mac OS X系統 // 建立一個GLFW視窗物件 GLFWwindow* window = glfwCreateWindow(screenWidth ,screenHeight,"texture",NULL,NULL); if(window == NULL) { std::cout<< "Failed to create GLFW window"<<std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // 視窗的上下文設定為當前執行緒的主上下文 // GLAD if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) // 初始化GLAD(初始化OpenGL函式指標) { std::cout<<"Failed to initialize GLAD"<<std::endl; return -1; } //*********** opengl 具體實現 ****************** // 著色器類 Shader shader("E:\\Project\\openglstudy2\\07_OpenglCamera\\07_OpenglCamera\\vertexSourceCode.vert", "E:\\Project\\openglstudy2\\07_OpenglCamera\\07_OpenglCamera\\fragmentSourceCode.frag"); // 頂點陣列物件 VAO unsigned int VAO; glGenVertexArrays(1,&VAO); // 建立頂點陣列物件 glBindVertexArray(VAO); // 繫結頂點陣列物件 // 頂點緩衝物件 VBO unsigned int VBO; glGenBuffers(1,&VBO); glBindBuffer(GL_ARRAY_BUFFER,VBO); // 繫結頂點緩衝物件 glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW); // 把之前定義的頂點資料複製到緩衝的記憶體中 // 連結頂點屬性 (告訴 gpu 如何解析記憶體中的頂點資料) // 0 位置座標 glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5 * sizeof(float),(void*)0); glEnableVertexAttribArray(0); // 1 紋理座標 glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,5 * sizeof(float),(void*)(3*sizeof(float))); glEnableVertexAttribArray(1); unsigned int texture1,texture2; // 紋理物件1 texture1 glGenTextures(1,&texture1); glBindTexture(GL_TEXTURE_2D,texture1); // 設定紋理影像環繞方式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // 設定紋理過濾 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 載入圖片 int width,height,nrChannels; unsigned char *data = stbi_load("E:\\Project\\openglstudy2\\resources\\textures\\container.jpg", &width,&height,&nrChannels,0); if(data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout << "Failed to load texture" << std::endl; } // 釋放掉影像資料 stbi_image_free(data); shader.use(); // 紋理物件2 texture2 glGenTextures(1,&texture2); glBindTexture(GL_TEXTURE_2D,texture2); // 設定紋理影像環繞方式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // 重複 // 設定紋理過濾 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 載入圖片 stbi_set_flip_vertically_on_load(true); data = stbi_load("E:\\Project\\openglstudy2\\resources\\textures\\awesomeface.png", &width,&height,&nrChannels,0); if(data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout << "Failed to load texture" << std::endl; } // 釋放掉影像資料 stbi_image_free(data); // 設定取樣器對應的紋理單元(unform) shader.use(); shader.setInt("texture1",0); shader.setInt("texture2",1); // 啟用深度測試 glEnable(GL_DEPTH_TEST); // 透視矩陣 glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)screenWidth / (float)screenHeight, 0.1f, 100.0f); shader.setMat4("projection", projection); //************************** // 視口 glViewport(0,0,screenWidth ,screenHeight); // 視口跟隨視窗大小改變 glfwSetFramebufferSizeCallback(window,framebuffer_size_callback); // 渲染迴圈 while(!glfwWindowShouldClose(window)) // 檢查GLFW是否被要求退出 { // 輸入 (使用者) processInput(window); // 輸入控制(自定義) //************** 渲染指令 (使用者)********** glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture2); shader.use(); // 建立轉換 glm::mat4 view = glm::mat4(1.0f); float radius = 35.0f; // 旋轉半徑 float camX = static_cast<float>(sin(glfwGetTime()) * radius); float camZ = static_cast<float>(cos(glfwGetTime()) * radius); view = glm::lookAt(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); shader.setMat4("view", view); // glBindVertexArray(VAO); for (unsigned int i = 0; i < 10; i++) { // 計算每個物件的模型矩陣,並在繪製前將其傳遞給shader glm::mat4 model = glm::mat4(1.0f); model = glm::translate(model, cubePositions[i]); float angle = 20.0f * i; model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); shader.setMat4("model", model); glDrawArrays(GL_TRIANGLES, 0, 36); } //*************************************** // 檢查並呼叫事件,交換緩衝 (預設操作) glfwSwapBuffers(window); // 交換顏色緩衝(它是一個儲存著GLFW視窗每一個畫素顏色值的大緩衝),它在這一迭代中被用來繪製,並且將會作為輸出顯示在螢幕上。 glfwPollEvents(); // 有沒有觸發什麼事件(比如鍵盤輸入、滑鼠移動等)、更新視窗狀態 } // 解綁 VAO VBO 著色器程式物件 glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); // 當渲染迴圈結束後我們需要正確釋放/刪除之前的分配的所有資源 glfwTerminate(); return 0; }
著色器程式碼:
頂點著色器:
#version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); }
片段著色器:
#version 330 core out vec4 FragColor; in vec3 ourColor; in vec2 TexCoord; // 定義一個全域性的2D紋理取樣器 uniform sampler2D texture1; uniform sampler2D texture2; void main() { // GLSL內建的mix函式需要接受兩個值作為引數,並對它們根據第三個引數進行線性插值。 FragColor = mix(texture(texture1, TexCoord), texture(texture2, vec2(-TexCoord.x,TexCoord.y)), 0.2); }