// openglTestDlg.cpp : 實(shí)現(xiàn)文件
//
#include "stdafx.h"
#include "openglTest.h"
#include "openglTestDlg.h"
#include "math.h"
#pragma comment( lib, "glaux.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于應(yīng)用程序“關(guān)于”菜單項(xiàng)的 CAboutDlg 對(duì)話框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 對(duì)話框數(shù)據(jù)
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實(shí)現(xiàn)
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CopenglTestDlg 對(duì)話框
CopenglTestDlg::CopenglTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CopenglTestDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CopenglTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CopenglTestDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_TIMER()
ON_WM_TIMER()
END_MESSAGE_MAP()
// CopenglTestDlg 消息處理程序
BOOL CopenglTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 將“關(guān)于...”菜單項(xiàng)添加到系統(tǒng)菜單中。
// IDM_ABOUTBOX 必須在系統(tǒng)命令范圍內(nèi)。
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);
}
}
// 設(shè)置此對(duì)話框的圖標(biāo)。當(dāng)應(yīng)用程序主窗口不是對(duì)話框時(shí),框架將自動(dòng)
// 執(zhí)行此操作
SetIcon(m_hIcon, TRUE); // 設(shè)置大圖標(biāo)
SetIcon(m_hIcon, FALSE); // 設(shè)置小圖標(biāo)
///////////////////////OPENGL INIT/////////////////////////
CWnd *wnd=GetDlgItem(IDC_RENDER);
wnd->MoveWindow(10,10,600,500);
hrenderDC=::GetDC(wnd->m_hWnd);
if(SetWindowPixelFormat(hrenderDC)==FALSE)
return 0;
if(CreateViewGLContext(hrenderDC)==FALSE)
return 0;
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_FILL);
///////////////////////////////////////////
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glViewport(0,0,600,500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
/////////////////////////////////////////////////////////////////////////
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
SetTimer(1,50,NULL);
////////////////////////////////////////////////////////////////
return TRUE; // 除非將焦點(diǎn)設(shè)置到控件,否則返回 TRUE
}
void CopenglTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向?qū)υ捒蛱砑幼钚』粹o,則需要下面的代碼
// 來(lái)繪制該圖標(biāo)。對(duì)于使用文檔/視圖模型的 MFC 應(yīng)用程序,
// 這將由框架自動(dòng)完成。
void CopenglTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于繪制的設(shè)備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使圖標(biāo)在工作矩形中居中
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;
// 繪制圖標(biāo)
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
RenderScene();
}
//當(dāng)用戶拖動(dòng)最小化窗口時(shí)系統(tǒng)調(diào)用此函數(shù)取得光標(biāo)顯示。
//
HCURSOR CopenglTestDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
BOOL CopenglTestDlg::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER |
PFD_TYPE_RGBA;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
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 = 0;
pixelDesc.cStencilBits = 1;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
PixelFormat = ChoosePixelFormat(hDC,&pixelDesc);
if(PixelFormat==0) // Choose default
{
PixelFormat = 1;
if(DescribePixelFormat(hDC,PixelFormat,
sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC,PixelFormat,&pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
BOOL CopenglTestDlg::CreateViewGLContext(HDC hDC)
{
hrenderRC = wglCreateContext(hDC);
if(hrenderRC==NULL)
return FALSE;
if(wglMakeCurrent(hDC,hrenderRC)==FALSE)
return FALSE;
return TRUE;
}
void CopenglTestDlg::RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f); // Move Left 1.5 Units And Into The Screen 6.0
glRotated(m_yRotate, 0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(1.0,1.0,0.0); /* red color */
glVertex3f( 0.0f, 1.0f, -0.0f); // Top
glVertex3f(-0.0f,-1.0f, 1.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
SwapBuffers(hrenderDC);
}
void CopenglTestDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息處理程序代碼和/或調(diào)用默認(rèn)值
RenderScene();
m_yRotate = m_yRotate + 10;
CDialog::OnTimer(nIDEvent);
}