OpenGL 仿射变换 齐次坐标// class NRHomogeneousPoint2D { public: float x, y, ho; NRHomogeneousPoint2D(); }; class NRHomogeneousPoint3D { public: float x, y, z, ho ; NRHomogeneousPoint3D(); }; class NRHomogeneousVector2D { public: float x, y, ho; NRHomogeneousVector2D(); }; class NRHomogeneousVector3D { public: float x, y, z, ho ; NRHomogeneousVector3D(); }; //仿射变换/// class NRMatrix2D { public: float matrixArray[3][3]; NRMatrix2D(); void Identity(); float* operator [](int i); NRMatrix2D operator *(NRMatrix2D rhs); NRHomogeneousPoint2D operator*( NRHomogeneousPoint2D rhs); }; class NRTranslateTransform2D { public: NRMatrix2D translateMatrix; NRTranslateTransform2D(); void Translate(float x, float y); void Identity(); NRHomogeneousPoint2D operator*( NRHomogeneousPoint2D rhs); }; class NRScaleTransform2D { public: NRMatrix2D scaleMatrix; NRScaleTransform2D(); void Scale(float x, float y); void Identity(); NRHomogeneousPoint2D operator*( NRHomogeneousPoint2D rhs); }; class NRRotateTransform2D { public: NRMatrix2D rotateMatrix ; NRRotateTransform2D(); void Rotate(float angle); void Identity(); NRHomogeneousPoint2D operator*( NRHomogeneousPoint2D rhs); }; class NRClipTransform2D { public: NRMatrix2D clipMatrix ; NRClipTransform2D() ; void Clip(float h, float g) ; void Identity() ; NRHomogeneousPoint2D operator*( NRHomogeneousPoint2D rhs) ; }; class NRCompositeTransform2D { public: NRMatrix2D compositeMatrix ; NRCompositeTransform2D(); void AddTransform(NRTranslateTransform2D transLateTransform); void AddTransform(NRScaleTransform2D scaleTransform); void AddTransform(NRRotateTransform2D rotateTransform); void AddTransform(NRClipTransform2D clipTransform) ; void Translate(float x, float y) ; void Scale(float x, float y) ; void Rotate(float angle) ; void Clip(float h, float g) ; void Identity(); NRHomogeneousPoint2D operator*( NRHomogeneousPoint2D rhs); }; 齐次坐标// NRHomogeneousPoint2D::NRHomogeneousPoint2D() { x=y=0; ho=1; // 表示一个点 }; NRHomogeneousPoint3D::NRHomogeneousPoint3D() { x=y=z=0; ho=1; //表示一个点 }; NRHomogeneousVector2D::NRHomogeneousVector2D() { x=y=0; ho=0; //表示一个向量 }; NRHomogeneousVector3D::NRHomogeneousVector3D() { x=y=z=0; ho=0;//表示一个向量 }; //仿射变换/// NRMatrix2D::NRMatrix2D() { Identity(); } float* NRMatrix2D::operator [](int i) { return matrixArray[i]; } void NRMatrix2D::Identity() { for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { this->matrixArray[i][j]=(i==j?(float)1.0:(float)0.0); } } } NRMatrix2D NRMatrix2D::operator *(NRMatrix2D rhs) { NRMatrix2D result ; for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { result[i][j] = matrixArray[i][0]*rhs.matrixArray[0][j]+matrixArray[i][1]*rhs.matrixArray[1][j]+matrixArray[i][2]*rhs.matrixArray[2][j]; } } return result ; } NRHomogeneousPoint2D NRMatrix2D::operator*( NRHomogeneousPoint2D rhs) { NRHomogeneousPoint2D result ; result.x = matrixArray[0][0]*rhs.x + matrixArray[0][1]*rhs.y + matrixArray[0][2]*rhs.ho ; result.y = matrixArray[1][0]*rhs.x + matrixArray[1][1]*rhs.y + matrixArray[1][2]*rhs.ho ; result.ho = matrixArray[2][0]*rhs.x + matrixArray[2][1]*rhs.y + matrixArray[2][2]*rhs.ho ; return result ; } //NRTranslateTransform2D NRTranslateTransform2D::NRTranslateTransform2D() { } void NRTranslateTransform2D::Translate(float x, float y) { translateMatrix[0][2] += x; translateMatrix[1][2] += y; } void NRTranslateTransform2D::Identity() { translateMatrix.Identity(); } NRHomogeneousPoint2D NRTranslateTransform2D::operator*( NRHomogeneousPoint2D rhs) { return translateMatrix * rhs ; } //NRScaleTransform2D NRScaleTransform2D::NRScaleTransform2D() { } void NRScaleTransform2D::Scale(float x, float y) { scaleMatrix[0][0] *= x; scaleMatrix[1][1] *= y ; } void NRScaleTransform2D::Identity() { scaleMatrix.Identity(); } NRHomogeneousPoint2D NRScaleTransform2D::operator*( NRHomogeneousPoint2D rhs) { return scaleMatrix * rhs ; } //NRRotateTransform2d NRRotateTransform2D::NRRotateTransform2D() { } void NRRotateTransform2D::Identity() { rotateMatrix.Identity(); } void NRRotateTransform2D::Rotate(float angle) { // counterclockwise NRMatrix2D lhs ; lhs.Identity(); float costheta= cos(angle); float sintheta = sin(angle); lhs.matrixArray[0][0] = costheta ; lhs.matrixArray[0][1] = -sintheta ; lhs.matrixArray[1][0] = sintheta ; lhs.matrixArray[1][1] = costheta ; rotateMatrix = lhs * rotateMatrix ; } NRHomogeneousPoint2D NRRotateTransform2D::operator*( NRHomogeneousPoint2D rhs) { return rotateMatrix * rhs ; } //class for NRClipTransform2D NRClipTransform2D::NRClipTransform2D() { } void NRClipTransform2D::Clip(float h, float g) { clipMatrix[0][1] += h ; clipMatrix[1][0] += g ; } void NRClipTransform2D::Identity() { clipMatrix.Identity(); } NRHomogeneousPoint2D NRClipTransform2D::operator*( NRHomogeneousPoint2D rhs) { return clipMatrix* rhs ; } //class NRCompositeTransform NRCompositeTransform2D::NRCompositeTransform2D() { } void NRCompositeTransform2D::AddTransform(NRTranslateTransform2D transLateTransform) { compositeMatrix = transLateTransform.translateMatrix *compositeMatrix ; } void NRCompositeTransform2D::AddTransform(NRScaleTransform2D scaleTransform) { compositeMatrix = scaleTransform.scaleMatrix * compositeMatrix ; } void NRCompositeTransform2D::AddTransform(NRRotateTransform2D rotateTransform) { compositeMatrix = rotateTransform.rotateMatrix * compositeMatrix ; } void NRCompositeTransform2D::AddTransform(NRClipTransform2D clipTransform) { compositeMatrix = clipTransform.clipMatrix * compositeMatrix ; } void NRCompositeTransform2D::Translate(float x, float y) { NRMatrix2D lhs ; lhs.matrixArray[0][2] = x ; lhs.matrixArray[1][2] = y ; compositeMatrix = lhs * compositeMatrix ; } void NRCompositeTransform2D::Scale(float x, float y) { NRMatrix2D lhs ; lhs.matrixArray[0][0] = x ; lhs.matrixArray[1][1] = y ; compositeMatrix = lhs * compositeMatrix ; } void NRCompositeTransform2D::Rotate(float angle) { // counterclockwise angle=-angle ; NRMatrix2D lhs ; float costheta= cos(angle); float sintheta = sin(angle); lhs.matrixArray[0][0] = costheta ; lhs.matrixArray[0][1] = -sintheta ; lhs.matrixArray[1][0] = sintheta ; lhs.matrixArray[1][1] = costheta ; compositeMatrix = lhs * compositeMatrix ; } void NRCompositeTransform2D::Clip(float h, float g) { NRMatrix2D lhs ; lhs.matrixArray[0][1] = h ; lhs.matrixArray[1][0] = g ; compositeMatrix = lhs * compositeMatrix ; } void NRCompositeTransform2D::Identity() { compositeMatrix.Identity(); } NRHomogeneousPoint2D NRCompositeTransform2D::operator*( NRHomogeneousPoint2D rhs) { return compositeMatrix * rhs ; } //TransForm.cpp //rorger, 2011 #include "nrglbase.h" #include "nrmath.h" #include "nrdraw.h" #include "iostream" #include "glut.h" #include "math.h" int main() { NRHomogeneousPoint2D testPoint ; testPoint.x = 5 ; testPoint.y = 5 ; cout<<"Original Point: ("<<testPoint.x<<","<<testPoint.y<<")"<<endl ; NRTranslateTransform2D translateTransform2D ; translateTransform2D.Translate(-1,-2) ; translateTransform2D.Translate(-1,-2) ; testPoint = translateTransform2D * testPoint ; cout<<"Using TranslateTransform Translate(-1,-2) , Translate(-1,-2)"<<"/tResult:"; cout<<"("<<testPoint.x <<","<< testPoint.y <<")"<< endl ; testPoint.x = 5 ; testPoint.y = 5 ; NRScaleTransform2D scaleTransform2D ; scaleTransform2D.Scale(0.5, 0.2) ; scaleTransform2D.Scale(2,1); testPoint = scaleTransform2D* testPoint ; cout<<"Using ScaleTransform Scale(0.5,0.2) ,Scale(2,1)"<<"/tResult:"; cout<<"("<<testPoint.x <<","<< testPoint.y <<")"<< endl ; float pi = acos(-1.0) ; //注意用的是弧度制 testPoint.x = 5 ; testPoint.y = 5 ; NRRotateTransform2D rotateTransform2D ; rotateTransform2D.Rotate(pi/2) ; testPoint = rotateTransform2D*testPoint ; cout<<"Using RotateTransform Rotate(pi/2)"<<"/tResult:"; cout<<"("<<testPoint.x <<","<< testPoint.y <<")"<< endl ; testPoint.x = 5 ; testPoint.y = 5 ; NRClipTransform2D clipTransform2D ; clipTransform2D.Clip(0.2,0) ; testPoint = clipTransform2D * testPoint ; cout<<"Using ClipTransform Clip(0.2,0)"<<"/tResult:"; cout<<"("<<testPoint.x <<","<< testPoint.y <<")"<< endl ; testPoint.x = 5 ; testPoint.y = 5 ; clipTransform2D.Clip(0,0.1) ; testPoint = clipTransform2D * testPoint ; cout<<"Using ClipTransform Clip(0,0.1)"<<"/tResult:"; cout<<"("<<testPoint.x <<","<< testPoint.y <<")"<< endl ; testPoint.x=0; testPoint.y=0; NRCompositeTransform2D compositeTransform2D ; compositeTransform2D.Translate(10,5) ; compositeTransform2D.Scale(0.5,1) ; compositeTransform2D.Rotate(pi/2) ; testPoint = compositeTransform2D* testPoint ; cout<<"Using CompositeTransform:Translate(10,5), Scale(0.5,1),Rotate(pi/2)"<<"/tResult:"; cout<<"("<<testPoint.x <<","<< testPoint.y <<")"<< endl ; return 0 ; } /*output: Original Point: (5,5) Using TranslateTransform Translate(-1,-2) , Translate(-1,-2) Result:(3,1) Using ScaleTransform Scale(0.5,0.2) ,Scale(2,1) Result:(5,1) Using RotateTransform Rotate(pi/2) Result:(-5,5) Using ClipTransform Clip(0.2,0) Result:(6,5) Using ClipTransform Clip(0,0.1) Result:(6,5.5) Using CompositeTransform:Translate(10,5), Scale(0.5,1),Rotate(pi/2) Result:( 5,-5) Press any key to continue . . . */