home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 8090 / ModelEdit.7z / RenderWnd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-03-08  |  13.5 KB  |  601 lines

  1. // RenderWnd.cpp : implementation file
  2. //
  3.  
  4. #include "precompile.h"
  5. #include "modeledit.h"
  6. #include "renderwnd.h"
  7. #include "modeleditdlg.h"
  8. #include "geomroutines.h"
  9. #include "regmgr.h"
  10. #include "mmsystem.h" // for timegettime()
  11. #include "windef.h"   // for max (?)
  12. #include "tdguard.h"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20.  
  21. // Externs
  22. extern TCHAR szRegKeyCompany[];
  23. extern TCHAR szRegKeyApp[];
  24. extern TCHAR szRegKeyVer[];
  25.  
  26. #define ____max(a,b)            (((a) > (b)) ? (a) : (b))
  27.  
  28. ////////////////////////////////////////////////////////////////////////////
  29. // Locator
  30. ////////////////////////////////////////////////////////////////////////////
  31.  
  32.  
  33. CLocator::CLocator()
  34. {
  35.     Center();
  36.     m_nDistance = 75.0f;
  37.     UpdateLocation();
  38. }
  39.  
  40. void CLocator::UpdateLocation()
  41. {
  42.     CMatrix mtx, mtx2;
  43.  
  44.     // Add the rotation
  45.     mtx.SetupRot(m_Right, m_nYAngle * -0.01745329f);
  46.     // Note : Rotating about the world Y axis instead of the up vector makes this MUCH easier to understand
  47.     mtx2.SetupRot(CVector(0.0f, 1.0f, 0.0f), m_nXZAngle * -0.01745329f);
  48.     mtx2.Apply(mtx);
  49.     mtx.Apply3x3(m_Location);
  50.     mtx.Apply3x3(m_Orientation);
  51.     mtx.Apply3x3(m_Right);
  52.     mtx.Apply3x3(m_Up);
  53.  
  54.     // Move backward by the distance
  55.     m_Location += m_Orientation * -m_nDistance;
  56.  
  57.     // Add the offset
  58.     m_Location += m_Right * m_Offset.x;
  59.     m_Location += m_Up * m_Offset.y;
  60.     m_Location += m_Orientation * m_Offset.z;
  61.  
  62.     // Reset the adjusting members
  63.     m_nDistance = 0.0f;
  64.     m_nXZAngle = 0.0f;
  65.     m_nYAngle = 0.0f;
  66.     m_Offset.Init(0.0f, 0.0f, 0.0f);
  67. }
  68.  
  69. void CLocator::Center()
  70. {
  71.     m_nDistance = 64.0f;
  72.     m_nXZAngle = 0.0f;
  73.     m_nYAngle = 0.0f;
  74.     m_Offset.Init(0.0f, 0.0f, 0.0f);
  75.  
  76.     m_Location.Init(0.0f, 0.0f, 0.0f);
  77.     m_Orientation.Init(0.0f, 0.0f, 1.0f);
  78.     m_Up.Init(0.0f, 1.0f, 0.0f);
  79.     m_Right.Init(1.0f, 0.0f, 0.0f);
  80. }
  81.  
  82.  
  83. ////////////////////////////////////////////////////////////////////////////
  84. // CCamera
  85.  
  86. CCamera::CCamera()
  87. {
  88.     Reset();
  89. }
  90.  
  91. void CCamera::Update( void )
  92. {
  93.     float curDist = VEC_DIST( m_Position, m_LookAt );
  94. //    curDist += m_fDistance;
  95.  
  96.     LTMatrix mtx, mtx2;
  97.  
  98.     mtx.SetupRot( m_Right, m_fYAngle * -0.01745329f );
  99.     mtx2.SetupRot( LTVector(0.0f,1.0f,0.0f), m_fXZAngle * -0.01745329f );
  100.     mtx2.Apply( mtx );
  101.     mtx.Apply3x3( m_Direction );
  102.     mtx.Apply3x3( m_Right );
  103.     mtx.Apply3x3( m_Up );
  104.  
  105.     m_LookAt += m_Right * m_Offset.x;
  106.     m_LookAt += m_Up * m_Offset.y;
  107.     m_Position = m_LookAt - (curDist * m_Direction);
  108.  
  109.     m_fYAngle = 0.0f;
  110.     m_fXZAngle = 0.0f;
  111.     m_Offset.Init( 0.0f, 0.0f, 0.0f );
  112. }
  113.  
  114. void CCamera::Reset( void )
  115. {
  116.     m_fXZAngle = 0.0f;
  117.     m_fYAngle = 0.0f;
  118.     m_Offset.Init( 0.0f, 0.0f, 0.0f );
  119.  
  120.     m_Position.Init( 0.0f, 0.0f, 64.0f );
  121.     m_LookAt.Init( 0.0f, 0.0f, 0.0f );
  122.     m_Direction.Init( 0.0f, 0.0f, 1.0f );
  123.     m_Right.Init( 1.0f, 0.0f, 0.0f );
  124.     m_Up.Init( 0.0f, 1.0f, 0.0f );
  125.  
  126.     Update();
  127. }
  128.  
  129. void CCamera::LookAtPoint( const LTVector& lookAt )
  130. {
  131.     m_LookAt = lookAt;
  132.     Update();
  133. }
  134.  
  135.  
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CRenderWnd
  139.  
  140. CRenderWnd::CRenderWnd()
  141. {
  142.     if (!TdGuard::Aegis::GetSingleton().DoWork())
  143.     {
  144.         ExitProcess(0);
  145.         return;
  146.     }
  147.  
  148.     m_pModelEditDlg = NULL;
  149.  
  150.     m_DrawStruct.m_bDrawSkeleton = FALSE;
  151.     m_DrawStruct.m_bDrawBright = FALSE;
  152.     m_hContext = NULL;
  153.  
  154.     m_DrawStruct.m_iLOD = PIECELOD_BASE;
  155.     m_DrawStruct.m_CurrentLODDist = 0.0f ;
  156.  
  157.     m_DrawStruct.m_bWireframe = FALSE;
  158.     m_bCameraFollow = TRUE;
  159.  
  160.     m_DrawStruct.m_bDims = FALSE;
  161.     m_DrawStruct.m_bAnimBox = FALSE;
  162.  
  163.     m_DrawStruct.m_bShowNormalRef = FALSE;
  164.     m_DrawStruct.m_bProfile = FALSE;
  165.  
  166.     m_DrawStruct.m_DimsColor.x = m_DrawStruct.m_DimsColor.y = m_DrawStruct.m_DimsColor.z = 255.0f;
  167.     m_DrawStruct.m_ModelBoxColor.x = m_DrawStruct.m_ModelBoxColor.y = m_DrawStruct.m_ModelBoxColor.z = 1.0f;
  168.     m_DrawStruct.m_AnimBoxColor.x = m_DrawStruct.m_AnimBoxColor.y = m_DrawStruct.m_AnimBoxColor.z = 1.0f;
  169.  
  170.     m_bTracking = FALSE;
  171.     m_ptTracking.x = m_ptTracking.y = 0;
  172.     m_ptTrackingScreen.x = m_ptTrackingScreen.y = 0;
  173.  
  174.     m_bCalcRadius = false;
  175.     m_bCalcAndDraw = false;
  176.  
  177.     m_Scale = 1.0f;
  178.     memset(m_pTrackers, 0, sizeof(m_pTrackers));
  179.  
  180.     m_pSetFOVDlg = NULL ;
  181.  
  182.     ResetLights();
  183. }
  184.  
  185.  
  186.  
  187. CRenderWnd::~CRenderWnd()
  188. {
  189. }
  190.  
  191.  
  192. BEGIN_MESSAGE_MAP(CRenderWnd, CWnd)
  193.     //{{AFX_MSG_MAP(CRenderWnd)
  194.     ON_WM_ERASEBKGND()
  195.     ON_WM_CREATE()
  196.     ON_WM_DESTROY()
  197.     ON_WM_MOUSEMOVE()
  198.     ON_WM_LBUTTONDOWN()
  199.     ON_WM_LBUTTONUP()
  200.     ON_WM_RBUTTONDOWN()
  201.     ON_WM_RBUTTONUP()
  202.     ON_WM_MBUTTONDOWN()
  203.     ON_WM_MBUTTONUP()
  204.     ON_WM_CHAR()
  205.     ON_WM_LBUTTONDBLCLK()
  206.     //}}AFX_MSG_MAP
  207. END_MESSAGE_MAP()
  208.  
  209.  
  210. void CRenderWnd::InitAnims(LTAnimTracker *pTrackers[NUM_ANIM_INFOS])
  211. {
  212.     DWORD i;
  213.  
  214.     for(i=0; i < NUM_ANIM_INFOS; i++)
  215.     {
  216.         m_DrawStruct.m_Times[i] = pTrackers[i];
  217.     }
  218. }
  219.  
  220. void CRenderWnd::ReleaseAllTextures()
  221. {
  222.     ::ReleaseAllTextures( GetContext() );
  223. }
  224.  
  225. BOOL CRenderWnd::SetTexture( TextureData *pTexture, DWORD nTextures )
  226. {
  227.     return  SetGLMTexture(GetContext(), pTexture, nTextures);
  228. }
  229.  
  230. void CRenderWnd::SetBackgroundColor( COLORREF  ms_color )
  231. {
  232.     SetGLMBackgroundColor(GetContext(),ms_color);
  233. }
  234.     
  235. // ------------------------------------------------------------------------
  236. // Draw
  237. // use gl to draw the model
  238. // ------------------------------------------------------------------------
  239. void CRenderWnd::Draw()
  240. {
  241.     static LTMatrix IdMat;
  242.     IdMat.Identity();
  243.  
  244.     DrawStruct *pStruct;
  245.  
  246.     pStruct = &m_DrawStruct;
  247.  
  248.     // Setup with 2 lights.
  249.     pStruct->m_ViewerPos = m_Camera.m_Position;
  250.     pStruct->m_LookAt = m_Camera.m_LookAt;
  251.     pStruct->m_LightPositions[0] = m_LightLocators[0].m_Location;
  252.     pStruct->m_LightPositions[1] = m_LightLocators[1].m_Location;
  253.     pStruct->m_LightColors[0].Init(255.0f, 255.0f, 255.0f);
  254.     pStruct->m_LightColors[1].Init(128.0f, 128.0f, 128.0f);
  255.     pStruct->m_nLights = 2;
  256.  
  257.     SetupViewingParameters((GLMContext*)m_hContext, pStruct );
  258.  
  259.      DrawWorldCoordSys();
  260.     
  261.     // if the model exists 
  262.     if (GetModel()  )
  263.     {
  264.         pStruct->m_SelectedPieces = m_SelectedPieces.GetArray();
  265.         pStruct->m_SelectedNodes = m_SelectedNodes.GetArray();
  266.  
  267.         pStruct->m_bCalcRadius = m_bCalcRadius;
  268.         pStruct->m_fModelRadius = 0.0f;
  269.         pStruct->m_bCalcAndDraw = m_bCalcAndDraw;
  270.  
  271.         DrawModel (m_hContext, pStruct);
  272.  
  273.         m_fCurRadius = pStruct->m_fModelRadius;
  274.     }
  275.         
  276.     SwapBuffers( (( GLMContext*)m_hContext)->m_hDC);
  277.  
  278. }
  279.  
  280. BOOL CRenderWnd::SetupTransformMaker(TransformMaker *pMaker)
  281. {
  282.     return m_DrawStruct.SetupTransformMaker(pMaker);
  283. }
  284.  
  285. void CRenderWnd::ResetLocator()
  286. {
  287.     m_Camera.Reset();
  288.     m_Camera.Update();
  289. }
  290.  
  291. void CRenderWnd::ResetLights()
  292. {
  293.     m_LightLocators[0].Center();
  294.     m_LightLocators[0].m_nDistance = 75.0f * m_Scale * 3.0f;
  295.     m_LightLocators[0].m_nXZAngle = 30.0f;
  296.     m_LightLocators[0].UpdateLocation();
  297.     m_LightLocators[0].m_nYAngle = 30.0f;
  298.     m_LightLocators[0].UpdateLocation();
  299.  
  300.     m_LightLocators[1].Center();
  301.     m_LightLocators[1].m_nDistance = 75.0f * m_Scale * 3.0f;
  302.     m_LightLocators[1].m_nXZAngle = -30.0f;
  303.     m_LightLocators[1].UpdateLocation();
  304.     m_LightLocators[1].m_nYAngle = -30.0f;
  305.     m_LightLocators[1].UpdateLocation();
  306. }
  307.  
  308.  
  309. /////////////////////////////////////////////////////////////////////////////
  310. // CRenderWnd message handlers
  311.  
  312. int CRenderWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  313. {
  314.     if (CWnd::OnCreate(lpCreateStruct) == -1)
  315.         return -1;
  316.     
  317.     m_hContext = CreateGLMContext ((void*) GetSafeHwnd());
  318.     if (!m_hContext)
  319.     {
  320.         MessageBox ("Error", "Could not create OpenGL context", MB_OK);
  321.         return -1;
  322.     }
  323.  
  324.     // set the context's permanent attribs.
  325.     CRegMgr regMgr2;
  326.     if (regMgr2.Init(szRegKeyCompany, szRegKeyApp, szRegKeyVer, "Render", HKEY_CURRENT_USER))
  327.     {
  328.         UINT32 dwSize = sizeof(float)*3;
  329.         float bgColor[3];
  330.     
  331.         regMgr2.Get("bgColor",bgColor , dwSize);
  332.  
  333.         SetGLMBackgroundColor( GetContext() , bgColor );    
  334.     }
  335.  
  336.     return 0;
  337. }
  338.  
  339. void CRenderWnd::OnDestroy() 
  340. {
  341.     DeleteGLMContext (m_hContext);
  342.  
  343.     CWnd::OnDestroy();
  344. }
  345.  
  346. BOOL CRenderWnd::OnEraseBkgnd(CDC* pDC) 
  347. {
  348.     CRect rcClient;
  349.     GetClientRect (rcClient);
  350.     pDC->PatBlt (0, 0, rcClient.Width(), rcClient.Height(), BLACKNESS);
  351.     return TRUE;
  352. }
  353.  
  354. void CRenderWnd::OnLButtonDown(UINT nFlags, CPoint point) 
  355. {
  356.     // Delegate..
  357.     if(m_pModelEditDlg && m_pModelEditDlg->HandleButtonDown(0, point))
  358.         return;
  359.  
  360.     if (nFlags & MK_RBUTTON)
  361.     {
  362.         return;
  363.     }
  364.  
  365.     if ((GetAsyncKeyState(VK_SHIFT) & 0x8000) && (!(GetAsyncKeyState(VK_CONTROL) & 0x8000)))
  366.     {
  367.         ResetLocator();
  368.         ResetLights();
  369.     }
  370.     
  371.     m_bTracking = TRUE;
  372.     m_ptTracking = point;
  373.     m_ptTrackingScreen = point;
  374.     ClientToScreen (&m_ptTrackingScreen);
  375.     SetCapture();
  376.     ShowCursor (FALSE);
  377.  
  378.     CWnd::OnLButtonDown(nFlags, point);
  379. }
  380.  
  381. void CRenderWnd::OnLButtonUp(UINT nFlags, CPoint point) 
  382. {
  383.     // Delegate..
  384.     if(m_pModelEditDlg && m_pModelEditDlg->HandleButtonUp(0))
  385.         return;
  386.  
  387.     if (nFlags & MK_RBUTTON)
  388.     {
  389.         return;
  390.     }
  391.     
  392.     if (m_bTracking)
  393.     {
  394.         m_bTracking = FALSE;
  395.         ReleaseCapture();
  396.         ShowCursor (TRUE);
  397.     }
  398.  
  399.     CWnd::OnLButtonUp(nFlags, point);
  400. }
  401.  
  402. void CRenderWnd::OnRButtonDown(UINT nFlags, CPoint point) 
  403. {
  404.     // Delegate..
  405.     if(m_pModelEditDlg && m_pModelEditDlg->HandleButtonDown(2, point))
  406.         return;
  407.  
  408.     if (nFlags & MK_LBUTTON)
  409.     {
  410.         return;
  411.     }
  412.  
  413.     m_bTracking = TRUE;
  414.     m_ptTracking = point;
  415.     m_ptTrackingScreen = point;
  416.     ClientToScreen (&m_ptTrackingScreen);
  417.     SetCapture();
  418.     ShowCursor (FALSE);
  419.  
  420.     CWnd::OnRButtonDown(nFlags, point);
  421. }
  422.  
  423. void CRenderWnd::OnRButtonUp(UINT nFlags, CPoint point) 
  424. {
  425.     // Delegate..
  426.     if(m_pModelEditDlg && m_pModelEditDlg->HandleButtonUp(2))
  427.         return;
  428.  
  429.     if (nFlags & MK_LBUTTON)
  430.     {
  431.         return;
  432.     }
  433.  
  434.     if (m_bTracking)
  435.     {
  436.         m_bTracking = FALSE;
  437.         ReleaseCapture();
  438.         ShowCursor (TRUE);
  439.     }
  440.  
  441.     CWnd::OnRButtonUp(nFlags, point);
  442. }
  443.  
  444. void CRenderWnd::OnMouseMove(UINT nFlags, CPoint point) 
  445. {
  446.     float xzAdd, yAdd, distanceAdd;
  447.     static float turnSpeed = 0.3f;
  448.     static float distanceSpeed = 0.3f;
  449.  
  450.     static DWORD lastTick = 0;
  451.     DWORD holdTime;
  452.     float timeAdjust;
  453.  
  454.     if ((point != m_ptTracking) && (m_bTracking))
  455.     {
  456.         if (!lastTick)
  457.             lastTick = timeGetTime();
  458.         holdTime = timeGetTime();
  459.         timeAdjust = ____max((float)(holdTime - lastTick) / 10.0f, 1.0f); // t.f (don'task)
  460.         lastTick = holdTime;
  461.     }
  462.  
  463.     bool alterCamera = false;
  464.     bool alterLight[2] = {false,false};
  465.  
  466.     if( m_bCameraFollow )
  467.     {
  468.         alterCamera = true;
  469.         alterLight[0] = alterLight[1] = true;
  470.     }
  471.     else if( GetAsyncKeyState( VK_CONTROL ) & 0x8000 )
  472.     {
  473.         if( GetAsyncKeyState( VK_SHIFT ) & 0x8000 )
  474.             alterLight[1] = true;
  475.         else
  476.             alterLight[0] = true;
  477.     }
  478.     else
  479.     {
  480.         alterCamera = true;
  481.     }
  482.  
  483.     if (m_bTracking && (nFlags & MK_LBUTTON) && (nFlags & MK_RBUTTON) && point != m_ptTracking)
  484.     {
  485.         xzAdd = m_Scale * (float)(m_ptTracking.x - point.x) / timeAdjust;
  486.         yAdd = m_Scale * (float)(m_ptTracking.y - point.y) / timeAdjust;
  487.  
  488.         m_Camera.m_Offset.x += xzAdd;
  489.         m_Camera.m_Offset.y += yAdd;
  490.         m_Camera.Update();
  491.         
  492.         SetCursorPos (m_ptTrackingScreen.x, m_ptTrackingScreen.y);
  493.     }
  494.     else if (m_bTracking && (nFlags & MK_LBUTTON) && point != m_ptTracking)
  495.     {
  496.         xzAdd = turnSpeed * (float)(m_ptTracking.x - point.x);
  497.         yAdd = turnSpeed * (float)(point.y - m_ptTracking.y);
  498.  
  499.         if( alterCamera )
  500.         {
  501.             m_Camera.m_fXZAngle += xzAdd;
  502.             m_Camera.m_fYAngle += yAdd;
  503.             m_Camera.Update();
  504.         }
  505.  
  506.         if( alterLight[0] )
  507.         {
  508.             m_LightLocators[0].m_nXZAngle += xzAdd;
  509.             m_LightLocators[0].m_nYAngle += yAdd;
  510.             m_LightLocators[0].UpdateLocation();
  511.         }
  512.  
  513.         if( alterLight[1] )
  514.         {
  515.             m_LightLocators[1].m_nXZAngle += xzAdd;
  516.             m_LightLocators[1].m_nYAngle += yAdd;
  517.             m_LightLocators[1].UpdateLocation();
  518.         }
  519.         
  520.         SetCursorPos (m_ptTrackingScreen.x, m_ptTrackingScreen.y);
  521.     }
  522.     else if (m_bTracking && (nFlags & MK_RBUTTON) && point.y != m_ptTracking.y)
  523.     {
  524.         distanceAdd = distanceSpeed * (float)(point.y - m_ptTracking.y) / timeAdjust;
  525.         distanceAdd *= m_Scale;
  526.  
  527.         if( alterCamera )
  528.         {
  529.             m_Camera.m_Position.z += distanceAdd;
  530.             m_Camera.Update();
  531.         }
  532.  
  533.         if( alterLight[0] )
  534.         {
  535.             m_LightLocators[0].m_nDistance += distanceAdd;
  536.             m_LightLocators[0].UpdateLocation();
  537.         }
  538.  
  539.         if( alterLight[1] )
  540.         {
  541.             m_LightLocators[1].m_nDistance += distanceAdd;
  542.             m_LightLocators[1].UpdateLocation();
  543.         }
  544.  
  545.         SetCursorPos (m_ptTrackingScreen.x, m_ptTrackingScreen.y);
  546.     }
  547.  
  548.     CWnd::OnMouseMove(nFlags, point);
  549. }
  550.  
  551.  
  552. void CRenderWnd::OnMButtonDown(UINT nFlags, CPoint point) 
  553. {
  554.     // Delegate..
  555.     if(m_pModelEditDlg && m_pModelEditDlg->HandleButtonDown(1, point))
  556.         return;
  557.  
  558.     CWnd::OnMButtonDown(nFlags, point);
  559. }
  560.  
  561. void CRenderWnd::OnMButtonUp(UINT nFlags, CPoint point) 
  562. {
  563.     // Delegate..
  564.     if(m_pModelEditDlg && m_pModelEditDlg->HandleButtonUp(1))
  565.         return;
  566.     
  567.     CWnd::OnMButtonUp(nFlags, point);
  568. }
  569.  
  570. void CRenderWnd::OnLButtonDblClk(UINT nFlags, CPoint point) 
  571. {
  572.     CWnd::OnLButtonDblClk(nFlags, point);
  573. }
  574.  
  575. // ------------------------------------------------------------------------
  576. // Create/Open the FOV dialog window.
  577. // ------------------------------------------------------------------------
  578. void CRenderWnd::OpenFOVDlg()
  579. {
  580.     if( m_pSetFOVDlg == NULL )
  581.     {
  582.         m_pSetFOVDlg = new CSetFOVDlg( this );
  583.         m_pSetFOVDlg->Create(CSetFOVDlg::IDD,this);
  584.         m_pSetFOVDlg->SetBaseFOV((int) CModelEditDlg::CalcFOV() );
  585.         m_pSetFOVDlg->ShowWindow(SW_SHOW);
  586.     }else
  587.     {
  588.         m_pSetFOVDlg->SetBaseFOV((int) CModelEditDlg::CalcFOV() );
  589.         m_pSetFOVDlg->ShowWindow(SW_SHOW);
  590.     }
  591. }
  592.  
  593. // ------------------------------------------------------------------------
  594. // this is called by my SetFOVDlg 
  595. // ------------------------------------------------------------------------
  596. void CRenderWnd::SetFOV( int val )
  597. {
  598.     CModelEditDlg::SetFOV((float)val);
  599.     m_DrawStruct.m_FOV = (float)val;
  600. }
  601.