setup openGL 3.0 with window

hello all
i read hehe lesson 01 to set up opengl 3.0 with windows (api)
but the code is very long so i try to write simple code.
this is the code.

  • what you need ?
    GL3/gl3.h

#include <windows.h>
#include "gl.h"
#include <GL/glu.h>
#include "glxext.h"
#include "glext.h"
#include "wglext"

HDC hDC=NULL; //GDI Device Context
HGLRC hRC=NULL; //Permanent Rendering Context
HWND hWnd=NULL; //Holds Our Window Handle
HINSTANCE hInstance; //Holds The Instance Of The Application

/* WndProc is the Windows event handling routine
	 * hWnd - the window handle
	 * uMsg - event message for this window
	 * wParam, lParam - additional information depending on the message
*/

LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

/*	This method creates our OpenGL window.  Parameters are:					*
	 *	title			- Title To Appear At The Top Of The Window			*
	 *	width			- Width Of The GL Window Or Fullscreen Mode			*
	 *	height			- Height Of The GL Window Or Fullscreen Mode			*
	 *	bits			- Number Of Bits To Use For Color (8/16/24/32)			*
	 *	fullscreenflag	- Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)	*/

void CreateGLWindo(char *title,int width,int height,int bits);
void InitGL();
void Render();
void ShutDown();


int CreateGLWindow(char *title,int width,int hieght,int bits)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
RECT WindowRect; //// Grabs Rectangle Upper Left / Lower Right Values
// Set Left Value To 0
// Set Right Value To Requested Width
// Set Top Value To 0
// Set Bottom Value To Requested Height
WindowRect.left=(long)0;
WindowRect.right=(long)width;
WindowRect.top=(long)0;
WindowRect.bottom=(long)hieght;

// Grab An Instance For Our Window
// Redraw On Move, And Own DC For Window
// WndProc Handles Messages - Trick here!
// No Extra Window Data
// No Extra Window Data
// Set The Instance
// Load The Default Icon
// Load The Arrow Pointer
// No Background Required For GL
// We Don't Want A Menu
// Set The Class Name
hInstance =GetModuleHandle(NULL);
wc.style =CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
wc.lpfnWndProc=(WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";

if(!RegisterClass(&wc))	// Attempt To Register The Window Class
	{
		MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;	// Exit And Return FALSE
	}
AdjustWindowRectEx(&WindowRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);  // Adjust Window To True Requested Size
hWnd=CreateWindowEx(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
       "OpenGL",
       title,
       WS_OVERLAPPEDWINDOW |
       WS_CLIPSIBLINGS |
       WS_CLIPCHILDREN,
       0, 0,
       WindowRect.right-WindowRect.left,
       WindowRect.bottom-WindowRect.top,
       NULL,
       NULL,
       hInstance,
       NULL);
 hDC=GetDC(hWnd);
/******************** important *******************/
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 = 16;
   pfd.cDepthBits = 16;


    PixelFormat=ChoosePixelFormat(hDC,&pfd);
    SetPixelFormat(hDC,PixelFormat,&pfd);
/****************************************************/

	HGLRC tempContext = wglCreateContext(hDC);
	wglMakeCurrent(hDC,tempContext);
	int attribs[] = {
		 WGL_CONTEXT_MAJOR_VERSION_ARB, 3,//we want a 3.0 context
		 WGL_CONTEXT_MINOR_VERSION_ARB, 0,
		 //and it shall be forward compatible so that we can only use up to date functionality
		 WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
	0}; //zero indicates the end of the array

	PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL; //pointer to the method
	wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB");
	if(wglCreateContextAttribsARB == NULL) //OpenGL 3.0 is not supported
	{
		MessageBox(NULL,"Cannot get Proc Adress for CreateContextAttribs", "ERROR",MB_OK);
		Destroy();
		wglDeleteContext(tempContext);
		return false;
	}

	if (!(m_hRC=wglCreateContextAttribsARB(m_hDC,0, attribs)))
	{
		wglDeleteContext(tempContext);
		Destroy();								// Reset The Display
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;								// Return false
	}
	wglDeleteContext(tempContext);

	if(!wglMakeCurrent(m_hDC,m_hRC))						// Try To Activate The Rendering Context
	{
		Destroy();							// Reset The Display
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// Return FALSE
	}

/*****************************************************/
    ShowWindow(hWnd,SW_SHOW);
    SetForegroundWindow(hWnd);
    SetFocus(hWnd);
    ShowCursor(FALSE);
    return 0;
};

