win32 + opengl ?

Windows specific questions.
bluatigro
Posts: 651
Joined: Apr 25, 2012 10:35
Location: netherlands

win32 + opengl ?

Postby bluatigro » May 03, 2012 8:58

this is a try at win32 + opengl
i shoot get a blue blank screen
[ the blue is only there to see if opengl is started right ]

Code: Select all



#include once "windows.bi"
#include once "GL/gl.bi"
#include once "GL/glu.bi"


declare function        WinMain     ( byval hInstance as HINSTANCE, _
                                      byval hPrevInstance as HINSTANCE, _
                                      byval szCmdLine as string, _
                                      byval iCmdShow as integer ) as integer
                                 
declare sub opengl_init( w as integer , h as integer )

end WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )

'':::::
function WndProc ( byval hWnd as HWND, _
                   byval wMsg as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT
   
  function = 0
  dim rct as RECT
  dim pnt as PAINTSTRUCT
  dim hDC as HDC

  select case( wMsg )
    case WM_CREATE     
      GetClientRect( hWnd , @rct )
      opengl_init rct.right , rct.bottom
      exit function
    case WM_PAINT
      hDC = BeginPaint( hWnd, @pnt )
      GetClientRect( hWnd, @rct )
      glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT       

           
      EndPaint( hWnd, @pnt )
      exit function           
    case WM_KEYDOWN
         if( lobyte( wParam ) = 27 ) then
            PostMessage( hWnd, WM_CLOSE, 0, 0 )
         end if
    case WM_DESTROY
      PostQuitMessage( 0 )
      exit function
  end select
  function = DefWindowProc( hWnd, wMsg, wParam, lParam )   
end function

'':::::
function WinMain ( byval hInstance as HINSTANCE, _
                   byval hPrevInstance as HINSTANCE, _
                   byval szCmdLine as string, _
                   byval iCmdShow as integer ) as integer   
     
    dim wMsg as MSG
    dim wcls as WNDCLASS     
    dim hWnd as HWND
     
    function = 0
     
    with wcls
       .style         = CS_HREDRAW or CS_VREDRAW
       .lpfnWndProc   = @WndProc
       .cbClsExtra    = 0
       .cbWndExtra    = 0
       .hInstance     = hInstance
       .hIcon         = LoadIcon( NULL, IDI_APPLICATION )
       .hCursor       = LoadCursor( NULL, IDC_ARROW )
       .hbrBackground = GetStockObject( BLACK_BRUSH )
       .lpszMenuName  = NULL
       .lpszClassName = @"HelloWin"
    end with
         
    if( RegisterClass( @wcls ) = FALSE ) then
       MessageBox( null, "Failed to register wcls", "Error", MB_ICONERROR )
       exit function
    end if
   
    hWnd = CreateWindowEx( 0, _
                       @"HelloWin", _
                           "", _
                           WS_OVERLAPPEDWINDOW or WS_MAXIMIZE , _
                           0 , _
                           0 , _
                           CW_USEDEFAULT, _
                           CW_USEDEFAULT, _
                           NULL, _
                           NULL, _
                           hInstance, _
                           NULL )
                         

    ShowWindow( hWnd, SW_SHOWMAXIMIZED )''iCmdShow )
    UpdateWindow( hWnd )
     
    while( GetMessage( @wMsg, NULL, 0, 0 ) <> FALSE )   
        TranslateMessage( @wMsg )
        DispatchMessage( @wMsg )
    wend
   
    function = wMsg.wParam

end function
sub opengl_init( w as integer , h as integer )
   '' ReSizeGLScene
   glViewport 0, 0, w, h                     '' Reset The Current Viewport
   glMatrixMode GL_PROJECTION                     '' Select The Projection Matrix
   glLoadIdentity                                 '' Reset The Projection Matrix
   gluPerspective 45.0, 640.0/480.0, 0.1, 100.0   '' Calculate The Aspect Ratio Of The Window
   glMatrixMode GL_MODELVIEW                      '' Select The Modelview Matrix
   glLoadIdentity                                 '' Reset The Modelview Matrix
   
   '' All Setup For OpenGL Goes Here
   glShadeModel GL_SMOOTH                         '' Enable Smooth Shading
   glClearColor 0.0, 0.0, 1.0, 1               '' Black Background [ now blue to see if this works ]
   glClearDepth 1.0                               '' Depth Buffer Setup
   glEnable GL_DEPTH_TEST                         '' Enables Depth Testing
   glDepthFunc GL_LEQUAL                          '' The Type Of Depth Testing To Do
   glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST    '' Really Nice Perspective Calculations

  glEnable( gl_lighting )
  dim as single lightpos( 3 ) = { 0 , 100 , 0 , 1 }
  dim as single diffuse( 3 ) = { 1 , 1 , 1 , 1 }
  glLightfv( gl_light0 , gl_position, @lightpos(0) )
  glLightfv( gl_light0 , gl_diffuse , @diffuse(0) )
  glEnable( gl_light0 )
 
end sub
marcov
Posts: 2963
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: win32 + opengl ?

Postby marcov » May 03, 2012 12:39

You nowhere pass to opengl where the opengl window should appear (iow the opengl window has no parent)

I use something like this in Delphi to do that.

Code: Select all

var
 pfd : TPIXELFORMATDESCRIPTOR;
    FRC: HGLRC;
    FDC: HDC;


  FDC := GetDC(Handle); // handle of form

  pfd.nSize := SizeOf(pfd);
  pfd.nVersion := 1;
  pfd.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
  pfd.iPixelType := PFD_TYPE_RGBA;
  pfd.cColorBits := 32;

  pf := ChoosePixelFormat(FDC, @pfd);
  b:=SetPixelFormat(FDC, pf, @pfd);

  FRC := wglCreateContext(FDC);
  b:=wglMakeCurrent(FDC, FRC);
TJF
Posts: 3581
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: win32 + opengl ?

Postby TJF » May 03, 2012 15:56

Why don't you use glut (or freeglut) to create the window?

Find an example here

matwachich
Posts: 70
Joined: Jun 11, 2011 3:36

Re: win32 + opengl ?

Postby matwachich » May 03, 2012 21:18

Or GLFW, that is (i think) easier and fast learned.

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 5 guests