Skip to content

Latest commit

 

History

History
108 lines (85 loc) · 2.28 KB

21.添加缩放变换.md

File metadata and controls

108 lines (85 loc) · 2.28 KB

GameEngine Java 3D V2.0

21.添加缩放变换

同理添加缩放变化 要为四维矩阵类添加相应的变化公式

    /*
    * 初始化缩放
    * */
    public Matrix4f initScale(float x, float y, float z)
    {
        m[0][0] = x;	m[0][1] = 0;	m[0][2] = 0;	m[0][3] = 0;
        m[1][0] = 0;	m[1][1] = y;	m[1][2] = 0;	m[1][3] = 0;
        m[2][0] = 0;	m[2][1] = 0;	m[2][2] = z;	m[2][3] = 0;
        m[3][0] = 0;	m[3][1] = 0;	m[3][2] = 0;	m[3][3] = 1;

        return this;
    }
public class Transform {

    /*
    * 平移
    * */
    private Vector3f translation;

    /*
    * 旋转
    * */
    private Vector3f rotation;

    /*
    * 缩放
    * */
    private Vector3f scale;

    /*
    * 构造函数
    * */
    public Transform() {
        translation = new Vector3f(0,0,0);
        rotation = new Vector3f(0,0,0);
        scale = new Vector3f(1, 1, 1);
    }
    /*
    * 获得变换矩阵
    * */
    public Matrix4f getTransformation()
    {
        Matrix4f translationMatrix = new Matrix4f().initTranslation(translation.getX(), translation.getY(), translation.getZ());
        Matrix4f rotationMatrix = new Matrix4f().initRotation(rotation.getX(), rotation.getY(), rotation.getZ());
        Matrix4f scaleMatrix = new Matrix4f().initScale(scale.getX(), scale.getY(), scale.getZ());
        return translationMatrix.mul(rotationMatrix).mul(scaleMatrix);
    }

    /*
    * getter setter
    *
    * */
    public Vector3f getTranslation() {
        return translation;
    }
    public void setTranslation(Vector3f translation) {
        this.translation = translation;
    }

    public void setTranslation(float x, float y, float z)
    {
        this.translation = new Vector3f(x, y, z);
    }

    public Vector3f getRotation() {
        return rotation;
    }
    public void setRotation(Vector3f rotation) {
        this.rotation = rotation;
    }
    public void setRotation(float x, float y, float z)
    {
        this.rotation = new Vector3f(x, y, z);
    }

    public Vector3f getScale() {
        return scale;
    }

    public void setScale(Vector3f scale) {
        this.scale = scale;
    }
    public void setScale(float x, float y, float z)
    {
        this.scale = new Vector3f(x, y, z);
    }


}

再Game类里添加上缩放变化即可