[DirectX]Programming.Role.Playing.Games:GameCore

       按照《Programming.Role.Playing.Games》书上顺序,这一次应该是是第5章,Networking with DirectPlay,看了一会准备去写代码的时候,发现DirectPlay已经不支持了,想了一下,网络这块也不是必须要用到Direct Play,就懒得再去细究了。直接跳过,到下一章,也就是本书第2部分的结束,第6章:GameCore,相当于总结一样。

       核心内容就是把之前的代码封装起来,调用方便,也没什么好说的。自己参考了一下源码和DXUT的源码,写了个静态库。突然发现自己在封装Matrix时,发现有点吃力。

#include "Math/SMatrix.h"

void SMatrix::MatrixIdentity(SMatrix* matrix)
{
	memset(matrix, 0, sizeof(SMatrix));

	matrix->v[0][0] = 1.0f;
	matrix->v[0][1] = 0.0f;
	matrix->v[0][2] = 0.0f;
	matrix->v[0][3] = 0.0f;

	matrix->v[1][0] = 0.0f;
	matrix->v[1][1] = 1.0f;
	matrix->v[1][2] = 0.0f;
	matrix->v[1][3] = 0.0f;

	matrix->v[2][0] = 0.0f;
	matrix->v[2][1] = 0.0f;
	matrix->v[2][2] = 1.0f;
	matrix->v[2][3] = 0.0f;

	matrix->v[3][0] = 0.0f;
	matrix->v[3][1] = 0.0f;
	matrix->v[3][2] = 0.0f;
	matrix->v[3][3] = 1.0f;
}

void SMatrix::GetScaleMatrix(SMatrix* scaleMatrix, const SVector3& scale)
{
	memset(scaleMatrix, 0, sizeof(SMatrix));

	scaleMatrix->v[0][0] = scale.x;
	scaleMatrix->v[0][1] = 0.0f;
	scaleMatrix->v[0][2] = 0.0f;
	scaleMatrix->v[0][3] = 0.0f;

	scaleMatrix->v[1][0] = 0.0f;
	scaleMatrix->v[1][1] = scale.y;
	scaleMatrix->v[1][2] = 0.0f;
	scaleMatrix->v[1][3] = 0.0f;

	scaleMatrix->v[2][0] = 0.0f;
	scaleMatrix->v[2][1] = 0.0f;
	scaleMatrix->v[2][2] = scale.z;
	scaleMatrix->v[2][3] = 0.0f;

	scaleMatrix->v[3][0] = 0.0f;
	scaleMatrix->v[3][1] = 0.0f;
	scaleMatrix->v[3][2] = 0.0f;
	scaleMatrix->v[3][3] = 1.0f;
}

void SMatrix::GetRotationMatrix(SMatrix* rotationMatrix, const SVector3& rotation)
{
	float xRadian, yRadian, zRadian;
	SMatrix xRotationMatrix, yRotationMatrix, zRotationMatrix;

	xRadian = rotation.x * D3DX_PI / 180.0f;
	yRadian = rotation.y * D3DX_PI / 180.0f;
	zRadian = rotation.z * D3DX_PI / 180.0f;

	// Calculate the Rotation Matrix of X axis
	memset(&xRotationMatrix, 0, sizeof(SMatrix));
	xRotationMatrix.v[0][0] = 1.0f;
	xRotationMatrix.v[0][1] = 0.0f;
	xRotationMatrix.v[0][2] = 0.0f;
	xRotationMatrix.v[0][3] = 0.0f;

	xRotationMatrix.v[1][0] = 0.0f;
	xRotationMatrix.v[1][1] = (float)cos(xRadian);
	xRotationMatrix.v[1][2] = (float)sin(xRadian);
	xRotationMatrix.v[1][3] = 0.0f;

	xRotationMatrix.v[2][0] = 0.0f;
	xRotationMatrix.v[2][1] = (float)-sin(xRadian);
	xRotationMatrix.v[2][2] = (float)cos(xRadian);
	xRotationMatrix.v[2][3] = 0.0f;

	xRotationMatrix.v[3][0] = 0.0f;
	xRotationMatrix.v[3][1] = 0.0f;
	xRotationMatrix.v[3][2] = 0.0f;
	xRotationMatrix.v[3][3] = 1.0f;

	// Calculate the Rotation Matrix of Y axis
	memset(&yRotationMatrix, 0, sizeof(SMatrix));
	yRotationMatrix.v[0][0] = (float)cos(yRadian);
	yRotationMatrix.v[0][1] = 0.0f;
	yRotationMatrix.v[0][2] = (float)-sin(yRadian);
	yRotationMatrix.v[0][3] = 0.0f;

	yRotationMatrix.v[1][0] = 0.0f;
	yRotationMatrix.v[1][1] = 1.0f;
	yRotationMatrix.v[1][2] = 0.0f;
	yRotationMatrix.v[1][3] = 0.0f;

	yRotationMatrix.v[2][0] = (float)sin(yRadian);
	yRotationMatrix.v[2][1] = 0.0f;
	yRotationMatrix.v[2][2] = (float)cos(yRadian);
	yRotationMatrix.v[2][3] = 0.0f;

	yRotationMatrix.v[3][0] = 0.0f;
	yRotationMatrix.v[3][1] = 0.0f;
	yRotationMatrix.v[3][2] = 0.0f;
	yRotationMatrix.v[3][3] = 1.0f;

	// Calculate the Rotation Matrix of Z axis
	memset(&zRotationMatrix, 0, sizeof(SMatrix));
	zRotationMatrix.v[0][0] = (float)cos(zRadian);
	zRotationMatrix.v[0][1] = (float)sin(zRadian);
	zRotationMatrix.v[0][2] = 0.0f;
	zRotationMatrix.v[0][3] = 0.0f;

	zRotationMatrix.v[1][0] = (float)-sin(zRadian);
	zRotationMatrix.v[1][1] = (float)cos(zRadian);
	zRotationMatrix.v[1][2] = 0.0f;
	zRotationMatrix.v[1][3] = 0.0f;

	zRotationMatrix.v[2][0] = 0.0f;
	zRotationMatrix.v[2][1] = 0.0f;
	zRotationMatrix.v[2][2] = 1.0f;
	zRotationMatrix.v[2][3] = 0.0f;

	zRotationMatrix.v[3][0] = 0.0f;
	zRotationMatrix.v[3][1] = 0.0f;
	zRotationMatrix.v[3][2] = 0.0f;
	zRotationMatrix.v[3][3] = 1.0f;

	MatrixIdentity(rotationMatrix);

	*rotationMatrix *= xRotationMatrix;
	*rotationMatrix *= yRotationMatrix;
	*rotationMatrix *= zRotationMatrix;
}

void SMatrix::GetTranslationMatrix(SMatrix* translationMatrix, const SVector3& position)
{
	memset(translationMatrix, 0, sizeof(SMatrix));

	translationMatrix->v[0][0] = 1.0f;
	translationMatrix->v[0][1] = 0.0f;
	translationMatrix->v[0][2] = 0.0f;
	translationMatrix->v[0][3] = 0.0f;

	translationMatrix->v[1][0] = 0.0f;
	translationMatrix->v[1][1] = 1.0f;
	translationMatrix->v[1][2] = 0.0f;
	translationMatrix->v[1][3] = 0.0f;

	translationMatrix->v[2][0] = 0.0f;
	translationMatrix->v[2][1] = 0.0f;
	translationMatrix->v[2][2] = 1.0f;
	translationMatrix->v[2][3] = 0.0f;

	translationMatrix->v[3][0] = position.x;
	translationMatrix->v[3][1] = position.y;
	translationMatrix->v[3][2] = position.z;
	translationMatrix->v[3][3] = 1.0f;
}

void SMatrix::Convert2D3DXMATRIX(D3DXMATRIX* outMatrix, const SMatrix&inputMatrix)
{
	memset(outMatrix, 0, sizeof(D3DXMATRIX));

	outMatrix->m[0][0] = inputMatrix.v[0][0];
	outMatrix->m[0][1] = inputMatrix.v[0][1];
	outMatrix->m[0][2] = inputMatrix.v[0][2];
	outMatrix->m[0][3] = inputMatrix.v[0][3];

	outMatrix->m[1][0] = inputMatrix.v[1][0];
	outMatrix->m[1][1] = inputMatrix.v[1][1];
	outMatrix->m[1][2] = inputMatrix.v[1][2];
	outMatrix->m[1][3] = inputMatrix.v[1][3];

	outMatrix->m[2][0] = inputMatrix.v[2][0];
	outMatrix->m[2][1] = inputMatrix.v[2][1];
	outMatrix->m[2][2] = inputMatrix.v[2][2];
	outMatrix->m[2][3] = inputMatrix.v[2][3];

	outMatrix->m[3][0] = inputMatrix.v[3][0];
	outMatrix->m[3][1] = inputMatrix.v[3][1];
	outMatrix->m[3][2] = inputMatrix.v[3][2];
	outMatrix->m[3][3] = inputMatrix.v[3][3];
}

SMatrix::SMatrix()
{
	v[0][0] = 0.0f;
	v[0][1] = 0.0f;
	v[0][2] = 0.0f;
	v[0][3] = 0.0f;

	v[1][0] = 0.0f;
	v[1][1] = 0.0f;
	v[1][2] = 0.0f;
	v[1][3] = 0.0f;

	v[2][0] = 0.0f;
	v[2][1] = 0.0f;
	v[2][2] = 0.0f;
	v[2][3] = 0.0f;

	v[3][0] = 0.0f;
	v[3][1] = 0.0f;
	v[3][2] = 0.0f;
	v[3][3] = 0.0f;
}

SMatrix::SMatrix(float v00, float v01, float v02, float v03,
	float v10, float v11, float v12, float v13,
	float v20, float v21, float v22, float v23,
	float v30, float v31, float v32, float v33)
{
	v[0][0] = v00;
	v[0][1] = v01;
	v[0][2] = v02;
	v[0][3] = v03;

	v[1][0] = v10;
	v[1][1] = v11;
	v[1][2] = v12;
	v[1][3] = v13;

	v[2][0] = v20;
	v[2][1] = v21;
	v[2][2] = v22;
	v[2][3] = v23;

	v[3][0] = v30;
	v[3][1] = v31;
	v[3][2] = v32;
	v[3][3] = v33;
}

SMatrix SMatrix::operator=(const SMatrix& value)
{
	v[0][0] = value.v[0][0];
	v[0][1] = value.v[0][1];
	v[0][2] = value.v[0][2];
	v[0][3] = value.v[0][3];

	v[1][0] = value.v[1][0];
	v[1][1] = value.v[1][1];
	v[1][2] = value.v[1][2];
	v[1][3] = value.v[1][3];

	v[2][0] = value.v[2][0];
	v[2][1] = value.v[2][1];
	v[2][2] = value.v[2][2];
	v[2][3] = value.v[2][3];

	v[3][0] = value.v[3][0];
	v[3][1] = value.v[3][1];
	v[3][2] = value.v[3][2];
	v[3][3] = value.v[3][3];

	return *this;
}

SMatrix SMatrix::operator=(const D3DXMATRIX& value)
{
	v[0][0] = value.m[0][0];
	v[0][1] = value.m[0][1];
	v[0][2] = value.m[0][2];
	v[0][3] = value.m[0][3];

	v[1][0] = value.m[1][0];
	v[1][1] = value.m[1][1];
	v[1][2] = value.m[1][2];
	v[1][3] = value.m[1][3];

	v[2][0] = value.m[2][0];
	v[2][1] = value.m[2][1];
	v[2][2] = value.m[2][2];
	v[2][3] = value.m[2][3];

	v[3][0] = value.m[3][0];
	v[3][1] = value.m[3][1];
	v[3][2] = value.m[3][2];
	v[3][3] = value.m[3][3];

	return *this;
}

SMatrix SMatrix::operator*(const SMatrix& value)
{
	SMatrix result;
	float v0, v1, v2, v3;

	memset(&result, 0, sizeof(SMatrix));

	v0 = (*this).v[0][0];
	v1 = (*this).v[0][1];
	v2 = (*this).v[0][2];
	v3 = (*this).v[0][3];

	result.v[0][0] = v0 * value.v[0][0] + v1 * value.v[1][0] + v2 * value.v[2][0] + v3 * value.v[3][0];
	result.v[0][1] = v0 * value.v[0][1] + v1 * value.v[1][1] + v2 * value.v[2][1] + v3 * value.v[3][1];
	result.v[0][2] = v0 * value.v[0][2] + v1 * value.v[1][2] + v2 * value.v[2][2] + v3 * value.v[3][2];
	result.v[0][3] = v0 * value.v[0][3] + v1 * value.v[1][3] + v2 * value.v[2][3] + v3 * value.v[3][3];

	v0 = (*this).v[1][0];
	v1 = (*this).v[1][1];
	v2 = (*this).v[1][2];
	v3 = (*this).v[1][3];

	result.v[1][0] = v0 * value.v[0][0] + v1 * value.v[1][0] + v2 * value.v[2][0] + v3 * value.v[3][0];
	result.v[1][1] = v0 * value.v[0][1] + v1 * value.v[1][1] + v2 * value.v[2][1] + v3 * value.v[3][1];
	result.v[1][2] = v0 * value.v[0][2] + v1 * value.v[1][2] + v2 * value.v[2][2] + v3 * value.v[3][2];
	result.v[1][3] = v0 * value.v[0][3] + v1 * value.v[1][3] + v2 * value.v[2][3] + v3 * value.v[3][3];

	v0 = (*this).v[2][0];
	v1 = (*this).v[2][1];
	v2 = (*this).v[2][2];
	v3 = (*this).v[2][3];

	result.v[2][0] = v0 * value.v[0][0] + v1 * value.v[1][0] + v2 * value.v[2][0] + v3 * value.v[3][0];
	result.v[2][1] = v0 * value.v[0][1] + v1 * value.v[1][1] + v2 * value.v[2][1] + v3 * value.v[3][1];
	result.v[2][2] = v0 * value.v[0][2] + v1 * value.v[1][2] + v2 * value.v[2][2] + v3 * value.v[3][2];
	result.v[2][3] = v0 * value.v[0][3] + v1 * value.v[1][3] + v2 * value.v[2][3] + v3 * value.v[3][3];

	v0 = (*this).v[3][0];
	v1 = (*this).v[3][1];
	v2 = (*this).v[3][2];
	v3 = (*this).v[3][3];

	result.v[3][0] = v0 * value.v[0][0] + v1 * value.v[1][0] + v2 * value.v[2][0] + v3 * value.v[3][0];
	result.v[3][1] = v0 * value.v[0][1] + v1 * value.v[1][1] + v2 * value.v[2][1] + v3 * value.v[3][1];
	result.v[3][2] = v0 * value.v[0][2] + v1 * value.v[1][2] + v2 * value.v[2][2] + v3 * value.v[3][2];
	result.v[3][3] = v0 * value.v[0][3] + v1 * value.v[1][3] + v2 * value.v[2][3] + v3 * value.v[3][3];

	return result;
}

