| 分類:[C++/CLI]
はじめまして、はじめて質問いたします。
Windowsフォームアプリケーション上でOpenGLを使って シミュレーションの結果を描画させたいと考えています。
プロトタイプとなるものをC++/CLIで作成しました。
Form1.hへ必要と思われる内容を記述していますがアプリケーションを 「ビルド」⇒「実行」⇒「終了」 とさせてもタスクマネージャにプロセスが残ってしまい 正常終了させることができません。
どこかコードの記述に誤りがあると思いますが、コードを見て 頂いて誤りを指摘して頂きたいと思っています。
以下にForm1.hのコードを記載させていただきます。 どうぞ宜しくお願いいたします。
#pragma once
#include <windows.h> #include <iostream> #include <math.h> #include <gl/gl.h> #include <gl/glu.h> #include <stdio.h>
using namespace std;
namespace test_1 {
///////////<追加したコード@>/////////////////////////////////
// グローバル変数 double dt = 0.005; // 時間ステップサイズ int FrameCounter = 0; // フレーム数
//////////////////////////////////////////////////////////////
using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing;
/// <summary> /// Form1 の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary>
// Form1クラス宣言 public ref class Form1 : public System::Windows::Forms::Form {
////////<追加したコードA>///////////
private: HDC hDC; HGLRC hRC;
/////////////////////////////////////
private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2;
public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します //
/////////////////////<追加したコードB>/////////////////////////////////////
hDC = GetDC((HWND)(pictureBox->Handle.ToInt32()));
PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 32, 0,0,0,0,0,0, 0,0,0,0,0,0,0, 16, 0,0, PFD_MAIN_PLANE, 0, 0,0,0 };
int pixelFormat = ChoosePixelFormat(hDC,&pfd); BOOL success = SetPixelFormat(hDC,pixelFormat,&pfd); hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC); wglMakeCurrent(0,0);
glClearColor( 1,1,1,0 ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
wglMakeCurrent(hDC,0); SwapBuffers(hDC);
}
///////////////////////////////////////////////////////////////////////////////
protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary>
//デストラクタ ~Form1()
{
//////////////////////////<追加したコードC>//////////////////////////////////// wglMakeCurrent(hDC,0); wglDeleteContext(hRC); ReleaseDC((HWND)pictureBox->Handle.ToInt32(),hDC); ////////////////////////////////////////////////////////////////////////////////
if (components) { delete components; } }
private: System::Windows::Forms::Button^ button; protected: private: System::Windows::Forms::PictureBox^ pictureBox;
private: /// <summary> /// 必要なデザイナ変数です。 /// </summary> System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code /// <summary> /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// </summary> void InitializeComponent(void) { this->pictureBox = (gcnew System::Windows::Forms::PictureBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox))->BeginInit(); this->SuspendLayout(); // // pictureBox // this->pictureBox->BackColor = System::Drawing::SystemColors::Window; this->pictureBox->Location = System::Drawing::Point(12, 59); this->pictureBox->Name = L"pictureBox"; this->pictureBox->Size = System::Drawing::Size(268, 195); this->pictureBox->TabIndex = 0; this->pictureBox->TabStop = false; // // button1 // this->button1->Location = System::Drawing::Point(28, 20); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(96, 23); this->button1->TabIndex = 1; this->button1->Text = L"描画開始"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // button2 // this->button2->Location = System::Drawing::Point(171, 20); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(84, 23); this->button2->TabIndex = 2; this->button2->Text = L"描画終了"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 266); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->pictureBox); this->Name = L"Form1"; this->Text = L"Form1"; (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox))->EndInit(); this->ResumeLayout(false);
}
#pragma endregion
// ボタン2 private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { this->Close(); }
// ボタン1 private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { for(;;) { wglMakeCurrent(hDC,hRC);
// OpenGLの関数を実行 display(); Application::DoEvents(); System::Threading::Thread::Sleep(1000/30); wglMakeCurrent(hDC,0); SwapBuffers(hDC); } }
// 描画用の関数 public: System::Void display() {
double u; u=1.0; double t; glViewport(0, 0, (GLsizei)pictureBox->Width, (GLsizei)pictureBox->Height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0,(GLsizei)pictureBox->Width / (GLsizei)pictureBox->Height,0.1,20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslated(0.0, 0.0, -2.0); glEnable(GL_POINT_SMOOTH); glColor3d(1.0, 0.0 ,0.0); glPointSize(100); glBegin(GL_POINTS);
int i; for (i=0; i<30; i++) { t = dt*(double)FrameCounter; u = sin(t);
FrameCounter++; if( FrameCounter>=10000) { FrameCounter = 0; // 初期時刻へ戻す u=1.0; // 初期状態に戻す } }
glEnable(GL_POINT_SMOOTH); glColor3d(1.0, 0.0 ,0.0); glPointSize(10); glBegin(GL_POINTS); glVertex3d(0.0, 0.5*u, 0.0); glEnd(); glPopMatrix(); glFlush(); }
}; }
|