📄 2dcubedlg.cpp
字号:
// 2DCubeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "2DCube.h"
#include "2DCubeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define SAFE_RELEASE(a) {if (a != NULL) {delete a;a = NULL;}}
/////////////////////////////////////////////////////////////////////////////
// CMy2DCubeDlg dialog
CMy2DCubeDlg::CMy2DCubeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy2DCubeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy2DCubeDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_lpScene = new CScene;
m_lpthePolygon = new Vert3[4];
m_lpthePolygon1 = new Vert3[4];
m_lpthePolygon2 = new Vert3[4];
m_lpthePolygon3 = new Vert3[4];
m_nPolyVertices = 4; //每个多边形四个顶点
m_angx = m_angy = 0; //初始角度为0
}
void CMy2DCubeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy2DCubeDlg)
DDX_Control(pDX, IDC_SLIDER2, m_y_rot);
DDX_Control(pDX, IDC_SLIDER1, m_x_rot);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMy2DCubeDlg, CDialog)
//{{AFX_MSG_MAP(CMy2DCubeDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_WM_HSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy2DCubeDlg message handlers
BOOL CMy2DCubeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
Vert3 * Polycur ;
Polycur= m_lpthePolygon;
Vert3 polyvertarray[4] ={
{10.0f,10.0f,0.0f,1.0f},
{10.0f,-10.0f,0.0f,1.0f},
{-10.0f,-10.0f,0.0f,1.0f},
{-10.0f,10.0f,0.0f,1.0f},
};
//用数据填充多边形顶点缓冲
int i;
for (i = 0;i < m_nPolyVertices; i++)
{
Polycur->x = polyvertarray[i].x;
Polycur->y = polyvertarray[i].y;
Polycur->z = polyvertarray[i].z;
Polycur->w = polyvertarray[i].w;
Polycur ++;
}
Vert3 polyvertarray1[4] ={
{10.0f,10.0f,-20.0f,1.0f},
{10.0f,-10.0f,-20.0f,1.0f},
{-10.0f,-10.0f,-20.0f,1.0f},
{-10.0f,10.0f,-20.0f,1.0f},
};
Polycur = m_lpthePolygon1;
for (i = 0;i < m_nPolyVertices; i++)
{
Polycur->x = polyvertarray1[i].x;
Polycur->y = polyvertarray1[i].y;
Polycur->z = polyvertarray1[i].z;
Polycur->w = polyvertarray1[i].w;
Polycur ++;
}
Vert3 polyvertarray2[4] ={
{10.0f,10.0f,-20.0f,1.0f},
{10.0f,-10.0f,-20.0f,1.0f},
{10.0f,-10.0f,0.0f,1.0f},
{10.0f,10.0f,0.0f,1.0f},
};
Polycur = m_lpthePolygon2;
for (i = 0;i < m_nPolyVertices; i++)
{
Polycur->x = polyvertarray2[i].x;
Polycur->y = polyvertarray2[i].y;
Polycur->z = polyvertarray2[i].z;
Polycur->w = polyvertarray2[i].w;
Polycur ++;
}
Vert3 polyvertarray3[4] ={ {-10.0f,10.0f,-20.0f,1.0f},
{-10.0f,-10.0f,-20.0f,1.0f},
{-10.0f,-10.0f,0.0f,1.0f},
{-10.0f,10.0f,0.0f,1.0f},
};
Polycur = m_lpthePolygon3;
for (i = 0;i < m_nPolyVertices; i++)
{
Polycur->x = polyvertarray3[i].x;
Polycur->y = polyvertarray3[i].y;
Polycur->z = polyvertarray3[i].z;
Polycur->w = polyvertarray3[i].w;
Polycur ++;
}
m_x_rot.SetRange( 0, 359 ); //设置滑块控件的范围
m_y_rot.SetRange( 0, 359 );
Vert3 lookat = {0.0f,0.0f,0.0f,1.0f}; //摄像机观察目标点位置
Vert3 Eye = {0.0f,0.0f,-50.0f,1.0f}; //摄像机位置
Vert3 up = {-1.0f,0.0f,0.0f,1.0f}; //摄像机上方向
Matrix matView,matProj; //用来设置视矩阵和投影矩阵
m_lpScene->SetViewLookAt(&matView,&lookat,&Eye,&up); //设置matView
m_lpScene->SetProjection(&matProj,-30.0f,10.0f,90.0f); //设置matProj
m_lpScene->SetMatrix(MAT_VIEW,&matView); //用matView设置视矩阵
m_lpScene->SetMatrix(MAT_PROJ,&matProj); //用matProj设置投影矩阵
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CMy2DCubeDlg::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
{
HDC hdc = ::GetDC(m_hWnd); //获得设备环境
Matrix matWorld,matrot; //用于世界变换的矩阵
m_lpScene->IdentifyMatrix(&matWorld); //矩阵单位化
m_lpScene->IdentifyMatrix(&matrot);
m_lpScene->MatRotation(&matWorld,(float)m_angx*3.1415f/180.0f,AXIS_X); //绕x轴旋转
m_lpScene->MatRotation(&matrot,(float)m_angy*3.1415f/180.0f,AXIS_Y); //绕y轴旋转
m_lpScene->MultMatrix(&matWorld,&matWorld,&matrot); //将旋转变换复合
m_lpScene->SetMatrix(MAT_WORLD,&matWorld); //设置世界变换矩阵
RECT rect; //清除用来绘图的矩形区域
GetClientRect(&rect);
rect.right -= 100;
rect.top += 16;
rect.left += 16;
rect.bottom -= 16;
::FillRect(hdc,&rect,NULL);
m_lpScene->DrawPrimtive(hdc,4,m_lpthePolygon); //使用当前变换绘制图元
m_lpScene->DrawPrimtive(hdc,4,m_lpthePolygon1);
m_lpScene->DrawPrimtive(hdc,4,m_lpthePolygon2);
m_lpScene->DrawPrimtive(hdc,4,m_lpthePolygon3);
::ReleaseDC(m_hWnd,hdc); //释放设备环境
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMy2DCubeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMy2DCubeDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
SAFE_RELEASE(m_lpScene);
SAFE_RELEASE(m_lpthePolygon);
SAFE_RELEASE(m_lpthePolygon1);
SAFE_RELEASE(m_lpthePolygon2);
SAFE_RELEASE(m_lpthePolygon3);
CDialog::OnClose();
}
void CMy2DCubeDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if(m_angx != m_x_rot.GetPos())
{
m_angx = m_x_rot.GetPos();
SendMessage(WM_PAINT);
}
if(m_angy != m_y_rot.GetPos())
{
m_angy = m_y_rot.GetPos();
}
SendMessage(WM_PAINT);
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -