Updated:
The problem is solved! the solution is inspired by an advice in comments:
I just updated the graphic driver and now all my code works fine. For information, the mentioned driver is "Inter(R) UHD Graphics 620" that I updated to the version 25.20.10.6472 (10/12/2018).
Very sorry for the inconvenience, and thank you for all your support.
Using ScreenToGif (thanks to Strive Sun) here is what it looks like : the main (OpenGL) window is an external view of some kind of space ship (with colorful boosters), and at the bottom right the (GDI) window is the view from the AI pilote which is learning to eat green gums and avoid red ones).
If you, Mr StackOverflow, think this kind of rambling is not appropriate, please remove it...
Below, the original question:
I have a small sample of OpenGl code under windows that works fine with windows 7 + visual studio 2010. But on my new PC with windows 10 + visual studio 2017, the OpenGL rendering window is not displayed. Compilation is ok and no error is returned during execution.
So, in order to post it here without bothering you with tons of stuff, I wrote a short extract of my code. It uses only basic native gdi+opengl functions provided by the standard win API (gl.h/glu.h), but not any additional library (as glew/glut/freeglut/...).
With Windows7+VS2010 I have 3 windows: a GDI rendering, an OpenGL rendering and a console.
- On the first: some crossing blue lines (using "LineTo").
- On the second: one violet triangle (using "glVertex3d").
- On the console a message telling me that all functions well returned.
With Windows10+VS2017, the OpenGL window is not visible. I don't even see a ghost when I play with alt-tab keys.
I suppose that something has changed in the API, but I can't figure out what...
So here is my code :
#include <windows.h>
#include <cstdio>
#include <GL/GLU.h>
static LRESULT CALLBACK win32callback ( HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam )
{
return DefWindowProcA( hWnd,message,wParam,lParam ) ;
}
HWND create_window ( BOOL & ok , int x )
{
WNDCLASSEXA wcx ;
memset( &wcx,0,sizeof( wcx )) ;
wcx.cbSize = sizeof( wcx ) ;
wcx.lpfnWndProc = win32callback ;
wcx.lpszClassName = "spam" ;
RegisterClassExA( &wcx ) ;
HWND hwnd = CreateWindowExA( 0,"spam",NULL,WS_POPUP,x+10,10,500,500,NULL,NULL,NULL,NULL ) ;
ok = ok && SetWindowPos( hwnd,HWND_TOPMOST,x+10,10,500,500,SWP_NOOWNERZORDER|SWP_SHOWWINDOW ) ;
ok = ok && ShowWindow( hwnd,SW_SHOWNORMAL ) ;
return hwnd ;
}
void play_opengl ( HWND hwnd , BOOL & ok )
{
HDC dc = GetWindowDC( hwnd ) ;
PIXELFORMATDESCRIPTOR pfd ;
memset( &pfd,0,sizeof( PIXELFORMATDESCRIPTOR )) ;
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.cDepthBits = 32 ;
pfd.iLayerType = PFD_MAIN_PLANE ;
int PixFormat = ChoosePixelFormat( dc,&pfd ) ;
ok = ok && SetPixelFormat( dc,PixFormat,&pfd ) ;
HGLRC glrc = wglCreateContext( dc ) ;
ok = ok && wglMakeCurrent( dc,glrc ) ;
glViewport( 0,0,500,500 ) ;
glEnable( GL_COLOR_MATERIAL ) ;
glMatrixMode( GL_PROJECTION ) ;
gluPerspective( 70,1,0.1,1000 ) ;
glMatrixMode( GL_MODELVIEW ) ;
glLoadIdentity() ;
glColor3b( 100,50,120 ) ;
glTranslated( 0,0,-20 ) ;
glBegin( GL_TRIANGLES ) ;
glVertex3d( 0,0,0 ) ;
glVertex3d( 5,5,0 ) ;
glVertex3d( 0,5,0 ) ;
glEnd() ;
ok = ok && SwapBuffers( dc ) ;
}
void play_gdi ( HWND hwnd , BOOL & ok )
{
HDC windc = GetWindowDC( hwnd ) ;
HDC dc = CreateCompatibleDC( windc ) ;
HBITMAP bitmap = CreateCompatibleBitmap( windc,500,500 ) ;
HPEN pen = CreatePen( PS_SOLID,1,0xffaa55 ) ;
ok = ok && (SelectObject( dc,bitmap ) != NULL) ;
ok = ok && (SelectObject( dc,pen ) != NULL) ;
for ( int i = 0 ; i < 100 ; i++ )
ok = ok && LineTo( dc,(i&1?i:100-i)*4,(i&1)*400 ) ;
BitBlt( windc,0,0,500,500,dc,0,0,SRCCOPY ) ;
}
void main ()
{
BOOL ok = TRUE ;
HWND hwnd1 = create_window( ok,0 ) ;
HWND hwnd2 = create_window( ok,600 ) ;
play_gdi( hwnd1,ok ) ;
play_opengl( hwnd2,ok ) ;
// sorry I don't clean up anything...
if (ok) printf("\n all correct!\n") ;
printf("\n\n press ENTER") ;
getchar() ;
}