/* =============================================================================== HelloGL.mc : OpenGL の確認 --- 黒地に白の正方形を表示する =============================================================================== Copyright (C) 2010 Masahiko Watanabe Edition History: Ver.1.00 2010.11.24( GK Library: Rev.2010.11.20 使用 ) -----------------------------------------------------------------------------*/ /*****/ // GK Library のモジュールをロードする場合 #include #include #include if( ::Module.Include( "GkLibrary" ) > 0 ) Main( argc, argv ); return; /*****/ /****** // GK Library のソースをインクルードする場合 #include Main( argc, argv ); return; /*****/ //----------------------------------------------------------------------------- // デバッグプリント無効化(リリース時) #set pc #comment #set pv // デバッグプリント有効化(デバッグ時) //#set pc print //#set pv print #set p print //----------------------------------------------------------------------------- /// マクロ定義 /// #set WndTitle "OpenGL 入門: 黒地に白の正方形を表示" // ウィンドウ表題 #set WndXo // ウィンドウの左端位置(空白はデフォールト位置) #set WndYo // ウィンドウの上端位置(空白はデフォールト位置) #set FigXd 480 // 図形描画領域の幅 #set FigYd 480 // 図形描画領域の高さ //============================================================================= function Main( argc, argv ) { #pc "開始"; ^DefValue = null; if( SetupOpenGL() == #OK ) { class ^MainWindow : ::GK.Window {} R = ::GK.Rect( 0, 0, #FigXd, #FigYd ); ::GK.Window.GetAdjRect( R ); ^MainWindow.Construct( #WndTitle, #WndXo, #WndYo, R.Xd, R.Yd ); ^MainWindow.Open(); ::GK.WindowMsgLoop(); delete ^MainWindow; } DeleteOpenGL(); #pc "終了"; } //----------------------------------------------------------------------------- function ^MainWindow.OnCreate() { #pc "OnCreate"; if( .InizOpenGL() != #OK ) { print "OpenGL 初期設定エラー!"; .Close(); } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - function ^MainWindow.OnDestroy() { #pc "OnDestroy"; .TermOpenGL(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - function ^MainWindow.OnPaint() { #pc "OnPaint"; .DrawOpenGL(); } //============================================================================= // OpenGL 関連のマクロ定義 #set PFD_MAIN_PLANE 0 #set PFD_TYPE_RGBA 0 #set PFD_DRAW_TO_WINDOW 0x00000004 #set PFD_SUPPORT_OPENGL 0x00000020 #set GL_COLOR_BUFFER_BIT 0x00004000 #set GL_POLYGON 0x0009 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - function SetupOpenGL() // OpenGL のDLLから必要なAPIを取得 { #pv ^ChoosePixelFormat = ::GK.Gdi32.GetEntry( "ChoosePixelFormat" ); #pv ^SetPixelFormat = ::GK.Gdi32.GetEntry( "SetPixelFormat" ); #pv ^GL = ::DLL.Link( "OPENGL32", "sys" ); if( ^GL == null ) { print "OPENGL32.DLL がありません!"; return #NG; } #pv ^wglCreateContext = ^GL.GetEntry( "wglCreateContext" ); #pv ^wglDeleteContext = ^GL.GetEntry( "wglDeleteContext" ); #pv ^wglGetCurrentContext = ^GL.GetEntry( "wglGetCurrentContext" ); #pv ^wglMakeCurrent = ^GL.GetEntry( "wglMakeCurrent" ); #pv ^glBegin = ^GL.GetEntry( "glBegin" ); #pv ^glClear = ^GL.GetEntry( "glClear" ); #pv ^glEnd = ^GL.GetEntry( "glEnd" ); #pv ^glFlush = ^GL.GetEntry( "glFlush" ); #pv ^glVertex2d = ^GL.GetEntry( "glVertex2d" ); return #OK; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - function DeleteOpenGL() // OpenGL のDLLを破棄 { delete ^GL; } //----------------------------------------------------------------------------- function ^MainWindow.InizOpenGL() // OpenGL の初期設定 { #pc "InizOpenGL"; PIXELFORMATDESCRIPTOR ::= { .nSize 'USHORT; .nVersion 'USHORT = 1; .dwFlags 'ULONG = #PFD_DRAW_TO_WINDOW | #PFD_SUPPORT_OPENGL ; .iPixelType 'UBYTE = #PFD_TYPE_RGBA; .cColorBits 'UBYTE = 24; // 24-bit color depth .cRedBits 'UBYTE = 0; // color bits ignored .cRedShift 'UBYTE = 0; // 〃 .cGreenBits 'UBYTE = 0; // 〃 .cGreenShift 'UBYTE = 0; // 〃 .cBlueBits 'UBYTE = 0; // 〃 .cBlueShift 'UBYTE = 0; // 〃 .cAlphaBits 'UBYTE = 0; // no alpha buffer .cAlphaShift 'UBYTE = 0; // shift bit ignored .cAccumBits 'UBYTE = 0; // accum bits ignored .cAccumRedBits 'UBYTE = 0; // 〃 .cAccumGreenBits'UBYTE = 0; // 〃 .cAccumBlueBits 'UBYTE = 0; // 〃 .cAccumAlphaBits'UBYTE = 0; // 〃 .cDepthBits 'UBYTE = 32; // 32-bit z-buffer .cStencilBits 'UBYTE = 0; // no stencil buffer .cAuxBuffers 'UBYTE = 0; // no auxiliary buffer .iLayerType 'UBYTE = #PFD_MAIN_PLANE; // main layer .bReserved 'UBYTE = 0; .dwLayerMask 'ULONG = 0; // layer masks ignored .dwVisibleMask 'ULONG = 0; // 〃 .dwDamageMask 'ULONG = 0; // 〃 } PIXELFORMATDESCRIPTOR.nSize = PIXELFORMATDESCRIPTOR'size; pfd = ::Buffer( PIXELFORMATDESCRIPTOR.nSize ); pfd.Write( PIXELFORMATDESCRIPTOR ); pfd.Seek(0); if(( i = ^ChoosePixelFormat( .gc.hdc, pfd )) <= 0 ) return #NG; if( ^SetPixelFormat( .gc.hdc, i, pfd ) == #FALSE ) return #NG; if(( .hRC = ^wglCreateContext( .gc.hdc )) == #NULL ) return #NG; if( ^wglMakeCurrent( .gc.hdc, .hRC ) == #FALSE ) return #NG; return #OK; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - function ^MainWindow.TermOpenGL() // OpenGL の終了設定 { #pc "TermOpenGL"; ^wglMakeCurrent( #NULL, #NULL ); ^wglDeleteContext( .hRC ); } //----------------------------------------------------------------------------- function ^MainWindow.DrawOpenGL() // OpenGL で黒地に白の四角形を描画 { ^glClear( #GL_COLOR_BUFFER_BIT ); ^glBegin( #GL_POLYGON ); ^glVertex2d( -0.5, -0.5 ); ^glVertex2d( -0.5, +0.5 ); ^glVertex2d( +0.5, +0.5 ); ^glVertex2d( +0.5, -0.5 ); ^glEnd(); ^glFlush(); } //-----------------------------------------------------------------------------