opengl 矩陣變換

2021-06-22 18:53:10 字數 3916 閱讀 7149

opengl 矩陣變換

opengl 座標系的變換如下圖:

我們給我們的圖形,設定好頂點座標後,通過model matrix 變換為世界座標,然後 view matrix相機座標,projection matrix 螢幕座標x,y ∈(-1,1)。

矩陣變換的結果是把三維的世界最終裁剪為二維的螢幕,數學的說法就是從乙個集合到另乙個集合的對映。

#include "glew.h"

#include #include "common/loadshader.h"

#include "glm.hpp"

#include "ext.hpp"

int main(void)

/* make the window's context current */

glfwmakecontextcurrent(window);

// needed in core profile

if( glewinit() != glew_ok)

// an array of 3 vectors which represents 3 vertices

static const glfloat g_vertex_buffer_data = ;

//this will identify our vertex buffer

gluint vertexbuffer;

//generate 1 buffer,put the resulting identifier in vertexbuffer

glgenbuffers(1,&vertexbuffer);

//the following commands will talk about our 'vertexbuffer' buffer

glbindbuffer(gl_array_buffer,vertexbuffer);

//give our vertices to opengl.

glbufferdata(gl_array_buffer,sizeof(g_vertex_buffer_data),g_vertex_buffer_data,gl_static_draw);

gluint programid = loadshaders("./shader/vertex.shader","./shader/fragment.shader");

gluseprogram(programid);

glclearcolor(0.0f, 0.0f, 0.4f, 0.0f);

/* loop until the user closes the window */

// projection matrix : 45° field of view, 4:3 ratio, display range : 0.1 unit 100 units

//glm::mat4 projection = glm::ortho(-4.0f/3.0f, 4.0f/3.0f, -1.0f, 1.0f, 0.1f, 100.0f);

glm::mat4 projection = glm::perspective(45.0f,4.0f/3.0f,0.1f,100.f);

glm::mat4 view = glm::lookat(

glm::vec3(4,3,3), // camera is at (4,3,3), in world space

glm::vec3(0,0,0), // and looks at the origin

glm::vec3(0,1,0) // head is up (set to 0,-1,0 to look upside-down)

);// model matrix : an identity matrix (model will be at the origin)

glm::mat4 model = glm::mat4(1.0f);

model = glm::translate(model,glm::vec3(2.0f,0.0f,0.0f));

model = glm::rotate(model,45.f,glm::vec3(0.0f,1.0f,0.0f));

model = glm::scale(model,glm::vec3(1.0f,2.0f,1.0f));

// our modelviewprojection : multiplication of our 3 matrices

glm::mat4 mvp = projection * view * model;// remember, matrix multiplication is the other way around

// get a handle for our "mvp" uniform.

// only at initialisation time.

gluint matrixid = glgetuniformlocation(programid,"mvp");

// send our transformation to the currently bound shader,

// in the "mvp" uniform

// for each model you render, since the mvp will be different (at least the m part)

gluniformmatrix4fv(matrixid,1,gl_false,&mvp[0][0]);

while (!glfwwindowshouldclose(window))

glfwterminate();

return 0;

}

首先,model為乙個單位矩陣,glm::mat4 model = glm::mat4(1.0f);

然後平移,旋轉,縮放,

model = glm::translate(model,glm::vec3(2.0f,0.0f,0.0f));

model = glm::rotate(model,45.f,glm::vec3(0.0f,1.0f,0.0f));

model = glm::scale(model,glm::vec3(1.0f,2.0f,1.0f));

最後通過矩陣乘法,連線到一起。

glm::mat4 projection = glm::perspective(45.0f,4.0f/3.0f,0.1f,100.f);

glm::mat4 view = glm::lookat(

glm::vec3(4,3,3), // camera is at (4,3,3), in world space

glm::vec3(0,0,0), // and looks at the origin

glm::vec3(0,1,0) // head is up (set to 0,-1,0 to look upside-down)

);// our modelviewprojection : multiplication of our 3 matrices

glm::mat4 mvp = projection * view * model;// remember, matrix multiplication is the other way around

得到shader中變數指標

gluint matrixid = glgetuniformlocation(programid,"mvp");

將矩陣傳入shader

gluniformmatrix4fv(matrixid,1,gl_false,&mvp[0][0]);

來看shader的語法:

#version 330 core

layout(location = 0) in vec3 vertexposition_modelspace;

uniform mat4 mvp;

void main()

shader裡將當前頂點與之前矩陣相乘。

OpenGL矩陣變換

現在考慮object座標系中的點p px,p y,pz 向camera座標系進行變換,object座標軸分別為xyz,camera座標軸分別為uvn p pxx pyy pz z 先只考慮旋轉變換,camera座標系的基向量可表示為 u v n u.xx u.yy u.zz v.xx v.yy v....

從矩陣的定義開始談OpenGL矩陣變換

前言 矩陣的定義 先從2d場景說起.先從乙個最簡單的問題開始.q1 乙個點p x,y 它關於原點o的對稱點為p x y 請問x y 與x,y有什麼關係?下面兩個問題稍微難一些。q2 點 繞o逆時針旋轉45 得到點p x y 求p 的座標。a2 設p座標為p x,y 且滿足 由題意 p旋轉45 後得到...

OpenGL 矩陣變換GLM庫的使用

glm和mvp矩陣操作速記 連續工作15小時,累了,睡覺。若未特別說明,以下示例均假設矩陣 向量為四維 glm mat4 mat glm vec4 vec 對於vec來說,第四位為1代表座標,0代表方向 平移矩陣 1 0 0 x 0 1 0 y 0 0 1 z 0 0 0 1 構造平移矩陣 glm ...