2009/03/23

OpenGL 로 간단하게 2D Drawing 하는 기본 소스

OpenGL 로 간단하게 2D 드로잉 하는 걸 공부하고 있는데 진도가 느리다.



아래 소스는 Visual C++ 에서 OpenGL을 이용하여 간단하게 2D 드로잉을 하는 기본 소스이다.



// stdafx.h
////////////////////////////////////////////////////////////////////////////////
// for OpenGL
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glu32.lib")
#pragma comment (lib, "glaux.lib")

#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/GLAux.h>
// for OpenGL
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Header
HGLRC m_hRC; //Rendering Context
CDC* m_pDC; //Device Context
int m_nPixelFormat;

BOOL InitializeOpenGL();
void RenderScene();


////////////////////////////////////////////////////////////////////////////////
// Source
int CGLTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

InitializeOpenGL();

return 0;
}

BOOL CGLTestView::InitializeOpenGL()
{
//Get a DC for the Client Area
m_pDC = new CClientDC(this);

//Failure to Get DC
if(m_pDC == NULL)
{
MessageBox( _T("Error Obtaining DC") );
return FALSE;
}

////////////////////////////////////////////////////////////////////////////////
// Setup Pixel Format
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
32, // color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accumulation bits ignored
32, // z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};

int m_nPixelFormat = ::ChoosePixelFormat( m_pDC->GetSafeHdc(), &pfd );

if ( m_nPixelFormat == 0 )
{
return FALSE;
}

if ( ::SetPixelFormat( m_pDC->GetSafeHdc(), m_nPixelFormat, &pfd ) == FALSE )
{
return FALSE;
}
////////////////////////////////////////////////////////////////////////////////

//Create Rendering Context
m_hRC = ::wglCreateContext( m_pDC->GetSafeHdc() );

//Failure to Create Rendering Context
if( m_hRC == 0 )
{
MessageBox( _T("Error Creating RC") );
return FALSE;
}

//Make the RC Current
if( ::wglMakeCurrent ( m_pDC->GetSafeHdc (), m_hRC ) == FALSE )
{
MessageBox( _T("Error making RC Current") );
return FALSE;
}

//Specify Black as the clear color
::glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);

//Specify the back of the buffer as clear depth
//::glClearDepth( 1.0f );

//Enable Depth Testing
//::glEnable( GL_DEPTH_TEST );
::glDisable( GL_DEPTH_TEST ); // For 2D

return TRUE;
}

void CGLTestView::OnDraw(CDC* /*pDC*/)
{
CGLTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

// 컬러버퍼와 깊이버퍼를 초기화
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

RenderScene();

// Tell OpenGL to flush its pipeline
::glFinish();

// Now Swap the buffers
::SwapBuffers( m_pDC->GetSafeHdc() );
}

void CGLTestView::RenderScene()
{
glBegin(GL_TRIANGLES);
{
glVertex2f( 0.0f, 0.0f );
glVertex2f( 20.0f, 0.0f );
glVertex2f( 0.0f, 20.0f );
}
glEnd();
}

void CGLTestView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

GLdouble aspect_ratio; // width/height ratio

if ( 0 >= cx || 0 >= cy )
{
return;
}

// select the full client area
::glViewport(0, 0, cx, cy);

// compute the aspect ratio
// this will keep all dimension scales equal
aspect_ratio = (GLdouble)cx/(GLdouble)cy;

// select the projection matrix and clear it
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();

// select the viewing volume
//::gluPerspective(45.0f, aspect_ratio, .01f, 200.0f);

GLfloat nRange = 50.0f;
if (cx <= cy)
{
glOrtho(-nRange, nRange, -nRange * cy / cx,
nRange * cy / cx, -nRange, nRange );
}
else
{
glOrtho(-nRange * cx / cy,
nRange * cx / cy,
-nRange, nRange, -nRange, nRange);
}

// switch back to the modelview matrix and clear it
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
}




결과 화면

결과 화면



glut를 사용하지 않고 기본으로 포함되어있는 OpenGL 만을 이용한 소스이다. 분석을 하여 나중에는 glut까지 이용하는 코드를 작성해 보아야겠다.


No comments :

Post a Comment