using System;
using System.Runtime.InteropServices;
using Tao.OpenGl;
using Tao.Platform.Windows;
using VJProjector;
using VJProjector.GFX.Immerse.Types;
 
namespace VJProjector.GFX.Immerse.GPU
{ 
  /// <summary>
	/// Implements an OpenGL Rendering Context
	/// </summary>
	public class RenderingContext : IDisposable
	{
    /// <summary>
    /// Constants
    /// </summary>
    public const int ClearBuffer = Gl.GL_COLOR_BUFFER_BIT;
    public const int ClearZBuffer = Gl.GL_DEPTH_BUFFER_BIT;
    public const int ClearStencil = Gl.GL_STENCIL_BUFFER_BIT;
 
//    private bool ZBufferTrickEven = false;
 
    /// <summary>
    /// Private members
    /// </summary>
    protected bool initialized;
 
    protected IntPtr hDC;
    protected IntPtr hRC;
    protected int pixelFormat;
    protected Gdi.PIXELFORMATDESCRIPTOR pfd;
    protected int clrMode;
    protected ColorEX clearColor;
    protected int width, height;
 
 
    /// <summary>
    /// Public Properties
    /// </summary>
    public int ClearMode { get { return clrMode; } set { clrMode = value; } }
    public ColorEX ClearColor { get { return clearColor; } set { clearColor = value; } }
    public int Width { get { return width; } }
    public int Height { get { return height; } }
    public IntPtr DC { get { return hDC; } }
    public IntPtr RC { get { return hRC; } }
 
    /// <summary>
    /// Constructor
    /// </summary>
    public RenderingContext()
		{
      initialized = false;
		}
 
    public virtual void CreateContext(IntPtr hDC, int ColorBits, int ZBufferBits, int StencilBits) 
    {
      CreateContext(hDC, 0, 0, ColorBits, ZBufferBits, StencilBits);
    }
 
    public virtual void CreateContext(IntPtr hDC, int width, int height, int ColorBits, int ZBufferBits, int StencilBits)
    {
      this.hDC = hDC;
      this.width = width;
      this.height = height;
 
      // size of this pfd
      pfd.nSize = (short)Marshal.SizeOf(pfd);            
      // version number
      pfd.nVersion = 1;            
      // support window, support OpenGL, double buffered
      pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DOUBLEBUFFER;
      // RGBA type
      pfd.iPixelType = Gdi.PFD_TYPE_RGBA;            
      // color buffer bits
      pfd.cColorBits = (byte)ColorBits;
      // color bits and shift bits ignored
      pfd.cRedBits = 0;
      pfd.cRedShift = 0;
      pfd.cGreenBits = 0;
      pfd.cGreenShift = 0;
      pfd.cBlueBits = 0;
      pfd.cBlueShift = 0;
      pfd.cAlphaBits = 0;
      pfd.cAlphaShift = 0;            
      // no accumulation buffer, accum bits ignored
      pfd.cAccumBits = 0;
      pfd.cAccumRedBits = 0;
      pfd.cAccumGreenBits = 0;
      pfd.cAccumBlueBits = 0;
      pfd.cAccumAlphaBits = 0; 
      // ZBuffer Bits
      pfd.cDepthBits = (byte)ZBufferBits;
      // Stencil buffer bits
      pfd.cStencilBits = (byte)StencilBits; 
      // no auxiliary buffer
      pfd.cAuxBuffers = 0;            
      // main layer
      pfd.iLayerType = Gdi.PFD_MAIN_PLANE;            
      // reserved
      pfd.bReserved = 0;            
      // layer masks ignored
      pfd.dwLayerMask = 0;
      pfd.dwVisibleMask = 0;
      pfd.dwDamageMask = 0;
 
      pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd);
      Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd);
      hRC = Wgl.wglCreateContext(hDC);
 
      initialized = true;
      MakeCurrent();
      Gl.glClearColor(0,0,0,1);
      Gl.glClear(Gl.GL_COLOR_BUFFER_BIT|Gl.GL_DEPTH_BUFFER_BIT|Gl.GL_STENCIL_BUFFER_BIT);
      ContextHelper.MakeCurrent(hDC, hRC);
    }
 
    protected void CheckInitialized() 
    {
      if(!initialized)
        throw new Exception("PBuffer not initialized");
    }
    /// <summary>
    /// Makes this Rendering Context current
    /// </summary>
    public virtual void MakeCurrent() 
    {
      CheckInitialized();
      ContextHelper.MakeCurrent(hDC, hRC);
    }
 
    public virtual void UnsetCurrent()
    {
      ContextHelper.UnsetCurrent();
    }
 
    public virtual void DeleteContext()
    {
      Wgl.wglDeleteContext(hRC);
    }
 
    #region Clear Functions
    /// <summary>
    /// Clears the Rendering Context;
    /// </summary>
    public void Clear()
    {
      CheckInitialized();
      Gl.glClearColor(clearColor.R, clearColor.G, clearColor.B, clearColor.A);
      DoClear();
    }
    public void Clear(float r, float g, float b, float a)
    {
      CheckInitialized();
      Gl.glClearColor(r, g, b, a);
      DoClear();
    }
    public void Clear(ColorEX c)
    {
      CheckInitialized();
      Gl.glClearColor(c.R, c.G, c.B, c.A);
      DoClear();
    }
 
    private void DoClear() 
    {
      CheckInitialized();
/*      if((clrMode & Window.ClearBuffer) != 0)
        Gl.glColorMask(Gl.GL_TRUE, Gl.GL_TRUE, Gl.GL_TRUE, Gl.GL_TRUE);
      if((clrMode & Window.ClearStencil) != 0)
        Gl.glStencilMask(Gl.GL_TRUE);
      if((clrMode & Window.ClearZBuffer) != 0)
        Gl.glDepthMask(Gl.GL_TRUE);*/
      Gl.glClear(clrMode);
    }
    #endregion
 
    /// <summary>
    /// Swaps the buffer (double/triple)
    /// </summary>
    public void Swap() 
    {
      CheckInitialized();
      Gdi.SwapBuffersFast(hDC);
    }
    #region Miembros de IDisposable
 
    public void Dispose()
    {
      if(initialized)
        DeleteContext();
    }
 
    #endregion
  }
}
 

Download renderingcontext.cs renderingcontext.cs - 5.4 KB