简体   繁体   English

OpenGL + Visual Studio 2017:未显示渲染窗口(Visual 2010 没问题)

[英]OpenGL + Visual Studio 2017 : rendering window not displayed (was OK with Visual 2010)

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).有关信息,提到的驱动程序是我更新到版本 25.20.10.6472 (10/12/2018) 的“Inter(R) UHD Graphics 620”。

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).使用ScreenToGif (感谢Strive Sun )如下所示:主(OpenGL)窗口是某种太空船(带有彩色助推器)的外部视图,右下角的(GDI)窗口是视图来自正在学习吃绿色口香糖并避免红色口香糖的 AI 飞行员)。

遗传神经网络

If you, Mr StackOverflow , think this kind of rambling is not appropriate, please remove it... StackOverflow先生,如果你觉得这种乱七八糟的东西不合适,请删掉它……

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.我在 windows 下有一小部分 OpenGl 代码示例,可以在 windows 7 + Visual Studio 2010 上正常工作。但是在我装有 windows 10 + visual studio 2017 的新电脑上,没有显示 OpenGL 渲染窗口。 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/...).它仅使用标准 win API (gl.h/glu.h) 提供的基本原生 gdi+opengl 函数,但不使用任何其他库(如 glew/glut/freeglut/...)。

With Windows7+VS2010 I have 3 windows: a GDI rendering, an OpenGL rendering and a console.使用 Windows7+VS2010 我有 3 个窗口:一个 GDI 渲染、一个 OpenGL 渲染和一个控制台。

  • On the first: some crossing blue lines (using " LineTo ").第一个:一些交叉的蓝线(使用“ LineTo ”)。
  • On the second: one violet triangle (using " glVertex3d ").第二个:一个紫色三角形(使用“ glVertex3d ”)。
  • On the console a message telling me that all functions well returned.在控制台上有一条消息告诉我所有的功能都很好地返回。

With Windows10+VS2017, the OpenGL window is not visible.使用Windows10+VS2017,OpenGL窗口不可见。 I don't even see a ghost when I play with alt-tab keys.当我使用 alt-tab 键时,我什至没有看到鬼魂。

I suppose that something has changed in the API, but I can't figure out what...我想 API 中发生了一些变化,但我不知道是什么......

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() ;
    }

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.您的 for 循环一直在球体矩阵周围循环。

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可以看到效果很好,我的环境配置:VS2017 Window 10

Updated:更新:

2

Try linking "Glu32.lib" and "Opengl32.lib"尝试链接“Glu32.lib”和“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.显卡不支持当前OpenGL版本,下载EVEREST旗舰版,安装运行后依次点击显示设备> OpenGL查看是否支持相关信息,如果不支持只能更换显卡或更换它与支持的OpenGL版本来解决问题。

  2. The graphics card driver is not supported.不支持显卡驱动程序。 Updating the graphics card driver is enough.更新显卡驱动就够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM