OpenGL中三维坐标的处理类 可以直接复制到工程里

本文详细介绍了用于OpenGL编程的三维向量类CVector3的实现方法,包括向量的创建、基本运算、向量间的点积与叉积计算、向量的标准化、长度计算以及围绕各个轴的旋转操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发现了之前大学期间做的OpenGL时候写的类

头文件

#ifndef VECTOR3D_H
#define VECTOR3D_H


class CVector3
{
public:
	// 初始化
	CVector3(void) : x(0.0f), y(0.0f), z(0.0f) {}
	CVector3(float newX, float newY, float newZ) : x(newX), y(newY), z(newZ) {}
	CVector3(const float * rhs)	: x(*rhs), y(*(rhs+1)), z(*(rhs+2)) {}
	CVector3(const CVector3 & rhs) : x(rhs.x), y(rhs.y), z(rhs.z) {}


	// 类的析构函数
	~CVector3() {}


	// 设置向量的三个属性
	void Set(float newX, float newY, float newZ);


	// 计算向量的叉乘
	CVector3 CrossProduct(const CVector3 &rhs) const;


	// 计算向量的点乘
	float DotProduct(const CVector3 &rhs) const;


	// 向量单位化
	void Normalize();
	CVector3 GetNormalized() const;


	// 计算向量的长度
	float GetLength() const;


	// 计算向量的长度的平方
	float GetSquaredLength() const;


	// 向量的旋转
	void RotateX(double angle);
	CVector3 GetRotatedX(double angle) const;
	void RotateY(double angle);
	CVector3 GetRotatedY(double angle) const;
	void RotateZ(double angle);
	CVector3 GetRotatedZ(double angle) const;
	void RotateAxis(double angle, const CVector3 &axis);
	CVector3 GetRotatedAxis(double angle, const CVector3 &axis) const;


	// 运算符重载
	CVector3 operator+(const CVector3 &rhs) const;
	CVector3 operator-(const CVector3 &rhs) const;
	CVector3 operator*(const float rhs) const;
	CVector3 operator/(const float rhs) const;


	bool operator==(const CVector3 &rhs) const;
	bool operator!=(const CVector3 &rhs) const;


	// 类成员	
	float x;
	float y;
	float z;
};
#endif			// CVector3_H

cpp文件

#include "VECTOR3D.h"
#include <math.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

// 设置向量的三个属性
void CVector3::Set(float newX, float newY, float newZ)
{
	x = newX;
	y = newY;
	z = newZ;
}

// 计算向量的叉乘
CVector3 CVector3::CrossProduct(const CVector3 &rhs) const
{
	return CVector3(y*rhs.z-z*rhs.y, z*rhs.x-x*rhs.z, x*rhs.y-y*rhs.x);
}

// 计算向量的点乘
float CVector3::DotProduct(const CVector3 &rhs) const
{
	return x*rhs.x + y*rhs.y + z*rhs.z;
}

// 向量单位化
void CVector3::Normalize()
{
	float length=GetLength();

	if(length==1 || length==0)
		return;

	float scalefactor = 1.0f/length;
	x *= scalefactor;
	y *= scalefactor;
	z *= scalefactor;
}

// 向量单位化
CVector3 CVector3::GetNormalized() const
{
	CVector3 result(*this);

	result.Normalize();

	return result;
}

// 计算向量的长度
float CVector3::GetLength() const
{
	return (float)sqrt((x*x)+(y*y)+(z*z));
}

// 计算向量的长度的平方
float CVector3::GetSquaredLength() const
{
	return (x*x)+(y*y)+(z*z);
}

// 绕X轴旋转向量,返回旋转后的向量
CVector3 CVector3::GetRotatedX(double angle) const
{
	if(angle==0.0)
		return (*this);

	float sinAngle=(float)sin(M_PI*angle/180);
	float cosAngle=(float)cos(M_PI*angle/180);

	return CVector3(x, y*cosAngle - z*sinAngle, y*sinAngle + z*cosAngle);
}