void ReShape(GLsizei width,GLsizei height)
{
 if (height==0)
    {
 height=1;
    }

    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
};
void InitGL()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnable(GL_DEPTH_TEST);
};
void ShutDown( void )
{

    if(  hRC != NULL )
    {
 wglMakeCurrent( NULL, NULL );
 wglDeleteContext(  hRC );
  hRC = NULL;
    }

    if( hDC != NULL )
    {
 ReleaseDC( hWnd, hDC );
 hDC = NULL;
    }
};
void Render()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
   glLoadIdentity();
};
int WINAPI WinMain(    HINSTANCE hInstance_,HINSTANCE hPrevInstance,
LPSTR    lpCmdLine,int nCmdShow)
{
    MSG  msg;
    BOOL    done=FALSE;

    CreateGLWindow("OpenGL Framework",640,480,16  );
    ReShape(640,800);
    InitGL();

    while(!done)
    {
  PeekMessage(&msg,NULL,0,0,PM_REMOVE);

     if (msg.message==WM_QUIT)
     {
   done=TRUE;
     }
     else
     {

     Render();
     SwapBuffers(hDC);


     TranslateMessage(&msg);
     DispatchMessage(&msg);
     }





    }

 ShutDown();
    return (msg.wParam);

}
LRESULT CALLBACK WndProc( HWND  hWnd, UINT  msg,WPARAM wParam, LPARAM lParam )
{

    switch( msg )
	{

        case WM_KEYDOWN:
		{
			if( wParam ==VK_ESCAPE )
			 	PostQuitMessage(0);

		}
        break;

 		case WM_SIZE:
		{
			/* 
			   HIWORD(lParam) 
			   LOWORD(lParam) 
			*/

			ReShape(LOWORD(lParam),HIWORD(lParam));
		}
		break;

		case WM_CLOSE:
        case WM_DESTROY:
		{
            PostQuitMessage(0);
		}
        break;

		default:
		{
			return DefWindowProc( hWnd, msg, wParam, lParam );
		}
		break;
	}

	return 0;
}


you can try it end modfied .
bey

hello
i am sorry the code make error so i fixed it
end this is a good code end word 100%


#include <windows.h>
#include <GL/glu.h>
#include "wglext.h"
#include "gl.h"
#include <stdlib.h>

typedef HGLRC (APIENTRYP PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int * attribList);
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
#define WGL_CONTEXT_FLAGS_ARB 0x2094
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002

HDC hDC=NULL; //GDI Device Context
HGLRC hRC=NULL; //Permanent Rendering Context
HWND hWnd=NULL; //Holds Our Window Handle
HINSTANCE hInstance; //Holds The Instance Of The Application

/* WndProc is the Windows event handling routine
	 * hWnd - the window handle
	 * uMsg - event message for this window
	 * wParam, lParam - additional information depending on the message
*/

LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

/*	This method creates our OpenGL window.  Parameters are:					*
	 *	title			- Title To Appear At The Top Of The Window			*
	 *	width			- Width Of The GL Window Or Fullscreen Mode			*
	 *	height			- Height Of The GL Window Or Fullscreen Mode			*
	 *	bits			- Number Of Bits To Use For Color (8/16/24/32)			*
	 *	fullscreenflag	- Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)	*/

void CreateGLWindo(char *title,int width,int height,int bits);
void InitGL();
void Render();
void ShutDown();


int CreateGLWindow(char *title,int width,int hieght,int bits)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
RECT WindowRect; //// Grabs Rectangle Upper Left / Lower Right Values
// Set Left Value To 0
// Set Right Value To Requested Width
// Set Top Value To 0
// Set Bottom Value To Requested Height
WindowRect.left=(long)0;
WindowRect.right=(long)width;
WindowRect.top=(long)0;
WindowRect.bottom=(long)hieght;

