8,304
社区成员
发帖
与我相关
我的任务
分享
#include <GL/glew.h>
#include <GL/wglew.h>
#include <windows.h>
// 下面是OpenGL环境初始化函数
void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
PIXELFORMATDESCRIPTOR pfd = // PixelFormatDescriptor, PFD structure
{
sizeof(PIXELFORMATDESCRIPTOR), //WORD nSize;
1, //WORD nVersion should be 1
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //DWORD dwFlags, properties of the pixel buffer //Flags
PFD_TYPE_RGBA, //BYTE iPixelType, type of pixel data //The kind of framebuffer. RGBA or palette.
32, //BYTE cColorBits, number of color bitplanes in each color buffer //Colordepth of the framebuffer.
0, //BYTE cRedBits, the number of red bitplanes in each RGBA color buffer
0, //BYTE cRedShift, the shift count for red bitplanes in each RGBA color buffer
0, //BYTE cGreenBits
0, //BYTE cGreenShift
0, //BYTE cBlueBits
0, //BYTE cBlueShift
0, //BYTE cAlphaBits, the number of alpha bitplanes in each RGBA color buffer. Alpha bitplanes are not supported.
0, //BYTE cAlphaShift, the shift count for alpha bitplanes in each RGBA color buffer. Alpha bitplanes are not supported.
0, //BYTE cAccumBits, the total number of bitplanes in the accumulation buffer
0, //BYTE cAccumRedBits
0, //BYTE cAccumGreenBits
0, //BYTE cAccumBlueBits
0, //BYTE cAccumAlphaBits
24, //BYTE cDepthBits, the depth of the depth (z-axis) buffer //Number of bits for the depthbuffer
8, //BYTE cStencilBits, the depth of the stencil buffer //Number of bits for the stencilbuffer
0, //BYTE cAuxBuffers, the number of auxiliary buffers. Not supported //Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE, //BYTE iLayerType, ignored, no longer used
0, //BYTE bReserved, the number of overlay and underlay planes
0, //DWORD dwLayerMask, ignored, no longer used
0, //DWORD dwVisibleMask, the transparent color or index of an underlay plane
0 //DWORD dwDamageMask, ignored, no longer used
};
int iFormat;
*hDC = GetDC(hwnd);
iFormat = ChoosePixelFormat(*hDC, &pfd);
SetPixelFormat(*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext(*hDC); // 建立一个假的OpenGL设备环境
wglMakeCurrent( *hDC , *hRC );
GLenum err;
err = glewInit();
if (GLEW_OK != err) // 初始化GLEW,没有返回错误
{
// Problem: glewInit failed, something is seriously wrong.
char szErrorMsg[20];
sprintf( szErrorMsg , "Error: %s\n" , glewGetErrorString(err) );
MessageBox( hwnd, szErrorMsg , "GLEW initialization error" , MB_OK );
}
else
{
char szErrorMsg[20];
sprintf( szErrorMsg , "GLEW: %s\n" , glewGetErrorString(err) );
MessageBox( hwnd, szErrorMsg , "OpenGL" , MB_OK );
}
char openGLVersion[20];
sprintf(openGLVersion,"OpenGL version: %s", (char*)glGetString(GL_VERSION) );
MessageBox(hwnd,openGLVersion,"OpenGL version",MB_OK); // 显示OpenGL版本是3.0
/* if (GLEW_ARB_vertex_program) // 为什么调用 GLEW_ 宏也编译出错?
{
// It is safe to use the ARB_vertex_program extension here.
MessageBox(hwnd,"It is safe to use the ARB_vertex_program extension here","ARB_vertex_program is OK",MB_OK);
}
*/
/* if (glewIsSupported("GL_ARB_vertex_program")) // 这样是可以的
{
MessageBox(hwnd,"It is safe to use the ARB_vertex_program extension here","ARB_vertex_program is OK",MB_OK);
}
*/
const int attribList[] =
{
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_COLOR_BITS_ARB, 32,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
0, //End
};
int pixelFormat;
UINT numFormats;
// wglChoosePixelFormatARB(*hDC, attribList, NULL, 1, &pixelFormat, &numFormats); // 直接用不行,编译提示错误
PFNWGLCHOOSEPIXELFORMATARBPROC pChoosePixelFormat;
pChoosePixelFormat = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB"); // 用这个函数获得函数指针就可以用了
pChoosePixelFormat(*hDC, attribList, NULL, 1, &pixelFormat, &numFormats);
SetPixelFormat(*hDC, pixelFormat , NULL );
}