在vc++中有CDC类,同样也可以开发基于OPENGL的OPenGLDC类,这样可以像调用CDC类一样调用OPenGLDC类
首先给出两个工具类,点类和向量类
typedef struct tagVector3D
{
double dx;
double dy;
double dz;
} VECTOR3D;
class CVector3D : public VECTOR3D
{
public:
CVector3D();
virtual ~CVector3D();
CVector3D(VECTOR3D v);
CVector3D(CVector3D &v);
CVector3D(const double *p);
CVector3D(double ix, double iy, double iz);
public:
double GetLength() const;
void Normal();
CVector3D operator*(VECTOR3D v) const;
CVector3D operator*(CVector3D v) const;
void operator=(CVector3D v) ;
void operator-=(VECTOR3D v);
void operator-=(CVector3D v);
CVector3D operator+(VECTOR3D v) const;
CVector3D operator+(CVector3D v) const;
CVector3D operator-(VECTOR3D v) const;
CVector3D operator-(CVector3D v) const;
CVector3D operator*(double d) const;
};
typedef struct tagPoint3D
{
double x;
double y;
double z;
} POINT3D;
class CPoint3D : public POINT3D
{
public:
CPoint3D();
CPoint3D(double ix,double iy,double iz=0.0);
CPoint3D(const double *p);
CPoint3D(POINT3D p);
virtual ~CPoint3D();
public:
CPoint3D operator-(VECTOR3D v) const;
CPoint3D operator+(VECTOR3D v) const;
void operator+=(VECTOR3D v);
void operator-=(VECTOR3D v);
CPoint3D operator-(CVector3D v) const;
CPoint3D operator+(CVector3D v) const;
void operator+=(CVector3D v);
void operator-=(CVector3D v);
};
具体实现如下
CVector3D::CVector3D()
{
}
CVector3D::~CVector3D()
{
}
CVector3D::CVector3D(VECTOR3D v)
{
dx = v.dx;
dy = v.dy;
dz = v.dz;
}
CVector3D::CVector3D(CVector3D &v)
{
dx = v.dx;
dy = v.dy;
dz = v.dz;
}
CVector3D::CVector3D(const double *p)
{
dx = p[0];
dy = p[1];
dz = p[2];
}
CVector3D::CVector3D(double ix, double iy, double iz)
{
dx = ix;
dy = iy;
dz = iz;
}
double CVector3D::GetLength() const//向量的长度
{
double len = 0;
len = sqrt(dx * dx + dy * dy + dz * dz);
return len;
}
void CVector3D::Normal()//单位化
{
double len = GetLength();
dx /= len;
dy /= len;
dz /= len;
}
//向量叉乘,参数为结构或者类
CVector3D CVector3D::operator*(VECTOR3D v) const
{
return CVector3D(dy * v.dz - dz * v.dy, dz * v.dx - dx * v.dz, dx * v.dy - dy * v.dx);
}
CVector3D CVector3D::operator*(CVector3D v) const
{
return CVector3D(dy * v.dz - dz * v.dy, dz * v.dx - dx * v.dz, dx * v.dy - dy * v.dx);
}
void CVector3D::operator-=(VECTOR3D v)
{
dx -= v.dx;
dy -= v.dy;
dz -= v.dz;
}
void CVector3D::operator-=(CVector3D v)
{
dx -= v.dx;
dy -= v.dy;
dz -= v.dz;
}
CVector3D CVector3D::operator+(VECTOR3D v) const
{
return CVector3D(dx + v.dx, dy + v.dy, dz + v.dz);
}
CVector3D CVector3D::operator+(CVector3D v) const
{
return CVector3D(dx + v.dx, dy + v.dy, dz + v.dz);
}
CVector3D CVector3D::operator-(VECTOR3D v) const
{
return CVector3D(dx - v.dx, dy - v.dy, dz - v.dz);
}
CVector3D CVector3D::operator-(CVector3D v) const
{
return CVector3D(dx - v.dx, dy - v.dy, dz - v.dz);
}
CVector3D CVector3D::operator*(double d) const
{
return CVector3D(dx * d, dy * d, dz * d);
}
void CVector3D::operator=(CVector3D v)
{
dx = v.dx;
dy = v.dy;
dz = v.dz;
}
CPoint3D::CPoint3D()
{
}
CPoint3D::~CPoint3D()
{
}
CPoint3D::CPoint3D(double ix,double iy,double iz)
{
x = ix;
y = iy;
z = iz;
}
CPoint3D::CPoint3D(const double *p)
{
x = p[0];
y = p[1];
z = p[2];
}
CPoint3D::CPoint3D(POINT3D p)
{
x = p.x;
y = p.y;
z = p.z;
}
CPoint3D CPoint3D::operator-(VECTOR3D v) const
{
return CPoint3D(x - v.dx, y - v.dy, z - v.dz);
}
CPoint3D CPoint3D::operator+(VECTOR3D v) const
{
return CPoint3D(x + v.dx, y + v.dy, z + v.dz);
}
void CPoint3D::operator+=(VECTOR3D v)
{
x += v.dx;
y += v.dy;
z += v.dz;
}
void CPoint3D::operator-=(VECTOR3D v)
{
x -= v.dx;
y -= v.dy;
z -= v.dz;
}
CPoint3D CPoint3D::operator-(CVector3D v) const
{
return CPoint3D(x - v.dx, y - v.dy, z - v.dz);
}
CPoint3D CPoint3D::operator+(CVector3D v) const
{
return CPoint3D(x + v.dx, y + v.dy, z + v.dz);
}
void CPoint3D::operator+=(CVector3D v)
{
x += v.dx;
y += v.dy;
z += v.dz;
}
void CPoint3D::operator-=(CVector3D v)
{
x -= v.dx;
y -= v.dy;
z -= v.dz;
}