#define宏定义解释:
对于宏定义来说熟悉编程语言的爱好者都不会感到陌生,其实宏定义的用处很多
宏定义是C语言提供的预处理功能,其简单格式为#define 标识符 字符串,
掌握"宏"概念的关键是“换”。一切以换为前提、做任何事情之前先要换,准确理解之前就要“换”。即在对相关命令或语句的含义和功能作具体分析之前就要换。#define M(row,col) m[col*4+row]中 如果改变标识符 M的值,则相应地m的值也会随之改变,反之亦成立
举例如有关资料的部分代码:
// 宏定义define测试.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <memory.h>
#include <iostream>
using namespace std;
typedef float M3DMatrix44f[16];
// 4x4 float单位化矩阵
void m3dLoadIdentity44(M3DMatrix44f m)
{
<span style="white-space:pre"> </span>// 列优先顺序
<span style="white-space:pre"> </span>static M3DMatrix44f<span style="white-space:pre"> </span>identity = { 1.0f, 0.0f, 0.0f, 0.0f,
<span style="white-space:pre"> </span>0.0f, 1.0f, 0.0f, 0.0f,
<span style="white-space:pre"> </span>0.0f, 0.0f, 1.0f, 0.0f,
<span style="white-space:pre"> </span>0.0f, 0.0f, 0.0f, 1.0f };
<span style="white-space:pre"> </span>memcpy(m, identity, sizeof(M3DMatrix44f));
}
void m3dRotationMatrix44(M3DMatrix44f m, float angle, float x, float y, float z)
{
<span style="white-space:pre"> </span>float mag, s, c;
<span style="white-space:pre"> </span>float xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
<span style="white-space:pre"> </span>s = float(sin(angle));
<span style="white-space:pre"> </span>c = float(cos(angle));
<span style="white-space:pre"> </span>mag = float(sqrt(x*x + y*y + z*z));
<span style="white-space:pre"> </span>// 单位化矩阵
<span style="white-space:pre"> </span>if (mag == 0.0f) {
<span style="white-space:pre"> </span>m3dLoadIdentity44(m);
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>// 旋转矩阵标准化
<span style="white-space:pre"> </span>x /= mag;
<span style="white-space:pre"> </span>y /= mag;
<span style="white-space:pre"> </span>z /= mag;
#define M(row,col) m[col*4+row]
<span style="white-space:pre"> </span>xx = x * x;
<span style="white-space:pre"> </span>yy = y * y;
<span style="white-space:pre"> </span>zz = z * z;
<span style="white-space:pre"> </span>xy = x * y;
<span style="white-space:pre"> </span>yz = y * z;
<span style="white-space:pre"> </span>zx = z * x;
<span style="white-space:pre"> </span>xs = x * s;
<span style="white-space:pre"> </span>ys = y * s;
<span style="white-space:pre"> </span>zs = z * s;
<span style="white-space:pre"> </span>one_c = 1.0f - c;
<span style="white-space:pre"> </span>M(0, 0) = (one_c * xx) + c;
<span style="white-space:pre"> </span>M(0, 1) = (one_c * xy) - zs;
<span style="white-space:pre"> </span>M(0, 2) = (one_c * zx) + ys;
<span style="white-space:pre"> </span>M(0, 3) = 0.0f;
<span style="white-space:pre"> </span>M(1, 0) = (one_c * xy) + zs;
<span style="white-space:pre"> </span>M(1, 1) = (one_c * yy) + c;
<span style="white-space:pre"> </span>M(1, 2) = (one_c * yz) - xs;
<span style="white-space:pre"> </span>M(1, 3) = 0.0f;
<span style="white-space:pre"> </span>M(2, 0) = (one_c * zx) - ys;
<span style="white-space:pre"> </span>M(2, 1) = (one_c * yz) + xs;
<span style="white-space:pre"> </span>M(2, 2) = (one_c * zz) + c;
<span style="white-space:pre"> </span>M(2, 3) = 0.0f;
<span style="white-space:pre"> </span>M(3, 0) = 0.0f;
<span style="white-space:pre"> </span>M(3, 1) = 0.0f;
<span style="white-space:pre"> </span>M(3, 2) = 0.0f;
<span style="white-space:pre"> </span>M(3, 3) = 1.0f;
#undef M
}
int _tmain(int argc, _TCHAR* argv[])
{
<span style="white-space:pre"> </span>M3DMatrix44f matrix;
<span style="white-space:pre"> </span>m3dLoadIdentity44(matrix);
<span style="white-space:pre"> </span>for (int i = 0; i < 16; i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (i%4==0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout << endl;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout << matrix[i]<<" ";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout << endl;
<span style="white-space:pre"> </span>m3dRotationMatrix44(matrix, 3.1415926, 1.0, 0.0, 0.0);
<span style="white-space:pre"> </span>for (int i = 0; i < 16; i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (i % 4 == 0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout << endl;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout << matrix[i]<<" ";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout << endl;
#define a matrix[0]
<span style="white-space:pre"> </span>a = 3.0;
#undef a
<span style="white-space:pre"> </span>for (int i = 0; i < 16; i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (i % 4 == 0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout << endl;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout << matrix[i] << " ";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout << endl;
#define b matrix[2]
<span style="white-space:pre"> </span>matrix[2] = 10;
<span style="white-space:pre"> </span>cout << b << endl;
#undef b
<span style="white-space:pre"> </span>system("pause");
<span style="white-space:pre"> </span>return 0;
}