SMatrix SMatrix::operator*=(const SMatrix& value)
{
	float v0, v1, v2, v3;

	v0 = v[0][0];
	v1 = v[0][1];
	v2 = v[0][2];
	v3 = v[0][3];

	v[0][0] = v0 * value.v[0][0] + v1 * value.v[1][0] + v2 * value.v[2][0] + v3 * value.v[3][0];
	v[0][1] = v0 * value.v[0][1] + v1 * value.v[1][1] + v2 * value.v[2][1] + v3 * value.v[3][1];
	v[0][2] = v0 * value.v[0][2] + v1 * value.v[1][2] + v2 * value.v[2][2] + v3 * value.v[3][2];
	v[0][3] = v0 * value.v[0][3] + v1 * value.v[1][3] + v2 * value.v[2][3] + v3 * value.v[3][3];

	v0 = v[1][0];
	v1 = v[1][1];
	v2 = v[1][2];
	v3 = v[1][3];

	v[1][0] = v0 * value.v[0][0] + v1 * value.v[1][0] + v2 * value.v[2][0] + v3 * value.v[3][0];
	v[1][1] = v0 * value.v[0][1] + v1 * value.v[1][1] + v2 * value.v[2][1] + v3 * value.v[3][1];
	v[1][2] = v0 * value.v[0][2] + v1 * value.v[1][2] + v2 * value.v[2][2] + v3 * value.v[3][2];
	v[1][3] = v0 * value.v[0][3] + v1 * value.v[1][3] + v2 * value.v[2][3] + v3 * value.v[3][3];

	v0 = v[2][0];
	v1 = v[2][1];
	v2 = v[2][2];
	v3 = v[2][3];

	v[2][0] = v0 * value.v[0][0] + v1 * value.v[1][0] + v2 * value.v[2][0] + v3 * value.v[3][0];
	v[2][1] = v0 * value.v[0][1] + v1 * value.v[1][1] + v2 * value.v[2][1] + v3 * value.v[3][1];
	v[2][2] = v0 * value.v[0][2] + v1 * value.v[1][2] + v2 * value.v[2][2] + v3 * value.v[3][2];
	v[2][3] = v0 * value.v[0][3] + v1 * value.v[1][3] + v2 * value.v[2][3] + v3 * value.v[3][3];

	v0 = v[3][0];
	v1 = v[3][1];
	v2 = v[3][2];
	v3 = v[3][3];

	v[3][0] = v0 * value.v[0][0] + v1 * value.v[1][0] + v2 * value.v[2][0] + v3 * value.v[3][0];
	v[3][1] = v0 * value.v[0][1] + v1 * value.v[1][1] + v2 * value.v[2][1] + v3 * value.v[3][1];
	v[3][2] = v0 * value.v[0][2] + v1 * value.v[1][2] + v2 * value.v[2][2] + v3 * value.v[3][2];
	v[3][3] = v0 * value.v[0][3] + v1 * value.v[1][3] + v2 * value.v[2][3] + v3 * value.v[3][3];

	return *this;
}

       代码其实8月份就写好了,主要是项目正处于测试调整阶段,自己又试着把之前用DX9写的代码都接入自己写的静态库,就推迟了很久。估计接下来《Programming.Role.Playing.Games》的学习也会停下来,还需要再重头看一遍。因为需要把之前HLSL的代码在试着改一遍。

源码下载:下载地址

猜你喜欢

转载自blog.csdn.net/zp288105109a/article/details/82829827