我正在使用Windows 7.我正在使用OpenGL进行编程。但我发现我可以使用一些功能。所以我想检查一下我系统上OpenGL的版本。 我使用下面的代码来检查它
const char* version = (const char*)glGetString(GL_VERSION);
但我得到一个空指针。如果我想升级我的OpenGL,我该怎么办?
答案 0 :(得分:6)
在询问您的版本之前,您需要GL上下文当前。
首先,创建一个上下文,在其上调用wglMakeCurrent,之后你应该能够调用glGetString。
报告的版本来自您已安装的驱动程序。您的硬件可以支持的OpenGL版本本身并不“可升级”(因为缺少某些硬件功能以支持最新和最好的)。
所以你能做的最好的事情就是升级你的驱动程序,但是不要把你的希望提高到高,这会导致更新的OpenGL。
答案 1 :(得分:5)
最简单,最快捷的方法是使用GPU Caps Viewer等诊断工具。
您也可以使用glGetString(GL_VERSION)
但请记住,您显示的版本是给定OpenGL上下文的版本 - 这不一定是GPU可以执行的最高版本。
但是,如果使用默认设置创建上下文,您可能会在兼容性配置文件中获得尽可能高的OpenGL上下文,所以是的,这种方法很有用。
此外,由于glGetString(GL_VERSION)
指的是给定的OpenGL上下文,因此您需要事先创建它。实际上,需要GL上下文来调用任何 gl*
函数。
确实,升级驱动程序可能会为您提供更高版本的GL版本,但主要版本不太可能发生变化。例如,如果你发现自己支持GL 3.1,那么最新的驱动程序很可能会给你GL 3.3,而不是GL 4.0。
答案 2 :(得分:0)
尝试使用以下代码,它适用于我:
cout << "OpenGL Version : " << glGetString(GL_VERSION) << endl;
确保在程序中包含字符串和iostream。
答案 3 :(得分:0)
在调用glGetString(GL_VERSION)之前,您需要创建OpenGL上下文(WGL):
#include <windows.h>
#include <GL/GL.h>
#pragma comment (lib, "opengl32.lib")
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
MSG msg = {0};
WNDCLASS wc = {0};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = L"oglversionchecksample";
wc.style = CS_OWNDC;
if( !RegisterClass(&wc) )
return 1;
CreateWindowW(wc.lpszClassName,L"openglversioncheck",WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,640,480,0,0,hInstance,0);
while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
DispatchMessage( &msg );
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
PFD_TYPE_RGBA, //The kind of framebuffer. RGBA or palette.
32, //Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, //Number of bits for the depthbuffer
8, //Number of bits for the stencilbuffer
0, //Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
HDC ourWindowHandleToDeviceContext = GetDC(hWnd);
int letWindowsChooseThisPixelFormat;
letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd);
SetPixelFormat(ourWindowHandleToDeviceContext,letWindowsChooseThisPixelFormat, &pfd);
HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
wglMakeCurrent (ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);
MessageBoxA(0,(char*)glGetString(GL_VERSION), "OPENGL VERSION",0);
wglDeleteContext(ourOpenGLRenderingContext);
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}