📄 objectpropertydlg.cpp
字号:
// ObjectPropertyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MagicScissors.h"
#include "ObjectPropertyDlg.h"
#include "EdgeList.h"
#include "EdgePoint.h"
#include "3DObject.h"
#include "Plane.h"
#include "Sphere.h"
#include "Cylinder.h"
#include "Ellipsoid.h"
#include "DepthDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CObjectPropertyDlg dialog
CObjectPropertyDlg::CObjectPropertyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CObjectPropertyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CObjectPropertyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hGLContext = NULL;
m_pQuadObj = NULL;
m_pObject = NULL;
m_pList = NULL;
m_nType = 0;
m_fDepth = -128;
m_fRotate = 0;
}
CObjectPropertyDlg::~CObjectPropertyDlg()
{
if( m_pObject ) delete m_pObject;
}
void CObjectPropertyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CObjectPropertyDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CObjectPropertyDlg, CDialog)
//{{AFX_MSG_MAP(CObjectPropertyDlg)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_CONTEXTMENU()
ON_COMMAND(IDM_PLANE, OnPlane)
ON_COMMAND(IDM_CYLINDER, OnCylinder)
ON_COMMAND(IDM_SPHERE, OnSphere)
ON_COMMAND(IDM_SET_DEPTH, OnSetDepth)
ON_WM_KEYDOWN()
ON_COMMAND(IDM_ELLIPSOID, OnEllipsoid)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CObjectPropertyDlg message handlers
int CObjectPropertyDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
if( !InitGL() ) return -1;
return 0;
}
void CObjectPropertyDlg::OnDestroy()
{
CloseGL();
CDialog::OnDestroy();
// TODO: Add your message handler code here
}
void CObjectPropertyDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if( cx == 0 || cy == 0 ) return;
// TODO: Add your message handler code here
CDC* pDC = GetDC();
wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
glClearColor( 0.f, 0.f, 0.f, 1.f );
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_FILL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0, cx, 0, cy, -256, 256 );
glViewport(0, 0, cx, cy);
glMatrixMode(GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
GLfloat ambientProperties[] = {0.4f, 0.4f, 0.4f, 1.f};
GLfloat diffuseProperties[] = {0.4f, 0.4f, 0.4f, 1.f};
GLfloat specularProperties[] = {0.4f, 0.4f, 0.4f, 1.f};
GLfloat position[] = {0.f, 0.f, 100.f, 0.f};
glLightfv( GL_LIGHT0, GL_AMBIENT, ambientProperties);
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
glLightfv( GL_LIGHT0, GL_SPECULAR, specularProperties);
glLightfv( GL_LIGHT0, GL_POSITION, position );
//glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
//float pos[4] = { -50, 10, 100, 0 };
//glLightfv( GL_LIGHT0, GL_POSITION, pos );
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
wglMakeCurrent(NULL, NULL);
ReleaseDC(pDC);
}
BOOL CObjectPropertyDlg::InitGL()
{
CDC* pDC = GetDC();
if (!SetupPixelFormat(pDC))
return FALSE;
m_hGLContext = wglCreateContext(pDC->GetSafeHdc());
if( !m_hGLContext ) return FALSE;
//wglMakeCurrent(pDC->GetSafeHdc(), m_hGLContext);
//gluLookAt( 30, 20, 100, 0, 0, -1 , 0, 1, 0 );
CreateQuadricObject();
//wglMakeCurrent(NULL, NULL);
ReleaseDC(pDC);
return TRUE;
}
void CObjectPropertyDlg::CloseGL()
{
if( wglGetCurrentContext() != NULL )
wglMakeCurrent(NULL,NULL);
if(m_hGLContext)
wglDeleteContext( m_hGLContext );
if( m_pQuadObj )
gluDeleteQuadric(m_pQuadObj);
}
BOOL CObjectPropertyDlg::SetupPixelFormat(CDC* pDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1; // version number
pixelDesc.dwFlags =
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER; // double buffered
//PFD_STEREO_DONTCARE;
pixelDesc.iPixelType = PFD_TYPE_RGBA; // RGBA type
pixelDesc.cColorBits = 24; // Color depth
pixelDesc.cRedBits = 0;
pixelDesc.cRedShift = 0;
pixelDesc.cGreenBits = 0;
pixelDesc.cGreenShift = 0;
pixelDesc.cBlueBits = 0;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 0;
pixelDesc.cAccumRedBits = 0;
pixelDesc.cAccumGreenBits = 0;
pixelDesc.cAccumBlueBits = 0;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 16;
pixelDesc.cStencilBits = 0;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
int GLPixelIndex = ChoosePixelFormat(pDC->GetSafeHdc(),&pixelDesc);
if(GLPixelIndex==0) // Choose default
{
GLPixelIndex = 1;
if(!DescribePixelFormat(pDC->GetSafeHdc(),GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc))
return FALSE;
}
if(!SetPixelFormat(pDC->GetSafeHdc(),GLPixelIndex,&pixelDesc))
return FALSE;
return TRUE;
}
BOOL CObjectPropertyDlg::CreateQuadricObject()
{
m_pQuadObj = gluNewQuadric();
gluQuadricDrawStyle(m_pQuadObj, GLU_FILL);
gluQuadricNormals(m_pQuadObj, GLU_SMOOTH);
gluQuadricOrientation(m_pQuadObj, GLU_OUTSIDE);
gluQuadricTexture(m_pQuadObj, GL_FALSE);
return TRUE;
}
void CObjectPropertyDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
if( m_hGLContext )
{
wglMakeCurrent(dc.GetSafeHdc(), m_hGLContext);
Render();
glFinish();
SwapBuffers(wglGetCurrentDC());
wglMakeCurrent(NULL,NULL);
}
// Do not call CDialog::OnPaint() for painting messages
}
BOOL CObjectPropertyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CSize size = m_Rect.Size();
SetWindowPos( &wndTop, 0, 0, size.cx+26, size.cy+50, SWP_NOMOVE );
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CObjectPropertyDlg::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int n = m_pList->GetLength();
int i;
CPoint pt;
int sx = m_Rect.left;
int sy = m_Rect.top;
int h = m_Rect.Height();
// draw edge point
if( m_pList )
{
glPushMatrix();
glTranslatef( 10, 10, -100 );
glBegin(GL_POINTS);
glColor3f(1.f,0.f,0.f);
for( i = 0 ; i < n ; i++ )
{
pt = *(m_pList->GetPoint(i));
pt.x -= sx;
pt.y -= sy;
pt.y = h - pt.y;
glVertex3f( (float)pt.x, (float)pt.y, 0 );
}
glEnd();
glPopMatrix();
}
////////////////////////////////
// draw object
if( m_pObject )
{
glPushMatrix();
glTranslatef( 10, 10, -100 );
m_pObject->Render(m_pQuadObj, NULL,FALSE);
glPopMatrix();
}
}
BOOL CObjectPropertyDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
//return CDialog::OnEraseBkgnd(pDC);
}
void CObjectPropertyDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CPoint pnt = point;
ScreenToClient(&pnt);
int x,y;
long lParam;
x = pnt.x;
y = pnt.y;
lParam = (y << 16) | x;
//SendMessage(WM_LBUTTONDOWN, 0, lParam);
CMenu menu;
if (menu.LoadMenu(IDR_PROPERTY))
{
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
pPopup->TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN, point.x, point.y, this);
}
}
void CObjectPropertyDlg::OnPlane()
{
// TODO: Add your command handler code here
if( m_pObject )
{
if( m_pObject->GetType() == PLANE ) return;
delete m_pObject;
}
m_pObject = new CPlane;
m_pObject->Create( m_pList, m_Rect, 0 );
Invalidate();
}
void CObjectPropertyDlg::OnCylinder()
{
// TODO: Add your command handler code here
if( m_pObject )
{
if( m_pObject->GetType() == CYLINDER ) return;
delete m_pObject;
}
m_pObject = new CCylinder;
m_pObject->Create( m_pList, m_Rect, 0 );
Invalidate();
}
void CObjectPropertyDlg::OnSphere()
{
// TODO: Add your command handler code here
if( m_pObject )
{
if( m_pObject->GetType() == SPHERE ) return;
delete m_pObject;
}
m_pObject = new CSphere;
m_pObject->Create( m_pList, m_Rect, 0 );
Invalidate();
}
void CObjectPropertyDlg::OnEllipsoid()
{
// TODO: Add your command handler code here
if( m_pObject )
{
if( m_pObject->GetType() == ELLIPSOID ) return;
delete m_pObject;
}
m_pObject = new CEllipsoid;
m_pObject->Create( m_pList, m_Rect, 0 );
Invalidate();
}
void CObjectPropertyDlg::OnSetDepth()
{
// TODO: Add your command handler code here
if( m_pObject )
{
CDepthDlg dlg;
dlg.m_nDepth = (int)-m_pObject->GetDepth()-250;
if( dlg.DoModal() == IDOK )
m_pObject->SetDepth( (float)-(dlg.m_nDepth+250) );
}
}
void CObjectPropertyDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if( m_pObject )
{
short ctrl = GetKeyState(VK_CONTROL);
short shift = GetKeyState(VK_SHIFT);
if( ctrl == -127 || ctrl == -128 )
{
switch( nChar )
{
case VK_LEFT:
m_pObject->Move( 0 );
break;
case VK_RIGHT:
m_pObject->Move( 1 );
break;
case VK_DOWN:
m_pObject->Move( 2 );
break;
case VK_UP:
m_pObject->Move( 3 );
break;
}
}
else if( shift == -127 || shift == -128 )
{
switch( nChar )
{
case VK_LEFT:
m_pObject->Rotate( 0 );
break;
case VK_RIGHT:
m_pObject->Rotate( 1 );
break;
}
}
else
{
switch( nChar )
{
case VK_LEFT: // X规氢 荐绵
m_pObject->ChangeScale( 0, -1 );
break;
case VK_RIGHT: // X规氢 犬厘
m_pObject->ChangeScale( 0, 1 );
break;
case VK_DOWN: // Y规氢 荐绵
m_pObject->ChangeScale( 1, -1 );
break;
case VK_UP: // Y规氢 犬厘
m_pObject->ChangeScale( 1, 1 );
break;
}
}
Invalidate();
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
BOOL CObjectPropertyDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if( pMsg->message == WM_KEYDOWN )
{
SendMessage( pMsg->message, pMsg->wParam, pMsg->lParam );
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -