答案:文章介绍了在C++图形学编程中实现基础数学库的方法,包含Vec3类用于三维向量运算如加减、点积、叉积和单位化,Mat4类实现4x4矩阵的乘法与向量变换,支持平移、旋转、缩放及透视投影等操作,并通过Transform工具类提供常用变换函数,最后给出组合变换的使用示例,适用于教学和原型开发。

在C++图形学编程中,向量(Vec3)和4x4矩阵(Mat4)是基础中的基础。实现一个简单的数学库能帮助你理解底层运算,同时为后续的渲染、变换、光照计算打下基础。下面教你如何从零开始写一个轻量级但实用的 Vec3 和 Mat4 类。
Vec3 表示三维空间中的点或方向,支持加减、数乘、点积、叉积等操作。
基本结构:
// Vec3.h #pragma once #includeclass Vec3 { public: float x, y, z;
// 构造函数
Vec3() : x(0), y(0), z(0) {}
Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
// 向量加法
Vec3 operator+(const Vec3& other) const {
return Vec3(x + other.x, y + other.y, z + other.z);
}
// 向量减法
Vec3 operator-(const Vec3& other) const {
return Vec3(x - other.x, y - other.y, z - other.z);
}
// 数乘
Vec3 operator*(float scalar) const {
return Vec3(x * scalar, y * scalar, z * scalar);
}
// 点积
float dot(const Vec3& other) const {
return x * other.x + y * other.y + z * other.z;
}
// 叉积
Vec3 cross(const Vec3& other) const {
return Vec3(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x
);
}
// 向量长度
float length() const {
return std::sqrt(x*x + y*y + z*z);
}
// 单位化
Vec3 normalize() const {
float len = length();
return len != 0 ? *this * (1.0f / len) : Vec3(0, 0, 0);
}};
立即学习“C++免费学习笔记(深入)”;
// 支持 scalar vector inline Vec3 operator(float scalar, const Vec3& v) { return v * scalar; }
Mat4 用于表示空间变换:平移、旋转、缩放和投影。我们用列主序存储16个浮点数。
// Mat4.h #pragma onceclass Mat4 { public: float m[16]; // 列主序:m[0-3] 是第一列
// 单位矩阵构造
Mat4() {
for (int i = 0; i < 16; i++) {
m[i] = (i % 5 == 0) ? 1.0f : 0.0f; // 对角线为1
}
}
// 手动设置元素
void set(int row, int col, float value) {
m[col * 4 + row] = value; // 列主序索引
}
// 矩阵乘法
Mat4 operator*(const Mat4& other) const {
Mat4 result;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
float sum = 0;
for (int k = 0; k < 4; k++) {
sum += m[i * 4 + k] * other.m[k * 4 + j];
}
result.m[i * 4 + j] = sum;
}
}
return result;
}
// 矩阵与向量相乘(齐次坐标)
Vec3 operator*(const Vec3& v) const {
float x = m[0]*v.x + m[4]*v.y + m[8]*v.z + m[12];
float y = m[1]*v.x + m[5]*v.y + m[9]*v.z + m[13];
float z = m[2]*v.x + m[6]*v.y + m[10]*v.z + m[14];
float w = m[3]*v.x + m[7]*v.y + m[11]*v.z + m[15];
if (w != 0 && w != 1) {
return Vec3(x/w, y/w, z/w);
}
return Vec3(x, y, z);
}};
静态函数生成特定变换矩阵,方便调用。
// TransformUtils.h #pragma once #include "Mat4.h" #includeclass Transform { public: // 平移矩阵 static Mat4 translate(float x, float y, float z) { Mat4 mat; mat.set(0, 3, x); mat.set(1, 3, y); mat.set(2, 3, z); return mat; }
// 缩放矩阵
static Mat4 scale(float x, float y, float z) {
Mat4 mat;
mat.set(0, 0, x);
mat.set(1, 1, y);
mat.set(2, 2, z);
return mat;
}
// 绕X轴旋转
static Mat4 rotateX(float rad) {
Mat4 mat;
float c = cosf(rad);
float s = sinf(rad);
mat.set(1, 1, c); mat.set(1, 2, -s);
mat.set(2, 1, s); mat.set(2, 2, c);
return mat;
}
// 绕Y轴旋转
static Mat4 rotateY(float rad) {
Mat4 mat;
float c = cosf(rad);
float s = sinf(rad);
mat.set(0, 0, c); mat.set(0, 2, s);
mat.set(2, 0, -s); mat.set(2, 2, c);
return mat;
}
// 绕Z轴旋转
static Mat4 rotateZ(float rad) {
Mat4 mat;
float c = cosf(rad);
float s = sinf(rad);
mat.set(0, 0, c); mat.set(0, 1, -s);
mat.set(1, 0, s); mat.set(1, 1, c);
return mat;
}
// 透视投影矩阵(简化版)
static Mat4 perspective(float fov, float aspect, float near, float far) {
Mat4 mat;
float f = 1.0f / tanf(fov * 0.5f);
mat.set(0, 0, f / aspect);
mat.set(1, 1, f);
mat.set(2, 2, (far + near) / (near - far));
mat.set(2, 3, (2 * far * near) / (near - far));
mat.set(3, 2, -1);
mat.set(3, 3, 0);
return mat;
}};
int main() { Vec3 pos(1, 0, 0);
Mat4 model = Transform::translate(2, 0, 0);
Mat4 rot = Transform::rotateZ(3.14159f / 4.0f);
Mat4 scale = Transform::scale(2, 2, 2);
Mat4 transform = model * rot * scale;
Vec3 transformed = transform * pos;
std::cout << "Transformed: ("
<< transformed.x << ", "
<< transformed.y << ", "
<< transformed.z << ")\n";
return 0;}
基本上就这些。这个简易数学库没有过度优化,但足够教学和原型开发使用。你可以逐步添加更多功能,比如欧拉角转矩阵、LookAt 矩阵、逆矩阵等。关键是理解每一步运算的几何意义。
以上就是C++如何实现一个简单的向量数学库_为C++图形学编程编写Vec3和Mat4类的详细内容,更多请关注php中文网其它相关文章!
编程怎么学习?编程怎么入门?编程在哪学?编程怎么学才快?不用担心,这里为大家提供了编程速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号