3DHelperDemoDlg.cpp
上传用户:qzpk666
上传日期:2022-08-04
资源大小:59k
文件大小:9k
- // 3DHelperDemoDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "3DHelperDemo.h"
- #include "3DHelperDemoDlg.h"
- #include "MemDC.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define _USE_NET_DISPLAY_
- //#undef _USE_NET_DISPLAY_
- const int ID_TIMER_REDRAW = 1;
- const UINT TIMER_REDRAW_INTERVAL = 25;
- #ifndef RAD
- #define RAD(x) ((x)*3.14159265f/180.0f)
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMy3DHelperDemoDlg dialog
- CMy3DHelperDemoDlg::CMy3DHelperDemoDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMy3DHelperDemoDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMy3DHelperDemoDlg)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_pVectorData = NULL;
- }
- void CMy3DHelperDemoDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMy3DHelperDemoDlg)
- DDX_Control(pDX, IDC_CHECK_COORD_AXES, m_ctrlAxes);
- DDX_Control(pDX, IDC_CHECK_COORDS, m_ctrlCoordinates);
- DDX_Control(pDX, IDC_CHECK_VISIBILITY_CUBE, m_ctrlVisibilityCube);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMy3DHelperDemoDlg, CDialog)
- //{{AFX_MSG_MAP(CMy3DHelperDemoDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_CHECK_VISIBILITY_CUBE, OnCheckVisibilityCube)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMy3DHelperDemoDlg message handlers
- BOOL CMy3DHelperDemoDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- m_ctrlVisibilityCube.SetCheck(0);
- m_ctrlCoordinates.EnableWindow(FALSE);
- m_ctrlAxes.EnableWindow(FALSE);
- initialize();
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CMy3DHelperDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CMy3DHelperDemoDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CMy3DHelperDemoDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CMy3DHelperDemoDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if(nIDEvent == ID_TIMER_REDRAW) {
- KillTimer(nIDEvent);
- CWnd *pTarget = GetDlgItem(IDC_STATIC_PLACEHOLDER);
- if(pTarget==NULL) {
- return;
- }
- m_c3DHelper.UpdateTrackBall(pTarget->GetDC());
- redraw();
- SetTimer(ID_TIMER_REDRAW, TIMER_REDRAW_INTERVAL, NULL);
- }
- CDialog::OnTimer(nIDEvent);
- }
- void CMy3DHelperDemoDlg::redraw()
- {
- // Create off-screen dc (prevents flickering)
- CWnd *pTarget = GetDlgItem(IDC_STATIC_PLACEHOLDER);
- if(pTarget==NULL) {
- return;
- }
- CRect rcDraw;
- pTarget->GetClientRect(rcDraw);
- // Create the offscreen dc
- CDC *pTDC = pTarget->GetDC();
- if(pTDC==NULL) {
- return;
- }
- CMemDC cDC(pTDC, &rcDraw);
- CDC *pDC = (CDC*)cDC;
- // Clean up the screen an draw a hint
- pDC->FillSolidRect(rcDraw.left, rcDraw.top, rcDraw.Width(),rcDraw.Height(), RGB(0,0,0));
- pDC->SetTextColor(RGB(255,255,0));
- pDC->SetBkMode(OPAQUE);
- pDC->TextOut(1,1, _T("Drag mouse to rotate..."));
- // Macros easing the access to data
- #define ID(xx,yy) (((yy)*m_iXSize)+(xx))
- #define V(xx,yy) m_pVectorData[ID(xx,yy)]
- #define Vx(xx,yy) V(xx,yy).x
- #define Vy(xx,yy) V(xx,yy).y
- #define Vxy(xx,yy) Vx(xx,yy),Vy(xx,yy)
-
- int x,y;
- float fWaves = 3.0f;
- float xFact = fWaves/(float)m_iXSize*100.0f;
- float yFact = fWaves/(float)m_iYSize*100.0f;
- for(y=0; y<m_iYSize;y++) {
- for(x=0; x<m_iXSize;x++) {
- // Data *should be* in range of (-1,-1,-1 / 1,1,1) - looks prettier!
- V(x,y).s3D.x = -1.0f+2.0f*((float)x/(float)m_iXSize);
- V(x,y).s3D.y = -1.0f+2.0f*((float)y/(float)m_iYSize);
- // Draw a *pretty* curve
- V(x,y).s3D.z = (cosf(RAD(((m_iCurrAngle+y)%360)*yFact))+sinf(RAD(((m_iCurrAngle+x)%360)*xFact)))/2.0f;
- }
- }
- m_iCurrAngle ++;
- m_iCurrAngle= m_iCurrAngle%360;
- BYTE col = 0;
- // Step 1: Calculate 2D coordinates
- NeHe::Vector cIn, cOut;
- for(y=0; y<m_iYSize;y++) {
- for(x=0; x<m_iXSize;x++) {
- cIn.x = V(x,y).s3D.x;
- cIn.y = V(x,y).s3D.y;
- cIn.z = V(x,y).s3D.z;
- cOut = m_c3DHelper.RenderPoint(cIn);
- V(x,y).x = (int)cOut.x;
- V(x,y).y = (int)cOut.y;
- #ifndef _USE_NET_DISPLAY_
- //col = (BYTE)(((float)255.0f/(float)m_iYSize)*(float)y);
- col = 255;
- pDC->SetPixelV((int)cOut.x, (int)cOut.y, RGB(col,col,col));
- #endif
- }
- }
- // More complex / maybe slower: Draw a 3D grid/net
- #ifdef _USE_NET_DISPLAY_
- // Step 2: Draw "3D-net"
- CPen cPen;
- cPen.CreatePen(PS_SOLID,1,RGB(128,128,128));
- CPen *pOld = pDC->SelectObject(&cPen);
- for(y=0; y<m_iYSize-1;y++) {
- for(x=0; x<m_iXSize-1;x++) {
- pDC->MoveTo(Vxy(x ,y ));
- pDC->LineTo(Vxy(x+1,y ));
- pDC->MoveTo(Vxy(x ,y ));
- pDC->LineTo(Vxy(x ,y+1));
-
- //pDC->MoveTo(Vxy(x ,y ));
- //pDC->LineTo(Vxy(x+1,y+1));
- }
- }
- // Draw final frame line
- pDC->MoveTo(Vxy(0,m_iYSize-1));
- for(x=0; x<m_iXSize;x++) {
- pDC->LineTo(Vxy(x,m_iYSize-1));
- }
- pDC->MoveTo(Vxy(m_iXSize-1,0));
- for(y=0; y<m_iYSize;y++) {
- pDC->LineTo(Vxy(m_iXSize-1,y));
- }
- pDC->SelectObject(pOld);
- #endif //_USE_NET_DISPLAY_
- BOOL bCubeEnabled = (1==m_ctrlVisibilityCube.GetCheck());
- BOOL bCoords = (1 == m_ctrlCoordinates.GetCheck());
- BOOL bAxes = (1 == m_ctrlAxes.GetCheck());
- if(bCubeEnabled==TRUE) {
- m_c3DHelper.DrawVisibilityCube(pDC, bAxes , bCoords);
- }
- // cDC will be automatically destruced
- #undef V
- #undef Vx
- #undef Vy
- #undef Vxy
- }
- void CMy3DHelperDemoDlg::initialize()
- {
- #ifdef _USE_NET_DISPLAY_
- m_iXSize = 50;
- m_iYSize = 50;
- #else
- m_iXSize = 70;
- m_iYSize = 70;
- #endif
-
- if(m_pVectorData!=NULL) {
- delete m_pVectorData;
- }
- // Allocate memory needed for 100x100 vectors
- m_pVectorData = new DATA3D[m_iXSize*m_iYSize];
- ZeroMemory((void*)m_pVectorData, m_iXSize*m_iXSize*sizeof(DATA3D));
-
- CWnd *pTarget = GetDlgItem(IDC_STATIC_PLACEHOLDER);
- if(pTarget==NULL) {
- return;
- }
- m_c3DHelper.Initialize(pTarget->GetDC(), NeHe::Vector(-1.0f, -1.0f, -1.0f), NeHe::Vector(1.0f,1.0f,1.0f), TRUE);
- m_c3DHelper.Rotate(45.0f, 10.0f, 0.0f, TRUE);
- SetTimer(ID_TIMER_REDRAW, TIMER_REDRAW_INTERVAL, NULL);
- }
- void CMy3DHelperDemoDlg::OnOK()
- {
- // TODO: Add extra validation here
- cleanup();
- CDialog::OnOK();
- }
- void CMy3DHelperDemoDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- cleanup();
- CDialog::OnCancel();
- }
- void CMy3DHelperDemoDlg::cleanup()
- {
- try {
- if(m_pVectorData!=NULL) {
- delete m_pVectorData;
- m_pVectorData = NULL;
- }
- } catch(...) {
- return;
- }
- }
- void CMy3DHelperDemoDlg::OnCheckVisibilityCube()
- {
- // TODO: Add your control notification handler code here
- BOOL bEnabled = (1==m_ctrlVisibilityCube.GetCheck());
- m_ctrlCoordinates.EnableWindow(bEnabled);
- m_ctrlAxes.EnableWindow(bEnabled);
- }