1.移动轴的变换矩阵:

2.绕某一轴旋转推导公式:

假设从坐标(x1,y1) 旋转到坐标 (x2,y2)

x1=cos(a1)
y1=sin(a1)
x2=cos(a1+a2)
y2=sin(a1+a2)

三角函数诱导公式:

cos(a+b) = cosacosb - sinasinb

sin(a+b) = sinacosb+cosasinb

可得:

x2=cos(a1+a2) = cosa1cosa- sina1sina2 = x1cosa- y1sina2
y2=sin(a1+a2) = sina1cosa+ cosa1sina2 = y1cosa+ x1sina2

得出绕Z轴逆时针旋转α°的矩阵为:

同理,绕Y轴旋转的变换矩阵:

绕X轴旋转的变换矩阵:

vertex shader:

#version 330

layout (location=0) in vec3 Position;

uniform mat4 gWorld;

void main()
{
        gl_Position = gWorld * vec4(Position, 1.0);  
}

application:

static void RenderSceneCB()
{
    glClear(GL_COLOR_BUFFER_BIT);

    static float Scale = 0.0f;

    Scale += 0.001f;

    Matrix4f World;
    
   //! 设置World移动矩阵(坐标x轴移动sin(Scale)个单位)
///////////////////////////////////////////////////////////////////////////////////////////////////// World.m[
0][0] = 1.0f; World.m[0][1] = 0.0f; World.m[0][2] = 0.0f; World.m[0][3] = sinf(Scale); World.m[1][0] = 0.0f; World.m[1][1] = 1.0f; World.m[1][2] = 0.0f; World.m[1][3] = 0.0f; World.m[2][0] = 0.0f; World.m[2][1] = 0.0f; World.m[2][2] = 1.0f; World.m[2][3] = 0.0f; World.m[3][0] = 0.0f; World.m[3][1] = 0.0f; World.m[3][2] = 0.0f; World.m[3][3] = 1.0f;
///////////////////////////////////////////////////////////////////////////////////////////////////// glUniformMatrix4fv(gWorldLocation,
1, GL_TRUE, &World.m[0][0]); //! 设置gWorld 到vertex shader glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, VBO); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glDrawArrays(GL_TRIANGLES, 0, 3); glDisableVertexAttribArray(0); glutSwapBuffers(); }

 

posted on 2014-07-29 23:09  kid_coder  阅读(459)  评论(0)    收藏  举报