简单渲染流水管线C++代码实现(六)---绕任意过原点的轴旋转矩阵

三维矩阵当中,绕x、y、z轴旋转其实是很简单的,这里不做推导了,和之前的二维旋转矩阵推导很类似,下面主要讲如何推导绕任意过原点的轴旋转矩阵。
在这里插入图片描述

	//绕过原点指定轴旋转
	Matrix4& Rotate(const Vector3& n, float a)
	{
   
		//单位化指定轴向量
		Vector3 any_nomalize = n.Normalize();

		//处理数据
		float xx = any_nomalize.x * any_nomalize.x;
		float yy = any_nomalize.y * any_nomalize.y;
		float zz = any_nomalize.z * any_nomalize.z;
		float xy = any_nomalize.x * any_nomalize.y;
		float yz = any_nomalize.y * any_nomalize.z;
		float zx = any_nomalize.z * any_nomalize.x;
		float c = cos(a);
		float one_sub_c = 1.0f - c;
		float s = sin(a);

		m[_M_11] = xx * one_sub_c + c;
		m[_M_12] = xy * one_sub_c + any_nomalize.z * s;
		m[_M_13] = zx * one_sub_c - any_nomalize.y * s;
		m[_M_14] = 0.0f;

		m[_M_21] = xy * one_sub_c - any_nomalize.z * s;
		m[_M_22] = yy * one_sub_c + c;
		m[_M_23] = yz * one_sub_c + any_nomalize.x * s;
		m[_M_24] = 0.0f;

		m[_M_31] = zx * one_sub_c + any_nomalize.y * s;
		m[_M_32] = yz * one_sub_c - any_nomalize.x * s;
		m[_M_33] = zz * one_sub_c + c;
		m[_M_34] = 0.0f;

		m[_M_41] = 0.0f;
		m[_M_42] = 0.0f;
		m[_M_43] = 0.0f;
		m[_M_44] = 1.0f;

		return *this;
	}

补充三维向量Vector3、三维矩阵Matrix4的代码实现

#pragma once
#include "vector2.h"

class Vector3
{
   
public:
	float x, y, z;
	//构造
	Vector3(float _x = 0.0f, float _y = 0.0f, float _z = 0.0f) :x(_x), y(_y), z(_z) {
   }
	void Set(float _x = 0.0f, float _y = 0.0f, float _z = 0.0f)
	{
   
		x = _x;
		y = _y;
		z = _z;
	}

	float Length() const
	{
   
		return sqrt(x * x + y * y + z * z);
	}

	Vector3 Normalize() const
	{
   
		float length = sqrt(x * x + y * y + z * z);
		assert(!(length >= -0.001f && length <= 0.001f));
		return Vector3(x / length, y / length, z / length);
	}

	//向量相等判定重载
	bool IsEqual(const Vector3& that) const
	{
   
		if (x == that.x && y == that.y && z == that.z)
			return true;
		return false;
	}

	Vector3 operator + (const Vector3& that) const
	{
   
		return Vector3(x + that.x, y + that.y, z + that.z);
	}
	Vector3& operator += (const Vector3& that)
	{
   
		x += that.x;
		y += that.y;
		z += that.z;
		return *this;
	}

	//负号重载
	Vector3 operator - () const
	{
   
		return Vector3(-x, -y, -z);
	}
	//减号重载
	Vector3 operator - (const Vector3& that) const
	{
   
		return Vector3(x - that.x, y - that.y, z - that.z);
	}
	Vector3& operator -= (const Vector3& that)
	{
   
		x -= that.x;
		y -= that.y;
		z -= that.z;
		return *this;
	}
	//向量 * 标量
	Vector3 operator * (float num) const
	{
   
		return Vector3(x * num, y * num, z * num);
	}
	//向量 *= 标量
	Vector3& operator *= (float num)
	{
   
		x *= num;
		y *= num;
		z *= num;
		return *this;
	}
	//向量 / 标量
	Vector3 operator / (float t) const
	{
   
		//断言标量非零
		assert(!(t >= -0.001f && t <= 0.001f));
		return Vector3(x / t, y / t, z / t);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值