OpenGLを利用した簡単な3次元オブジェクトの描画を行うプログラム例を 紹介します。MFCとは, Microsoft Foundation Class ライブラリのことで、 C++言語でのWindows GUIを用いたプログラムが簡単に作成できるように Microsoftが提供しているライブラリ群のことである。Window作成やダイアログの作成が 簡単に実現できる。
int CGlSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: この位置に固有の作成用コードを追加してください
return 0;
}
int CGlSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: この位置に固有の作成用コードを追加してください
m_pDC = new CClientDC(this) ; // DCの生成
SetDCPixelFormat(m_pDC->m_hDC) ; // OpenGL用にPixel Formatを指定
m_GLRC = wglCreateContext (m_pDC->m_hDC); // Rendering contextの生成
wglMakeCurrent (m_pDC->m_hDC, m_GLRC); // 現在のcontext設定
InitializeOpenGL() ; //3Dシーンを初期化する関数を用意する
MakeGLObject(); //3Dオブジェクトを生成する
return 0;
}
int CGlSampleView::SetDCPixelFormat (HDC hdc)
{
static PIXELFORMATDESCRIPTOR pfd = {
sizeof (PIXELFORMATDESCRIPTOR), // Specifies the size of this data structure
1, // Specifies the version of this data structure
PFD_DRAW_TO_WINDOW | // ピクセルバッファのビットフラグの設定
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, // RGBA pixel values
32, // 32-bitカラーと指定
0, 0, 0, 0, 0, 0, // Specifies the number of red bitplanes in each RGBA color buffer
0, 0, // Specifies the number of alpha bitplanes in each RGBA color buffer
0, 0, 0, 0, 0, // Specifies the total number of bitplanes in the accumulation buffer
32, // Specifies the depth(bit) of the depth (z-axis) buffer
32, // Specifies the depth of the stencil buffer
0, // Specifies the number of auxiliary buffers
PFD_MAIN_PLANE, // Layer type Ignored...
0, // Specifies the number of overlay and underlay planes
0, // Ignored
0, // Specifies the transparent color or index of an underlay plane
0 // Ignored
};
int nPixelFormat;
nPixelFormat = ChoosePixelFormat (hdc, &pfd);
if (SetPixelFormat(hdc, nPixelFormat, &pfd) == FALSE) {
return FALSE ;
}
if (DescribePixelFormat(hdc, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd) == 0) {
return FALSE ;
}
return TRUE ;
}
GLint InitializeOpenGL(void)
{
//3Dシーンの背景を黒にする。
glClearColor(0.0f, 0.0f, 0.0f, 1.0f) ;
glDepthFunc(GL_LEQUAL) ;
//デプスバッファを有効化
glEnable(GL_DEPTH_TEST) ;
//Lightingを有効化
glEnable(GL_LIGHTING) ;
glEnable(GL_LIGHT0) ;
glEnable(GL_LIGHT1) ;
return (0) ;
}
int MakeGLObject()
{
//ここに事前に作成しておくオブジェクトの記述を加える
//
return (0) ;
}
void DrawGLobject()
{
//シーンをクリアする。色情報、デプスバッファをクリア
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT) ;
//オブジェクト描画モードにする。
glMatrixMode(GL_MODELVIEW) ;
glLoadIdentity() ;
//////////////////////////////////////////////
//必要な機能の有効化
//
glEnable(GL_NORMALIZE) ;
glEnable(GL_AUTO_NORMAL) ;
glShadeModel (GL_SMOOTH);
glEnable( GL_DEPTH_TEST ); //Zバッファを使用する
//とりあえず今の座標系を保存しておく
//glPopMatrix()で保存された座標系に戻すことができる。
glPushMatrix();
//これ以降に実際に描画したいオブジェクトを表示する記述を加える
//
//座標系を戻しておく
glPopMatrix();
glDisable(GL_AUTO_NORMAL) ;
glDisable(GL_NORMALIZE) ;
return;
}
void CGlSampleView::OnDraw(CDC* pDC)
{
CGlSampleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: この場所にネイティブ データ用の描画コードを追加します。
DrawGLobject() ;
SwapBuffers(m_pDC->m_hDC) ; // Double buffer
}
// オペレーション public: CDC* m_pDC ; // Device Context HGLRC m_GLRC ; // OpenGL Rendering Context int SetDCPixelFormat (HDC hdc);
#include < GL/gl.h > #include < GL/glu.h >
