这一篇接着来一步步实现。
本篇主要是介绍矩阵,并推导渲染流水管线中使用的缩放、旋转、平移矩阵,然后利用C++实现它,并且做一个简单的小游戏(使用平移、旋转、缩放)
1.首先介绍矩阵并实现它
理论:
C++代码实现:matrix3
#pragma once
#include "vector2.h"
//此处的宏定义主要是为了读者能区分矩阵元素的位置
#define _M_11 0
#define _M_12 1
#define _M_13 2
#define _M_21 3
#define _M_22 4
#define _M_23 5
#define _M_31 6
#define _M_32 7
#define _M_33 8
class Matrix3
{
public:
float m[9];
//构造
Matrix3()
{
//默认为单位矩阵
Identity();
}
//单位化
Matrix3& Identity()
{
for (int i = 0; i < 9; ++i)
m[i] = 0.0f;
m[_M_11] = m[_M_22] = m[_M_33] = 1.0f;
return *this;
}
//平移
Matrix3& Translate(float x, float y)
{
m[_M_11] = 1.0f; m[_M_12] = 0.0f; m[_M_13] = 0.0f;
m[_M_21] = 0.0f; m[_M_22] = 1.0f; m[_M_23] = 0.0f;
m[_M_31] = x; m[_M_32] = y; m[_M_33] = 1.0f;
return *this;
}
Matrix3& Translate(Vector2 v)
{
m[_M_11] = 1.0f; m[_M_12] = 0.0f; m[_M_13] = 0.0f;
m[_M_21] = 0.0f; m[_M_22] = 1.0f; m[_M_23] = 0.0f;
m[_M_31] = v.x; m[_M_32] = v.y; m[_M_33] = 1.0f;
return *this;
}
//旋转
Matrix3& Rotation(float angle)
{
m[_M_11] = cos(angle); m[_M_12] = sin(angle); m[_M_13] = 0.0f;
m[_M_21] = -m[_M_12]; m[_M_22] = m[_M_11]; m[_M_23] = 0.0f;
m[_M_31] = 0.0f; m[_M_32] = 0.0f; m[_M_33] = 1.0f;
return *this;
}
//缩放
Matrix3& Scale(float x, float y)
{
m[_M_11] = x; m[_M_12] = 0.0f; m[_M_13] = 0.0f;
m[_M_21] = 0.0f; m[_M_22] = y; m[_M_23] = 0.0f;
m[_M_31] = 0.0f; m[_M_32] = 0.0f; m[_M_33] = 1.0f;
return *this;
}
Matrix3& Scale(Vector2 v)
{
m[_M_11] = v.x; m[_M_12] = 0.0f; m[_M_13] = 0.0f;
m[_M_21] = 0.0f; m[_M_22] = v.y; m[_M_23] = 0.0f;
m[_M_31] = 0.0f; m[_M_32] = 0.0f; m[_M_33] = 1.0f;
return *this;
}
};
inline Vector2* V2_x_M3(const Vector2* v, const Matrix3* m, Vector2* r)
{
float temp[] =
{
v->x * m->m[_M_11] + v->y * m->m[_M_21] + m->m[_M_31],
v->x * m->m[_M_12] + v->y * m->m[_M_22] + m->m[_M_32],
v->x * m->m[_M_13] + v->y * m->m[_M_23] + m->m[_M_33],
};
r->x = temp[0] / temp[2];
r->y = temp[1] / temp[2];
return r;
}
inline Vector2 operator * (const Vector2& v, const Matrix3& m)
{
Vector2 r;
return *V2_x_M3(&v, &m, &r);
}
inline Matrix3* M3_x_M3(const Matrix3* m1, const Matrix3* m2, Matrix3* r)
{
r->m[_M_11] =
m1->m[_M_11] * m2->m[_M_11] +
m1->m[_M_12] * m2->m[_M_21] +
m1->m[_M_13] * m2->m[_M_31];
r->m[_M_12] =
m1->m[_M_11] * m2->m[_M_12] +
m1->m[_M_12] * m2->m[_M_22] +
m1->m[_M_13] * m2->m[_M_32];
r->m[_M_13] =
m1->m[_M_11] * m2->m[_M_13] +
m1->m[_M_12] * m2->m[_M_23] +
m1->m[_M_13] * m2->m[_M_33];
r->m[_M_21] =
m1->m[_M_21] * m2->m[_M_11] +
m1->m[_M_22] * m2->m[_M_21] +
m1->m[_M_23] * m2->m[_M_31];
r->m[_M_22] =
m1->m[_M_21] * m2->m[_M_12] +
m1->m[_M_22] * m2->m[_M_22] +
m1->m[_M_23] * m2->m[_M_32];
r->m[_M_23] =
m1->m[_M_21] * m2->m[_M_13] +
m1->m[_M_22] * m2->m[_M_23] +
m1->m[_M_23] * m2->m[_M_33];
r->m[_M_31] =
m1->m[_M_31] * m2->m[_M_11] +
m1->m[_M_32] * m2->m[_M_21] +
m1->m[_M_33] * m2->m[_M_31];
r->m[_M_32] =
m1->m[_M_31] * m2->m[_M_12] +
m1->m[_M_32] * m2->m[_M_22] +
m1->m[_M_33] * m2->m[_M_32];
r->m[_M_33] =
m1->m[_M_31] * m2->m[_M_13] +
m1->m[_M_32] * m2->m[_M_23] +
m1->m[_M_33] * m2->m[_M_33];
return r;
}
inline Matrix3 operator * (const Matrix3& m1, const Matrix3& m2)
{
Matrix3 r;
return *M3_x_M3(&m1, &m2, &r);
}
2.游戏部分:
#include <iostream>
#include <windows.h>
#include <vector>
#include "vector2.h"
#include "matrix3.h"
#pragma comment(lib, "Msimg32.lib")
//定义一些窗口常量
#define _CLIENT_PW 640 //屏幕像素宽
#define _CLIENT_PH 480 //屏幕像素高
#define _FRAME_PER_SECOND 40 //屏幕每秒刷新帧数
#define _FRAME_SLEEPTIME (1000/_FRAME_PER_SECOND) //每帧休息时间
#define _PI 3.1415 //pai
//全局窗口句柄
HWND g_hWnd = nullptr;
//窗口活动
BOOL g_Active = FALSE;
//函数声明
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void GameInit();
void GameRun();
void GameEnd();
//程序入口点
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//填充窗口类别结构体
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszMenuName = nullptr;
wc.lpszClassName = __TEXT("My3D");
//注册窗口类别结构体
RegisterClass(&wc);
//根据客户区矩形计算窗口矩形
int screen_pw = GetSystemMetrics(SM_CXSCREEN);
int screen_ph = GetSystemMetrics(SM_CYSCREEN);
RECT r =
{
(screen_pw - _CLIENT_PW) / 2,
(screen_ph - _CLIENT_PH) / 2,
r.left + _CLIENT_PW,
r.top + _CLIENT_PH
};
//校准窗口矩形
AdjustWindowRect(&r, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE);
//创建窗口
g_hWnd = CreateWindow(
wc.lpszClassName,
__TEXT("My3D"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
r.left, r.top, r.right - r.left, r.bottom - r.top, //窗口左上角位置和宽高
HWND_DESKTOP,
NULL,
wc.hInstance,
NULL);
//更新窗口
UpdateWindow(g_hWnd);
//显示窗口
ShowWindow(g_hWnd, nCmdShow);
//游戏初始化
GameInit();
//消息循环
MSG msg = {};
while (WM_QUIT != msg.message)
{
//下面的代码表示,一旦有消息就调用消息处理函数来处理消息
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//当没有消息且窗口激活那么就执行游戏循环
else if (TRUE == g_Active)
{
//游戏运行
GameRun();
}
//当没有消息且窗口未激活就等待消息
else
WaitMessage();
}
//游戏结束
GameEnd();
return (int)msg.wParam;
}
//消息回调函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_ACTIVATEAPP:
{
g_Active = static_cast<BOOL>(wParam);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(1);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
//主显示设备
HDC g_MainDC = nullptr;
//后备显示设备
HDC g_BackDC = nullptr;
//英雄位置
Vector2 HeroPos(_CLIENT_PW / 2, _CLIENT_PH / 2);
//目的地位置
Vector2 Des = HeroPos;
//移动速度(标量)
float speed = 5.0f;
//移动方向
Vector2 Dir(0.0f, 0.0f);
//是否达到目的地
BOOL isArrive = true;
//矩形模型:顶点数据
Vector2 rect[4];
//角度
float angle = 0.0f;
//缩放比例
Vector2 scale(1.0f, 1.0f);
Vector2 multiple(0.1f, 0.1f);
void GameInit()
{
//得到主显示设备
g_MainDC = GetDC(g_hWnd);
//创建后备显示设备
g_BackDC = CreateCompatibleDC(g_MainDC);
//创建和主显示设备兼容的位图
HBITMAP hbmp = CreateCompatibleBitmap(g_MainDC, _CLIENT_PW, _CLIENT_PH);
//将兼容位图选入兼容设备,删除老的位图
DeleteObject(SelectObject(g_BackDC, hbmp));
//初始化模型顶点数据
rect[0].Set(15, 15);
rect[1].Set(-15, 15);
rect[2].Set(-15, -15);
rect[3].Set(15, -15);
}
// 设备 起点x,y 终点x,y
void DrawLine(HDC hdc, float b_x1, float b_y1, float e_x2, float e_y2)
{
MoveToEx(hdc, b_x1, b_y1, 0);
LineTo(hdc, e_x2, e_y2);
}
void GameRun()
{
//得到从操作系统启动一瞬间到目前的毫秒数
ULONGLONG begin_time = GetTickCount64();
//清屏
BitBlt(g_BackDC, 0, 0, _CLIENT_PW, _CLIENT_PH, nullptr, 0, 0, WHITENESS);
//创建变换矩阵:先缩放,再旋转,最后平移
Matrix3 M[4];
M[0].Scale(scale);
M[1].Rotation(angle);
M[2].Translate(HeroPos);
M[3] = M[0] * M[1] * M[2];
//创建模型接受变换后的数值:原始数据不能随意修改
Vector2 temp[4];
for (int i = 0; i < 4; ++i)
{
temp[i] = rect[i] * M[3];
}
//绘制
DrawLine(g_BackDC, temp[0].x, temp[0].y, temp[1].x, temp[1].y);
DrawLine(g_BackDC, temp[1].x, temp[1].y, temp[2].x, temp[2].y);
DrawLine(g_BackDC, temp[2].x, temp[2].y, temp[3].x, temp[3].y);
DrawLine(g_BackDC, temp[3].x, temp[3].y, temp[0].x, temp[0].y);
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000)
{
//得到当前光标相对于桌面的座标
POINT p;
GetCursorPos(&p);
//将桌面的座标转换到客户区的座标
ScreenToClient(g_hWnd, &p);
Des.Set(p.x, p.y);
//计算移动方向
Vector2 t = Des - HeroPos;
Dir = t.Normalize();
}
scale += multiple;
if (scale.x >= 3.0f || scale.x <= 1.0f)
multiple = -multiple;
angle += 0.05f;
if (angle >= 2*_PI)
angle = 0.0f;
if ((HeroPos - Des).Length() <= 5)
isArrive = true;
else
isArrive = false;
//位置改变
if (isArrive == false)
{
HeroPos += (Dir * speed);
}
//后备显示设备颜色传输给主显示设备
BitBlt(
g_MainDC,
0, 0, _CLIENT_PW, _CLIENT_PH,
g_BackDC,
0, 0,
SRCCOPY);
//得到本次游戏循环总的消耗时间
ULONGLONG frame_time = GetTickCount() - begin_time;
//进行本次游戏循环休息
if (_FRAME_SLEEPTIME > frame_time)
Sleep(_FRAME_SLEEPTIME - frame_time);
else
Sleep(1); //必须休息一下
}
void GameEnd()
{
//删除后备设备,释放主设备
DeleteDC(g_BackDC);
ReleaseDC(g_hWnd, g_MainDC);
}
游戏效果: