2006-05-09 16:44:59Jonah

在Visual C++.NET 中,使用CsGL來展現OpenGL的方法論

步驟一:新增專案->Windows Form應用
步驟二:新增檔案->標頭檔(.h)->取名OpenGL.h
步驟三:將以下內容複製貼在OpenGL.h當中

#using
#using
#using
#using
#using

using namespace System;
using namespace System::Windows::Forms;
using namespace CsGL::OpenGL;

__gc class Model : public GL
{
public:
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5F, 0.0F, -6.0F);
glBegin(GL_TRIANGLES);
glColor3f(1.0F, 0.0F, 0.0F);
glVertex3f(0.0F, 1.0F, 0.0F);
glColor3f(0.0F, 1.0F, 0.0F);
glVertex3f(-1.0F, -1.0F, 0.0F);
glColor3f(0.0F, 0.0F, 1.0F);
glVertex3f(1.0F, -1.0F, 0.0F);
glEnd();
glTranslatef(3.0F, 0.0F, 0.0F);
glColor3f(0.5F, 0.5F, 1.0F);
glBegin(GL_QUADS);
glVertex3f(-1.0F, 1.0F, 0.0F);
glVertex3f(1.0F, 1.0F, 0.0F);
glVertex3f(1.0F, -1.0F, 0.0F);
glVertex3f(-1.0F, -1.0F, 0.0F);
glEnd();
}
};
__gc class GLView : public OpenGLControl
{
Model* model;

public:
GLView() : model(new Model)
{
Dock = DockStyle::Fill;
}
virtual void glDraw()
{
model->Draw();
}

protected:
virtual void OnSizeChanged(EventArgs* e)
{
OpenGLControl::OnSizeChanged(e);

System::Drawing::Size s = Size;
double aspect_ratio = s.Width / (double) s.Height;

GL::glMatrixMode(GL::GL_PROJECTION);
GL::glLoadIdentity();
GL::gluPerspective(45.0F, aspect_ratio, 0.1F, 100.0F);

GL::glMatrixMode(GL::GL_MODELVIEW);
GL::glLoadIdentity();
}
};

步驟四:利用工具箱在From1.h[設計]中拉出一個Panel取名Panel1
步驟五:修改Form1.h的程式碼
增加#include ”OpenGL.h”
步驟六:修改Form1(void)的函式內容
Form1(void)
{
GLView * OpenGl_Viewer;
OpenGl_Viewer = new GLView();
this->panel1->Controls->Add(OpenGl_Viewer);
}
步驟七:將csgl.dll,csgl.native.dll 兩個檔案,拷貝到Debug或是Release的目錄下面
步驟八:Compile 就大功告成了