Click here to Skip to main content
Licence CPOL
First Posted 31 Dec 2007
Views 37,454
Downloads 1,393
Bookmarked 23 times

Use OpenGL in CSharp Application

By | 31 Dec 2007 | Article
This is a simple example about using OpenGL in CSharp application.

Introduction

This is a simple example about using OpenGL in CSharp application.

The Workflow of Using OpenGL

  1. Create OpenGL with a specific area.
    1. Create a DIB (Device independent bitmap)
    2. Create OpenGL rendering context and set the device
    3. Make the OpenGL render context the current rendering context

    Note: When we create the OpenGL, we don’t need any device handle. We will create a device handle with DIB by ourselves. Then set the created handle to OpenGL. But the graphics on this device handle can't be shown, so we should copy the graphics to the view when we want to show the graphics of the DIB.

  2. Do some initialization for OpenGL.
  3. Draw graphics with OpenGL.
    1. Make the OpenGL rendering context the current rendering context.
    2. Draw graphics with OpenGL.
    3. Swap buffers.
    4. Copy image to the view of control.

    Note: In fact, we draw the graphics to the DIB. Then copy the graphics from the DIB to the specific view area.

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;
}	

History

This is the first version. It is easy to add some complicated functions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jeffrey Sun



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmembermikkojay14:13 21 Dec '10  
GeneralWatch Online Uruguay vs Ghana Live Match FIFA 2010 &&& Watch Online Brazil vs Netherlands Live Match FIFA 2010 Pinmemberned_cis21:33 1 Jul '10  
Generalmy vote is 3 Pinmembermheidari3:51 11 Jul '09  
GeneralHi Pinmemberdefineconst20:42 9 Apr '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 31 Dec 2007
Article Copyright 2007 by Jeffrey Sun
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid