Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Code just does not work.

  1. #1
    Guest

    Code just does not work.

    I've just started to learn openGL. I bought this big thick book that should show me how to make it all work. The problem is that I can't get this first script to work. Aparently, it's supposed to display a rotating triangle in a Windows display. This is what I have. Any help will be really helpful
    Code :
    // Includes
     
    #include <windows.h>
    #include <gl/gl.h>
     
    // Function Declarations
     
    LRESULT CALLBACK WndProc( HWND hWnd,
                              UINT message,
                              WPARAM wParam,
                              LPARAM lParam );
     
    VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC );
    VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC );
     
    HDC g_HDC;
     
    // WinMain
    int WINAPI WinMain( HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine,
                        int iCmdShow )
    {
          WNDCLASS wc;
          HWND hWnd;
          HDC hDC;
          HGLRC hRC;
          MSG msg;
          BOOL bQuit = FALSE;
          float angle = 0.0f;
     
          // register window class
          wc.style         = CS_HREDRAW | CS_VREDRAW;
          wc.lpfnWndProc   = WndProc;
          wc.cbClsExtra    = 0;
          wc.cbWndExtra    = 0;
          wc.hInstance     = hInstance;
          wc.hIcon         = LoadIcon( NULL, IDI_APPLICATION );
          wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
          wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
          wc.lpszMenuName  = NULL;
          wc.lpszClassName = "GLSample";
          RegisterClass( &amp;wc );
     
          // create main window
          hWnd = CreateWindow( "GLSample",
                               "My OpenGL Program",
                               WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
                               100, 100, 400, 400,
                               NULL, NULL, hInstance, NULL );
     
          ShowWindow(hWnd, SW_SHOW);
          UpdateWindow(hWnd);
     
          // program main loop
          while ( !bQuit )
          {
                // check for messages
                if ( PeekMessage( &amp;msg, NULL, 0, 0, PM_REMOVE ) )
                {
                      // handle or dispatch messages
                      if ( msg.message == WM_QUIT )
                      {
                            bQuit = TRUE;
                      }
                      else
                      {
                            TranslateMessage( &amp;msg );
                            DispatchMessage( &amp;msg );
                      }
                }
                else
                {
                      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                      glLoadIdentity();
     
                      angle = angle + 0.1f;
                      if (angle >= 360.0f)
                            angle = 0.0f;
                      glTranslatef(0.0f, 0.0f, -5.0f);
                      glRotatef(angle, 0.0f, 0.0f, 1.0f);
     
                      glColor3f(1.0f, 0.0f, 0.0f);
                      glBegin(GL_TRIANGLES);
                            glVertex3f(0.0f, 0.0f, 0.0f);
                            glVertex3f(1.0f, 0.0f, 0.0f);
                            glVertex3f(1.0f, 1.0f, 0.0f);
                      glEnd();
     
                      SwapBuffers(g_HDC);
                }
          }
     
          // shutdown OpenGL
          DisableOpenGL( hWnd, hDC, hRC );
          // destroy the window explicitly
          DestroyWindow( hWnd );
          return msg.wParam;
     
    }
     
    // Window Procedure
    LRESULT CALLBACK WndProc( HWND hWnd,
                              UINT message,
                              WPARAM wParam,
                              LPARAM lParam )
    {
          static HGLRC hRC;
          static HDC   hDC;
     
          switch( message )
          {
                case WM_CREATE:
                     hDC = GetDC(hWnd);
                     g_HDC = hDC;
                     // enable OpenGL for the window
                     EnableOpenGL( hWnd, &amp;hDC, &amp;hRC );
                     hRC = wglCreateContext(hDC);
                     wglMakeCurrent(hDC, hRC);
                     return 0;
                     break;
                case WM_CLOSE:
                     wglMakeCurrent(hDC, NULL);
                     wglDeleteContext(hRC);
                     PostQuitMessage( 0 );
                     return 0;
                     break;
                case WM_KEYDOWN:
                     switch ( wParam )
                     {
                           case VK_ESCAPE:
                                 PostQuitMessage( 0 );
                                 break;
                     }
                     break;
                default:
                      return DefWindowProc( hWnd, message, wParam, lParam );
          }
    }
     
    // Enable OpenGL
    VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
    {
          PIXELFORMATDESCRIPTOR pfd;
          int iFormat;
     
          // get the device context (DC)
          *hDC = GetDC( hWnd );
     
          // set the pixel format for the DC
          ZeroMemory( &amp;pfd, sizeof( pfd ) );
          pfd.nSize      = sizeof( pfd );
          pfd.nVersion   = 1;
          pfd.dwFlags    = PFD_DRAW_TO_WINDOW |
                           PFD_SUPPORT_OPENGL |
                           PFD_DOUBLEBUFFER;
          pfd.cColorBits = 24;
          pfd.cDepthBits = 16;
          pfd.iLayerType = PFD_MAIN_PLANE;
          iFormat        = ChoosePixelFormat( *hDC, &amp;pfd );
          SetPixelFormat( *hDC, iFormat, &amp;pfd );
     
          // create and enable the render context (RC)
          *hRC = wglCreateContext( *hDC );
          wglMakeCurrent( *hDC, *hRC );
    }
     
    // Disable OpenGL
    VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
    {
          wglMakeCurrent( NULL, NULL );
          wglDeleteContext( hRC );
          ReleaseDC( hWnd, hDC );
    }

  2. #2
    Guest

    Re: Code just does not work.

    To be more specific about the proplem, when I run the program, a window pops up with a black background, but there's nothing inside.

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2004
    Location
    india
    Posts
    20

    Re: Code just does not work.

    Hello
    The problem is with the line
    Code :
       if (angle >= 360.0f)
                angle = 0.0f;
             glTranslatef(0.0f, 0.0f, -5.0f);
    It should be
    Code :
             if (angle >= 360.0f)
                angle = 0.0f;
             glTranslatef(0.0f, 0.0f, 0.0f);
    It is is going outside the default model volume.

    regards
    sophia.
    Sophia.

  4. #4
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Code just does not work.

    There are also various problems with the HDC usage. You should only GetDC() in WM_CREATE, store it in g_hDC, and always use g_hDC throughout the program. Especially the GetDC() in EnableOpenGL() is obsolete.
    Every GetDC() should be paired with a ReleaseDC() if you want your code to be clean, the WM_CLOSE message should release g_hDC again.

  5. #5
    Intern Contributor
    Join Date
    Mar 2002
    Location
    Figueira da Foz, Portugal
    Posts
    84

    Re: Code just does not work.

    Shouldn't he have to add CS_OWNDC to the window class style to keep the DC?

  6. #6
    Guest

    Re: Code just does not work.

    Wow! Thank you so much for your replys. It was moving outside the default model volume.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •