OpenGL 坐标变换
生活随笔
收集整理的這篇文章主要介紹了
OpenGL 坐标变换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
參考資料:
- 正射投影:https://www.cnblogs.com/hackpig/p/5790379.html
- 透射投影:https://blog.csdn.net/linuxheik/article/details/78969526
坐標(biāo)變換
在頂點(diǎn)著色器定義一個(gè)頂點(diǎn)變換矩陣,與輸入的頂點(diǎn)向量相乘,得到新的頂點(diǎn)位置
#version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord;out vec2 TexCoord;uniform mat4 transform;void main() {gl_Position = transform * vec4(aPos, 1.0f);TexCoord = vec2(aTexCoord.x, 1.0 - aTexCoord.y); }其中uniform mat4 transform 就是一個(gè)全局變換矩陣,它可以用glm庫(kù)函數(shù)來(lái)賦值:
// create transformationsglm::mat4 transform = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix firsttransform = glm::translate(transform, glm::vec3(0.5f, -0.5f, 0.0f));transform = glm::rotate(transform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));// get matrix's uniform location and set matrixourShader.use();unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));坐標(biāo)空間
OpenGL一共有5個(gè)坐標(biāo)系,通過(guò)5個(gè)坐標(biāo)系的變換,將一個(gè)物體的局部坐標(biāo)下的形狀最終變換到屏幕坐標(biāo)下。這幾個(gè)坐標(biāo)系都有明確的物理含義:
其中需要注意以下裁剪坐標(biāo):
裁剪坐標(biāo)有兩種方式正射投影和透視投影,透視投影能模仿人眼看遠(yuǎn)方的效果但是正射投影不能。正射投影將一個(gè)長(zhǎng)方體裁剪區(qū)映射到設(shè)備坐標(biāo),但是透視投影將一個(gè)視椎體區(qū)域映射到設(shè)備坐標(biāo)。
放兩張圖,具體原理可以看上面參考資料:
正視投影
透視投影
坐標(biāo)空間變換代碼:
其它
通過(guò)使用不同的空間坐標(biāo)系,在屏幕上展現(xiàn)多個(gè)同一張圖像
glBindVertexArray(VAO); for(unsigned int i = 0; i < 10; i++) {glm::mat4 model;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));ourShader.setMat4("model", model);glDrawArrays(GL_TRIANGLES, 0, 36); }總結(jié)
以上是生活随笔為你收集整理的OpenGL 坐标变换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenGL:纹理Textures
- 下一篇: OpenGL uniform变量赋值的三