3DWindow.cpp
资源名称:estereo2.zip [点击查看]
上传用户:fengshi120
上传日期:2014-07-17
资源大小:6155k
文件大小:30k
源码类别:
3D图形编程
开发平台:
C/C++
- ////////////////////////////////////////////////////////////////////////////////////////////////
- // Copyright: Frey Research
- // Unit Name: 3DWindow.cpp
- // Author: Gary Gray
- // Description: custom control for displaying 3-D shapes
- /////////////////////////////////////////////////////////////////////////////////////////////////
- // 3DWindowSmall.cpp : implementation file
- //
- #include "stdafx.h"
- #include "StereoPlus.h"
- #include "StereoPlusDlg.h"
- #include <mmSystem.h>
- #include "DxUtil.h"
- #include <d3d9types.h>
- #include "3DWindow.h"
- #include ".3DWindow.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CStereoPlusDlg* pMainDialog;
- #define DELETENULL(object) if (object) {delete object;object = NULL;}
- #define RELEASENULL(object) if (object) {object->Release();object = NULL;}
- //////////////////////////////////////////////////////////////////////////////
- // Globals ///////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- struct POINT_VERTEX
- {
- D3DXVECTOR3 p;
- D3DCOLOR color;
- // static const DWORD FVF;
- };
- const DWORD POINT_VERTEX_FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE ;
- struct MESH_VERTEX_TEX
- {
- D3DXVECTOR3 p;
- D3DXVECTOR3 n;
- FLOAT tu,tv;
- // static const DWORD FVF;
- };
- const DWORD MESH_VERTEX_TEX_FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0);
- BOOL C3DWindow::m_Registered = FALSE;
- BOOL C3DWindow::m_bInhibitUpdate = FALSE;
- /*lint -save*/
- /*lint -e620 ignore Suspicious constant (L or one?) these are all from Microsoft !*/
- /*lint -e834 ignore comments on confusing operators there not to me !*/
- /////////////////////////////////////////////////////////////////////////////
- // C3DWindow
- C3DWindow::C3DWindow() : CWnd()
- {
- m_bOrthographic = false;
- m_pD3D = NULL;
- m_pD3DDevice = NULL;
- m_GUITimerID = NULL;
- m_fScale = 1.0F;
- m_bRenderZAxis = false;
- for(int k=0;k<3;k++)
- m_pAxesVB[k] = NULL;
- //initialise some memory to please PC-Lint
- ZeroMemory(&m_light,sizeof(D3DLIGHT9));
- ZeroMemory(&m_D3DsdBackBuffer,sizeof(D3DSURFACE_DESC));
- m_strRegistry = "3D Orientation";
- UINT uNBytes;
- CD3DArcBall* pArcBallProfile = NULL;
- theApp.GetProfileBinary("3D Window",m_strRegistry,(LPBYTE*)&pArcBallProfile,&uNBytes);
- if(uNBytes == sizeof(CD3DArcBall) && pArcBallProfile)
- m_ArcBall = *pArcBallProfile;
- DELETENULL(pArcBallProfile);
- m_pFont = new CD3DFont( _T("Arial"), 10,NULL);// D3DFONT_BOLD );
- m_pFontLarge = new CD3DFont( _T("Arial"), 16,NULL);// D3DFONT_BOLD );
- m_pTextureAxisLabelX = NULL;
- m_pTextureAxisLabelY = NULL;
- m_pTextureAxisLabelZ = NULL;
- m_pPixelVB = NULL;
- m_nPoints = 0;
- for(int iCone = 0;iCone<3;iCone++)
- m_pConeMesh[iCone] = NULL;
- }
- void C3DWindow::SetRegistryKey(CString strRegistry)
- {
- m_strRegistry = strRegistry;
- UINT uNBytes;
- CD3DArcBall* pArcBallProfile = NULL;
- theApp.GetProfileBinary("3D Window",m_strRegistry,(LPBYTE*)&pArcBallProfile,&uNBytes);
- if(uNBytes == sizeof(CD3DArcBall) && pArcBallProfile)
- m_ArcBall = *pArcBallProfile;
- DELETENULL(pArcBallProfile);
- }
- C3DWindow::~C3DWindow()
- {
- try
- {
- SAFE_DELETE( m_pFont );
- SAFE_DELETE( m_pFontLarge );
- ASSERT(m_pD3D == NULL);
- ASSERT(m_pD3DDevice == NULL);
- }
- catch (...) {};
- }
- void C3DWindow::OnNcDestroy()
- {
- CWnd::OnNcDestroy();
- delete this;
- }
- BEGIN_MESSAGE_MAP(C3DWindow, CWnd)
- //{{AFX_MSG_MAP(C3DWindow)
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_WM_NCDESTROY()
- ON_WM_RBUTTONUP()
- ON_WM_INITMENUPOPUP()
- //}}AFX_MSG_MAP
- ON_WM_PAINT()
- ON_WM_CHAR()
- ON_WM_TIMER()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_LBUTTONDBLCLK()
- END_MESSAGE_MAP()
- void C3DWindow::InterpretError(HRESULT hr) const
- {
- CString strError;
- strError = DXGetErrorString9 (hr);
- #ifdef _DEBUG
- AfxMessageBox(strError,MB_OK);
- #endif
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3DWindow message handlers
- void C3DWindow::OnRButtonUp(UINT nFlags, CPoint point)
- {
- long ControlID = GetWindowLong(m_hWnd,GWL_ID);
- CWnd* pParent = GetParent();
- LRESULT lResult = pParent->SendMessage(WM_COMMAND,MAKEWPARAM(ControlID,WM_RBUTTONUP),(LPARAM)(HWND)m_hWnd);
- if(!lResult)
- CWnd::OnRButtonUp(nFlags, point);
- }
- void C3DWindow::ShowMenu(UINT idrMenu)
- {
- CMenu Pum, *pPum;
- Pum.LoadMenu(idrMenu);
- pPum = Pum.GetSubMenu(0);
- CPoint pt;
- GetCursorPos(&pt);
- pPum->TrackPopupMenu(TPM_CENTERALIGN | TPM_LEFTBUTTON, pt.x, pt.y-18, this);
- }
- BOOL C3DWindow::OnCommand(WPARAM wParam, LPARAM lParam)
- {
- if(lParam == 0)
- {
- }
- return CWnd::OnCommand(wParam, lParam);
- }
- void C3DWindow::OnInitMenuPopup(CMenu* pPopupMenu, UINT /*nIndex*/, BOOL /*bSysMenu*/)
- {
- }
- HRESULT C3DWindow::ReleaseD3DX()
- {
- RELEASENULL(m_pD3DDevice);
- RELEASENULL(m_pD3D);
- return S_OK;
- }
- HRESULT C3DWindow::InitD3DX(void)
- {
- CWaitCursor WaitCursor;
- if ((m_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == 0)
- {
- AfxMessageBox(_T("DirectX 9.0c not installed.nPlease install this or later version."));
- exit(0);
- }
- else
- {
- // Get the current desktop display mode
- D3DDISPLAYMODE d3ddm;
- if(SUCCEEDED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
- {
- HRESULT hr;
- if( FAILED( hr = m_pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
- d3ddm.Format, D3DUSAGE_DEPTHSTENCIL,
- D3DRTYPE_SURFACE,
- D3DFMT_D16
- ) ) )
- {
- if( hr == D3DERR_NOTAVAILABLE )
- AfxMessageBox("POTENTIAL PROBLEM: We need at least a 16-bit z-buffer!",MB_ICONSTOP);
- exit(0);
- }
- //
- // Do we support hardware vertex processing? if so, use it.
- // If not, downgrade to software.
- //
- D3DCAPS9 d3dCaps;
- if( FAILED( m_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL, &d3dCaps ) ) )
- {
- AfxMessageBox("failure of GetDeviceCaps!",MB_ICONSTOP);
- exit(0);
- }
- DWORD dwBehaviorFlags = 0;
- if( d3dCaps.VertexProcessingCaps != 0 )
- dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
- else
- dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- //
- // Everything checks out - create a simple, windowed device.
- //
- ZeroMemory(&m_d3dpp, sizeof(m_d3dpp));
- m_d3dpp.BackBufferFormat = d3ddm.Format;
- m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- m_d3dpp.Windowed = TRUE;
- m_d3dpp.EnableAutoDepthStencil = TRUE;
- m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
- CRect rcWindowClient;
- GetClientRect(&rcWindowClient);
- m_d3dpp.BackBufferWidth = (UINT)rcWindowClient.Width();
- m_d3dpp.BackBufferHeight = (UINT)rcWindowClient.Height();
- hr = m_pD3D->CreateDevice(
- D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL,
- m_hWnd,
- D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE,
- &m_d3dpp,
- &m_pD3DDevice);
- // Store render target surface desc
- LPDIRECT3DSURFACE9 pBackBuffer;
- m_pD3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
- pBackBuffer->GetDesc( &m_D3DsdBackBuffer );
- pBackBuffer->Release();
- InitDeviceObjects();
- RestoreDeviceObjects();
- }
- }
- return S_OK;
- }
- HRESULT C3DWindow::RestoreDeviceObjects(void)
- {
- ASSERT(m_pD3DDevice);
- // Setup render state
- D3DMATERIAL9 mtrl;
- D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f );
- m_pD3DDevice->SetMaterial( &mtrl );
- m_pD3DDevice->SetRenderState( D3DRS_WRAP0, TRUE );
- // Set miscellaneous render states
- m_pD3DDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
- m_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
- m_pD3DDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
- // Turn on lighting.
- // m_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
- // Enable ambient lighting to a dim, grey light, so objects that
- // are not lit by the other lights are not completely black
- m_pD3DDevice->SetRenderState( D3DRS_AMBIENT,D3DCOLOR_COLORVALUE( 0.5, 0.5, 0.5, 1.0 ) );
- FLOAT fAspect = (FLOAT)m_D3DsdBackBuffer.Width / (FLOAT)m_D3DsdBackBuffer.Height;
- if(m_bOrthographic)
- {
- D3DXMatrixOrthoOffCenterLH (&m_matProj, -10.0f*fAspect, 10.0f*fAspect, -10.0f,10.0f,-100.0f, 100.0f);
- }
- else
- {
- // Setup the projection matrix
- FLOAT fAspect = (FLOAT)m_D3DsdBackBuffer.Width / (FLOAT)m_D3DsdBackBuffer.Height;
- D3DXMatrixPerspectiveFovLH( &m_matProj, D3DX_PI/4, fAspect,m_fObjectRadius/64.0f, m_fObjectRadius*200.0f );
- }
- m_pD3DDevice->SetTransform( D3DTS_PROJECTION, &m_matProj );
- D3DXMatrixTranspose( &m_matProjT, &m_matProj );
- HRESULT hr;
- DWORD dwNumVertices = 2;
- for(int k=0;k<3;k++)
- {
- SAFE_RELEASE(m_pAxesVB[k]);
- if(SUCCEEDED(hr = m_pD3DDevice->CreateVertexBuffer( dwNumVertices*sizeof(POINT_VERTEX),
- D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY ,
- POINT_VERTEX_FVF, D3DPOOL_DEFAULT, &m_pAxesVB[k], NULL )))
- {
- POINT_VERTEX* pVertices;
- if(SUCCEEDED(m_pAxesVB[k]->Lock( 0, 0, (void**)&pVertices, 0 )) )
- {
- for(DWORD i=0;i<dwNumVertices;i++)
- {
- pVertices[i].color = D3DCOLOR_ARGB(255,0,255,0);
- }
- if(k == 0)
- {
- pVertices[0].p = D3DXVECTOR3(-10.0f,0.0f,0.0f);
- pVertices[1].p = D3DXVECTOR3( 10.0f,0.0f,0.0f);
- }
- else if(k==1)
- {//assume 4:3
- pVertices[0].p = D3DXVECTOR3(0.0f,-7.5f,0.0f);
- pVertices[1].p = D3DXVECTOR3(0.0f, 7.5f,0.0f);
- }
- else if(k==2)
- {
- pVertices[0].p = D3DXVECTOR3(0.0f,0.0f,-10.0f);
- pVertices[1].p = D3DXVECTOR3(0.0f,0.0f, 10.0f);
- }
- m_pAxesVB[k]->Unlock();
- }
- }
- else
- {
- return E_FAIL;
- }
- }
- if(m_pFont)
- {
- m_pFont->InvalidateDeviceObjects();
- m_pFont->RestoreDeviceObjects();
- }
- if(m_pFontLarge)
- {
- m_pFontLarge->InvalidateDeviceObjects();
- m_pFontLarge->RestoreDeviceObjects();
- }
- return S_OK;
- }
- void C3DWindow::SetUpdateTimer(bool bState)
- {
- if(bState && m_GUITimerID == NULL)
- {
- m_GUITimerID = SetTimer(GUI_UPDATE_TIMER,33,NULL);
- }
- else if(!bState && m_GUITimerID != NULL)
- {
- KillTimer(m_GUITimerID);
- m_GUITimerID = NULL;
- }
- }
- int C3DWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- return 0;
- }
- int C3DWindow::Start(bool bOrthographic)
- {
- m_bOrthographic = bOrthographic;
- HRESULT hr = InitD3DX();
- if ( FAILED(hr) )
- {
- InterpretError(hr);
- return -1;
- }
- m_GUITimerID = SetTimer(GUI_UPDATE_TIMER,33,NULL);
- return hr;
- }
- BOOL C3DWindow::Register()
- {
- if(m_Registered) return TRUE;
- WNDCLASS wc;
- wc.style = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
- wc.lpfnWndProc = C3DWindow::Window3DWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = NULL;
- wc.hIcon = NULL;
- wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = 0;
- wc.lpszClassName = "3DWindow";
- if(!::RegisterClass(&wc))
- {
- ASSERT(0);
- return FALSE;
- }
- m_Registered = TRUE;
- return TRUE;
- }
- LRESULT CALLBACK C3DWindow::Window3DWndProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
- {
- if(uiMsg == WM_NCCREATE)
- {
- C3DWindow *pCtrl = new C3DWindow;
- ASSERT(pCtrl);
- BOOL b = pCtrl->SubclassWindow(hWnd);
- ASSERT(b);
- return b;
- }
- return ::DefWindowProc(hWnd, uiMsg, wParam, lParam);
- }
- void C3DWindow::OnDestroy()
- {
- if(m_GUITimerID)
- {
- KillTimer(m_GUITimerID);
- m_GUITimerID = NULL;
- }
- DeleteDeviceObjects();
- ReleaseD3DX();
- theApp.WriteProfileBinary("3D Window",m_strRegistry,(LPBYTE)&m_ArcBall,sizeof(CD3DArcBall));
- CWnd::OnDestroy();
- }
- //-----------------------------------------------------------------------------
- // Name: Render3DEnvironment()
- // Desc: Draws the scene.
- //-----------------------------------------------------------------------------
- HRESULT C3DWindow::Render3DEnvironment(void)
- {
- if(C3DWindow::m_bInhibitUpdate)
- return S_OK;
- ASSERT(m_pD3DDevice);
- HRESULT hr;
- // Test the cooperative level to see if it's okay to render
- if( FAILED( hr = m_pD3DDevice->TestCooperativeLevel() ) )
- {
- if (D3DERR_DEVICELOST == hr)
- {
- //The device has been lost but cannot be reset at this time. Therefore, rendering is not possible.
- return S_OK;
- }
- else if (D3DERR_DEVICENOTRESET == hr)
- {
- //The device has been lost but can be reset at this time.
- //Normally, you would call Reset(), but we will cheat here for simplicity's sake.
- DeleteDeviceObjects();
- ReleaseD3DX();
- if (FAILED(InitD3DX()))
- {
- DeleteDeviceObjects();
- ReleaseD3DX();
- return S_OK;
- }
- RestoreDeviceObjects();
- }
- }
- // Frame move the scene
- D3DXMatrixTranslation( &m_matWorld,-m_vObjectCenter.x,
- -m_vObjectCenter.y,
- -m_vObjectCenter.z );
- D3DXMatrixMultiply( &m_matWorld, &m_matWorld, m_ArcBall.GetRotationMatrix() );
- D3DXMatrixMultiply( &m_matWorld, &m_matWorld, m_ArcBall.GetTranslationMatrix() );
- m_pD3DDevice->SetTransform( D3DTS_WORLD, &m_matWorld );
- D3DXMATRIX matView;
- D3DXVECTOR3 vecEye( 0.0f, 0.0f, -2.0f*m_fObjectRadius );
- D3DXVECTOR3 vecAt( 0.0f, 0.0f, 0.0f );
- D3DXVECTOR3 vecUp( 0.0f, 1.0f, 0.0f );
- D3DXMatrixLookAtLH( &matView, &vecEye,&vecAt,&vecUp );
- m_pD3DDevice->SetTransform( D3DTS_VIEW, &matView );
- // Render the scene as normal
- if( FAILED( hr = Render() ) )
- return hr;
- // Show the frame on the primary surface.
- m_pD3DDevice->Present( NULL, NULL, NULL, NULL );
- return S_OK;
- }
- HRESULT C3DWindow::Render(void)
- {
- HRESULT hr = S_OK;;
- if(!m_pD3DDevice || !m_hWnd)
- return E_FAIL;
- //clear the back buffer
- D3DXCOLOR colorClear = D3DXCOLOR(0.4f, 0.5f, 0.8f, 1.0f);
- m_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,colorClear, 1.0f, 0L );
- CString strError;
- D3DXVECTOR3 pos(0.0f,0.0f,0.0f);
- hr = m_pD3DDevice->BeginScene();
- if( SUCCEEDED( hr ) )
- {
- D3DXMATRIX matView;
- D3DXMATRIX matIdentity;
- m_pD3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
- m_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
- m_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
- m_pD3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
- m_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
- for(int iAxes=0;iAxes<3;iAxes++)
- {
- if(iAxes != 2 || m_bRenderZAxis )
- {
- m_pD3DDevice->SetStreamSource(0,m_pAxesVB[iAxes],0,sizeof(POINT_VERTEX));
- m_pD3DDevice->SetFVF( POINT_VERTEX_FVF );
- m_pD3DDevice->DrawPrimitive(D3DPT_LINELIST,0,1);
- }
- }
- bool bDrawAxesEnds = false;
- if(m_pPixelVB)
- bDrawAxesEnds = true;
- if(bDrawAxesEnds)
- {
- m_pD3DDevice->SetRenderState(D3DRS_WRAP0, D3DWRAPCOORD_0);
- for(int iCone=0;iCone<3;iCone++)
- {
- if(iCone != 2 || m_bRenderZAxis )
- {
- m_pD3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
- m_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
- m_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
- m_pD3DDevice->SetVertexShader(NULL);
- if(m_pConeMesh[iCone])
- {
- m_pD3DDevice->SetFVF( MESH_VERTEX_TEX_FVF );
- if(iCone == 0 && m_pTextureAxisLabelX)
- m_pD3DDevice->SetTexture(0,m_pTextureAxisLabelX);
- if(iCone == 1 && m_pTextureAxisLabelY)
- m_pD3DDevice->SetTexture(0,m_pTextureAxisLabelY);
- if(iCone == 2 && m_pTextureAxisLabelZ)
- m_pD3DDevice->SetTexture(0,m_pTextureAxisLabelZ);
- m_pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- m_pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- m_pD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
- m_pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- hr = m_pConeMesh[iCone]->DrawSubset(0);
- }
- }
- }
- m_pD3DDevice->SetTexture(0,NULL);
- m_pD3DDevice->SetTransform( D3DTS_WORLD, &m_matWorld );
- }
- if(m_pPixelVB)
- {
- m_pD3DDevice->SetFVF( POINT_VERTEX_FVF );
- int np = m_nPoints;
- m_pD3DDevice->SetStreamSource(0,m_pPixelVB,0,sizeof(POINT_VERTEX));
- if(m_bUsePoints)
- {
- m_pD3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
- m_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
- m_pD3DDevice->DrawPrimitive(D3DPT_POINTLIST,0,np);
- }
- else
- {
- m_pD3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
- m_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
- m_pD3DDevice->SetRenderState(D3DRS_COLORVERTEX,TRUE);
- m_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
- m_pD3DDevice->SetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1 );
- m_pD3DDevice->SetVertexShader(NULL);
- m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,np*4);
- }
- }
- RenderText();
- m_pD3DDevice->EndScene();
- }
- else
- {
- strError = DXGetErrorString9 (hr);
- }
- return hr;
- }
- HRESULT C3DWindow::DeleteDeviceObjects(void)
- {
- for(int k=0;k<3;k++)
- {
- SAFE_RELEASE(m_pAxesVB[k]);
- }
- for(int iCone=0;iCone<3;iCone++)
- SAFE_RELEASE(m_pConeMesh[iCone]);
- SAFE_RELEASE(m_pTextureAxisLabelX);
- SAFE_RELEASE(m_pTextureAxisLabelY);
- SAFE_RELEASE(m_pTextureAxisLabelZ);
- SAFE_RELEASE(m_pPixelVB);
- m_nPoints = 0;
- if(m_pFont)
- m_pFont->DeleteDeviceObjects();
- if(m_pFontLarge)
- m_pFontLarge->DeleteDeviceObjects();
- return S_OK;
- }
- void C3DWindow::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- if (m_pD3DDevice)
- Render3DEnvironment();
- }
- BOOL C3DWindow::PreTranslateMessage(MSG* pMsg)
- {
- // Pass mouse messages to the ArcBall so it can build internal matrices
- m_ArcBall.HandleMouseMessages( m_hWnd, pMsg->message,pMsg->wParam,pMsg->lParam );
- if(pMsg->message == WM_KEYUP || pMsg->message == WM_KEYDOWN)
- {
- }
- return CWnd::PreTranslateMessage(pMsg);
- }
- HRESULT C3DWindow::InitDeviceObjects(void)
- {
- HRESULT hr = S_OK;
- ASSERT(m_pD3DDevice);
- // Init the font
- hr = m_pFont->InitDeviceObjects( m_pD3DDevice );
- if( FAILED( hr ) )
- return DXTRACE_ERR( "m_pFont->InitDeviceObjects", hr );
- // Init the other font
- hr = m_pFontLarge->InitDeviceObjects( m_pD3DDevice );
- if( FAILED( hr ) )
- return DXTRACE_ERR( "m_pFontLarge->InitDeviceObjects", hr );
- m_vObjectCenter = D3DXVECTOR3(0.0f,0.0f,0.0f);
- // Setup the arcball parameters
- m_fObjectRadius = 10.0f;
- CRect rcClient;
- GetClientRect(&rcClient);
- m_ArcBall.SetWindow( rcClient.Width(), rcClient.Height(), 0.85f );
- m_ArcBall.SetRadius( m_fObjectRadius );
- m_light.Diffuse.r = 0.3f;
- m_light.Diffuse.g = 0.3f;
- m_light.Diffuse.b = 0.3f;
- m_light.Ambient.r = 0.1f;
- m_light.Ambient.g = 0.1f;
- m_light.Ambient.b = 0.1f;
- m_light.Specular.r = 0.0f;
- m_light.Specular.g = 0.0f;
- m_light.Specular.b = 0.0f;
- m_light.Type = D3DLIGHT_POINT;//D3DLIGHT_SPOT;
- m_light.Falloff = 1.0f;
- m_light.Direction.x = 0.0f;
- m_light.Direction.y = 0.0f;
- m_light.Direction.z = 1.0f;
- m_light.Attenuation0 = 1.0f;
- m_light.Attenuation1 = 0.0f;
- m_light.Attenuation2 = 0.0f;
- m_light.Range = 1000.0f;
- m_light.Position.x = 0.0f;
- m_light.Position.y = 0.0f;
- m_light.Position.z = -18.0f; //the light is behind us
- m_light.Theta = 0.0f;//these next two control the cone angles of the light
- m_light.Phi = 0.6f;
- m_pD3DDevice->SetLight( 0, &m_light );
- m_pD3DDevice->LightEnable( 0, TRUE );
- // m_pD3DDevice->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
- // m_pD3DDevice->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
- // m_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE);
- return S_OK;
- }
- void C3DWindow::OnTimer(UINT nIDEvent)
- {
- if(m_pD3DDevice && !pMainDialog->m_bClosing)
- Render3DEnvironment();
- CWnd::OnTimer(nIDEvent);
- }
- HRESULT C3DWindow::RenderText(void)
- {
- D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
- TCHAR szMsg[MAX_PATH] = TEXT("");
- // Output display stats
- FLOAT fNextLineL = 5.0f;
- FLOAT fNextLineR = 5.0f;
- if(m_strTopRightTextLine1 != "")
- {
- lstrcpy( szMsg,m_strTopRightTextLine1 );
- FLOAT fTextX = (FLOAT)(m_D3DsdBackBuffer.Width - 150);
- m_pFont->DrawText( fTextX, fNextLineR, fontColor, szMsg );
- }
- if(m_strTopLeftTextLine1 != "")
- {
- lstrcpy( szMsg,m_strTopLeftTextLine1 );
- FLOAT fTextX = (FLOAT)(5);
- m_pFont->DrawText( fTextX, fNextLineL, fontColor, szMsg );
- }
- if(m_strBottomLeftTextLine1 != "")
- {
- lstrcpy( szMsg,m_strBottomLeftTextLine1 );
- FLOAT fTextX = (FLOAT)(5);
- m_pFontLarge->DrawText( fTextX, (float)(m_d3dpp.BackBufferHeight - 30), fontColor, szMsg );
- }
- return S_OK;
- }
- void C3DWindow::SetLightOutput(float fDiffuse,float fAmbient)
- {
- m_light.Diffuse.r = fDiffuse;
- m_light.Diffuse.g = fDiffuse;
- m_light.Diffuse.b = fDiffuse;
- m_light.Ambient.r = fAmbient;
- m_light.Ambient.g = fAmbient;
- m_light.Ambient.b = fAmbient;
- m_light.Specular.r = 0.0f;
- m_light.Specular.g = 0.0f;
- m_light.Specular.b = 0.0f;
- m_light.Type = D3DLIGHT_POINT;//D3DLIGHT_SPOT;
- m_light.Falloff = 1.0f;
- m_light.Direction.x = 0.0f;
- m_light.Direction.y = 0.0f;
- m_light.Direction.z = 1.0f;
- m_light.Attenuation0 = 1.0f;
- m_light.Attenuation1 = 0.0f;
- m_light.Attenuation2 = 0.0f;
- m_light.Range = 1000.0f;
- m_light.Position.x = 0.0f;
- m_light.Position.y = 0.0f;
- m_light.Position.z = -18.0f; //the light is behind us
- m_light.Theta = 0.0f;//these next two control the cone angles of the light
- m_light.Phi = 0.6f;
- m_pD3DDevice->SetLight( 0, &m_light );
- m_pD3DDevice->LightEnable( 0, TRUE );
- }
- HRESULT C3DWindow::MakeConeMeshes(float fConeShiftX,float fConeShiftY, float fConeShiftZ)
- {
- for(int iCone=0;iCone<3;iCone++)
- SAFE_RELEASE(m_pConeMesh[iCone]);
- HRESULT hr;
- hr = D3DXCreateTextureFromResource(m_pD3DDevice,NULL,MAKEINTRESOURCE(IDB_BITMAP_XAXIS), &m_pTextureAxisLabelX);
- hr = D3DXCreateTextureFromResource(m_pD3DDevice,NULL,MAKEINTRESOURCE(IDB_BITMAP_YAXIS), &m_pTextureAxisLabelY);
- hr = D3DXCreateTextureFromResource(m_pD3DDevice,NULL,MAKEINTRESOURCE(IDB_BITMAP_ZAXIS), &m_pTextureAxisLabelZ);
- MESH_VERTEX_TEX* pConeVertices;
- D3DXMATRIX matTranslation;
- D3DXMATRIX matRotation;
- ID3DXMesh* pPrimativeMesh = NULL;
- double dConeHeight = 0.6;
- fConeShiftX += (float)(dConeHeight/2.0);
- fConeShiftY += (float)(dConeHeight/2.0);
- fConeShiftZ += (float)(dConeHeight/2.0);
- float fConeBaseR = 0.2f;
- double dTextureAR = (2.0f*D3DX_PI*fConeBaseR)/dConeHeight;
- if( SUCCEEDED(D3DXCreateCylinder(m_pD3DDevice,fConeBaseR,0.05f,(float)dConeHeight,20,2,&pPrimativeMesh,NULL)) )
- {
- for(int iCone=0;iCone<3;iCone++)
- {
- if( SUCCEEDED( pPrimativeMesh->CloneMeshFVF( D3DXMESH_MANAGED, MESH_VERTEX_TEX_FVF,m_pD3DDevice,&m_pConeMesh[iCone])))
- {
- if(iCone == 0)
- {
- D3DXMatrixTranslation(&matTranslation,fConeShiftX,0.0f,0.0f);
- D3DXMatrixRotationY(&matRotation,D3DX_PI/2.0f);
- }
- else if(iCone == 1)
- {
- D3DXMatrixTranslation(&matTranslation,0.0f,fConeShiftY,0.0f);
- D3DXMatrixRotationX(&matRotation,-D3DX_PI/2.0f);
- }
- else if(iCone == 2)
- {
- D3DXMatrixTranslation(&matTranslation,0.0f,0.0f,fConeShiftZ);
- D3DXMatrixRotationY(&matRotation,0.0f);//D3DX_PI/2.0f);
- }
- int nVertices = m_pConeMesh[iCone]->GetNumVertices();
- LPDIRECT3DVERTEXBUFFER9 pVB;
- if( SUCCEEDED( m_pConeMesh[iCone]->GetVertexBuffer(&pVB)) )
- {
- pVB->Lock( 0, 0, (void**)&pConeVertices, 0 );
- for(int k=0; k<nVertices; k++)
- {
- double dTheta = atan2((double)pConeVertices[k].p.x,(double)pConeVertices[k].p.y);
- if(dTheta < 0.0)dTheta += 2.0f*D3DX_PI;
- pConeVertices[k].tv = 0.5f - (float)(pConeVertices[k].p.z/dConeHeight);//0 at bottom 1 at top
- pConeVertices[k].tu = (float)(dTheta/(2.0f*D3DX_PI));
- D3DXVec3TransformCoord(&pConeVertices[k].p,&pConeVertices[k].p,&matRotation);
- D3DXVec3TransformCoord(&pConeVertices[k].p,&pConeVertices[k].p,&matTranslation);
- }
- pVB->Unlock();
- pVB->Release();
- }
- }
- else
- {
- return E_FAIL;
- }
- }
- SAFE_RELEASE(pPrimativeMesh);
- }
- return S_OK;
- }
- void C3DWindow::RemovePixelVB(void)
- {
- m_nPoints = 0;
- SAFE_RELEASE(m_pPixelVB);
- }
- void C3DWindow::ResetArcBall(void)
- {
- UINT uNBytes;
- CD3DArcBall* pArcBallProfile = NULL;
- CString strDefaultArcBall = m_strRegistry + " Default";
- theApp.GetProfileBinary("3D Window",strDefaultArcBall,(LPBYTE*)&pArcBallProfile,&uNBytes);
- if(uNBytes == sizeof(CD3DArcBall) && pArcBallProfile)
- m_ArcBall = *pArcBallProfile;
- DELETENULL(pArcBallProfile);
- }
- HRESULT C3DWindow::ExportPixelVB(CString strFilename)
- {
- if(m_nPoints <= 0)
- return S_FALSE;
- CStdioFile ExportFile;
- BOOL bFileOpen = FALSE;
- CString strExport;
- bFileOpen = ExportFile.Open(strFilename, CFile::modeWrite|CFile::modeCreate);
- if(!bFileOpen)
- return S_FALSE;
- POINT_VERTEX* pPixelVertices = NULL;
- if(FAILED(m_pPixelVB->Lock( 0, 0, (void**)&pPixelVertices, 0 )) )
- return S_FALSE;
- float x,y,z;
- for(int iPoint=0;iPoint<m_nPoints;iPoint++)
- {
- x = pPixelVertices[iPoint].p.x;
- y = pPixelVertices[iPoint].p.y;
- z = pPixelVertices[iPoint].p.z;
- strExport.Format("%f,%f,%fn",x,y,z);
- ExportFile.WriteString(strExport);
- }
- m_pPixelVB->Unlock();
- ExportFile.Flush();
- ExportFile.Close();
- return S_OK;
- }
- void C3DWindow::OnMouseMove(UINT nFlags, CPoint point)
- {
- CWnd::OnMouseMove(nFlags, point);
- }
- void C3DWindow::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CWnd::OnLButtonDown(nFlags, point);
- }
- void C3DWindow::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CWnd::OnLButtonUp(nFlags, point);
- }
- void C3DWindow::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- CWnd::OnLButtonDblClk(nFlags, point);
- }
- HRESULT C3DWindow::CreateVB(int nPoints,float* pX,float* pY,float* pZ,Color* pC,float* pS)
- {
- HRESULT hr;
- if(nPoints < 1)
- return FALSE;
- SAFE_RELEASE(m_pPixelVB);
- float fMinX = 3.3E38f;
- float fMaxX = -3.3E38f;
- float fMinY = 3.3E38f;
- float fMaxY = -3.3E38f;
- float fMinZ = 3.3E38f;
- float fMaxZ = -3.3E38f;
- double dXSum = 0.0f;
- double dYSum = 0.0f;
- double dZSum = 0.0f;
- for(int iP=0;iP<nPoints;iP++)
- {
- dXSum += (float)pX[iP];
- dYSum += (float)pY[iP];
- dZSum += (float)pZ[iP];
- fMaxX = max(fMaxX,pX[iP]);
- fMinX = min(fMinX,pX[iP]);
- fMaxY = max(fMaxY,pY[iP]);
- fMinY = min(fMinY,pY[iP]);
- fMaxZ = max(fMaxZ,pZ[iP]);
- fMinZ = min(fMinZ,pZ[iP]);
- }
- float fXOffset = -(float)(dXSum/(double)nPoints);
- float fYOffset = -(float)(dYSum/(double)nPoints);
- float fZOffset = -(float)(dZSum/(double)nPoints);
- float fExtentX = fMaxX - fMinX;
- float fExtentY = fMaxY - fMinY;
- float fMaxExtent = max(fExtentX,fExtentY);
- float fScale = 1.0f;
- if(fMaxExtent > 0.0f)
- fScale = 20.0f / (fMaxExtent);
- m_bUsePoints = false;
- if(m_bUsePoints)
- {
- if(FAILED(hr = m_pD3DDevice->CreateVertexBuffer( nPoints*sizeof(POINT_VERTEX),
- D3DUSAGE_WRITEONLY ,
- POINT_VERTEX_FVF, D3DPOOL_MANAGED, &m_pPixelVB, NULL )))
- {
- return FALSE;
- }
- POINT_VERTEX* pPixelVertices = NULL;
- if(FAILED(m_pPixelVB->Lock( 0, 0, (void**)&pPixelVertices, 0 )) )
- return FALSE;
- for(int iP=0;iP<nPoints;iP++)
- {
- Color c = pC[iP];
- pPixelVertices[iP].p.x = (fXOffset + pX[iP]) * fScale;
- pPixelVertices[iP].p.y = -(fYOffset + pY[iP]) * fScale;
- pPixelVertices[iP].p.z = (fZOffset + pZ[iP]) * fScale;
- pPixelVertices[iP].color = D3DCOLOR_ARGB(255,c.GetR(),c.GetG(),c.GetB());
- }
- m_pPixelVB->Unlock();
- }
- else
- {
- if(FAILED(hr = m_pD3DDevice->CreateVertexBuffer( 12*nPoints*sizeof(POINT_VERTEX),
- D3DUSAGE_WRITEONLY ,
- POINT_VERTEX_FVF, D3DPOOL_MANAGED, &m_pPixelVB, NULL )))
- {
- return FALSE;
- }
- POINT_VERTEX* pPixelVertices = NULL;
- if(FAILED(m_pPixelVB->Lock( 0, 0, (void**)&pPixelVertices, 0 )) )
- return FALSE;
- POINT_VERTEX Quad[4];
- int iV = 0;
- for(int iP=0;iP<nPoints;iP++)
- {
- float m_fPixelSize = pS[iP];//pixel size in 3-D world
- float fHalf = 0.5f*m_fPixelSize;
- Color c = pC[iP];
- Quad[0].p.x = (fXOffset + pX[iP]-fHalf) * fScale;
- Quad[0].p.y = (fYOffset - pY[iP]-fHalf) * fScale;
- Quad[0].p.z = (fZOffset + pZ[iP]) * fScale;
- Quad[0].color = D3DCOLOR_ARGB(255,c.GetR(),c.GetG(),c.GetB());
- Quad[1].p.x = (fXOffset + pX[iP]+fHalf) * fScale;
- Quad[1].p.y = (fYOffset - pY[iP]-fHalf) * fScale;
- Quad[1].p.z = Quad[0].p.z ;
- Quad[1].color = Quad[0].color;
- Quad[2].p.x = (fXOffset + pX[iP]+fHalf) * fScale;
- Quad[2].p.y = (fYOffset - pY[iP]+fHalf) * fScale;
- Quad[2].p.z = Quad[0].p.z ;
- Quad[2].color = Quad[0].color;
- Quad[3].p.x = (fXOffset + pX[iP]-fHalf) * fScale;
- Quad[3].p.y = (fYOffset - pY[iP]+fHalf) * fScale;
- Quad[3].p.z = Quad[0].p.z ;
- Quad[3].color = Quad[0].color;
- pPixelVertices[iV++] = Quad[0];
- pPixelVertices[iV++] = Quad[2];
- pPixelVertices[iV++] = Quad[1];
- pPixelVertices[iV++] = Quad[1];
- pPixelVertices[iV++] = Quad[2];
- pPixelVertices[iV++] = Quad[0];
- pPixelVertices[iV++] = Quad[0];
- pPixelVertices[iV++] = Quad[3];
- pPixelVertices[iV++] = Quad[2];
- pPixelVertices[iV++] = Quad[2];
- pPixelVertices[iV++] = Quad[3];
- pPixelVertices[iV++] = Quad[0];
- }
- m_pPixelVB->Unlock();
- }
- m_nPoints = nPoints;
- float fConeShiftX = 10.0f;
- float fConeShiftY = 7.5f;
- float fConeShiftZ = 10.0f;
- MakeConeMeshes(fConeShiftX,fConeShiftY,fConeShiftZ);
- return TRUE;
- }
- /*lint -restore*/