// Grab An Instance For Our Window
// Redraw On Move, And Own DC For Window
// WndProc Handles Messages - Trick here!
// No Extra Window Data
// No Extra Window Data
// Set The Instance
// Load The Default Icon
// Load The Arrow Pointer
// No Background Required For GL
// We Don't Want A Menu
// Set The Class Name
hInstance =GetModuleHandle(NULL);
wc.style =CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
wc.lpfnWndProc=(WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";

if(!RegisterClass(&wc))	// Attempt To Register The Window Class
	{
		MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;	// Exit And Return FALSE
	}
AdjustWindowRectEx(&WindowRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);  // Adjust Window To True Requested Size
hWnd=CreateWindowEx(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
       "OpenGL",
       title,
       WS_OVERLAPPEDWINDOW |
       WS_CLIPSIBLINGS |
       WS_CLIPCHILDREN,
       0, 0,
       WindowRect.right-WindowRect.left,
       WindowRect.bottom-WindowRect.top,
       NULL,
       NULL,
       hInstance,
       NULL);
 hDC=GetDC(hWnd);
/******************** important *******************/
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 = 16;
   pfd.cDepthBits = 16;


    PixelFormat=ChoosePixelFormat(hDC,&pfd);
    SetPixelFormat(hDC,PixelFormat,&pfd);
/****************************************************/

	HGLRC tempContext = wglCreateContext(hDC);
	wglMakeCurrent(hDC,tempContext);
	int attribs[] = {
		 WGL_CONTEXT_MAJOR_VERSION_ARB, 3,//we want a 3.0 context
		 WGL_CONTEXT_MINOR_VERSION_ARB, 0,
		 //and it shall be forward compatible so that we can only use up to date functionality
		 WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
	0}; //zero indicates the end of the array

	PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL; //pointer to the method
	wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB");
	if(wglCreateContextAttribsARB == NULL) //OpenGL 3.0 is not supported
	{
		MessageBox(NULL,"Cannot get Proc Adress for CreateContextAttribs", "ERROR",MB_OK);
        ShutDown();
		wglDeleteContext(tempContext);
		return FALSE;
	}

	if (!(hRC=wglCreateContextAttribsARB(hDC,0, attribs)))
	{
		wglDeleteContext(tempContext);
		 ShutDown();								// Reset The Display
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return false
	}
	wglDeleteContext(tempContext);

	if(!wglMakeCurrent(hDC,hRC))						// Try To Activate The Rendering Context
	{
		 ShutDown();						// Reset The Display
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// Return FALSE
	}

/*****************************************************/
    ShowWindow(hWnd,SW_SHOW);
    SetForegroundWindow(hWnd);
    SetFocus(hWnd);
    ShowCursor(FALSE);
    return 0;
};

void ReShape(GLsizei width,GLsizei height)
{
 if (height==0)
    {
 height=1;
    }

    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
};
void InitGL()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnable(GL_DEPTH_TEST);
};
void ShutDown( void )
{

    if(  hRC != NULL )
    {
 wglMakeCurrent( NULL, NULL );
 wglDeleteContext(  hRC );
  hRC = NULL;
    }

    if( hDC != NULL )
    {
 ReleaseDC( hWnd, hDC );
 hDC = NULL;
    }
};
void Render()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
   glLoadIdentity();
};
int WINAPI WinMain(    HINSTANCE hInstance_,HINSTANCE hPrevInstance,
LPSTR    lpCmdLine,int nCmdShow)
{
    MSG  msg;
    BOOL    done=FALSE;

    CreateGLWindow("OpenGL Framework",640,480,16  );
    ReShape(640,800);
    InitGL();

    while(!done)
    {
  PeekMessage(&msg,NULL,0,0,PM_REMOVE);

     if (msg.message==WM_QUIT)
     {
   done=TRUE;
     }
     else
     {

     Render();
     SwapBuffers(hDC);


     TranslateMessage(&msg);
     DispatchMessage(&msg);
     }





    }

 ShutDown();
    return (msg.wParam);

}
LRESULT CALLBACK WndProc( HWND  hWnd, UINT  msg,WPARAM wParam, LPARAM lParam )
{

    switch( msg )
	{

        case WM_KEYDOWN:
		{
			if( wParam ==VK_ESCAPE )
			 	PostQuitMessage(0);

		}
        break;

 		case WM_SIZE:
		{
			/* 
			   HIWORD(lParam) 
			   LOWORD(lParam) 
			*/

			ReShape(LOWORD(lParam),HIWORD(lParam));
		}
		break;

		case WM_CLOSE:
        case WM_DESTROY:
		{
            PostQuitMessage(0);
		}
        break;

		default:
		{
			return DefWindowProc( hWnd, msg, wParam, lParam );
		}
		break;
	}

	return 0;
}

bey