반응형
프로젝트 생성 후 다음 파일을 작성한다.
프로젝트 Property -> Linker ->Input 에서 opengl32.lib glu32.lib glut32.lib 을 Additonal Dependencies에 추가해 준다.

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

// global variables
char *szClassName = "Computer Graphics";

HWND MyWND = 0;

HDC MyDC;
HGLRC MyRC;

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

bool bSetupPixelFormat(void);
void Resize(const int cx, const int cy);
void DrawScene(void);

/*
* WinMain
*/
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nShowCmd )
{
// Registers the window class
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
wc.hCursor = LoadCursor( 0, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = szClassName;

RegisterClass(&wc);

// Create the main window
MyWND = CreateWindow( szClassName, 
"Simple OpenGL Example", 
WS_OVERLAPPEDWINDOW, 
CW_USEDEFAULT, 
CW_USEDEFAULT, 
CW_USEDEFAULT, 
CW_USEDEFAULT, 
0, 
0, 
hInstance, 
0 );

// Make sure that window was created 
if( !MyWND )
return false;

ShowWindow( MyWND, nShowCmd );
UpdateWindow( MyWND );

// Main message loop
MSG msg;
while( GetMessage( &msg, 0, 0, 0 ) ) 
{
TranslateMessage( &msg );
DispatchMessage (&msg );
}

UnregisterClass( szClassName, wc.hInstance );

return msg.wParam;
}


/*
* WndProc: to process messages for the main window
*/
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
RECT rect;

switch( msg ) 
{
case WM_CREATE: 
// Initialize for the OpenGL rendering
MyDC = GetDC(hWnd); 
if( !bSetupPixelFormat() ) 
PostQuitMessage (0); 

MyRC = wglCreateContext(MyDC); 
wglMakeCurrent(MyDC, MyRC); 

break; 

        // 창 크기 변경시
case WM_SIZE: 
GetClientRect(hWnd, &rect); 
Resize(rect.right, rect.bottom); 

                // 창을 다시 그려준다.
InvalidateRect(hWnd, NULL, FALSE);

break; 

case WM_DESTROY:
// Destroy all about OpenGL
if( MyRC ) 
wglDeleteContext(MyRC); 

if( MyDC ) 
ReleaseDC(hWnd, MyDC); 

PostQuitMessage(0);
break;

case WM_PAINT:
DrawScene();
ValidateRect(hWnd, NULL);
break;

case WM_KEYDOWN:
if( wParam == VK_ESCAPE ) 
{
DestroyWindow( MyWND );
}
break;

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

return 0;
}

/*
* bSetupPixelFormat : to setup the format of pixel for OpenGL
*/
bool bSetupPixelFormat(void) 
PIXELFORMATDESCRIPTOR pfd; 
int pixelformat; 

pfd.nSize= sizeof( PIXELFORMATDESCRIPTOR ); 
pfd.nVersion= 1; 
pfd.dwFlags= PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; 
pfd.dwLayerMask= PFD_MAIN_PLANE; 
pfd.iPixelType= PFD_TYPE_RGBA; 
pfd.cColorBits= 24;
pfd.cDepthBits= 16; 
pfd.cAccumBits= 0; 
pfd.cStencilBits= 0;

if( (pixelformat= ChoosePixelFormat( MyDC, &pfd ))==0 ) { 
MessageBox( NULL, "ChoosePixelFormat failed", "Error", MB_OK ); 
return false; 

if( SetPixelFormat( MyDC, pixelformat, &pfd )==false ) { 
MessageBox( NULL, "SetPixelFormat failed", "Error", MB_OK ); 
return false; 
}

return true; 
}

/*
* Resize : to re-initialize 
*/
void Resize(const int cx, const int cy) 
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport(0,0,cx,cy);

// 2D orthographic viewing
gluOrtho2D(0, 500, 0, 500);

return;
}    

/*
* DrawScene : to draw a scene
*/
void DrawScene(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
 
// 사각형
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_POLYGON);
       // openGL은 오른손 좌표계와 데카르트 좌표계를 사용하여
       //  좌측 하단부터 0, 0 으로 시작한다.
glVertex2f(100, 100);
glVertex2f(100, 200);
glVertex2f(200, 200);
glVertex2f(200, 100);
glEnd();

glFlush();

return;
}
반응형

'OpenGL' 카테고리의 다른 글

OpenGL 그리기 종류  (0) 2011.03.21
OpenGL 설치  (0) 2011.03.21

+ Recent posts