VectorCtl.h
上传用户:bjzhiyi
上传日期:2007-01-01
资源大小:40k
文件大小:10k
源码类别:

OpenGL

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                            *
  3. *   Vector control                                                           *
  4. *   ----------------                                                         *
  5. *                                                                            *
  6. *   A 3D vector MFC control derived from CButton.                            *
  7. *   Features:                                                                *
  8. *      - Real-time light rendering on a 3D ball.                             *
  9. *      - Variable ball radius and position.                                  *
  10. *      - Supports bitmap background (tiled).                                 *
  11. *      - Supports vertical gradient color background (from color to color).  *
  12. *      - Variable ball color (diffuse), light color and ambient color.       *
  13. *      - Variable specular intensity.                                        *
  14. *      - Supports attached controls (for automatic update).                  *
  15. *      - Variable mouse sensitivity.                                         *
  16. *      - Supports front clipping (vector will not have negative Z values).   *
  17. *      - Supports callback functions for the following events:               *
  18. *           1. The trackball has moved (vector is changing).                 *
  19. *           2. The user dropped the trackball (released left mouse button)   *
  20. *              i.e., the vector was changed.                                 *
  21. *                                                                            *
  22. *                                                                            *
  23. *****************************************************************************/
  24. #ifndef _VECTOR_CTL_H
  25. #define _VECTOR_CTL_H
  26. // Callback pointer prototype:
  27. typedef void (CALLBACK *VectorCtlCallbackProc)(double dVecX, double dVecY, double dVecZ);
  28. // The callback should look like: 
  29. //      void CALLBACK MyCallBack (double dVecX, double dVecY, double dVecZ);
  30. // or 
  31. //      static void CALLBACK MyClass::MyCallBack (double dVecX, double dVecY, double dVecZ);
  32. class CVectorCtl : public CButton
  33. {
  34. #define EPS                                 1.0e-6                  // Epsilon
  35. #define DEFAULT_VEC                         {0.00, 0.00, 1.00}      // Default start vector
  36. #define DEFAULT_DIFFUSE                     RGB( 30,   0, 200)      // Default diffuse color
  37. #define DEFAULT_AMBIENT                     RGB( 20,  20,  20)      // Default ambient color
  38. #define DEFAULT_LIGHT                       RGB(200, 200, 200)      // Default light color
  39. #define DEFAULT_START_BACKGROUND_COLOR      RGB(  0,   0,   0)      // Default gradient background start color
  40. #define DEFAULT_END_BACKGROUND_COLOR        RGB(140,   0, 120)      // Default gradient background end color
  41. #define DEFAULT_SPEC_EXP                    25.0                    // Default specular intensity
  42. #define VAL_NOT_IN_USE                      -50000                  // Internal use
  43. public:
  44.     CVectorCtl (); 
  45.     virtual ~CVectorCtl ();
  46.         // Owner-drawn control support function
  47.     virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
  48.         // Sets / Gets diffuse (ball) color. 
  49.     void SetDiffuseColor (COLORREF clr)                 { m_clrDiffuse = clr; Redraw (); }
  50.     COLORREF GetDiffuseColor ()                         { return m_clrDiffuse; }
  51.         // Sets / Gets ambient (background) color. 
  52.     void SetAmbientColor (COLORREF clr)                 { m_clrAmbient = clr; Redraw (); }
  53.     COLORREF GetAmbientColor ()                         { return m_clrAmbient; }
  54.         // Sets / Gets light color.
  55.     void SetLightColor (COLORREF clr)                   { m_clrLight = clr; Redraw (); }
  56.     COLORREF GetLightColor ()                           { return m_clrLight; }
  57.         // Sets background gradient color (from start to finish vertically)
  58.     void SetBackgroundColor (COLORREF clrStart, COLORREF clrEnd);
  59.         // Sets a background bitmap (resource ID)
  60.     BOOL SetBackgroundImage (UINT uBackgroundBitmapID);
  61.         // Sets / Gets specular intensity
  62.     BOOL SetSpecularExponent (double dExp);
  63.     double GetSpecularExponent ()                       { return m_dSpecularExponent; }
  64.         // Enables auto-update of axis controls.
  65.         // Place the control's ID and the SetWindowText function will be called
  66.         // for each vector component to display the value in the control.
  67.     void SetAxisControl (int nXCtl, int nYCtl, int nZCtl);
  68.         // Sets / Gets ball radius (in pixels)
  69.     void SetRadius (UINT uRadius);
  70.     UINT GetRadius ()                                   { return UINT(m_iRadius); }
  71.         // Sets / Gets ball position (in pixels)
  72.     void SetCenter (UINT uHorizPos, UINT uVertPos);
  73.     UINT GetHorizCenter ()                              { return UINT(m_iXCenter); }
  74.     UINT GetVertCenter ()                               { return UINT(m_iYCenter); }
  75.         // Sets / Gets vector components
  76.     void SetX (double dx)                               { SetAxis (dx, 0);  }
  77.     double GetX()                                       { return m_dVec[0]; }
  78.     void SetY (double dy)                               { SetAxis (dy, 1);  }
  79.     double GetY()                                       { return m_dVec[1]; }
  80.     void SetZ (double dz)                               { SetAxis (dz, 2);  }
  81.     double GetZ()                                       { return m_dVec[2]; }
  82.     void SetVector (double dx, double dy, double dz);
  83.         // Sets / Gets mouse sensitivity
  84.     BOOL SetSensitivity (UINT uSens);
  85.     UINT GetSensitivity ()                              { return UINT(m_dSensitivity); }
  86.         // Bounds / Unbounds vector to front (positive Z) only
  87.     void ClipToFront (BOOL bEnable)                     { m_bFrontVector = bEnable; }
  88.     
  89.         // Set user-defined callback function to call whenever the vector has changed.
  90.         // Set to NULL to disable callback.
  91.     void SetVectorChangingCallback (VectorCtlCallbackProc proc)
  92.         { m_procVectorChanging = proc; }
  93.         // Set user-defined callback function to call whenever the vector has finished
  94.         // changing (user dropped track-ball).
  95.         // Set to NULL to disable callback.
  96.     void SetVectorChangedCallback (VectorCtlCallbackProc proc)
  97.         { m_procVectorChanged = proc; }
  98.         // Overridable:
  99.     virtual LRESULT WindowProc( UINT message, WPARAM wParam, LPARAM lParam );
  100. private:
  101.         // Mouse is being dragged
  102.     void OnMouseDrag (int , int);   
  103.         // Create and measure off-screen buffer
  104.     void InitBitmap (LPDRAWITEMSTRUCT lpDrawItemStruct, CDC *pDC);
  105.         // Build image to BitBlt
  106.     void BuildImage (LPDRAWITEMSTRUCT lpDrawItemStruct);
  107.         // Free resources of background (non-ball) bitmap
  108.     void ClearBackgroundBitmap ();
  109.         // Normalize vector
  110.     BOOL Normalize ();
  111.         // Calculate lightning effect for a pixel on the ball
  112.     COLORREF CalcLight (double dx, double dy, double dz);
  113.         // Rotate our vector by X and Y angles
  114.     void RotateByXandY (double XRot, double YRot);
  115.         // Create background image resource
  116.     void CreateBackground ();
  117.         // Force redraw of entire image
  118.     void Redraw (BOOL bErase = FALSE);
  119.         // Update user-defined vector components controls
  120.     void UpdateAxisControls ();
  121.         // Sets a specific vector component to a specific value
  122.     void SetAxis (double d, int nAxis);
  123.     CBitmap     m_bmpBuffer,            // Buffer bitmap for BitBlt
  124.                 m_bmpBack;              // Background image bitmap
  125.     CDC         m_dcMem;                // Memory DC
  126.     BOOL        m_bBmpCreated,          // Was the bitmap created ?
  127.                 m_bBackgroundBitmapUsed,// Are we using a background bitmap ?
  128.                 m_bImageChange,         // Has the image changed ?
  129.                 m_bFrontVector,         // Is the vector constrained to be facing front (positive Z) ?
  130.                 m_bHasFocus,            // Does the control have the focus ?
  131.                 m_bSelected;            // Is the control selected ?
  132.     int         m_iWidth,               // Region width
  133.                 m_iHeight,              // Region height
  134.                 m_iRadius,              // Ball radius
  135.                 m_iSqrRadius,           // Ball radius to the power of two
  136.                 m_iXCenter,             // X center point
  137.                 m_iYCenter,             // Y center point
  138.                 m_iLastMouseX,          // Last mouse-drag X position
  139.                 m_iLastMouseY;          // Last mouse-drag Y position
  140.     CBitmap    *m_pOldBitmap;           // Previously selected bitmap    
  141.     COLORREF    m_clrDiffuse,           // Ball diffusion color (self color)
  142.                 m_clrAmbient,           // Ambient (background) color
  143.                 m_clrLight,             // Color of light
  144.                 m_clrBackgroundStart,   // Background color gradient start
  145.                 m_clrBackgroundEnd;     // Background color gradient end
  146.     CWnd       *pCtl[3];                // Pointers to axis display controls
  147.     double      m_dSpecularExponent,    // Specularity effect intensity
  148.                 m_dVec[3],              // Vector components
  149.                 m_dSensitivity;         // The bigger the number the less sensitive the mouse gets
  150.                                         // Valid ranges are 1..MAX_UINT
  151.     VectorCtlCallbackProc   m_procVectorChanging,
  152.                             m_procVectorChanged;
  153. };
  154. #endif