📄 gl2view.cpp
字号:
// gl2View.cpp : implementation of the CGl2View class
//
#include "stdafx.h"
#include "gl2.h"
#include "gl2Doc.h"
#include "gl2View.h"
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
#include "math.h"
#include "Hdibapi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGl2View
IMPLEMENT_DYNCREATE(CGl2View, CView)
BEGIN_MESSAGE_MAP(CGl2View, CView)
//{{AFX_MSG_MAP(CGl2View)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_TIMER()
ON_COMMAND(IDM_PLAY_PAUSE, OnPlayPause)
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_FILE_SAVETOBMP_CLIENTBMP, OnFileSavetobmpClientbmp)
ON_COMMAND(ID_FILE_SAVETOBMP_RECTANGLEBMP, OnFileSavetobmpRectanglebmp)
ON_COMMAND(ID_FILE_SAVETOBMP_WINDOWSBMP, OnFileSavetobmpWindowsbmp)
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGl2View construction/destruction
CGl2View::CGl2View()
{
displayfashion = 0; mutumodel = 1;
m_pDC=NULL;m_pOldPalette=NULL;
X_Angle=10.0f;
Y_Angle=1.0f;
Z_Angle=5.0f;
m_play=FALSE;
m_lightAmb[0]=0.2f;m_lightAmb[1]=0.2f;
m_lightAmb[2]=0.2f;m_lightAmb[3]=1.0f;
m_lightDif[0]=1.0f; m_lightDif[1]=1.0f;
m_lightDif[2]=0.3f; m_lightDif[3]=1.0f;
m_lightSpe[0]=1.0f;m_lightSpe[1]=1.0f;
m_lightSpe[2]=1.0f;m_lightSpe[3]=1.0f;
m_lightPos[0]=1.0f;m_lightPos[1]=1.0f;
m_lightPos[2]=1.0f;m_lightPos[3]=0.0f;
// TODO: add construction code here
}
CGl2View::~CGl2View()
{
}
BOOL CGl2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style|=WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGl2View drawing
void CGl2View::OnDraw(CDC* pDC)
{
CGl2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
DrawScene();
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CGl2View printing
BOOL CGl2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGl2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGl2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CGl2View diagnostics
#ifdef _DEBUG
void CGl2View::AssertValid() const
{
CView::AssertValid();
}
void CGl2View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGl2Doc* CGl2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGl2Doc)));
return (CGl2Doc*)m_pDocument;
}
#endif //_DEBUG
static unsigned char _threeto8[8] =
{
0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
};
static unsigned char _twoto8[4] =
{
0, 0x55, 0xaa, 0xff
};
static unsigned char _oneto8[2] =
{
0, 255
};
static int defaultOverride[13] =
{
0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
};
// Windows Default Palette
static PALETTEENTRY defaultPalEntry[20] =
{
{ 0, 0, 0, 0 },
{ 0x80,0, 0, 0 },
{ 0, 0x80,0, 0 },
{ 0x80,0x80,0, 0 },
{ 0, 0, 0x80, 0 },
{ 0x80,0, 0x80, 0 },
{ 0, 0x80,0x80, 0 },
{ 0xC0,0xC0,0xC0, 0 },
{ 192, 220, 192, 0 },
{ 166, 202, 240, 0 },
{ 255, 251, 240, 0 },
{ 160, 160, 164, 0 },
{ 0x80,0x80,0x80, 0 },
{ 0xFF,0, 0, 0 },
{ 0, 0xFF,0, 0 },
{ 0xFF,0xFF,0, 0 },
{ 0, 0, 0xFF, 0 },
{ 0xFF,0, 0xFF, 0 },
{ 0, 0xFF,0xFF, 0 },
{ 0xFF,0xFF,0xFF, 0 }
};
/////////////////////////////////////////////////////////////////////////////
// CGl2View message handlers
int CGl2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
Init();
return 0;
}
void CGl2View::OnDestroy()
{
HGLRC hrc;
hrc=::wglGetCurrentContext();
::wglMakeCurrent(NULL,NULL);
if(hrc)
::wglDeleteContext(hrc);
if(m_pOldPalette)
m_pDC->SelectPalette(m_pOldPalette,FALSE);
if(m_pDC)
delete m_pDC;
CView::OnDestroy();
// TODO: Add your message handler code here
}
void CGl2View::OnSize(UINT nType, int cx, int cy)
{
if(cy>0)
{
if((m_oldRect.right>cx)||(m_oldRect.bottom>cy))
RedrawWindow();
m_oldRect.right=cx;
m_oldRect.bottom=cy;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0,1.0,-1.0,1.0,3.0,7.0);
glViewport(0,0,cx,cy);
}
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
}
void CGl2View::Init()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
m_pDC=new CClientDC(this);
ASSERT(m_pDC!=NULL);
if(!bSetupPixelFormat()) return;
n = ::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
CreateRGBPalette();
hrc=wglCreateContext(m_pDC->GetSafeHdc());
wglMakeCurrent(m_pDC->GetSafeHdc(),hrc);
GetClientRect(&m_oldRect);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
BOOL CGl2View::bSetupPixelFormat()
{
// define default desired video mode (pixel format)
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL ,//| // support OpenGL
//PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
// let the user change some parameters if he wants
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
{
AfxMessageBox("ChoosePixelFormat failed");
return FALSE;
}
// try to set this video mode
if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
// the requested video mode is not available so get a default one
pixelformat = 1;
if (DescribePixelFormat(m_pDC->GetSafeHdc(), pixelformat, sizeof(PIXELFORMATDESCRIPTOR), &pfd)==0)
{
// neither the requested nor the default are available: fail
AfxMessageBox("SetPixelFormat failed (no OpenGL compatible video mode)");
return FALSE;
}
}
return TRUE;
}
void CGl2View::DrawScene()
{
static BOOL bBusy=FALSE;
if(bBusy) return;
bBusy=TRUE;
glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f,0.2f,0.5f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-4.5f);
CreatLights();
glPushMatrix();
glRotatef(X_Angle,1.0f,0.0f,0.0f);
glRotatef(Y_Angle,0.0f,1.0f,0.0f);
glRotatef(Z_Angle,0.0f,0.0f,1.0f);
DrawCube();
// glBegin(GL_POLYGON);
// glVertex3f(-0.5f,0.5f,0.5f);
// glVertex3f(-0.5f,-0.5f,0.5f);
// glVertex3f(0.5f,-0.5f,0.5f);
// glVertex3f(0.5f,0.5f,0.5f);
// glEnd();
//
// glBegin(GL_POLYGON);
// glVertex3f(-0.5f,-0.5f,0.5f);
// glVertex3f(-0.5f,0.5f,0.5f);
// glVertex3f(-0.5f,0.5f,-0.5f);
// glVertex3f(-0.5f,-0.5f,-0.5f);
//
// glEnd();
//
//
// glBegin(GL_POLYGON);
// glVertex3f(-0.5f,-0.5f,0.5f);
// glVertex3f(-0.5f,0.5f,0.5f);
// glVertex3f(-0.5f,0.5f,-0.5f);
// glVertex3f(-0.5f,-0.5f,-0.5f);
// glEnd();
//
// glBegin(GL_POLYGON);
// glVertex3f(0.5f,-0.5f,0.5f);
// glVertex3f(0.5f,-0.5f,-0.5f);
// glVertex3f(0.5f,0.5f,-0.5f);
// glVertex3f(0.5f,0.5f,0.5f);
//
// glEnd();
//
//
// glBegin(GL_POLYGON);
// glVertex3f(-0.5f,-0.5f,0.5f);
// glVertex3f(0.5f,-0.5f,0.5f);
// glVertex3f(0.5f,-0.5f,-0.5f);
// glVertex3f(-0.5f,-0.5f,-0.5f);
// glEnd();
//
//
// glBegin(GL_POLYGON);
// glVertex3f(-0.5f,0.5f,0.5f);
// glVertex3f(0.5f,0.5f,0.5f);
// glVertex3f(0.5f,0.5f,-0.5f);
// glVertex3f(-0.5f,0.5f,-0.5f);
// glEnd();
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
bBusy=FALSE;
}
void CGl2View::GLTextOut(GLuint id,const char * const textstring)
{
if ( 0 == id || 0 == textstring )
{
return;
}
GLsizei size = strlen( textstring );
::glListBase( id );
::glCallLists( size, GL_UNSIGNED_BYTE, (const GLvoid*)textstring );
}
void CGl2View::CreateRGBPalette()
{
PIXELFORMATDESCRIPTOR pfd;
LOGPALETTE *pPal;
int n, i;
// get the initially choosen video mode
n = ::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
// if is an indexed one...
if (pfd.dwFlags & PFD_NEED_PALETTE)
{
// ... construct an equilibrated palette (3 red bits, 3 green bits, 2 blue bits)
// NOTE: this code is integrally taken from MFC example Cube
n = 1 << pfd.cColorBits;
pPal = (PLOGPALETTE) new char[sizeof(LOGPALETTE) + n * sizeof(PALETTEENTRY)];
ASSERT(pPal != NULL);
pPal->palVersion = 0x300;
pPal->palNumEntries = n;
for (i=0; i<n; i++)
{
pPal->palPalEntry[i].peRed=ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
pPal->palPalEntry[i].peGreen=ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
pPal->palPalEntry[i].peBlue=ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
pPal->palPalEntry[i].peFlags=0;
}
// fix up the palette to include the default Windows palette
if ((pfd.cColorBits == 8) &&
(pfd.cRedBits == 3) && (pfd.cRedShift == 0) &&
(pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
(pfd.cBlueBits == 2) && (pfd.cBlueShift == 6)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -