Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Seyro97 en 8 Junio 2015, 05:05 am



Título: (Ayuda con OpenGL) La función 'SetPixelFormat' me da un error
Publicado por: Seyro97 en 8 Junio 2015, 05:05 am
Hola, muy buenas a tod@s, os comento:

Estaba creando un simple Hello World de OpenGL usando la API de Windows. He ido paso a paso, creando primero una ventana simple con la API de Windows (la cual se ha creado correctamente), y posteriormente he creado un contexto con el viejo OpenGL. El problema lo da la función SetPixelFormat, el cual da el código de error 3221684230 (en hexadecimal 0xC0070006). No tengo ni idea de que puede pasar, incluso he copiado/pegado un código de un contexto de OpenGL de Internet y me pasa lo mismo...

Código
  1. #ifndef UNICODE
  2. #define UNICODE
  3. #endif
  4.  
  5. #include <windows.h>
  6. #include <gl\gl.h>
  7.  
  8. #include <chrono>
  9. #include <thread>
  10.  
  11. #include <iostream>
  12.  
  13. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  14.  
  15. int main() {
  16. WNDCLASSEX cWindow;
  17. HWND hWnd;
  18. MSG Msg;
  19. HDC hDC;
  20. HGLRC hRC;
  21.  
  22. memset(&Msg, NULL, sizeof(MSG));
  23.  
  24. cWindow.cbSize = sizeof(WNDCLASSEX);
  25. cWindow.hInstance = GetModuleHandle(NULL);
  26. cWindow.lpszClassName = L"¡Título!";
  27. cWindow.lpfnWndProc = WndProc;
  28. cWindow.lpszMenuName = NULL;
  29. cWindow.style = CS_OWNDC;
  30. cWindow.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  31. cWindow.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  32. cWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
  33. cWindow.hbrBackground = (HBRUSH)3;
  34. cWindow.cbClsExtra = 0;
  35. cWindow.cbWndExtra = 0;
  36.  
  37. if(RegisterClassEx(&cWindow) == NULL) {
  38. MessageBox(NULL, L"Error registring a window class!", L"Fatal error", MB_OK | MB_ICONERROR);
  39. PostQuitMessage(0);
  40. }
  41.  
  42. hWnd = CreateWindowEx(0, L"¡Título!", L"¡Título!", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
  43.  640, 480, HWND_DESKTOP, NULL, GetModuleHandle(NULL), (void *)NULL);
  44.  
  45. if(hWnd = NULL) {
  46. MessageBox(NULL, L"Error creating a window!", L"Fatal error", MB_OK | MB_ICONERROR);
  47. PostQuitMessage(0);
  48. }
  49.  
  50. ShowWindow(hWnd, SW_SHOWDEFAULT);
  51.  
  52. PIXELFORMATDESCRIPTOR PFD;
  53. int choosenPFD;
  54.  
  55. memset(&PFD, NULL, sizeof(PIXELFORMATDESCRIPTOR));
  56. PFD.nVersion = 1;
  57. PFD.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  58. PFD.iPixelType = PFD_TYPE_RGBA;
  59. PFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  60. PFD.cColorBits = 24;
  61. PFD.cDepthBits = 16;
  62. PFD.iLayerType = PFD_MAIN_PLANE;
  63.  
  64. hDC = GetDC(hWnd);
  65.  
  66. choosenPFD = ChoosePixelFormat(hDC, &PFD);
  67.  
  68. if(!choosenPFD) {
  69. MessageBox(hWnd, L"Error choosing a pixel format (old)!", L"Fatal error", MB_OK | MB_ICONERROR);
  70. PostQuitMessage(0);
  71. }
  72.  
  73. if(!SetPixelFormat(hDC, choosenPFD, &PFD)) {
  74. std::clog << GetLastError();
  75. MessageBox(hWnd, L"Error setting a pixel format (old)!", L"Fatal error", MB_OK | MB_ICONERROR);
  76. PostQuitMessage(0);
  77. }
  78.  
  79. hRC = wglCreateContext(hDC);
  80.  
  81. if(!hRC) {
  82. MessageBox(hWnd, L"Error creating a OpenGL context (old)!", L"Fatal error", MB_OK | MB_ICONERROR);
  83. PostQuitMessage(0);
  84. }
  85.  
  86. if(!wglMakeCurrent(hDC, hRC)) {
  87. MessageBox(hWnd, L"Error linking a OpenGL context with a window!", L"Fatal error", MB_OK | MB_ICONERROR);
  88. PostQuitMessage(0);
  89. }
  90.  
  91. while(Msg.message != WM_QUIT) {
  92. if(PeekMessage(&Msg, hWnd, NULL, NULL, PM_REMOVE)) {
  93. TranslateMessage(&Msg);
  94. DispatchMessage(&Msg);
  95. }
  96.  
  97. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  98.  
  99.  
  100.  
  101. SwapBuffers(hDC);
  102.  
  103. std::this_thread::sleep_for(std::chrono::microseconds(11764));
  104. }
  105.  
  106. wglMakeCurrent(NULL, NULL);
  107. wglDeleteContext(hRC);
  108. ReleaseDC(hWnd, hDC);
  109. DestroyWindow(hWnd);
  110. UnregisterClass(L"¡Título!", GetModuleHandle(NULL));
  111.  
  112. return 0;
  113. }
  114.  
  115. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  116. switch(uMsg) {
  117. case WM_DESTROY:
  118. PostQuitMessage(0);
  119. }
  120.  
  121. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  122. }