OpenGL

         

Windows


Setting Up An OpenGL Window

, , . , - Visual C++. , C++, OpenGL.

Visual C++, OpenGL. Project/setting, LINK. "Object/Library Modules" "OpenGL32.lib GLu32.lib GLaux.lib". OK. OpenGL.

, , . :

#include <windows.h>// Windows

#include <gl\gl.h>// OpenGL32

#include <gl\glu.h>// GLu32

#include <gl\glaux.h>// GLaux

, , . OpenGL , . , - , OpenGL, .

, OpenGL Windows. OpenGL hRC. , Windows, hDC. DC GDI. RC OpenGL DC.

static HGLRC hRC;//

static HDC hDC;// GDI



, , , . , . .


BOOLkeys[256];//
OpenGL. , , , , , . , OpenGL .
GLvoid InitGL(GLsizei Width, GLsizei Height)// GL

{
, . , , , . 0.0f 1.0f, 0.0 , 1.0 . glClearColor - , , . 1.0f, . - . , . 0.0f. , .
, glClearColor(0.0f,0.0f,1.0f,0.0f) , . glClearColor(0.5f,0.0f,0.0f,0.0f) . (1.0f) (0.0f), . , (1.0f). - (0.0f).
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//
. . , . , OpenGL, 3D . , . OpenGL.


glClearDepth(1.0);//

glDepthFunc(GL_LESS);//

glEnable(GL_DEPTH_TEST);//
( - antialiasing)( ) . , . . 45 . 0.1f, 100.0f .
glMatrixMode(GL_PROJECTION) , . glLoadIdentity() . , . glMatrixMode(GL_MODELVIEW) , . , - , . , , .
glShadeModel(GL_SMOOTH);//

glMatrixMode(GL_PROJECTION);//

glLoadIdentity();//

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);

//

glMatrixMode(GL_MODELVIEW);//

}
, . , OpenGL , (, , , ). (, ), , . , , , .


GLvoid ReSizeGLScene(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);//

}
. , , . , . OpenGL, OpenGL, glLoadIdentity(). OpenGL, . , , , .
GLvoid DrawGLScene(GLvoid)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//

glLoadIdentity();

//

}
. Windows, , , , .
: hWnd . message , . wParam lParam , , .
LRESULT CALLBACK WndProc(HWNDhWnd,

UINTmessage,

WPARAMwParam,


LPARAMlParam)
. . , , . , OpenGL . , . . , , ( !).
{

RECTScreen;//

GLuintPixelFormat;

staticPIXELFORMATDESCRIPTOR pfd=

{

sizeof(PIXELFORMATDESCRIPTOR),//

1,// (?)

PFD_DRAW_TO_WINDOW |//

PFD_SUPPORT_OPENGL |// OpenGL

PFD_DOUBLEBUFFER,//

PFD_TYPE_RGBA,// RGBA

16,// 16

0, 0, 0, 0, 0, 0,// (?)

0,//

0,// (?)

0,//

0, 0, 0, 0,// (?)

16,// 16 Z- ( )

0,//

0,// (?)

PFD_MAIN_PLANE,//

0,// (?)

0, 0, 0// (?)

};
. , , , , , "case" . , , , .


switch (message)//

{
WM_CREATE , . DC ( ) . , . . , , . , , . - , . , , .
case WM_CREATE:

hDC = GetDC(hWnd);//

PixelFormat = ChoosePixelFormat(hDC, &pfd);

//
, . , OK, .
if (!PixelFormat)

{

MessageBox(0,"Can't Find A Suitable

PixelFormat.","Error",MB_OK|MB_ICONERROR);

PostQuitMessage(0);

// ,

break;//

}
, . - , , , , OK, .
if(!SetPixelFormat(hDC,PixelFormat,&pfd))

{

MessageBox(0,"Can't Set The

PixelFormat.","Error",MB_OK|MB_ICONERROR);

PostQuitMessage(0);

break;


}
, , DC ( ), . , OpenGL DC. wglCreateContext hRC. - , . OK .
hRC = wglCreateContext(hDC);

if(!hRC)

{

MessageBox(0,"Can't Create A GL Rendering

Context.","Error",MB_OK|MB_ICONERROR);

PostQuitMessage(0);

break;

}
, , OpenGL . , , . OK .
if(!wglMakeCurrent(hDC, hRC))

{

MessageBox(0,"Can't activate GLRC.","Error",MB_OK|MB_ICONERROR);

PostQuitMessage(0);

break;

}
, , OpenGL. GetClientRect . , . , OpenGL. InitGL, ( ).
GetClientRect(hWnd, &Screen);

InitGL(Screen.right, Screen.bottom);

break;
WM_DESTROY WM_CLOSE . , , ALT-F4, PostQuitMessage(0) , .
ChangeDisplaySettings(NULL,0) , , . ReleaseDC(hWnd,hDC) . OpenGL.


case WM_DESTROY:

case WM_CLOSE:

ChangeDisplaySettings(NULL, 0);

wglMakeCurrent(hDC,NULL);

wglDeleteContext(hRC);

ReleaseDC(hWnd,hDC);

PostQuitMessage(0);

break;
WM_KEYDOWN . , , wParam. , ... , 'A'. , . , 'A' TRUE. , , TRUE, , 'A' .
case WM_KEYDOWN:

keys[wParam] = TRUE;

break;
WM_KEYUP , . , , wParam. , 'A', 'A' FALSE. , 'A', FALSE, ", ".
case WM_KEYUP:

keys[wParam] = FALSE;

break;
, - . , , , , , OpenGL . , .
WM_SIZE Windows - , . LOWORD(lParam) HIWORD(lParam). ReSizeGLScene . .
case WM_SIZE:

ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));

break;
, Windows , .
default:


return (DefWindowProc(hWnd, message, wParam, lParam));

}

return (0);

}
, , , , . .
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,

LPSTR lpCmdLine,int nCmdShow)

{

MSGmsg;// Windows

WNDCLASSwc; // Windows

HWNDhWnd;//
CS_HREDRAW CS_VREDRAW , . CS_OWNDC DC . , DC . WndProc - , . hIcon , , ICON , . ( GL). , NULL, .
wc.style= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

wc.lpfnWndProc= (WNDPROC) WndProc;

wc.cbClsExtra= 0;

wc.cbWndExtra= 0;

wc.hInstance= hInstance;

wc.hIcon= NULL;

wc.hCursor= LoadCursor(NULL, IDC_ARROW);

wc.hbrBackground= NULL;

wc.lpszMenuName= NULL;

wc.lpszClassName= "OpenGL WinClass";
. , . OK .
if(!RegisterClass(&wc))

{

MessageBox(0,"Failed To Register The Window

Class.","Error",MB_OK|MB_ICONERROR);

return FALSE;

}


. , , OpenGL , WM_CREATE . WS_CLIPCHILDREN WS_CLIPSIBLINGS OpenGL. , . , .
hWnd = CreateWindow(

"OpenGL WinClass",

"Jeff Molofee's GL Code Tutorial ... NeHe '99",//

WS_POPUP |

WS_CLIPCHILDREN |

WS_CLIPSIBLINGS,

0, 0,//

640, 480,//

NULL,

NULL,

hInstance,

NULL);
- . - , . OK .
if(!hWnd)

{

MessageBox(0,"Window Creation Error.","Error",MB_OK|MB_ICONERROR);

return FALSE;

}
. , , - , , .
, . , , Windows . , , , GL .
, C. .CPP .
DEVMODE dmScreenSettings;//

memset(&dmScreenSettings, 0, sizeof(DEVMODE));//


dmScreenSettings.dmSize= sizeof(DEVMODE);// Devmode

dmScreenSettings.dmPelsWidth= 640;//

dmScreenSettings.dmPelsHeight= 480;//

dmScreenSettings.dmFields= DM_PELSWIDTH | DM_PELSHEIGHT;//

ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);

//
ShowWindow - , . , , , . UpdateWindow , SetFocus , wglMakeCurrent(hDC,hRC) , .
ShowWindow(hWnd, SW_SHOW);

UpdateWindow(hWnd);

SetFocus(hWnd);
. , - ESC. , .
while (1)

{

//

while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))

{

if (GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

else

{

return TRUE;

}

}
DrawGLScene , OpenGL. , - . , OpenGL .
SwapBuffers(hDC) . . , ( ). , , . , .


DrawGLScene();//

SwapBuffers(hDC);//

if (keys[VK_ESCAPE]) SendMessage(hWnd,WM_CLOSE,0,0);// ESC -

}

}
, OpenGL , ESC. 3 13 . , , . , - , , . OpenGL . .
Jeff Molofee (NeHe)


PMG

 6 2001 (c) 


d=document;rn=Math.random(); y="
"; d.write(y);