0

I do a simple draw in opengl on a Win32 Window. I know how to clear the borders and its good to see but when i draw in OpenGL on this window I have a black background. I tested a lot things find on the web but none works. I show you the code and maybe someone will know why ?

#include <iostream>
#include <windows.h>
#include <gl\gl.h>

LRESULT CALLBACK WindowProcedure( HWND window, UINT message, WPARAM wParam,     LPARAM lParam ) {
if ( message ) {     
    return DefWindowProc( window, message, wParam, lParam );
}

return 0;
}

int main()
{
POINT  ptVertex[4]; 
HDC    hDC = NULL;      
HGLRC  hRC = NULL;      
GLuint PixelFormat;

WNDCLASSEX wincl;    

wincl.hInstance = 0;
wincl.lpszClassName = "Window";
wincl.lpfnWndProc = WindowProcedure;    
wincl.style = CS_HREDRAW | CS_VREDRAW;       
wincl.cbSize = sizeof (WNDCLASSEX);

wincl.hIcon        = NULL;
wincl.hIconSm      = NULL;
wincl.hCursor      = NULL;
wincl.lpszMenuName = NULL;               

wincl.cbClsExtra = 0;                  
wincl.cbWndExtra = 0;                 

wincl.hbrBackground = HBRUSH(COLOR_WINDOW);

if( RegisterClassEx( &wincl ) )
{
    HWND window = CreateWindowEx( WS_EX_LAYERED, "Window", 0,
               WS_OVERLAPPED | WS_POPUP, 0, 0,
               200, 200, 0, 0, 0, 0 ) ;
    if( window )
    {
        ptVertex[0].x = 0;
        ptVertex[0].y = 0;
        ptVertex[1].x = 0;
        ptVertex[1].y = 200;
        ptVertex[2].x = 200;
        ptVertex[2].y = 200;
        ptVertex[3].x = 200;
        ptVertex[3].y = 0;

        SetWindowLong( window, GWL_EXSTYLE, GetWindowLong( window, GWL_EXSTYLE ) | WS_EX_LAYERED );
        SetLayeredWindowAttributes(window, 0x0, 0, LWA_COLORKEY);
        HRGN region = CreatePolygonRgn( ptVertex, 4, ALTERNATE );
        SetWindowRgn( window, region, false );
        ShowCursor( false );

        hDC = GetDC( window );

    PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
8,0, 8,8, 8,16, 8,24, //A guess for lil endian; usually these are 0 (making them 0 doesn't help)
0, 0,0,0,0,
24, //depth
8,  //stencil
0,  //aux
PFD_MAIN_PLANE,
0,
0, 0, 0
};

        PixelFormat=ChoosePixelFormat( hDC, &pfd );
        SetPixelFormat( hDC, PixelFormat, &pfd );
        hRC = wglCreateContext( hDC );
        wglMakeCurrent( hDC, hRC )
        ShowWindow( window, SW_SHOWDEFAULT ) ;

glColor3f(0, 1, 1);
glBegin(GL_TRIANGLES);                              // Drawing Using Triangles
    glColor3f(1.0f,0.0f,0.0f);                      // Set The Color To Red
    glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top
    glColor3f(0.0f,1.0f,0.0f);                      // Set The Color To Green
    glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
    glColor3f(0.0f,0.0f,1.0f);                      // Set The Color To Blue
    glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
glEnd();

SwapBuffers( hDC );

        MSG msg ;

        for( ;; ) {
            if ( GetMessage( &msg, 0, 0, 0 ) )
                DispatchMessage( &msg ) ;

        }
    }
}
}
user3511595
  • 77
  • 2
  • 7
  • 1
    "*when i draw in OpenGL on this window I have a black background*" So... what kind of background do you want? Please explain the problem more thoroughly. – Nicol Bolas Jun 24 '16 at 15:37
  • 1
    Assuming you want to have a transparent background this might be relevant to your questions: http://stackoverflow.com/questions/4052940/how-to-make-an-opengl-rendering-context-with-transparent-background – Floris Velleman Jun 24 '16 at 15:39
  • I would like to have a transparent background – user3511595 Jun 24 '16 at 16:25

0 Answers0