Winapi GetLastError()在调用SwapBuffers(HDC)后返回错误\u无效\u句柄/6
每当我尝试调用SwapBuffers()时,GetLastError()返回6/ERROR\u INVALID\u句柄。有一段时间,我试图通过以不同的方式重写代码来解决这个问题,试图找到错误的不同来源,并大体上看一下我可能做错了什么。但是,我还没有得出一个结论,关于是什么导致了这一点,或者我能做些什么来解决这一问题 当我调用OpenGL函数时,例如glUseProgram()和 glVertexAttributePointer(),glGetError()返回1282/GL\u无效\u操作Winapi GetLastError()在调用SwapBuffers(HDC)后返回错误\u无效\u句柄/6,winapi,opengl,c++11,opengl-3,getlasterror,Winapi,Opengl,C++11,Opengl 3,Getlasterror,每当我尝试调用SwapBuffers()时,GetLastError()返回6/ERROR\u INVALID\u句柄。有一段时间,我试图通过以不同的方式重写代码来解决这个问题,试图找到错误的不同来源,并大体上看一下我可能做错了什么。但是,我还没有得出一个结论,关于是什么导致了这一点,或者我能做些什么来解决这一问题 当我调用OpenGL函数时,例如glUseProgram()和 glVertexAttributePointer(),glGetError()返回1282/GL\u无效\u操作 Wi
Window::Window(string title, int xPos, int yPos, int width, int height, string icon_path)
{
this->title = title;
this->width = width;
this->height = height;
if (this->height == 0)
this->height = 1;
if (!Create(xPos, yPos))
{
cout << "Window Creation Failure!" << endl;
exit(EXIT_FAILURE);
}
HANDLE hIcon = LoadImage(0, icon_path.c_str(), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM) hIcon);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
Window::~Window()
{
wglMakeCurrent(hDC, 0);
wglDeleteContext(hRC);
ReleaseDC(hWnd, hDC);
}
bool Window::Create(int xPos, int yPos)
{
WNDCLASS WndClass;
DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
DWORD dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX;
this->hInstance = GetModuleHandle(nullptr);
if (!this->hInstance)
return false;
WndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
WndClass.lpfnWndProc = (WNDPROC) WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = this->hInstance;
WndClass.hIcon = LoadIcon(nullptr, IDI_WINLOGO);
WndClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
WndClass.hbrBackground = nullptr;
WndClass.lpszMenuName = nullptr;
WndClass.lpszClassName = this->title.c_str();
if (!RegisterClass(&WndClass))
return false;
this->hWnd = CreateWindowEx(dwExStyle, this->title.c_str(), this->title.c_str(), dwStyle,
xPos, yPos, this->width, this->height, nullptr, nullptr, this->hInstance, nullptr);
if (!this->hWnd)
return false;
if (!this->CreateContext())
return false;
ShowWindow(this->hWnd, SW_SHOW);
UpdateWindow(this->hWnd);
return true;
}
bool Window::CreateContext()
{
this->hDC = GetDC(hWnd);
if (!this->hDC)
return false;
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
int nPixelFormat = ChoosePixelFormat(this->hDC, &pfd);
if (!nPixelFormat)
return false;
bool bResult = SetPixelFormat(this->hDC, nPixelFormat, &pfd);
if (!bResult)
return false;
HGLRC tempOpenGLContext = wglCreateContext(this->hDC);
wglMakeCurrent(this->hDC, tempOpenGLContext);
GLenum err = glewInit();
if (GLEW_OK != err)
return false;
int attributes[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 2,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0
};
if (wglewIsSupported("WGL_ARB_create_context") == true)
{
this->hRC = wglCreateContextAttribsARB(this->hDC, NULL, attributes);
wglMakeCurrent(nullptr, nullptr);
wglDeleteContext(tempOpenGLContext);
wglMakeCurrent(this->hDC, this->hRC);
if (!this->hRC)
return false;
}
else
{
this->hRC = tempOpenGLContext;
}
int glVersion[2] = {-1, -1};
glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]);
glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]);
cout << "Opengl is running on context version : " << glVersion[0] << ", " << glVersion[1] << endl;
return true;
}
int Window::GameLoop(Core *core)
{
MSG msg;
while (core->IsRunning())
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
core->SetRunning(false);
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
core->Run();
SwapBuffers(this->hDC);
}
}
Core::Destroy();
return (int)(msg.wParam);
}
Window::Window(字符串标题、int-xPos、int-yPos、int-width、int-height、字符串图标路径)
{
这->标题=标题;
这个->宽度=宽度;
这个->高度=高度;
如果(此->高度==0)
这个->高度=1;
如果(!创建(XPO、YPO))
{
(简体)
返回false;
WndClass.style=CS|HREDRAW | CS|VREDRAW | CS|OWNDC;
WndClass.lpfnWndProc=(WNDPROC)WNDPROC;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hInstance=此->hInstance;
WndClass.hIcon=加载图标(nullptr,IDI_WINLOGO);
WndClass.hCursor=加载光标(nullptr,IDC_箭头);
WndClass.hbrBackground=nullptr;
WndClass.lpszMenuName=nullptr;
WndClass.lpszClassName=this->title.c_str();
if(!RegisterClass(&WndClass))
返回false;
this->hWnd=CreateWindowEx(dwExStyle,this->title.c_str(),this->title.c_str(),dwStyle,
xPos、YPO、本->宽度、本->高度、空PTR、空PTR、本->高度、空PTR);
如果(!this->hWnd)
返回false;
如果(!this->CreateContext())
返回false;
显示窗口(此->硬件显示,软件显示);
更新域(此->hWnd);
返回true;
}
bool窗口::CreateContext()
{
此->hDC=GetDC(hWnd);
如果(!this->hDC)
返回false;
像素描述符pfd;
memset(&pfd,0,sizeof(像素格式描述符));
pfd.nSize=sizeof(像素格式描述符);
pfd.dwFlags=pfd_DOUBLEBUFFER | pfd_SUPPORT_OPENGL | pfd_DRAW_TO_WINDOW;
pfd.iPixelType=pfd_TYPE_RGBA;
pfd.cColorBits=32;
pfd.cDepthBits=32;
pfd.iLayerType=pfd_主平面;
int nPixelFormat=ChoosePixelFormat(此->hDC和pfd);
如果(!nPixelFormat)
返回false;
bool-bResult=SetPixelFormat(此->hDC、nPixelFormat和pfd);
如果(!bResult)
返回false;
HGLRC tempOpenGLContext=wglCreateContext(this->hDC);
wglMakeCurrent(此->hDC,tempOpenGLContext);
GLenum err=glewInit();
如果(GLEW_OK!=错误)
返回false;
int属性[]=
{
WGL\U上下文\U主要\U版本\U ARB,3,
WGL_上下文_次要_版本_ARB,2,
WGL\u上下文\u标志\u ARB,WGL\u上下文\u转发\u兼容\u位\u ARB,
0
};
if(wglewIsSupported(“WGL\u ARB\u create\u context”)==true)
{
this->hRC=wglCreateContextAttribsARB(this->hDC,NULL,属性);
wglMakeCurrent(nullptr,nullptr);
wglDeleteContext(tempOpenGLContext);
wglMakeCurrent(此->hDC,此->hRC);
如果(!此->hRC)
返回false;
}
其他的
{
这->hRC=tempOpenGLContext;
}
int glVersion[2]={-1,-1};
glGetIntegerv(GL_主要版本,&glVersion[0]);
glGetIntegerv(GL_次要版本和glVersion[1]);
cout colorshader=新着色器(“./Shaders/Vertex/BasicColour.vert”、”/Shaders/Vertex/BasicColour.vert”);
this->colorVertexArray=新的VertexArray(新的VertexBuffer(数据,大小f(数据),GL_三角形,3,大小f(GLfloat)*3,this->colorShader));
cout SetFloat4((colorVertexArray->GetVertexBufferObject()->GetShader())->GetUniform(“颜色”),1.0f、0.0f、0.0f、1.0f;
(colorVertexArray)->AddVertexBufferObjectTarget((colorVertexArray->GetVertexBufferObject()->GetShader())->GetAttribute(“位置”);
(ColorVertexArray)->渲染();
}
无论上一个API调用是否失败,您都在调用GetLastError
。只有当文档指示您应该调用时,您才应该调用GetLastError
。通常是当您调用的函数的返回值指示失败时
API调用完全有可能成功,并且
GetLastError
返回一个非零值。这里很可能就是这样的情况。您没有检查wglCreateContextAttribsARB是否返回NULL。您的代码没有调用GetLastError。请发布与此问题相关的代码。@David Heffernan抱歉,我用其他方式调用了它这里-处于游戏状态class@Axalo它返回true not NULL。请按照我的要求执行并发布真正的代码谢谢!我仍然对glGetError()的整个“GL\u INVALID\u OPERATION”错误感到困惑。发布的代码应该绘制一个红色三角形,但是它没有。
#include "State.h"
using namespace Lumen::Core_Engine;
GLfloat Data[9] =
{
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
Example::Example()
{
this->colourShader = new Shader("./Shaders/Vertex/BasicColour.vert", "./Shaders/Vertex/BasicColour.vert");
this->colourVertexArray = new VertexArray(new VertexBuffer(Data, sizeof(Data), GL_TRIANGLES, 3, sizeof(GLfloat) * 3, this->colourShader));
cout << "'Example' state initialized!" << endl;
}
Example::~Example()
{
}
void Example::Update()
{
GLenum errGL = glGetError();
if (GLEW_NO_ERROR != errGL)
cout << errGL << endl;
DWORD errWin = GetLastError();
if (NO_ERROR != errWin)
cout << errWin << endl;
}
void Example::Render()
{
glUseProgram((colourVertexArray->GetVertexBufferObject()->GetShader())->GetProgram());
(colourVertexArray->GetVertexBufferObject()->GetShader())->SetFloat4((colourVertexArray->GetVertexBufferObject()->GetShader())->GetUniform("Colour"), 1.0f, 0.0f, 0.0f, 1.0f);
(colourVertexArray)->AddVertexBufferObjectTarget((colourVertexArray->GetVertexBufferObject()->GetShader())->GetAttribute("Position"));
(colourVertexArray)->Render();
}