程序要求如下
程序运用C++代码完成
程序能够达到运用鼠标或者键盘进行对图像的缩放和移动操作
3D图像形状不限 不需渲染等高级操作
请说明程序代码和操作方法
程序要求如下
程序运用C++代码完成
程序能够达到运用鼠标或者键盘进行对图像的缩放和移动操作
3D图像形状不限 不需渲染等高级操作
请说明程序代码和操作方法
这样的opengl例子很多,代码量都比较大,如果你要的话,告诉我你的邮箱,我发给你几个。现在我把其中比较简单的一个类定义及说明和里面主要的函数贴在下面:
放大/缩小/旋转效果是在显示时(响应WM_PAINT消息)实现的,操作仅仅是改变了显示的参数,这些参数包括:m_ScaleX、m_ScaleY、m_ScaleZ用来处理放大/缩小;m_xRotate、m_yRotate用来处理旋转。类定义如下:
class CDialogGLDlg : public CDialog
{
// Construction
public:
CDialogGLDlg(CWnd* pParent = NULL); // standard constructor
BOOL SetWindowPixelFormat(HDC hDC); // 设置当前窗口的属性适应opengl显示
BOOL CreateViewGLContext(HDC hDC); // 将opengl与当前窗口绑定
void BuildList(); // 将显示的图像信息传递到opengl系统中
HGLRC m_hGLContext;
int m_GLPixelIndex;
void RenderScene(); // 将操作时设置的显示参数传递到opengl系统中并显示图像
float m_ScaleX;
float m_ScaleY;
float m_ScaleZ;
GLdouble m_xRotate;
GLdouble m_yRotate;
CPoint m_LeftDownPos; // 处理鼠标拖动操作时用来设置旋转参数
BOOL m_LeftButtonDown; // 用来判断鼠标是否在拖动
// Dialog Data
enum { IDD = IDD_DIALOGGL_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
afx_msg void OnContextMenu(CWnd*, CPoint point);
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);//处理鼠标左键拖动时旋转操作
afx_msg void OnTimer(UINT nIDEvent);// 定时改变坐标系,并刷新屏幕达到旋转的效果
afx_msg void OnPopupRotate();// 处理右键弹出菜单中的旋转命令
afx_msg void OnPopupSizeDecrease();//处理右键弹出菜单中的缩小命令
afx_msg void OnPopupSizeIncrease();//处理右键弹出菜单中的放大命令
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);//处理键盘输入的命令(放大、缩小)
DECLARE_MESSAGE_MAP()
};
int CDialogGLDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CDialog::OnCreate(lpCreateStruct) == -1)return -1;
HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if(SetWindowPixelFormat(hDC)==FALSE)return 0;
if(CreateViewGLContext(hDC)==FALSE)return 0;
// BackColor
COLORREF color = ::GetSysColor(COLOR_3DFACE);
glClearColor((float)GetRValue(color)/255.0f,(float)GetGValue(color)/255.0f,(float)GetBValue(color)/255.0f,1.0);
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_FILL);
glShadeModel(GL_SMOOTH);
BuildList();
SetTimer(0,10,NULL);
return 0;
}
BOOL CDialogGLDlg::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 8;
pixelDesc.cRedShift = 16;
pixelDesc.cGreenBits = 8;
pixelDesc.cGreenShift = 8;
pixelDesc.cBlueBits = 8;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 64;
pixelDesc.cAccumRedBits = 16;
pixelDesc.cAccumGreenBits = 16;
pixelDesc.cAccumBlueBits = 16;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 8;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(m_GLPixelIndex==0) // Choose default
{
m_GLPixelIndex = 1;
if(!DescribePixelFormat(hDC,m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc))return FALSE;
}
if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc))return FALSE;
return TRUE;
}
BOOL CDialogGLDlg::CreateViewGLContext(HDC hDC)
{
m_hGLContext = wglCreateContext(hDC);
if(m_hGLContext==NULL)return FALSE;
if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)return FALSE;
return TRUE;
}
void CDialogGLDlg::BuildList()
{
::glNewList(1,GL_COMPILE_AND_EXECUTE);
glColor3f(1.0f,1.0f,1.0f);
float x = 1.5f;
glBegin(GL_POLYGON);
glNormal3d(0.0,0.0,-1.0);
glVertex3d( x, x, x);
glVertex3d( x, -x, x);
glVertex3d(-x, -x, x);
glVertex3d(-x, x, x);
glEnd();
glBegin(GL_POLYGON);
glNormal3d(0.0,0.0,1.0);
glVertex3d(-x, x, -x);
glVertex3d(-x, -x, -x);
glVertex3d( x, -x, -x);
glVertex3d( x, x, -x);
glEnd();
glBegin(GL_POLYGON);
glNormal3d(-1.0,0.0,0.0);
glVertex3d( x, x, x);
glVertex3d( x, x, -x);
glVertex3d( x, -x, -x);
glVertex3d( x, -x, x);
glEnd();
glBegin(GL_POLYGON);
glNormal3d(-1.0,0.0,0.0);
glVertex3d(-x, x, x);
glVertex3d(-x, x, -x);
glVertex3d(-x, -x, -x);
glVertex3d(-x, -x, x);
glEnd();
glBegin(GL_POLYGON);
glNormal3d(0.0,1.0,0.0);
glVertex3d(-x, -x, x);
glVertex3d( x, -x, x);
glVertex3d( x, -x, -x);
glVertex3d(-x, -x, -x);
glEnd();
glBegin(GL_POLYGON);
glNormal3d(0.0,1.0,0.0);
glVertex3d(-x, x, x);
glVertex3d( x, x, x);
glVertex3d( x, x, -x);
glVertex3d(-x, x, -x);
glEnd();
::glEndList();
}
void CDialogGLDlg::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
{
CPaintDC dc(this); // device context for painting
RenderScene();
SwapBuffers(dc.m_ps.hdc);
CDialog::OnPaint();
}
}
void CDialogGLDlg::RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
// Position / translation (mouse rotation)
glTranslated(0.0,0.0,-8.0);
glRotated(m_xRotate, 1.0, 0.0, 0.0);
glRotated(m_yRotate, 0.0, 1.0, 0.0);
glScalef(m_ScaleX,m_ScaleY,m_ScaleZ);
// Only one list, the box
::glCallList(1);
glPopMatrix();
}