OpenGL實現平移縮放旋轉
阿新 • • 發佈:2018-11-10
#define GLEW_STATIC #include <GL\glew.h> #include <GLFW/glfw3.h> #include <iostream> #include "Shader.h" #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #include <glm.hpp> #include <gtc/matrix_transform.hpp> #include <gtc/type_ptr.hpp> using namespace std; //QQ技術交流群:386476712 //terminate [ˈtɜ:mɪneɪt] 結束 終結 //hint [hɪnt] 提示 注意事項 void framebuffer_size_callback(GLFWwindow* window, int width, int height) { cout << "width : " << width << endl; glViewport(0, 0, width, height); } int main(){ //glfw初始化 //告訴glfw當前所用的OpenGL的版本號是3.3 //告訴glfw當前使用核心模式,意味著我們只能使用OpenGL功能的一個子集(沒有我們已不再需要的向後相容特性) glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //使用glfw建立一個視窗 GLFWwindow* window = glfwCreateWindow(800, 600, "Hunk Xu OpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } //通知GLFW將window的上下文設定為當前執行緒的主上下文,設定主活動視窗 glfwMakeContextCurrent(window); //glew初始化 if (glewInit() != GLEW_OK){ printf("glew init failed"); glfwTerminate(); return -1; } unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); // 為當前繫結的紋理物件設定環繞、過濾方式 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("wall.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); //告訴OpenGL視口(Viewport)大小 //前兩個引數為視窗左下角位置 //後兩個引數渲染視窗的寬和高(畫素) glViewport(0, 0, 800, 600); //每當視窗調整大小時候,就呼叫這個函式 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); float vertices[] = { // ---- 位置 ---- ---- 顏色 ---- - 紋理座標 - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // 右上 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // 右下 -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // 左下 }; unsigned int indices[] = { // note that we start from 0! 0, 1, 2, // first Triangle }; unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); 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, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // 顏色屬性 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); //將頂點資料buffer和之前的繫結關係進行解綁 用於打破之前的頂點資料buffer的繫結關係 //使OpenGL的頂點資料buffer繫結狀態恢復到預設狀態。 glBindBuffer(GL_ARRAY_BUFFER, 0); //將VAO繫結到預設的VAO處,一般用於打破之前的VAO繫結關係 //使OpenGL的VAO繫結狀態恢復到預設狀態 glBindVertexArray(0); Shader* myShader = new Shader("vertexSource.txt", "fragementSource.txt"); glm::mat4 trans; //trans = glm::translate(trans, glm::vec3(-0.5f, 0.0f, 0.0f)); //平移影象 //trans = glm::rotate(trans, glm::radians(45.0f), glm::vec3(0.0, 0.0, 1.0)); //旋轉影象 trans = glm::scale(trans, glm::vec3(1.5f, 1.5f, 1.5f)); //縮放影象 //渲染迴圈 while (!glfwWindowShouldClose(window)){ myShader->use(); glBindTexture(GL_TEXTURE_2D, texture); glBindVertexArray(VAO); unsigned int transformLoc = glGetUniformLocation(myShader->ID, "transform"); glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans)); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0); //3表示3個頂點索引 //雙緩衝(Double Buffer) //前緩衝儲存著最終輸出的影象,它會在螢幕上顯示; //而所有的的渲染指令都會在後緩衝上繪製。 //當所有的渲染指令執行完畢後,我們交換(Swap)前緩衝和後緩衝,這樣影象就立即呈顯出來 //不會出現影象閃爍的問題 glfwSwapBuffers(window); glfwPollEvents(); //輪詢使用者的輸入(鍵盤移動,滑鼠輸入) } //終止 glfwTerminate(); return 0; }
#version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; layout (location = 2) in vec2 aTexCoord; out vec3 ourColor; out vec2 TexCoord; uniform mat4 transform; void main() { gl_Position = transform*vec4(aPos, 1.0); ourColor = aColor; TexCoord = aTexCoord; }
下面是改動地方
下面就是放大後的圖形
FR:海濤高軟(Hunk Xu)
QQ技術群:386476712