2016-06-19 49 views
0

我目前正在爲遊戲引擎創建一個廣告牌着色器,並且我想在此着色器中重置四元組的旋轉角度。我在編程GLSL着色器,這裏是頂點着色器:如何修改旋轉矩陣的一個軸?

#version 400 core 

layout (location = 0) in vec3 in_position; 
layout (location = 1) in vec2 in_texcoords; 

out data 
{ 
    vec2 tex_coords; 
} vs_out; 

uniform mat4 pr_matrix; 
uniform mat4 ml_matrix = mat4(1.0); 
uniform mat4 vw_matrix = mat4(1.0); 

void main() 
{ 
    gl_Position = pr_matrix * vw_matrix * ml_matrix * vec4(in_position, 1.0); 
    vs_out.tex_coords = in_texcoords; 
} 

我知道,我可以通過設置在左上方重置模型矩陣的旋轉

1 0 0 
0 1 0 
0 0 1 

,但現在我希望四邊形能夠圍繞x軸和z軸旋轉,但不能圍繞Y軸旋轉。有人知道如何在一個軸上重置矩陣的旋轉嗎?

回答

0

這可能有幫助。解決方案使用解出的旋轉矩陣,除了移植到GLSL。根據是否要回收trig函數(cos/sin),有許多版本的旋轉矩陣。由於GFX在SIMD上更好,因此該功能就是按此編碼的。

mat4 rotationMatrix(vec3 axis, float angle) 
{ 
    axis = normalize(axis); 
    float s = sin(angle); 
    float c = cos(angle); 
    float oc = 1.0 - c; 

    return mat4(oc * axis.x * axis.x + c,   oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0, 
       oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c,   oc * axis.y * axis.z - axis.x * s, 0.0, 
       oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c,   0.0, 
       0.0,        0.0,        0.0,        1.0); 
} 

來源:http://www.neilmendoza.com/glsl-rotation-about-an-arbitrary-axis/

+0

謝謝您的回答,但你知道如何繞Y軸的歐拉角爲0?因爲多數民衆贊成我正在尋找:/ – RagingRabbit

+0

@RagingRabbit:你可以乘以y軸的倒數只,以撤消該特定的旋轉? – namar0x0309

+0

不,我只有轉換矩陣,所以在所有軸上旋轉。 – RagingRabbit