OpenGL绘制星空(太空之眼)
第一次上传作品,这个作品包含流星、彗星、星球、星空背景(k键打开)鼠标移动视角
画面1
画面2
画面3
文件配置
mathlib.h
#pragma once
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include
#include
// ------------------------------------------------------------
// constants
// ------------------------------------------------------------
#define FTP float
// PI & conversions
const FTP ML_2_PI = (FTP)6.283185307179586476;
const FTP ML_PI = (FTP)3.141592653589793238;
const FTP ML_DEG_TO_RAD = (FTP)0.017453292519943295;
const FTP ML_RAD_TO_DEG = (FTP)57.29577951308232286;
const FTP ML_PI_2 = (FTP)1.570796326794896558;
const FTP ML_PI_4 = (FTP)0.785398163397448279;
// tolerance values
const float ML_L_TOL = 0.000001f;
const float ML_TOL = 0.000100f;
const float ML_H_TOL = 0.010000f;
const double ML_D_L_TOL = 0.0000000001;
const double ML_D_TOL = 0.0000000010;
const double ML_D_H_TOL = 0.0000000100;
const double RAD_TO_DEG = 57.295779513;
const double DEG_TO_RAD = 0.0174532925;
// ------------------------------------------------------------
// basic functions
// ------------------------------------------------------------
inline int Sign(FTP f){ return f > 0 ? 1 : -1;}
inline void Clampf(FTP &f, FTP min, FTP max){ f = min > f ? min : (max > f ? f : max); }
inline void Clamp(int &i, int min, int max){ i = min > i ? min : (max > i ? i : max); }
inline FTP Lerp(FTP f, FTP from, FTP to){ return from + (to - from) * f;}
inline FTP Min(FTP f1, FTP f2){ return f1 < f2 ? f1 : f2;}
inline FTP Max(FTP f1, FTP f2){ return f1 > f2 ? f1 : f2;}
inline FTP Abs(FTP f){return f > 0 ? f : -f;}
inline FTP Sin(FTP f){ return sin(fML_DEG_TO_RAD); }
inline FTP Cos(FTP f){ return cos(fML_DEG_TO_RAD); }
inline FTP Tan(FTP f){ return tan(f*ML_DEG_TO_RAD); }
inline FTP Asin(FTP f){ Clampf(f,-1.0,1.0); return asin(f) * ML_RAD_TO_DEG;}
inline FTP AsinR(FTP f){ Clampf(f,-1.0,1.0); return asin(f);}
inline FTP Acos(FTP f){ Clampf(f,-1.0,1.0); return acos(f) * ML_RAD_TO_DEG;}
inline FTP AcosR(FTP f){ Clampf(f,-1.0,1.0); return acos(f);}
inline FTP Atan(FTP f){ return atan(f) * ML_DEG_TO_RAD;}
inline FTP AtanR(FTP f){ return atan(f);}
inline FTP Degree(FTP f){ return f * ML_RAD_TO_DEG;}
inline FTP Radians(FTP f){ return f * ML_DEG_TO_RAD;}
inline FTP Random(){ return (FTP)(rand()) / (FTP)(RAND_MAX);}
inline FTP Random(FTP f){ return Random() * f;}
inline FTP Random(FTP f1, FTP f2){ return Random() * (f2 - f1) + f1;}
inline void SeedRand(unsigned int seed){srand(seed);};
inline bool Coin(void) { return ((rand()%2)==0); }
inline bool IsZero(FTP f){ return fabs(f)<=std::numeric_limits::epsilon();}
inline bool IsZero(FTP f, FTP tol){ return fabs(f)<tol;}
inline bool IsEqual(FTP f1, FTP f2){ return fabs(f1-f2)<=std::numeric_limits::epsilon();}
inline bool IsEqual(FTP f1, FTP f2, FTP tol){ return fabs(f1-f2)<tol;}
void Solve3x3LinSysDoolittle(FTP a[][3], FTP b[], FTP x[]);
void Solve3x3LinSysGaussElim(FTP a[][3], FTP b[], FTP x[]);
// ------------------------------------------------------------
// classes
// ------------------------------------------------------------
class Vector2;
class Matrix3;
class Vector3;
class Matrix4;
class Plane;
// ------------------------------------------------------------
extern const Vector3 ML_A[3];
extern const Vector3 ML_O;
extern const Vector3 ML_AX;
extern const Vector3 ML_AY;
extern const Vector3 ML_AZ;
extern const Matrix3 ML_M3I;
FTP TriArea(const Vector3 &a, const Vector3 &b, const Vector3 &c);
// ------------------------------------------------------------
// Vector2
// ------------------------------------------------------------
class Vector2
{
public:
//members
union
{
struct { FTP x, y; };
struct { FTP v[2]; };
};
// constructors
Vector2();
Vector2(const FTP* args);
Vector2(FTP _v);
Vector2(FTP _x, FTP _y);
Vector2(const Vector2& v);
Vector2(const Vector3& v);
// set
void set(FTP _x, FTP _y);
void set(const Vector2& v);
void set(FTP* args);
void setzero(void) { v[0]=v[1]=0.0; }
// operators
Vector2 operator *(FTP f) const;
Vector2 operator /(FTP f) const;
Vector2 operator +(const Vector2& v) const;
Vector2 operator -(const Vector2& v) const;
Vector2 operator -() const;
Vector2& operator *=(FTP f);
Vector2& operator /=(FTP f);
Vector2& operator +=(const Vector2& v);
Vector2& operator -=(const Vector2& v);
int operator==(const Vector2& v);
int operator!=(const Vector2& v);
Vector2& operator=(const Vector2& v);
Vector2& operator=(const Vector3& v);
Vector2& operator&(const Vector2& v); // reverse copy
// Data access using indices
FTP& operator[](int i) { return (v[i]); }
const FTP& operator[](int i) const { return (v[i]); }
// linear algebra
FTP squaremagnitude() const;
FTP magnitude() const;
FTP squaredistance(const Vector2& v) const;
FTP distance(const Vector2& v) const;
void normalize();
void normalize(const Vector2& v);
FTP dot(const Vector2& v) const;
};
// ------------------------------------------------------------
// Matrix2
// 0 1
// 2 3
// ------------------------------------------------------------
class Matrix2
{
public:
// members
union
{
FTP M[4];
FTP m[2][2];
};
static FTP Identity_Matrix[];
Matrix2();
Matrix2(const FTP* other);
Matrix2(const Matrix2& other);
Vector2 mul(const Vector2& v) const;
};
// ------------------------------------------------------------
// Matrix3
// 0 1 2
// 3 4 5
// 6 7 8
// ------------------------------------------------------------
class Matrix3
{
public:
// members
union
{
FTP M[9];
FTP m[3][3];
};
static FTP Identity_Matrix[];
// constructors
Matrix3();
Matrix3(const FTP* other);
Matrix3(const Matrix3& other);
Matrix3(const Matrix4& other);
// set
Matrix3& operator=(const Matrix3& other);
void set(const FTP* other);
void setcols(const FTP* other);
void setorthocol(int c);
void setorthocol(int c1, int c2);
void setdiag(const FTP* diag);
void setdiag(FTP d1, FTP d2, FTP d3);
void set(const Matrix3& other);
void set(const Matrix4& other);
// simple methods
Matrix3& setidentity();
Matrix3& setzero();
bool iszero();
// this *= other
Matrix3& mul(const Matrix3& other);
// this = m1 * m2
Matrix3& mul(const Matrix3& m1, const Matrix3& m2);
// column c *= f
Matrix3& mulcol(int c, FTP f);
// this = other * f
Matrix3& mul(const Matrix3& other, FTP f);
// vo = this * vi
void mul(const Vector3& vi, Vector3& vo);
Vector3 mul(const Vector3& v) const;
// operators
Matrix3 operator*(const Matrix3& other) const;
Matrix3& operator*=(const Matrix3& other);
Matrix3 operator*(FTP f) const;
Matrix3& operator*=(FTP f);
Matrix3 operator/(FTP f) const;
Matrix3& operator/=(FTP f);
Matrix3 operator +(const Matrix3& v) const;
Matrix3& operator+=(const Matrix3& other);
Matrix3 operator -(const Matrix3& v) const;
Matrix3& operator-=(const Matrix3& other);
int operator==(const Matrix3& m);
int operator!=(const Matrix3& m);
friend int operator==(const Matrix3& m1, const Matrix3& m2);
friend int operator!=(const Matrix3& m1, const Matrix3& m2) { return !(m1 == m2); }
// transformation methods
Matrix3& translate(FTP tx, FTP ty);
Matrix3& settranslate(FTP tx, FTP ty);
Matrix3& rotate(FTP r);
Matrix3& setrotate(FTP r);
Matrix3& scale(FTP sx, FTP sy);
Matrix3& setscale(FTP sx, FTP sy);
bool isrotate();
//////////////////////////////////////////////////////////////////////////
Matrix3& setrotate3d(FTP angle, const Vector3& axis);
Matrix3& setreflect3d(const Vector3& p, const Vector3& n);
Matrix3& setreflect3d(const Vector3& n);
void getaxisangle3d(FTP& angle, Vector3& axis);
// Actions with Vector2
Vector2 transform(const Vector2& v);
static FTP det2x2(FTP a, FTP b, FTP c, FTP d);
static FTP det3x3(FTP a1, FTP a2, FTP a3,
FTP b1, FTP b2, FTP b3,
FTP c1, FTP c2, FTP c3);
static FTP det3x3(const FTP *m);
Matrix3& adjoint();
Matrix3& adjoint(const Matrix3& other);
Matrix3& transpose();
Matrix3& transpose(const Matrix3& other);
Matrix3& invert_exp();
Matrix3& invert_GE();
Matrix3& invert_D();
Matrix3& invert(const Matrix3& other);
void squarecolsymm(FTP *symm);
void squareupper(Matrix3& out);
FTP det() const;
FTP trace() const;
};
// ------------------------------------------------------------
// Vector3
// ------------------------------------------------------------
class Vector3
{
public:
// members
union
{
struct { FTP x, y, z; };
struct { FTP v[3]; };
};
// constructors
Vector3();
Vector3(const FTP* args);
Vector3(FTP _v);
Vector3(FTP _x, FTP _y, FTP _z);
Vector3(const Vector3& v);
Vector3(const Vector2& v);
// set
void set(const FTP* args);
void set(FTP _x, FTP _y, FTP _z);
void set(const Vector3& v);
void set(const Vector3* v);
// Per coordinate (explicit inline functions)
void setx(FTP newX) { v[0] = newX; }
void sety(FTP newY) { v[1] = newY; }
void setz(FTP newZ) { v[2] = newZ; }
void setzero(void) { v[0]=v[1]=v[2]=0.0; }
// Data access using indices
FTP& operator[](int i) { return (v[i]); }
const FTP& operator[](int i) const { return (v[i]); }
// operators
Vector3 operator *(FTP f) const;
Vector3 operator /(FTP f) const;
Vector3 operator +(const Vector3& v) const;
Vector3 operator -(const Vector3& v) const;
Vector3 operator -() const;
Matrix3 operator *(const Vector3& v) const;
Vector3& operator *=(FTP f);
Vector3& operator /=(FTP f);
Vector3& operator +=(const Vector3& v);
Vector3& operator -=(const Vector3& v);
int operator==(const Vector3& v);
int operator!=(const Vector3& v);
friend int operator==(const Vector3& v1, const Vector3& v2);
friend int operator!=(const Vector3& v1, const Vector3& v2) { return !(v1 == v2); }
Vector3& operator=(const Vector3& v);
Vector3& operator&(const Vector3& v); // reverse copy
// linear algebra
FTP squaremagnitude() const;
FTP magnitude() const;
FTP squaredistance(const Vector3& v) const;
FTP distance(const Vector3& v) const;
void normalize();
void normalize(const Vector3& v);
FTP dot(const Vector3& v) const;
Vector3 cross(const Vector3& v) const;
Matrix3 square() const;
void getorthovecs(Vector3 &v1, Vector3 &v2);
};
// ------------------------------------------------------------
// Matrix4
// 0 1 2 3
// 4 5 6 7
// 8 9 10 11
// 12 13 14 15
// ------------------------------------------------------------
class Matrix4
{
public:
// members
union
{
FTP M[16];
FTP m[4][4];
};
static FTP Identity_Matrix[];
static FTP Orientation_Switch_Matrix[];
static FTP Perspective_Matrix[];
public:
// constructors
Matrix4();
Matrix4(const float* other);
Matrix4(const double* other);
Matrix4(const Matrix4& other);
Matrix4(const std::vector<float> &other);
Matrix4(const std::vector<double> &other);
// set
Matrix4& operator=(const Matrix4& other);
void set(const float* other);
void set(const double* other);
void set(const Matrix4& other);
// Simple methods
Matrix4& setidentity();
Matrix4& setzero();
Matrix4& setperspective();
Matrix4& setswitchorientation();
// this *= other
Matrix4& mul_r(const Matrix4& other);
// this *= other
Matrix4& mul_l(const Matrix4& other);
// this = m1 * m2
Matrix4& mul(const Matrix4& m1, const Matrix4& m2);
// Operators
Matrix4& operator*=(const Matrix4& other);
Matrix4 operator*(const Matrix4& other) const;
Matrix4 operator*(FTP f) const;
Matrix4& operator*=(FTP f);
Matrix4 operator/(FTP f) const;
Matrix4& operator/=(FTP f);
// Transformation methods
Matrix4& settranslate(FTP tx, FTP ty, FTP tz);
Matrix4& settranslate(const Vector3 &t);
Matrix4& translate(FTP tx, FTP ty, FTP tz);
Matrix4& translate(const Vector3 &t);
Matrix4& setscale(FTP sx, FTP sy, FTP sz);
Matrix4& scale(FTP sx, FTP sy, FTP sz);
Matrix4& setrotate(const Matrix3& matrix);
void getrotate(Matrix3 &matrix) const;
// rotation around three euler-angles
Matrix4& setrotate(const Vector3& r);
Matrix4& setrotate(FTP rx, FTP ry, FTP rz);
Matrix4& rotate(const Vector3& r);
Matrix4& rotate(FTP rx, FTP ry, FTP rz);
Matrix4& setreflect(const Vector3& p, const Vector3& n);
Matrix4& setreflect(const Vector3& n);
// rotation euler-angle around axis
Matrix4& setrotate(FTP angle, const Vector3& r);
Matrix4& setrotate(FTP angle, FTP x, FTP y, FTP z);
Matrix4& rotate(FTP angle, const Vector3& r);
Matrix4& rotate(FTP angle, FTP x, FTP y, FTP z);
// Invert/Transpose
Matrix4& adjoint