请教各位如何在vc6.0中创建opengl窗口?最好是在win32平台上的
第一步:
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#pragma comment( lib, "opengl32.lib") // OpenGL32连接库
#pragma comment( lib, "glu32.lib") // GLu32连接库
#pragma comment( lib, "glaux.lib") // GLaux连接库
第二步:
case WM_CREATE:
hDC = GetDC( hWnd );
SetupPixelFormat( hDC );
hRC = wglCreateContext( hDC );
wglMakeCurrent( hDC, hRC );
break;
case WM_CLOSE:
wglMakeCurrent( hDC, NULL );
wglDeleteContext( hRC );
PostQuitMessage(0);
break;
case WM_SIZE:
Height = HIWORD( lParam );
Width = LOWORD( lParam );
SetupProjection( Width, Height );
break;
第三步:
while( !bExiting )
{
Render();
SwapBuffers(hDC );
while( PeekMessage( &msg, NULL, 0,0, PM_NOREMOVE ))
{
if( !GetMessage( &msg, NULL, 0,0 ))
{
bExiting = true;
break;
}
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
其中所用到的自定义函数如下:
void SetupPixelFormat( HDC hDC )
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof( PIXELFORMATDESCRIPTOR ),
1,
PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0,
};
int pixelformat;
if((pixelformat = ChoosePixelFormat(m_hDc, &pfd)) == 0)
{
MessageBox( "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}
if(SetPixelFormat(m_hDc, pixelformat, &pfd) == FALSE)
{
MessageBox( "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}
}
void SetupProjection( int Width, int Height )
{
if( Height == 0 )
{
Height = 1;
}
glViewport( 0, 0, Width, Height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 52.0f, (GLfloat)Width/(GLfloat)Height, 1.0, 1000.0f );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void Render()
{
glClearColor(0.7f, 0.7f, 0.9f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
}
[此贴子已经被作者于2006-12-31 19:53:18编辑过]