65.9K
CodeProject 正在变化。 阅读更多。
Home

在 CSharp 应用程序中使用 OpenGL

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.72/5 (9投票s)

2007 年 12 月 31 日

CPOL
viewsIcon

78145

downloadIcon

3845

这是一个关于在 C# 应用程序中使用 OpenGL 的简单示例。

引言

这是一个关于在 C# 应用程序中使用 OpenGL 的简单示例。

使用 OpenGL 的流程

  1. 使用特定区域创建 OpenGL。
    1. 创建一个 DIB(设备无关位图)。
    2. 创建 OpenGL 渲染上下文并设置设备。
    3. 将 OpenGL 渲染上下文设为当前渲染上下文。

    注意:当我们创建 OpenGL 时,我们不需要任何设备句柄。我们将自己使用 DIB 创建一个设备句柄。然后将创建的句柄设置为 OpenGL。但是,此设备句柄上的图形无法显示,因此当我们要显示 DIB 的图形时,应将图形复制到视图。

  2. 为 OpenGL 执行一些初始化。
  3. 使用 OpenGL 绘制图形。
    1. 将 OpenGL 渲染上下文设为当前渲染上下文。
    2. 使用 OpenGL 绘制图形。
    3. 交换缓冲区。
    4. 将图像复制到控件的视图。

    注意:实际上,我们是将图形绘制到 DIB。然后将图形从 DIB 复制到特定的视图区域。

public OpenGLBox()
{
    InitializeComponent();

    // 1.	Create OpenGL with a specific area.
    bool bRet = m_gl.Create(Width, Height);
    Debug.Assert(bRet == true);

    // 2.	Do some initialization for OpenGL.
    m_gl.ShadeModel(OpenGL.SMOOTH);
    m_gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    m_gl.ClearDepth(1.0f);
    m_gl.Enable(OpenGL.DEPTH_TEST);
    m_gl.DepthFunc(OpenGL.LEQUAL);
    m_gl.Hint(OpenGL.PERSPECTIVE_CORRECTION_HINT, OpenGL.NICEST);
}	
protected override void OnPaint(PaintEventArgs e)
{
    // 3.	Draw graphics with OpenGL.

    // 3.1	Make the OpenGL rendering context 
    // the current rendering context.
    m_gl.MakeCurrent();

    // 3.2	Draw graphics with OpenGL.
    if (OpenGLRender != null)
        OpenGLRender(this, e);

    // 3.3	Swap buffers.
    m_gl.SwapBuffers();

    //	3.4	Copy image to the view of control.
    e.Graphics.DrawImageUnscaled(m_gl.OpenGLBitmap, 0, 0);
}	
public virtual unsafe bool Create(int width, int height)
{
    if (width == 0 || height == 0)
        return false;

    //	1.1 Create DIB –device independent bitmap.
    m_DIBSection.Create(width, height, 24);

    // 1.2 Create OpenGL rendering context and set the device.
    if (handleRenderContext == IntPtr.Zero)
        handleRenderContext = wglCreateContext(m_DIBSection.HDC);
    handleDeviceContext = m_DIBSection.HDC;

    //	1.3	Make the OpenGL render context the current rendering context.
    wglMakeCurrent(m_DIBSection.HDC, handleRenderContext);
             
    return true;
}

public DIBSection()
{
    //	Create a blank DC.
    // Get the device context of the display.
    IntPtr screenDC = GetDC(IntPtr.Zero);
 
    // Create a memory device context 
    // which is compatible to a specific device.
    hDC = CreateCompatibleDC(screenDC);
}

public virtual unsafe bool Create(int width, int height, uint bitCount)
{
    this.width = width;
    this.height = height;
  
    // Destroy existing objects.
    Destroy();

    // Create a bitmap info structure.
    BITMAPINFO info = new BITMAPINFO();

    // Set the data.
    info.biSize = 40;
    info.biBitCount = (Int16)bitCount;
    info.biPlanes = 1;
    info.biWidth = width;
    info.biHeight = height;
 
    // Create the bitmap.
    IntPtr ppvBits;
    hBitmap = CreateDIBSection(hDC, info, DIB_RGB_COLORS,
    out ppvBits, IntPtr.Zero, 0);

    SelectObject(hDC, hBitmap);
  
    // Set the OpenGL pixel format.
    SetPixelFormat(bitCount);

    return true;
}	

历史

这是第一个版本。添加一些复杂的功能很容易。

© . All rights reserved.