MF3DHelper.h
上传用户:qzpk666
上传日期:2022-08-04
资源大小:59k
文件大小:8k
- // MF3DHelper.h: interface for the CMF3DHelper class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_MF3DHELPER_H__E4644642_E2CC_4BC1_8F4B_F127D7329F77__INCLUDED_)
- #define AFX_MF3DHELPER_H__E4644642_E2CC_4BC1_8F4B_F127D7329F77__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- #include <cmath>
- /*
- How to use this class(es):
- 1. Add a member variable of type CMF3DHelper to your class
- 2. At each window resize (or initially) call CMF3DHelper::Initialize function
- Parameters: vMin, vMax is a vector representing your entire data range
- bSupportTrackBall: TRUE if you want to use your mouse to control the 3D object
- 3. Call RenderPoint() function to get 2D-Screen coordinates from your 3D-point
- */
- namespace NeHe {
- // Code below (Matrix, Vector) was originally taken from NeHe-SDK, drilled down a little bit
- // ------ BEGIN OF NeHe specific class ------------
- // http://nehe.gamedev.net/
- // Nehe SDK
- // Copyright 2002 Jeff Molofee, Gregory Austwick and Others
-
- class Point {
- public:
- Point (float xx, float yy) { x=xx; y=yy; }
- Point() { x = y = 0.0f; }
- float x;
- float y;
- };
- typedef struct VECTOR3D {
- float x;
- float y;
- float z;
- } VECTOR3D, *PVECTOR3D;
-
- typedef struct VECTOR4D {
- float x;
- float y;
- float z;
- float w;
- } VECTOR4D, *PVECTOR4D;
-
- #define SQR(x) ((x)*(x))
- class Vector {
- public:
- float x,y,z,w;
- Vector() { x=y=z=w=0.0f; }; // clearing constructor
- Vector(float vx,float vy,float vz,float vw) { x=vx; y=vy; z=vz; w=vw; };
- Vector(float vx,float vy,float vz) { x=vx; y=vy; z=vz; w=1.0f; };
- Vector(const Vector &v) { x=v.x; y=v.y; z=v.z; w=v.w; };
-
- Vector operator + (Vector &v) { return Vector((x+v.x),(y+v.y),(z+v.z),(w+v.w)); };
- Vector operator - (Vector &v) { return Vector((x-v.x),(y-v.y),(z-v.z),(w-v.w)); };
- Vector operator * (Vector &v) { return Vector((x*v.x),(y*v.y),(z*v.z),(w*v.w)); };
- Vector operator * (float f) { return Vector((x*f),(y*f),(z*f),(w*f)); };
-
- // assignment arithmetic operators
- Vector operator += (Vector &v) {x+=v.x; y+=v.y; z+=v.z; w+=v.w; return *this;}
- Vector operator -= (Vector &v) {x-=v.x; y-=v.y; z-=v.z; w-=v.w; return *this;}
- Vector operator *= (Vector &v) {x*=v.x; y*=v.y; z*=v.z; w*=v.w; return *this;}
- Vector operator *= (float f) {x*=f; y*=f; z*=f; w*=f; return *this;}
- Vector operator /= (float f) {x/=f; y/=f; z/=f; w/=f; return *this;}
-
- // Vector functions
- void Null() { x = y = z = w = 0.0f; }
- // Magnitude function
- float Mag() { return (float)sqrt(SQR(x)+SQR(y)+SQR(z)+SQR(w)); };
- float Mag3() { return (float)sqrt(SQR(x)+SQR(y)+SQR(z)); };
- // Magnitude squared
- float MagSqr() { return (SQR(x)+SQR(y)+SQR(z)+SQR(w)); };
- float MagSqr3() { return (SQR(x)+SQR(y)+SQR(z)); };
- // Normalize vector
- void Normalize() { float mag=this->Mag(); x/=mag; y/=mag; z/=mag; w/=mag; };
- void Normalize3() { float mag=this->Mag3(); x/=mag; y/=mag; z/=mag; };
- // Dot product
- float Dot(Vector &v) { return (((x*v.x)+(y*v.y)+(z*v.z)+(w*v.w))/(this->Mag()*v.Mag())); };
- float Dot3(Vector &v) { return (((x*v.x)+(y*v.y)+(z*v.z))/(this->Mag3()*v.Mag3())); };
- float Length() { return MagSqr(); }
- float Length3() { return MagSqr3(); }
- // Cross product
- Vector Cross(Vector &v) { return Vector( (y*v.z)-(z*v.y) , (z*v.x)-(x*v.z) , (x*v.y)-(y*v.x) ); };
- void Trace() { TRACE(_T("Vector<%.2f / %.2f / %.2f / %.2f>"), x,y,z,w); }
- };
-
-
- #define Idx(x,y) ((x*4)+y)
- class Matrix {
- public:
- float m[16];
- Matrix() { this->Id(); };
- Matrix(const Matrix &mt) { for (int ct=0; ct<16; ct++) m[ct]=mt.m[ct]; };
-
- // Zero matrix
- void Zero() { for (int ct=0; ct<16; ct++) m[ct]=0.0f; };
- // Identity matrix
- void Id() { this->Zero(); m[0]=1.0f; m[5]=1.0f; m[10]=1.0f; m[15]=1.0f; };
- // Multiply matrix
- Matrix operator * (Matrix &mt) {
- Matrix temp; // the return matrix
- for (int cty=0; cty<4; cty++) {
- float row1=m[Idx(0,cty)],row2=m[Idx(1,cty)],row3=m[Idx(2,cty)],row4=m[Idx(3,cty)];
- for (int ctx=0; ctx<4; ctx++) {
- temp.m[Idx(ctx,cty)]=(row1*mt.m[Idx(ctx,0)])+(row2*mt.m[Idx(ctx,1)])+
- (row3*mt.m[Idx(ctx,2)])+(row4*mt.m[Idx(ctx,3)]);
- }
- }
- return temp;
- };
-
- // Multiply matrix and assign
- Matrix operator *= (Matrix &mt) {
- Matrix temp; // the return matrix
- for (int cty=0; cty<4; cty++) {
- // get the present row
- float row1=m[Idx(0,cty)],row2=m[Idx(1,cty)],row3=m[Idx(2,cty)],row4=m[Idx(3,cty)];
- // then xs
- for (int ctx=0; ctx<4; ctx++) {
- temp.m[Idx(ctx,cty)]=(row1*mt.m[Idx(ctx,0)])+(row2*mt.m[Idx(ctx,1)])+
- (row3*mt.m[Idx(ctx,2)])+(row4*mt.m[Idx(ctx,3)]);
- }
- }
- // copy back to ours
- for (int ct=0; ct<16; ct++) m[ct]=temp.m[ct];
- return *this;
- };
- // Transform a vector by the matrix
- Vector operator * (Vector &v) {
- Vector temp; // the return vector
- temp.x=v.x*m[Idx(0,0)]+v.y*m[Idx(0,1)]+v.z*m[Idx(0,2)]+v.w*m[Idx(0,3)];
- temp.y=v.x*m[Idx(1,0)]+v.y*m[Idx(1,1)]+v.z*m[Idx(1,2)]+v.w*m[Idx(1,3)];
- temp.z=v.x*m[Idx(2,0)]+v.y*m[Idx(2,1)]+v.z*m[Idx(2,2)]+v.w*m[Idx(2,3)];
- temp.w=v.x*m[Idx(3,0)]+v.y*m[Idx(3,1)]+v.z*m[Idx(3,2)]+v.w*m[Idx(3,3)];
- return Vector(temp);
- };
- };
- // CMFArcBall: Class to allow user to rotate view-port
- // Pressing <CTRL> allows user to rotate about Z-axis
- class CMFArcBall {
- public:
- CMFArcBall ();
- virtual ~CMFArcBall();
-
- void Initialize(int iWidth, int iHeight) {
- m_iWidth = iWidth;
- m_iHeight = iHeight;
- m_fXRotation = m_fYRotation = m_fZRotation = 0.0f;
- m_bLButtonDown = FALSE;
- m_fXDiv = (float)m_iWidth / (360.0f/2.0f); // whole window covers 180 degree
- m_fYDiv = (float)m_iHeight / (360.0f/2.0f);
- }
-
- void SetRotation(float fXRot, float fYRot, float fZRot) {
- m_fXRotation = fXRot;
- m_fYRotation = fYRot;
- m_fZRotation = fZRot;
- }
- BOOL Update(CDC* pDC, NeHe::Vector &cRotationDelta, NeHe::Vector &cRotationCumulative);
-
- protected:
- BOOL getMousePosition(BOOL &bLDown, BOOL &bLToggled, BOOL &bRDown, BOOL &bRToggled, int &xPos, int &yPos, CDC* pDC);
- private:
- void mouseToArc(CPoint cPt, float &fx, float &fy) ;
- private:
- CPoint m_cMousePoint;
- BOOL m_bLButtonDown;
- float m_fXRotation;
- float m_fYRotation;
- float m_fZRotation;
- int m_iWidth;
- int m_iHeight;
- float m_fXDiv;
- float m_fYDiv;
- };
- }; // namespace NeHe
- class CMF3DHelper {
- public:
- CMF3DHelper();
- virtual ~CMF3DHelper();
- // Call this function (after initialization) to get 2D-screen-coordinates from your 3D point
- /* Remark: Vector needs to be normalized to -1..1! */
- NeHe::Vector RenderPoint(NeHe::Vector v3DPoint);
-
- // Draws the cube representing your 3D-environment
- void DrawVisibilityCube(CDC* pDC, BOOL bDrawAxes=TRUE, BOOL bDrawBoundingCoordinates=FALSE);
- // Re-Initialize the 3D-environment
- BOOL Initialize(CDC *pTargetDC, NeHe::Vector vMin, NeHe::Vector vMax, BOOL bSupportTrackBall);
- BOOL Reset(CDC *pTargetDC, NeHe::Vector vMin, NeHe::Vector vMax, BOOL bSupportTrackBall);
- // Needs to be called cyclically, updates the 3D-environment according to mouse position
- void UpdateTrackBall(CDC *pDC);
- public:
- // Operations to modify viewport, mostly unused (internally only)
- void Rotate(float angleX,float angleY,float angleZ, BOOL bAbsolute);
- void Scale(float xFactor,float yFactor,float zFactor);
- void Translate(float x,float y,float z);
- void RotateVector(float fAngle,float fVectorX,float fVectorY, float fVectorZ, BOOL bAbsolute);
- private:
- float cotangens(float x);
- BOOL setupPerspective(float fovy, float aspect, float zNear, float zFar);
- void emul_glLoadIdentity();
- BOOL emul_gluPerspective(float fovy, float aspect, float zNear, float zFar);
- void emul_glViewport(int x, int y, int widht, int height);
- void emul_glRotatef(float angle,float x,float y, float z);
- void emul_glTranslatef(float x, float y, float z);
- void emul_glScalef(float x, float y, float z);
- private:
- NeHe::Matrix m_mProjection;
- NeHe::Matrix m_mRotation;
- NeHe::Matrix m_mModel;
- float m_fViewport_x;
- float m_fViewport_y;
- float m_fViewport_widht;
- float m_fViewport_height;
-
- NeHe::Vector m_vCubeMin;
- NeHe::Vector m_vCubeMax;
- // Try to speed up things a little bit...
- NeHe::Matrix m_mShadow;
- BOOL m_bShadowUpdateRequired;
- NeHe::CMFArcBall m_cTrackBall;
- };
- #endif // !defined(AFX_MF3DHELPER_H__E4644642_E2CC_4BC1_8F4B_F127D7329F77__INCLUDED_)