⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtxlib.h

📁 游戏编程精华01-含有几十个游戏编程例子
💻 H
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) Dante Treglia II and Mark A. DeLoura, 2000.  * All rights reserved worldwide. * * This software is provided "as is" without express or implied * warranties. You may freely copy and compile this source into * applications you distribute provided that the copyright text * below is included in the resulting source code, for example: * "Portions Copyright (C) Dante Treglia II and Mark A. DeLoura, 2000" *///==========================================================// C++ Matrix Library// Version: 2.6// Date: May 19, 2000// Authors: Dante Treglia II and Mark A. DeLoura// Thanks to: Miguel Gomez, Stan Melax, Pete Isensee, //   Gabor Nagy, Scott Bilas, James Boer, Eric Lengyel//==========================================================#ifndef _MTXLIB_H#define _MTXLIB_H#include <cstdio>#include <cmath>#include <cassert>static inline float DegToRad(float a) { return a*0.01745329252f;};static inline float RadToDeg(float a) { return a*57.29577951f;};class vector2;class vector3;class vector4;class matrix33;class matrix44;////////////////////////////////////////////////////////////// vector2 class//class vector2 {public:  // Members  float x, y;public:  // Constructors  vector2() {};  // Constructor with initializing float values  vector2(float inX, float inY): x(inX), y(inY) {}  // Constructor with initializing vector2  vector2(const vector2 &v): x(v.x), y(v.y) {}public:  // Array indexing  float                 &operator [] (unsigned int i) {    assert (i<2);    return *(&x+i);  }  // Array indexing  const float           &operator [] (unsigned int i) const {    assert (i<2);    return *(&x+i);  }  // Add a vector2 to this one  vector2               &operator += (const vector2 &v) {    x += v.x;    y += v.y;    return *this;  }  // Subtract a vector2 from this one  vector2               &operator -= (const vector2 &v) {    x -= v.x;    y -= v.y;    return *this;  }  // Multiply the vector2 by a float  vector2               &operator *= (float f) {    x *= f;    y *= f;    return *this;  }  // Divide the vector2 by a float  vector2               &operator /= (float f) {    x /= f;    y /= f;    return *this;  }  // Are these two vector2's equal?  friend bool           operator == (const vector2 &a, const vector2 &b) {    return((a.x == b.x) && (a.y == b.y));  }  // Are these two vector2's not equal?  friend bool           operator != (const vector2 &a, const vector2 &b) {    return((a.x != b.x) || (a.y != b.y));  }  // Negate this vector  friend vector2        operator - (const vector2 &a) {    return vector2(-a.x, -a.y);  }  // Add two vector2's  friend vector2        operator + (const vector2 &a, const vector2 &b) {    vector2 ret(a);    ret += b;    return ret;  }  // Subtract one vector2 from another  friend vector2        operator - (const vector2 &a, const vector2 &b) {    vector2 ret(a);    ret -= b;    return ret;  }  // Multiply vector2 by a float  friend vector2        operator * (const vector2 &v, float f) {    return vector2(f * v.x, f * v.y);  }  // Multiply vector2 by a float  friend vector2        operator * (float f, const vector2 &v) {    return vector2(f * v.x, f * v.y);  }  // Divide vector2 by a float  friend vector2        operator / (const vector2 &v, float f) {    return vector2(v.x / f, v.y / f);  }public:  // Methods  // Set Values  void                  set(float xIn, float yIn) {    x = xIn;    y = yIn;  }  // Get length of a vector2  float                 length() const {    return(float) sqrt(x*x + y*y);  }  // Get squared length of a vector2  float                 lengthSqr() const {    return(x*x + y*y);  }  // Does vector2 equal (0, 0)?  bool                  isZero() const {    return((x == 0.0F) && (y == 0.0F));  }  // Normalize a vector2  vector2               &normalize() {    float m = length();    if (m > 0.0F)      m = 1.0F / m;    else      m = 0.0F;    x *= m;    y *= m;    return *this;  }  // Debug  void                  fprint(FILE* file, char* str) const;};////////////////////////////////////////////////////////////// vector3 class//class vector3 {public:  // Members  float x, y, z;public:  // Constructors  vector3() {};  // Constructor with initializing float values  vector3(float inX, float inY, float inZ): x(inX), y(inY), z(inZ) {}  // Constructor with initializing vector3  vector3(const vector3 &v): x(v.x), y(v.y), z(v.z) {}  // Constructor with initializing vector2  explicit vector3(const vector2 &v): x(v.x), y(v.y), z(0.0F) {}  // Constructor with initializing vector4  // TODO  explicit vector3(const vector4 &v);public:  // Operators  // Array indexing  float                 &operator [] (unsigned int i) {    assert (i<3);    return *(&x+i);  }  // Array indexing  const float           &operator [] (unsigned int i) const {    assert (i<3);    return *(&x+i);  }  // Assign from a vector2  vector3               &operator =  (const vector2 &v) {    x = v.x;    y = v.y;    z = 0.0F;    return *this;  }  // Add a vector3 to this one  vector3               &operator += (const vector3 &v) {    x += v.x;    y += v.y;    z += v.z;    return *this;  }  // Subtract a vector3 from this one  vector3               &operator -= (const vector3 &v) {    x -= v.x;    y -= v.y;    z -= v.z;    return *this;  }  // Multiply the vector3 by a float  vector3               &operator *= (float f) {    x *= f;    y *= f;    z *= f;    return *this;  }  // Divide the vector3 by a float  vector3               &operator /= (float f) {    x /= f;    y /= f;    z /= f;    return *this;  }  // Are these two vector3's equal?  friend bool           operator == (const vector3 &a, const vector3 &b) {    return((a.x == b.x) && (a.y == b.y) && (a.z == b.z));  }  // Are these two vector3's not equal?  friend bool           operator != (const vector3 &a, const vector3 &b) {    return((a.x != b.x) || (a.y != b.y) || (a.z != b.z));  }  // Negate a vector3  friend vector3        operator - (const vector3 &a) {    return vector3(-a.x, -a.y, -a.z);  }  // Add two vector3's  friend vector3        operator + (const vector3 &a, const vector3 &b) {    vector3 ret(a);    ret += b;    return ret;  }  // Subtract one vector3 from another  friend vector3        operator - (const vector3 &a, const vector3 &b) {    vector3 ret(a);    ret -= b;    return ret;  }  // Multiply vector3 by a float  friend vector3        operator * (const vector3 &v, float f) {    return vector3(f * v.x, f * v.y, f * v.z);  }  // Multiply vector3 by a float  friend vector3        operator * (float f, const vector3 &v) {    return vector3(f * v.x, f * v.y, f * v.z);  }  // Divide vector3 by a float  friend vector3        operator / (const vector3 &v, float f) {    return vector3(v.x / f, v.y / f, v.z / f);  }public:  // Methods  // Set Values  void                  set(float xIn, float yIn, float zIn) {    x = xIn;    y = yIn;    z = zIn;  }  // Get length of a vector3  float                 length() const {    return(float) sqrt(x*x + y*y + z*z);  }  // Get squared length of a vector3  float                 lengthSqr() const {    return(x*x + y*y + z*z);  }  // Does vector3 equal (0, 0, 0)?  bool                  isZero() const {    return((x == 0.0F) && (y == 0.0F) && (z == 0.0F));  }  // Normalize a vector3  vector3               &normalize() {    float m = length();    if (m > 0.0F)      m = 1.0F / m;    else      m = 0.0F;    x *= m;    y *= m;    z *= m;    return *this;  }  // Debug  void                  fprint(FILE* file, char* str) const;};////////////////////////////////////////////////////////////// vector4 class//class vector4 {public:  // Members  float x, y, z, w;public:  // Constructors  // vector4(): x(0), y(0), z(0), w(0) {};  vector4() {};  // Constructor with initializing float values  vector4(float inX, float inY, float inZ, float inW): x(inX), y(inY), z(inZ), w(inW) {};  // Constructor with initializing vector4  vector4(const vector4 &v): x(v.x), y(v.y), z(v.z), w(v.w) {};  // Constructor with initializing vector3  explicit vector4(const vector3 &v): x(v.x), y(v.y), z(v.z), w(0.0F) {};  // Constructor with initializing vector2  explicit vector4(const vector2 &v): x(v.x), y(v.y), z(0.0F), w(0.0F) {};public:  // Operators  // Array indexing  float                 &operator [] (unsigned int i) {    assert (i<4);    //return *(&x+i);    return(i == 0) ? x : (i == 1) ? y : (i == 2) ? z : w;  }  // Array indexing  const float           &operator [] (unsigned int i) const {    assert (i<4);    //return *(&x+i);    return(i == 0) ? x : (i == 1) ? y : (i == 2) ? z : w;  }  // Assign from a vector3  vector4               &operator =  (const vector3 &v) {     x = v.x;    y = v.y;    z = v.z;    w = 0.0F;    return *this;  }  // Assign from a vector2  vector4               &operator =  (const vector2 &v) {    x = v.x;    y = v.y;    z = 0.0F;    w = 0.0F;    return *this;  }  // Add a vector4 to this one  vector4               &operator += (const vector4 &v) {    x += v.x;    y += v.y;    z += v.z;    w += v.w;    return *this;  }  // Subtract a vector4 from this one  vector4               &operator -= (const vector4 &v) {    x -= v.x;    y -= v.y;    z -= v.z;    w -= v.w;    return *this;  }  // Multiply the vector4 by a float  vector4               &operator *= (float f) {    x *= f;    y *= f;    z *= f;    w *= f;    return *this;  }  // Divide the vector4 by a float  vector4               &operator /= (float f) {    x /= f;    y /= f;    z /= f;    w /= f;    return *this;  }  // Are these two vector4's equal?  friend bool           operator == (const vector4 &a, const vector4 &b) {    return((a.x == b.x) && (a.y == b.y) &&        (a.z == b.z) && (a.w == b.w));  }  // Are these two vector4's not equal?  friend bool           operator != (const vector4 &a, const vector4 &b) {    return((a.x != b.x) || (a.y != b.y) ||        (a.z != b.z) || (a.w != b.w));  }  // Negate a vector4  friend vector4        operator - (const vector4 &a) {    return vector4(-a.x, -a.y, -a.z, -a.w);  }  // Add two vector4's  friend vector4        operator + (const vector4 &a, const vector4 &b) {    vector4 ret(a);    ret += b;    return ret;  }  // Subtract one vector4 from another  friend vector4        operator - (const vector4 &a, const vector4 &b) {    vector4 ret(a);    ret -= b;    return ret;  }  // Multiply vector4 by a float  friend vector4        operator * (const vector4 &v, float f) {    return vector4(f * v.x, f * v.y, f * v.z, f * v.w);  }  // Multiply vector4 by a float  friend vector4        operator * (float f, const vector4 &v) {    return vector4(f * v.x, f * v.y, f * v.z, f * v.w);  }  // Divide vector4 by a float  friend vector4        operator / (const vector4 &v, float f) {    return vector4(v.x / f, v.y / f, v.z / f, v.w / f);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -