📄 renderoglwnd.cpp
字号:
HRESULT hr;
// Get the DC
m_pDC = new CClientDC(this);
if (m_pDC == NULL) return E_FAIL;
// Find an appropriate pixel format
hr = SetupPixelFormat();
if (FAILED(hr))
return hr;
// Get a rendering context for the logo
m_hLogoRC = wglCreateContext(m_pDC->GetSafeHdc());
if (m_hLogoRC == 0) return E_FAIL;
// Make the logo context current
if (!wglMakeCurrent(m_pDC->GetSafeHdc(), m_hLogoRC))
return E_FAIL;
// Init the logo
m_bLogoValid = false;
hr = InitLogo();
if (FAILED(hr))
return hr;
// Get a rendering context for the effect
m_hRC = wglCreateContext(m_pDC->GetSafeHdc());
if (m_hRC == 0) return E_FAIL;
// Make the effect context current
if (!wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC))
return E_FAIL;
m_bClean = true;
m_bActive = true;
return S_OK;
}
HRESULT CRenderOGLWnd::ReleaseOpenGL()
{
if (!m_bActive)
return S_OK;
// Make the logo context current
if (!wglMakeCurrent(m_pDC->GetSafeHdc(), m_hLogoRC))
return E_FAIL;
if (m_bLogoValid)
ReleaseLogo();
// Make no context current
if (!wglMakeCurrent(NULL, NULL))
return E_FAIL;
// Delete the logo RCs
if (m_hLogoRC)
if (wglDeleteContext(m_hLogoRC) == false)
return E_FAIL;
m_hLogoRC = NULL;
// Delete the effect RCs
if (m_hRC)
if (wglDeleteContext(m_hRC) == false)
return E_FAIL;
m_hRC = NULL;
// Delete the DC
SAFE_DELETE(m_pDC);
m_bLogoValid = false;
m_bClean = false;
m_bActive = false;
return S_OK;
}
static int ChooseAcceleratedPixelFormat( HDC hdc, CONST PIXELFORMATDESCRIPTOR * ppfd )
{
int pixelformat = ChoosePixelFormat(hdc, ppfd);
if ( pixelformat == 0 ) return pixelformat;
PIXELFORMATDESCRIPTOR pfd;
if (!DescribePixelFormat(hdc, pixelformat, sizeof(pfd), &pfd)) return 0;
int accel = ((pfd.dwFlags)&PFD_GENERIC_FORMAT) > 0 ? 0 : 1;
if ( !accel ) return 0;
return pixelformat;
}
HRESULT CRenderOGLWnd::SetupPixelFormat(PIXELFORMATDESCRIPTOR* pPFD)
{
// Only do this once...
if (m_iPixelFormat != -1)
return S_OK;
// Default pixel format for a double-buffered,
// OpenGL-supporting, hardware-accelerated,
// RGBA-mode format. Pass in a pointer to a different
// pixel format if you want something else
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),// size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_DOUBLEBUFFER | // support double buffering
PFD_SUPPORT_OPENGL, // support OpenGL
PFD_TYPE_RGBA, // RGBA type
32, // color depth
0, 0, 0, 0, 0, 0, // color bits/shifts
8, 0, // alpha bits/shifts
0, // accum bits
0, 0, 0, 0, // r/g/b/a accum bits
24, // depth bits
8, // stencil bits
0, // auxiliary buffers
PFD_MAIN_PLANE, // main layer
0, 0, 0, 0 // masks
};
PIXELFORMATDESCRIPTOR* pPFDtoUse;
// let the user override the default pixel format
pPFDtoUse = (0 == pPFD)? &pfd : pPFD;
int pixelformat;
// First try to get a 24b depth buffer
pixelformat = ChooseAcceleratedPixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse);
if (pixelformat == 0) {
// Now try a 16b depth buffer
pPFDtoUse->cDepthBits = 16;
pixelformat = ChooseAcceleratedPixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse);
if (pixelformat == 0) {
// Now try a 24b depth buffer without stencil
pPFDtoUse->cDepthBits = 24;
pPFDtoUse->cStencilBits = 0;
pixelformat = ChooseAcceleratedPixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse);
if (pixelformat == 0) {
// Now try a 16b depth buffer without stencil
pPFDtoUse->cDepthBits = 16;
pixelformat = ChooseAcceleratedPixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse);
if (pixelformat == 0) {
// Try 16b color formats.
pPFDtoUse->cColorBits = 16;
pPFDtoUse->cAlphaBits = 0;
pPFDtoUse->cDepthBits = 24;
pPFDtoUse->cStencilBits = 8;
// First try to get a 24b depth buffer
pixelformat = ChooseAcceleratedPixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse);
if (pixelformat == 0) {
// Now try a 16b depth buffer
pPFDtoUse->cDepthBits = 16;
pixelformat = ChooseAcceleratedPixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse);
if (pixelformat == 0) {
// Now try a 24b depth buffer without stencil
pPFDtoUse->cDepthBits = 24;
pPFDtoUse->cStencilBits = 0;
pixelformat = ChooseAcceleratedPixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse);
if (pixelformat == 0) {
// Now try a 16b depth buffer without stencil
pPFDtoUse->cDepthBits = 16;
pixelformat = ChooseAcceleratedPixelFormat(m_pDC->GetSafeHdc(), pPFDtoUse);
if (pixelformat == 0) {
// Ok, no good buffer, fail
if (pixelformat == 0)
return false;
}
}
}
}
}
}
}
}
if (!SetPixelFormat( m_pDC->GetSafeHdc(), pixelformat, pPFDtoUse))
return E_FAIL;
// Now squirrel away the pixelformat
m_iPixelFormat = pixelformat;
if (!DescribePixelFormat(m_pDC->GetSafeHdc(), pixelformat, sizeof(m_sPFD), &m_sPFD))
return E_FAIL;
return S_OK;
}
HRESULT CRenderOGLWnd::InitLogo()
{
if (m_bLogoValid)
return S_OK;
m_bLogoValid = false;
// Get the logo bits
HRESULT hr;
BYTE *lpImg;
UINT w, h, w2, h2;
hr = GetRenderView()->GetSharedLogoBits(&lpImg, &w, &h, &w2, &h2);
if (FAILED(hr) || !lpImg)
return E_FAIL;
m_nLogoWidth = w;
m_nLogoWidth2 = w2;
m_nLogoHeight = h;
m_nLogoHeight2 = h2;
// Get the logo location
float tx, ty, sx, sy, cx, cy;
UINT winw, winh;
GetRenderView()->GetSharedLogoPosition(&tx, &ty, &sx, &sy, &cx, &cy, &winw, &winh);
// Make the logo context current
if (!wglMakeCurrent(m_pDC->GetSafeHdc(), m_hLogoRC))
return E_FAIL;
// Setup the transformation matrices
glViewport(0, 0, winw, winh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
// Setup the state
glAlphaFunc(GL_GREATER, 0.5f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
// Create the OpenGL texture
glGenTextures(1, &m_nLogoTexID);
glBindTexture(GL_TEXTURE_2D, m_nLogoTexID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, w2, h2, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, lpImg);
// Create the display list
m_nDispListID = glGenLists(1);
glNewList(m_nDispListID, GL_COMPILE);
{
glBegin(GL_QUADS);
{
float tc[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };
float vp[] = { -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f };
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
for (int i=0; i<4; i++) {
glTexCoord2f(tx*tc[2*i+0], ty*tc[2*i+1]);
glVertex3f(sx*vp[2*i+0]+cx, sy*vp[2*i+1]+cy, -1.0f);
}
}
glEnd();
}
glEndList();
// Check for errors with our setup...
GLenum err = glGetError();
ASSERT(err == GL_NO_ERROR);
m_bLogoValid = true;
return S_OK;
}
HRESULT CRenderOGLWnd::ReleaseLogo()
{
if (!m_bLogoValid)
return S_OK;
glDeleteLists(m_nDispListID, 1);
glDeleteTextures(1, &m_nLogoTexID);
m_bLogoValid = false;
return S_OK;
}
HRESULT CRenderOGLWnd::DrawLogo()
{
if (!m_bLogoValid)
return E_FAIL;
// Make the logo context current
if (!wglMakeCurrent(m_pDC->GetSafeHdc(), m_hLogoRC))
return E_FAIL;
// Draw the logo
glCallList(m_nDispListID);
// Check for errors with our logo rendering...
GLenum err = glGetError();
ASSERT(err == GL_NO_ERROR);
// Make the effect context current
if (!wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC))
return E_FAIL;
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -