0

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).

GeneticNeuralNetwork

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() ;
    }
Community
  • 1
  • 1
Captain'Flam
  • 479
  • 4
  • 12
  • 1
    How about adding some error handling to see if any of the calls you make fail? And if they do, let us know *how/with what errors*. Right now your code just ignores all (potential) errors and just forges on regardless. Also try running it step by step in your debugger and verify that (return, and other) values and control flow matches what you expect - and tell us how/where it deviates, if it doesn't match your expectations. – Jesper Juhl Jun 24 '19 at 17:51
  • 1. You're missing a message loop, 2. void main() is wrong, 3. ShowWindow(hwnd,TRUE) is also wrong, 4. DefWindowProc is wrong when using RegisterClassExA, 4. This is **not** minimal. All these OpenGL calls are superfluous. – Axalo Jun 24 '19 at 18:10
  • Thank you to both of you : I updated my code in order to display GDI and OpenGL (just to check the window creation), test all functions return (I did it already while debugging), correct some parameters and simplify my OpenGL calls. Unfortunately it didn't change anything... – Captain'Flam Jun 25 '19 at 14:58
  • You are still missing a [message loop](https://learn.microsoft.com/en-us/windows/desktop/winmsg/using-messages-and-message-queues#creating-a-message-loop) which is required. From the docs: `If the thread creates one or more windows, a message loop must be provided`. Also read [this](https://stackoverflow.com/q/204476) about the use of void main(). – Axalo Jun 25 '19 at 16:51
  • Thanks @Axalo, I edited my code with a proper signature of _main_ and add a _message loop_, but the result is always the same... I think it's a more basic/annoying problem because I found out that it works fine on an other PC with the same Win10+VS2017! Maybe some graphic driver issue... This is completely changing significance of this post. Maybe I should rewrite the question or post it as a new one... – Captain'Flam Jun 26 '19 at 12:58
  • Compile [this code](https://www.codeproject.com/Articles/215690/Minimal-WinApi-Window). If there's no window showing up something is wrong with your operating system or compiler. If it does work try to figure out what your code does differently. – Axalo Jun 26 '19 at 14:44

1 Answers1

1

What you're talking about is that ghost window are created by the following code.

for ( int z = -n ; z <= n ; z++ )
        for ( int y = -n ; y <= n ; y++ )
            for ( int x = -n ; x <= n ; x++ )
                {
                printf(" %d,%d,%d%s",x,y,z,x?"":"\n") ;
                SwapBuffers( dc ) ;
                glTranslated( x,y,z ) ;
                gluSphere( quadric,4,5,5 ) ;
                glTranslated( -x,-y,-z ) ;
                }

I don't know what window you want to render with this code, but that's the case. Your for loop has been looping around the sphere matrix.

I'm trying to replace some of your code with mine for testing purposes.

#include <windows.h>
#include <cstdio>
#include <math.h>
#include <GL/GLU.h>

#pragma comment (lib,"Glu32.lib")
#pragma comment (lib,"Opengl32.lib")

void draw();

int width, height;      // the desired width and height of the CLIENT AREA
HDC dc;

static LRESULT CALLBACK win32callback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hWnd, message, wParam, lParam);
}
void main()
{
    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, 10, 10, 500, 500, NULL, NULL, NULL, NULL);
    SetWindowPos(hwnd, HWND_TOPMOST, 10, 10, 800, 500, SWP_NOOWNERZORDER | SWP_SHOWWINDOW);
    ShowWindow(hwnd, TRUE);
    SetFocus(hwnd);

    dc = GetWindowDC(hwnd);

    RECT rect;
    SetRect(&rect, 50,  // left
        50,  // top
        850, // right
        650); // bottom

// Save width and height off.
    width = rect.right - rect.left;
    height = rect.bottom - rect.top;

    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 = 24;
    int PixFormat = ChoosePixelFormat(dc, &pfd);

    PIXELFORMATDESCRIPTOR bestMatch_pfd;
    DescribePixelFormat(dc, PixFormat, sizeof(pfd), &bestMatch_pfd);
    SetPixelFormat(dc, PixFormat, &pfd);

    HGLRC glrc = wglCreateContext(dc); 
    wglMakeCurrent(dc, glrc); 


    while (1)
    {
        draw();
    }    
    DestroyWindow(hwnd);
    wglMakeCurrent(0, 0);
    wglDeleteContext(glrc);
}

void draw()
{
    // 1. set up the viewport
    glViewport(0, 0, g.width, g.height); // set viewport

    // 2. projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (float)g.width / (float)g.height, 1, 1000);

    // 3. viewing transformation
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(0, 0, 10,
        0, 0, 0,
        0, 1, 0);

    // 4. modelling transformation and drawing
    glClearColor(0.5, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    static float i = 0.01f;


    i += 0.001f;     // increase i by 0.001 from its
    // it had on the LAST FUNCTION CALL to the draw() function

    float c = cos(i);
    float s = sin(i);

    glBegin(GL_TRIANGLES);
    glColor3f(c, 0, 0);      // red
    glVertex3f(1 + c, 0 + s, 0);

    glColor3f(c, s, 0);      // yellow
    glVertex3f(0 + c, 1 + s, 0);

    glColor3f(s, 0.1f, s);   // magenta
    glVertex3f(-1 + c, 0 + s, 0);
    glEnd();

    //7.  SWAP BUFFERS.
    SwapBuffers(dc);

}

1

You can see that it works very well, my environment configuration:VS2017 Window 10

Updated:

2

Try linking "Glu32.lib" and "Opengl32.lib"

Updated:

Try the following two methods:

  1. The graphics card does not support the current OpenGL version, download EVEREST Ultimate Edition, click on the display device > OpenGL in turn after installation and operation to check whether it supports the relevant information, if not, you can only replace the graphics card or replace it with the supported OpenGL version to solve the problem.

  2. The graphics card driver is not supported. Updating the graphics card driver is enough.

Strive Sun
  • 5,988
  • 1
  • 9
  • 26
  • Thanks a lot for your effort! I tried your code (just suppressed the "`g.`"). But the result is the same as with mine: no window appeared... I must be something in the configuration of my new PC. I tried "**OpenGL Extensions Viewer 5.1**": it tells me that I have OpenGL 4.5. It should be enough for this very simple demo :-( – Captain'Flam Jun 25 '19 at 15:06
  • @Captain'Flam I made some deletions before publishing the code. I didn't define variables correctly. I've corrected them. – Strive Sun Jun 26 '19 at 00:42
  • @Captain'Flam You don't seem to link to "Glu32.lib" and "Opengl32.lib",please see my updated answer. – Strive Sun Jun 26 '19 at 02:34
  • Thanks again _Strive Sun_ ! About the libs : I use the old school way to link them (from the project options). By the way, if this was the obstacle, I got some link error during compilation... But WOW! how do you get a GIF from screen capture? – Captain'Flam Jun 26 '19 at 12:41
  • About the main problem (which is not linking nor triangles nor spheres) I think it's more serious : It works fine on an other PC with the same configuration. Maybe some graphic driver problem. This is really annoying because I'm a developer, not an IT guy... – Captain'Flam Jun 26 '19 at 12:49
  • @Captain'Flam You can use the ScreenToGif tool to get screen images. In addition, maybe you can update the graphics card driver. – Strive Sun Jun 27 '19 at 01:41
  • Thanks *Strive Sun*! please have a look on the edited question. if it is not removed... – Captain'Flam Jun 27 '19 at 08:52
  • @Captain'Flam hi, I'm glad to be able to help you solve the problem. I've seen the question you edited. In fact, if you have a new question, you can post a new post to ask.In this way, more people will see your post and give you the fastest help. – Strive Sun Jun 27 '19 at 09:05