假设我有 COpenGLControl class downloaded here from codeguru假设the first event handler runned when creating the OpenGL window is OnCreate ,我试图 catch 这个类的错误。下面是用于在我的对话框的 .h 和 .cpp 文件中创建窗口的代码:
MyOpenGLTestDlg.h
COpenGLControl m_oglWindow;
MyOpenGLTestDlg.cpp
CRect rect;
// Get size and position of the picture control
GetDlgItem(ID_OPENGL)->GetWindowRect(rect);
// Convert screen coordinates to client coordinates
ScreenToClient(rect);
据了解,我认为函数 OnCreate
已被调用。事实上,我认为代码行 COpenGLControl m_oglWindow;
导致调用此函数!但我不确定,如果你能指导我一点这方面的知识,我将不胜感激?
无论如何,我没有对类做太多改变:
OpenGLControl.h
#pragma once
#include "afxwin.h"
#include "WinBase.h"
#include <gl/gl.h>
#include <gl/glu.h>
class COpenGLControl : public CWnd
{
public:
/******************/
/* Public Members */
/******************/
UINT_PTR m_unpTimer;
// View information variables
float m_fLastX;
float m_fLastY;
float m_fPosX;
float m_fPosY;
float m_fZoom;
float m_fRotX;
float m_fRotY;
bool m_bIsMaximized;
private:
/*******************/
/* Private Members */
/*******************/
// Window information
CWnd *hWnd; //window handle
HDC hdc; //device context handle
HGLRC hrc; //handle to GL Rendering Context
int m_nPixelFormat;
CRect m_rect;
CRect m_oldWindow;
CRect m_originalRect;
public:
COpenGLControl(void);
virtual ~COpenGLControl(void);
void oglCreate(CRect rect, CWnd *parent);
void oglInitialize(void);
void oglDrawScene(void);
// Added message classes:
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnDraw(CDC *pDC);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
OpenGLControl.cpp
int COpenGLControl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;
oglInitialize();
return 0;
}
void COpenGLControl::oglInitialize(void)
{
// Initial Setup:
//
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // bit depth
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24, // z-buffer depth
8,0,PFD_MAIN_PLANE, 0, 0, 0, 0,
};
// Get device context only once.
hdc = GetDC()->m_hDC;
// Pixel format.
m_nPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, m_nPixelFormat, &pfd);
// Create the OpenGL Rendering Context.
hrc = wglCreateContext(hdc);
GLenum error13 = glGetError();
wglMakeCurrent(hdc, hrc);
// Basic Setup:
//
// Set color to use when clearing the background.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
// Turn on backface culling
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
// Turn on depth testing
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
// Send draw request
OnDraw(NULL);
}
如您所见,我在 hrc = wglCreateContext(hdc);
之后编写了代码 GLenum error13 = glGetError();
以捕获它抛出的任何可能错误,是的,error13 的值是 1282
,这意味着 INVALID_OPERATION
所以我认为 OpenGL 渲染上下文的句柄
没有正确创建!
此外,如果您检查 hdc
和 hrc
的值,您还会遇到这些:
hdc -> 未使用 = ??? (错误:无法计算表达式)
hrc -> 未使用 = 0
你能帮我找出为什么会这样吗?问题是什么?
最佳答案
如果您在没有当前 GL 上下文的情况下调用 GL 函数(如 glGetError()
本身),结果是相当不确定的。
wglCreateContext()
是 Win33 API(而非 GL API)的一部分,将通过返回 NULL
指针来指示错误。如果在这种情况下需要详细信息,您可以调用 Windows API 函数 GetLastError()
,就像大多数其他 Windows API 函数一样。
关于windows - wglCreateContext 抛出 INVALID_OPERATION 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18280147/