操作系统:Windows 7,64位Visual Studio 2010,debugging,32位
我正在尝试一个简单的Windows程序来开始使用OpenGL:所有的程序应该做的是用glClear(GL_COLOR_BUFFER_BIT)清除颜色缓冲区。
在网上find的教程中,我发现人们在创build窗口期间(以及在处理WM_CREATE消息的过程中)创build和设置openGL上下文。 不知何故,我发现这不适合我的代码,我似乎无法弄清楚。 所以如果有人能指出我的正确方向。
请在下面find代码我期望它应该基于教程工作:
Windows上的Node.js本机扩展
如何在Windows上安装FTGL库?
如果我只使用OpenGL ES 1.x调用构build和链接OpenGL应用程序,它仍然可以工作吗?
如何诊断Windows上的堆损坏错误?
在linux mint中不能使用SOIL
#include <windows.h> #include <GLglew.h> #include <GLwglew.h> #include "Application.h" HWND ghMainWnd = 0; bool InitwindowsApp(HINSTANCE instanceHandle,int show); int Run(); LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WParaM wParam,LParaM lParam); HDC hDC; HglrC hglrC; HPALETTE hPalette; void SetupPixelFormat() { PIXELFORMATDESCRIPTOR pfd = {}; pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cAlphaBits = 8; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; int pixelFormat = ChoosePixelFormat(hDC,&pfd); if(pixelFormat == 0) { MessageBox(ghMainWnd,L"ChoosePixelFormat() Failed",L"Error",MB_ICONERROR | MB_OK); exit(1); } if(!SetPixelFormat(hDC,pixelFormat,&pfd)) { MessageBox(ghMainWnd,L"SetPixelFormat() Failed",MB_ICONERROR | MB_OK); exit(1); } } int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR pCmdLine,int nShowCmd) { if(!InitwindowsApp(hInstance,nShowCmd)) return 0; return Run(); } bool InitwindowsApp(HINSTANCE instanceHandle,int show) { WNDCLASS wc; wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = instanceHandle; wc.hIcon = LoadIcon(0,IDI_APPLICATION); wc.hCursor = LoadCursor(0,IDC_ARROW); wc.hbrBackground = (HBrush) GetStockObject(BLACK_Brush); wc.lpszMenuName = 0; wc.lpszClassName = L"BasicWndClass"; if(!RegisterClass(&wc)) { MessageBox(0,L"RegisterClass Failed",0); return false; } ghMainWnd = CreateWindow( L"BasicWndClass",L"BasicWindowsApp",WS_OVERLAPPEDWINDOW,600,instanceHandle,0); if(ghMainWnd == 0) { MessageBox(0,L"CreateWindow Failed",0); return false; } ShowWindow(ghMainWnd,show); UpdateWindow(ghMainWnd); return true; } int Run() { MSG msg = {0}; Application *pApp = new Application(); pApp->Init(); while(msg.message != WM_QUIT) { if(PeekMessage(&msg,PM_REMOVE)) { TranslateMessage(&msg); dispatchMessage(&msg); } else { //Do Other stuff pApp->Draw(); SwapBuffers(hDC); } } if(pApp) { delete pApp; pApp = nullptr; } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd,LParaM lParam) { switch(msg) { case WM_CREATE: hDC = GetDC(ghMainWnd); SetupPixelFormat(); hglrC = wglCreateContext(hDC); if(!hglrC) { MessageBox(ghMainWnd,L"wglCreateContext() Failed",MB_ICONERROR | MB_OK); exit(1); } if(!wglMakeCurrent(hDC,hglrC)) { MessageBox(ghMainWnd,L"wglMakeCurrent() Failed",MB_ICONERROR | MB_OK); exit(1); } glViewport(0,600); glClearColor(1.0f,0.0f,1.0f); return 0; case WM_LBUTTONDOWN: return 0; case WM_KEYDOWN: if(wParam == VK_ESCAPE) DestroyWindow(ghMainWnd); return 0; case WM_DESTROY: if(hglrC) { wglMakeCurrent(nullptr,nullptr); wglDeleteContext(hglrC); } ReleaseDC(ghMainWnd,hDC); PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,msg,wParam,lParam); }
下面,我find了一个“黑客”,使其工作:我基本上将opengl上下文创build到InitwindowsApp()函数…但为什么上面的代码不起作用?
#include <windows.h> #include <GLglew.h> #include <GLwglew.h> #include "Application.h" HWND ghMainWnd = 0; bool InitwindowsApp(HINSTANCE instanceHandle,show); UpdateWindow(ghMainWnd); hDC = GetDC(ghMainWnd); SetupPixelFormat(); hglrC = wglCreateContext(hDC); if(!hglrC) { MessageBox(ghMainWnd,1.0f); return true; } int Run() { MSG msg = {0}; Application *pApp = new Application(); pApp->Init(); while(msg.message != WM_QUIT) { if(PeekMessage(&msg,LParaM lParam) { switch(msg) { case WM_CREATE: return 0; case WM_LBUTTONDOWN: return 0; case WM_KEYDOWN: if(wParam == VK_ESCAPE) DestroyWindow(ghMainWnd); return 0; case WM_DESTROY: if(hglrC) { wglMakeCurrent(nullptr,lParam); }
MinGW找不到inttypes.h
在glBufferSubData中可能会导致这个opengl segfault?
superbible lnk2019 glew
glfwSwapBuffers()和Windows上的垂直刷新
简单的openGL程序无法在ubuntu中链接
当WM_CREATE到达时,您的ghMainWnd仍然是NULL (该消息在CreateWindow调用返回之前发送)。 而是在您的GetDC调用中使用消息的hWnd参数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。