// 绕X轴旋转向量
void CVector3::RotateX(double angle)
{
	(*this)=GetRotatedX(angle);
}

// 绕Y轴旋转向量,返回旋转后的向量
CVector3 CVector3::GetRotatedY(double angle) const
{
	if(angle==0.0)
		return (*this);

	float sinAngle=(float)sin(M_PI*angle/180);
	float cosAngle=(float)cos(M_PI*angle/180);

	return CVector3(x*cosAngle + z*sinAngle, y, -x*sinAngle + z*cosAngle);
}

// 绕Y轴旋转向量
void CVector3::RotateY(double angle)
{
	(*this)=GetRotatedY(angle);
}

// 绕Y轴旋转向量,返回旋转后的向量
CVector3 CVector3::GetRotatedZ(double angle) const
{
	if(angle==0.0)
		return (*this);

	float sinAngle=(float)sin(M_PI*angle/180);
	float cosAngle=(float)cos(M_PI*angle/180);
	
	return CVector3(x*cosAngle - y*sinAngle,
					x*sinAngle + y*cosAngle,
					z);
}

// 绕Y轴旋转向量
void CVector3::RotateZ(double angle)
{
	(*this)=GetRotatedZ(angle);
}

// 绕给定轴旋转向量,返回旋转后的向量
CVector3 CVector3::GetRotatedAxis(double angle, const CVector3 & axis) const
{
	if(angle==0.0)
		return (*this);

	CVector3 u=axis.GetNormalized();

	CVector3 result;

	float cosTheta = (float)cos(angle);
	float sinTheta = (float)sin(angle);

	result.x  = (float)(cosTheta + (1 - cosTheta) * u.x * u.x) * x;
	result.x += (float)((1 - cosTheta) * u.x * u.y - u.z * sinTheta) * y;
	result.x += (float)((1 - cosTheta) * u.x * u.z + u.y * sinTheta) * z;

	result.y  = (float)((1 - cosTheta) * u.x * u.y + u.z * sinTheta) * x;
	result.y += (float)(cosTheta + (1 - cosTheta) * u.y * u.y) * y;
	result.y += (float)((1 - cosTheta) * u.y * u.z - u.x * sinTheta) * z;

	result.z  = (float)((1 - cosTheta) * u.x * u.z - u.y * sinTheta) * x;
	result.z += (float)((1 - cosTheta) * u.y * u.z + u.x * sinTheta) * y;
	result.z += (float)(cosTheta + (1 - cosTheta) * u.z * u.z) * z;

	return result;
}

// 绕给定轴旋转向
void CVector3::RotateAxis(double angle, const CVector3 & axis)
{
	(*this)=GetRotatedAxis(angle, axis);
}

// 运算符重载 +
CVector3 CVector3::operator+(const CVector3 &rhs) const
{
	return CVector3(x + rhs.x, y + rhs.y, z + rhs.z);
}

// 运算符重载 -
CVector3 CVector3::operator-(const CVector3 &rhs) const
{
	return CVector3(x - rhs.x, y - rhs.y, z - rhs.z);
}

// 运算符重载 *
CVector3 CVector3::operator*(const float rhs) const
{
	return CVector3(x*rhs, y*rhs, z*rhs);
}

// 运算符重载 /
CVector3 CVector3::operator/(const float rhs) const
{
	return (rhs==0.0f) ? CVector3(0.0f, 0.0f, 0.0f) : CVector3(x/rhs, y/rhs, z/rhs);
}

// 判断两个向量是否相同
bool CVector3::operator==(const CVector3 & rhs) const
{
	if(x==rhs.x && y==rhs.y && z==rhs.z)
		return true;

	return false;
}

// 判断两个向量是否不同
bool CVector3::operator!=(const CVector3 & rhs) const
{
	return !((*this)==rhs);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值