OpenGL 使用GLEW没有返回错误,可是GLEW_宏和wglChoosePixelFormatARB函数都不可用这是怎么回事?

Silent_Lamb 2012-11-25 10:52:40


#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 );
}



我这是在Code::Block下编译的,因为我的是64位win7系统,VC6.0不能支持64位的 glew.lib 文件,改用32位版本的 glew.lib 提示invalid machine type ;而在Code::Block 下虽然也不支持64位的 glew.lib,但是32位的编译通过,不知道是不是因为这样导致glew没有起作用。可是之前我忘了把假的OpenGL设备环境设为当前,也就是少了这句wglMakeCurrent( *hDC , *hRC ); glew返回了错误missing GL version,加上那句以后没有返回错误,这说明 glew 应该是有用的呀。。好奇怪
...全文
276 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Silent_Lamb 2012-11-30
  • 打赏
  • 举报
回复
问题解决了。我在32位虚拟机下面运行没有问题,GLEW_宏和wglChoosePixelFormatARB函数都可以用。就是因为64位系统下面不能用32位的glew32.lib文件

8,304

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