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

  
// 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( &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( &msg, NULL, 0, 0, PM_REMOVE ) )
            {
                  // handle or dispatch messages
                  if ( msg.message == WM_QUIT )
                  {
                        bQuit = TRUE;
                  }
                  else
                  {
                        TranslateMessage( &msg );
                        DispatchMessage( &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, &hDC, &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( &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, &pfd );
      SetPixelFormat( *hDC, iFormat, &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 );
} 

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.

Hello
The problem is with the line

       
   if (angle >= 360.0f)
            angle = 0.0f;
         glTranslatef(0.0f, 0.0f, -5.0f);
 

It should be

 
         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.

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.

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

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