Commits

Tobias Bohnen committed 119450a

* added new examples

Comments (0)

Files changed (36)

             var lines = new List<string>();
             var buffer = new List<byte>();
 
-            for (; ; )
+            for (;;)
             {
                 byte* b = *byteArray;
-                for (; ; )
+                for (;;)
                 {
                     if (b == null || *b == '\0')
                     {
             }
         }
 
+        public string LastListing
+        {
+            get
+            {
+                return Marshal.PtrToStringAnsi(CgNativeMethods.cgGetLastListing(this.Handle));
+            }
+
+            set
+            {
+                CgNativeMethods.cgSetLastListing(this.Handle, value);
+            }
+        }
+
         public ParameterSettingMode ParameterSettingMode
         {
             get
             return ptr == IntPtr.Zero ? null : new State(ptr);
         }
 
-        public string GetLastListing()
-        {
-            return Marshal.PtrToStringAnsi(CgNativeMethods.cgGetLastListing(this.Handle));
-        }
-
         public Effect GetNamedEffect(string name)
         {
             var ptr = CgNativeMethods.cgGetNamedEffect(this.Handle, name);

CgNet/D3D9/CgD3D9.cs

     using System;
     using System.Runtime.InteropServices;
 
-    using ParameterType = global::CgNet.ParameterType;
+    using ParameterType = CgNet.ParameterType;
 
-    using ResourceType = global::CgNet.ResourceType;
+    using ResourceType = CgNet.ResourceType;
 
     using SlimDX;
     using SlimDX.Direct3D9;

CgNet/D3D9/CgD3D9Context.cs

  */
 namespace CgNet.D3D9
 {
-    using CgNet.D3D9;
-
     public static class CgD3D9Context
     {
         #region Methods

CgNet/D3D9/CgD3D9NativeMethods.cs

         //CGD3D9DLL_API void CGD3D9ENTRY cgD3D9EnableDebugTracing(CGbool enable);
         [DllImport(CgD3D9NativeLibrary, CallingConvention = Convention)]
         [SuppressUnmanagedCodeSecurity]
-        internal static extern void cgD3D9EnableDebugTracing([MarshalAs(UnmanagedType.Bool)]bool enable);
+        internal static extern void cgD3D9EnableDebugTracing([MarshalAs(UnmanagedType.Bool)] bool enable);
 
         //CGD3D9DLL_API HRESULT CGD3D9ENTRY cgD3D9EnableParameterShadowing(CGprogram prog, CGbool enable);
         [DllImport(CgD3D9NativeLibrary, CallingConvention = Convention)]
         [SuppressUnmanagedCodeSecurity]
-        internal static extern int cgD3D9EnableParameterShadowing(IntPtr prog, [MarshalAs(UnmanagedType.Bool)]bool enable);
+        internal static extern int cgD3D9EnableParameterShadowing(IntPtr prog, [MarshalAs(UnmanagedType.Bool)] bool enable);
 
         //CGD3D9DLL_API IDirect3DDevice9 * CGD3D9ENTRY cgD3D9GetDevice(void);
         [DllImport(CgD3D9NativeLibrary, CallingConvention = Convention)]
         //CGD3D9DLL_API HRESULT CGD3D9ENTRY cgD3D9LoadProgram(CGprogram prog, CGbool paramShadowing, DWORD assemFlags);
         [DllImport(CgD3D9NativeLibrary, CallingConvention = Convention)]
         [SuppressUnmanagedCodeSecurity]
-        internal static extern int cgD3D9LoadProgram(IntPtr prog, [MarshalAs(UnmanagedType.Bool)]bool paramShadowing, uint assemFlags);
+        internal static extern int cgD3D9LoadProgram(IntPtr prog, [MarshalAs(UnmanagedType.Bool)] bool paramShadowing, uint assemFlags);
 
         //CGD3D9DLL_API void CGD3D9ENTRY cgD3D9RegisterStates(CGcontext ctx);
         [DllImport(CgD3D9NativeLibrary, CallingConvention = Convention)]
         //CGD3D9DLL_API void CGD3D9ENTRY cgD3D9SetManageTextureParameters(CGcontext ctx, CGbool flag);
         [DllImport(CgD3D9NativeLibrary, CallingConvention = Convention)]
         [SuppressUnmanagedCodeSecurity]
-        internal static extern void cgD3D9SetManageTextureParameters(IntPtr ctx, [MarshalAs(UnmanagedType.Bool)]bool flag);
+        internal static extern void cgD3D9SetManageTextureParameters(IntPtr ctx, [MarshalAs(UnmanagedType.Bool)] bool flag);
 
         //CGD3D9DLL_API HRESULT CGD3D9ENTRY cgD3D9SetSamplerState(CGparameter param, D3DSAMPLERSTATETYPE type, DWORD value);
         [DllImport(CgD3D9NativeLibrary, CallingConvention = Convention)]

CgNet/D3D9/CgD3D9Parameter.cs

 {
     using System;
 
-    using CgNet.D3D9;
-
     using SlimDX.Direct3D9;
 
     public static class CgD3D9Parameter

CgNet/GL/CgGLBuffer.cs

  */
 namespace CgNet.GL
 {
-    using System;
-
-    using Buffer = Buffer;
-
-    using CgNet.GL;
-
     public static class CgGLBuffer
     {
         #region Methods

CgNet/GL/CgGLContext.cs

 {
     using System;
 
-    using Buffer = Buffer;
-
-    using CgNet.GL;
+    using Buffer = CgNet.Buffer;
 
     using OpenTK.Graphics.OpenGL;
 

CgNet/GL/CgGLNativeMethods.cs

     using OpenTK;
     using OpenTK.Graphics.OpenGL;
 
-    /// <summary>
-    ///     Cg core runtime binding for .NET, implementing Cg 1.4.1.
-    /// </summary>
-    /// <remarks>
-    ///     Binds functions and definitions in cgGL.dll or libcgGL.so.
-    /// </remarks>
     internal static class CgGLNativeMethods
     {
         #region Fields

CgNet/GL/CgGLParameter.cs

     using System;
     using System.Runtime.InteropServices;
 
-    using CgNet;
-    using CgNet.GL;
-
     using OpenTK;
     using OpenTK.Graphics.OpenGL;
 

CgNet/GL/CgGLProgram.cs

  */
 namespace CgNet.GL
 {
-    using System;
-
-    using CgNet;
-    using CgNet.GL;
-
     public static class CgGLProgram
     {
         #region Methods
             CgNativeMethods.cgResetPassState(this.Handle);
         }
 
+        public void SetLastListing(string listing)
+        {
+            CgNativeMethods.cgSetLastListing(this.Handle, listing);
+        }
+
         public void SetState()
         {
             CgNativeMethods.cgSetPassState(this.Handle);

CgNet/StateAssignment.cs

             return CgNativeMethods.cgSetBoolArrayStateAssignment(this.Handle, value);
         }
 
+        public void SetLastListing(string listing)
+        {
+            CgNativeMethods.cgSetLastListing(this.Handle, listing);
+        }
+
         public bool SetProgram(Program program)
         {
             return CgNativeMethods.cgSetProgramStateAssignment(this.Handle, program.Handle);

CgNet/Technique.cs

                                                };
         }
 
+        public void SetLastListing(string listing)
+        {
+            CgNativeMethods.cgSetLastListing(this.Handle, listing);
+        }
+
         public bool Validate()
         {
             return CgNativeMethods.cgValidateTechnique(this.Handle);

ExampleBrowser/Data/C5E2v_fragmentLighting.cg

+// This is C5E2v_fragmentLighting from "The Cg Tutorial" (Addison-Wesley, ISBN
+// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 124.
+
+void C5E2v_fragmentLighting(float4 position : POSITION,
+                            float3 normal   : NORMAL,
+
+                        out float4 oPosition : POSITION,
+                        out float3 objectPos : TEXCOORD0,
+                        out float3 oNormal   : TEXCOORD1,
+
+                    uniform float4x4 modelViewProj)
+{
+  oPosition = mul(modelViewProj, position);
+  objectPos = position.xyz;
+  oNormal = normal;
+}
+

ExampleBrowser/Data/C5E3f_basicLight.cg

+// This is C5E3f_basicLight from "The Cg Tutorial" (Addison-Wesley, ISBN
+// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 125.
+
+void C5E3f_basicLight(float4 position  : TEXCOORD0,                        
+                      float3 normal    : TEXCOORD1,
+
+                  out float4 color     : COLOR,
+
+              uniform float3 globalAmbient,
+              uniform float3 lightColor,
+              uniform float3 lightPosition,
+              uniform float3 eyePosition,
+              uniform float3 Ke,
+              uniform float3 Ka,
+              uniform float3 Kd,
+              uniform float3 Ks,
+              uniform float  shininess)
+{
+  float3 P = position.xyz;
+  float3 N = normalize(normal);
+
+  // Compute emissive term
+  float3 emissive = Ke;
+
+  // Compute ambient term
+  float3 ambient = Ka * globalAmbient;
+
+  // Compute the diffuse term
+  float3 L = normalize(lightPosition - P);
+  float diffuseLight = max(dot(L, N), 0);
+  float3 diffuse = Kd * lightColor * diffuseLight;
+
+  // Compute the specular term
+  float3 V = normalize(eyePosition - P);
+  float3 H = normalize(L + V);
+  float specularLight = pow(max(dot(H, N), 0), shininess);
+  if (diffuseLight <= 0) specularLight = 0;
+  float3 specular = Ks * lightColor * specularLight;
+
+  color.xyz = emissive + ambient + diffuse + specular;
+  color.w = 1;
+}

ExampleBrowser/Data/C9E5v_projTex.cg

+// This is C9E5v_projTex from "The Cg Tutorial" (Addison-Wesley, ISBN
+// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 254.
+
+void C9E5v_projTex(float4 position : POSITION,
+                   float3 normal   : NORMAL,
+       
+               out float4 oPosition       : POSITION,
+               out float4 texCoordProj    : TEXCOORD0,
+               out float4 diffuseLighting : COLOR,
+       
+           uniform float Kd,
+           uniform float4x4 modelViewProj,
+           uniform float3   lightPosition, 
+           uniform float4x4 textureMatrix)
+{
+  oPosition = mul(modelViewProj, position);
+
+  // Compute texture coordinates for 
+  // querying the projective texture
+  texCoordProj = mul(textureMatrix, position);
+  
+  // Compute diffuse lighting
+  float3 N = normalize(normal);
+  float3 L = normalize(lightPosition - position.xyz);
+  diffuseLighting = Kd * max(dot(N, L), 0);
+}

ExampleBrowser/Data/C9E6f_projTex.cg

+// This is C9E6f_projTex from "The Cg Tutorial" (Addison-Wesley, ISBN
+// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 254.
+
+void C9E6f_projTex(float4 texCoordProj    : TEXCOORD0,
+                   float4 diffuseLighting : COLOR,
+
+               out float4 color : COLOR,
+
+           uniform sampler2D projectiveMap)
+{
+  // Fetch color from the projective texture
+  float4 textureColor = tex2Dproj(projectiveMap, 
+                                  texCoordProj);
+
+  color = textureColor * diffuseLighting;
+}   

ExampleBrowser/ExampleBrowser.csproj

     <FileAlignment>512</FileAlignment>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
-    <PlatformTarget>AnyCPU</PlatformTarget>
+    <PlatformTarget>x86</PlatformTarget>
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
   <ItemGroup>
     <Compile Include="Data\ImageBrick.cs" />
     <Compile Include="Examples\OpenTK\Basic\Bulge.cs" />
+    <Compile Include="Examples\OpenTK\Basic\FragmentLighting.cs" />
+    <Compile Include="Examples\OpenTK\Basic\ProjectiveTexturing.cs" />
+    <Compile Include="Examples\OpenTK\Basic\VertexLighting.cs" />
     <Compile Include="Examples\SlimDX\Basic\VertexFragmentProgram.cs" />
     <Compile Include="Examples\OpenTK\Example.cs" />
     <Compile Include="Examples\SlimDX\Example.cs" />
     <Compile Include="ExampleSelector.Designer.cs">
       <DependentUpon>ExampleSelector.cs</DependentUpon>
     </Compile>
+    <Compile Include="NativeMethods.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Examples\OpenTK\Basic\UniformParameter.cs" />
     <None Include="Data\C5E1v_basicLight.cg">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
+    <None Include="Data\C5E2v_fragmentLighting.cg">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+    <None Include="Data\C5E3f_basicLight.cg">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
     <None Include="Data\C6E1v_bulge.cg">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
     <None Include="Data\C8E2f_bumpSurf.cg">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
+    <None Include="Data\C9E5v_projTex.cg">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+    <None Include="Data\C9E6f_projTex.cg">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>

ExampleBrowser/Examples/IExample.cs

 {
     using System;
 
-    interface IExample : IDisposable
+    internal interface IExample : IDisposable
     {
         #region Methods
 

ExampleBrowser/Examples/OpenTK/Basic/Bulge.cs

 namespace ExampleBrowser.Examples.OpenTK.Basic
 {
     using System;
-    using System.Runtime.InteropServices;
 
     using CgNet;
     using CgNet.GL;
     {
         #region Fields
 
-        private const float myPi = 3.14159265358979323846f;
         private const string MyVertexProgramFileName = "Data/C6E1v_bulge.cg";
         private const string MyVertexProgramName = "C6E1v_bulge";
 
             this.vertexParamModelViewProj.SetMatrix(modelViewProjMatrix);
             this.vertexParamKd.Set(0.1f, 0.7f, 0.1f, 1); /* Green */
             vertexProgram.UpdateParameters();
-            glutSolidSphere(1.0, 40, 40);
+            NativeMethods.glutSolidSphere(1.0, 40, 40);
 
             /*** Render red solid bulging torus ***/
 
             this.vertexParamModelViewProj.SetMatrix(modelViewProjMatrix);
             this.vertexParamKd.Set(0.8f, 0.1f, 0.1f, 1); /* Red */
             vertexProgram.UpdateParameters();
-            glutSolidTorus(0.15, 1.7, 40, 40);
+            NativeMethods.glutSolidTorus(0.15, 1.7, 40, 40);
 
             /*** Render light as emissive yellow ball ***/
 
             /* Set matrix parameter with row-major matrix. */
             this.lightVertexParamModelViewProj.SetMatrix(modelViewProjMatrix);
             vertexProgram.UpdateParameters();
-            glutSolidSphere(0.1, 12, 12);
+            NativeMethods.glutSolidSphere(0.1, 12, 12);
 
             CgGL.DisableProfile(vertexProfile);
 
         protected override void OnUpdateFrame(FrameEventArgs e)
         {
             /* Repeat rotating light around front 180 degrees. */
-            if (myLightAngle > myPi / 2)
+            if (myLightAngle > Pi / 2)
             {
-                myLightAngle = myPi / 2;
+                myLightAngle = Pi / 2;
                 lightVelocity = -lightVelocity;
             }
-            else if (myLightAngle < -myPi / 2)
+            else if (myLightAngle < -Pi / 2)
             {
-                myLightAngle = -myPi / 2;
+                myLightAngle = -Pi / 2;
                 lightVelocity = -lightVelocity;
             }
             myLightAngle += lightVelocity; /* Add a small angle (in radians). */
 
         #endregion Protected Methods
 
-        #region Private Static Methods
-
-        [DllImport("glut32.dll")]
-        private static extern void glutSolidSphere(double radius, int slices, int stacks);
-
-        [DllImport("glut32.dll")]
-        private static extern void glutSolidTorus(double radius, double outerRadius, int nsides, int rings);
-
-        #endregion Private Static Methods
-
         #region Private Methods
 
         private void Reshape(int width, int height)

ExampleBrowser/Examples/OpenTK/Basic/BumpMapWall.cs

 namespace ExampleBrowser.Examples.OpenTK.Basic
 {
     using System;
-    using System.Runtime.InteropServices;
 
     using All = global::OpenTK.Graphics.OpenGL.All;
 
     using EnableCap = global::OpenTK.Graphics.OpenGL.EnableCap;
 
     using ExampleBrowser.Data;
-    using ExampleBrowser.Examples.OpenTK;
 
     using GL = global::OpenTK.Graphics.OpenGL.GL;
 
 
             GL.Translate(lightPosition[0], lightPosition[1], lightPosition[2]);
             GL.Color3(0.8f, 0.8f, 0.1f); /* yellow */
-            glutSolidSphere(0.4, 12, 12);
+            NativeMethods.glutSolidSphere(0.4, 12, 12);
 
             this.SwapBuffers();
         }
 
         #endregion Protected Methods
 
-        #region Private Static Methods
-
-        [DllImport("glut32.dll")]
-        private static extern void glutSolidSphere(double radius, int slices, int stacks);
-
-        #endregion Private Static Methods
-
         #endregion Methods
     }
 }

ExampleBrowser/Examples/OpenTK/Basic/FragmentLighting.cs

+namespace ExampleBrowser.Examples.OpenTK.Basic
+{
+    using System;
+
+    using CgNet;
+    using CgNet.GL;
+
+    using global::OpenTK;
+    using global::OpenTK.Graphics.OpenGL;
+    using global::OpenTK.Input;
+
+    [ExampleDescription(NodePath = "OpenTK/Basic/10 Fragment Lighting")]
+    public class FragmentLighting : Example
+    {
+        #region Fields
+
+        private readonly float[] myGlobalAmbient = { 0.1f, 0.1f, 0.1f }; /* Dim */
+        private readonly float[] myLightColor = { 0.95f, 0.95f, 0.95f }; /* White */
+        private readonly float[] myProjectionMatrix = new float[16];
+
+        private Parameter myCgVertexParam_modelViewProj, myCgFragmentParam_globalAmbient, myCgFragmentParam_lightColor, myCgFragmentParam_lightPosition, myCgFragmentParam_eyePosition, myCgFragmentParam_Ke, myCgFragmentParam_Ka, myCgFragmentParam_Kd, myCgFragmentParam_Ks, myCgFragmentParam_shininess;
+        private double myLightAngle = -0.4f; /* Angle light rotates around scene. */
+        private string myVertexProgramFileName = "Data/C5E2v_fragmentLighting.cg",
+            /* Page 124 */    myVertexProgramName = "C5E2v_fragmentLighting",
+                  myFragmentProgramFileName = "Data/C5E3f_basicLight.cg",
+            /* Page 125 */    myFragmentProgramName = "C5E3f_basicLight";
+        private ProfileType vertexProfile, fragmentProfile;
+        private Program vertexProgram, fragmentProgram;
+
+        #endregion Fields
+
+        #region Constructors
+
+        public FragmentLighting()
+            : base("10_fragment_lighting")
+        {
+        }
+
+        #endregion Constructors
+
+        #region Methods
+
+        #region Protected Methods
+
+        /// <summary>
+        /// Setup OpenGL and load resources here.
+        /// </summary>
+        /// <param name="e">Not used.</param>
+        protected override void OnLoad(EventArgs e)
+        {
+            GL.ClearColor(0.1f, 0.3f, 0.6f, 0.0f); /* Blue background */
+            GL.Enable(EnableCap.DepthTest);
+            this.CgContext = CgNet.Context.Create();
+
+            CgGL.SetDebugMode(false);
+            this.CgContext.ParameterSettingMode = ParameterSettingMode.Deferred;
+
+            this.vertexProfile = CgGL.GetLatestProfile(ProfileClass.Vertex);
+            CgGL.SetOptimalOptions(this.vertexProfile);
+
+            this.vertexProgram =
+                this.CgContext.CreateProgramFromFile(
+                    ProgramType.Source, /* Program in human-readable form */
+                    this.myVertexProgramFileName, /* Name of file containing program */
+                    this.vertexProfile, /* Profile: OpenGL ARB vertex program */
+                    this.myVertexProgramName, /* Entry function name */
+                    null); /* No extra compiler options */
+            this.vertexProgram.Load();
+
+            this.myCgVertexParam_modelViewProj = this.vertexProgram.GetNamedParameter("modelViewProj");
+
+            this.fragmentProfile = CgGL.GetLatestProfile(ProfileClass.Fragment);
+            CgGL.SetOptimalOptions(this.fragmentProfile);
+
+            /* Specify "color passthrough" fragment program with a string. */
+            this.fragmentProgram =
+                this.CgContext.CreateProgramFromFile(
+                    ProgramType.Source, /* Program in human-readable form */
+                    this.myFragmentProgramFileName,
+                    this.fragmentProfile, /* Profile: latest fragment profile */
+                    this.myFragmentProgramName, /* Entry function name */
+                    null); /* No extra compiler options */
+            this.fragmentProgram.Load();
+
+            this.myCgFragmentParam_globalAmbient = this.fragmentProgram.GetNamedParameter("globalAmbient");
+            this.myCgFragmentParam_lightColor = this.fragmentProgram.GetNamedParameter("lightColor");
+            this.myCgFragmentParam_lightPosition = this.fragmentProgram.GetNamedParameter("lightPosition");
+            this.myCgFragmentParam_eyePosition = this.fragmentProgram.GetNamedParameter("eyePosition");
+            this.myCgFragmentParam_Ke = this.fragmentProgram.GetNamedParameter("Ke");
+            this.myCgFragmentParam_Ka = this.fragmentProgram.GetNamedParameter("Ka");
+            this.myCgFragmentParam_Kd = this.fragmentProgram.GetNamedParameter("Kd");
+            this.myCgFragmentParam_Ks = this.fragmentProgram.GetNamedParameter("Ks");
+            this.myCgFragmentParam_shininess = this.fragmentProgram.GetNamedParameter("shininess");
+
+            /* Set light source color parameters once. */
+            this.myCgFragmentParam_globalAmbient.Set(this.myGlobalAmbient);
+            this.myCgFragmentParam_lightColor.Set(this.myLightColor);
+        }
+
+        /// <summary>
+        /// Add your game rendering code here.
+        /// </summary>
+        /// <param name="e">Contains timing information.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnRenderFrame(FrameEventArgs e)
+        {
+            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
+
+            /* World-space positions for light and eye. */
+            float[] eyePosition = { 0, 0, 13, 1 };
+            float[] lightPosition = {
+                                        5 * (float)Math.Sin(this.myLightAngle),
+                                        1.5f,
+                                        5 * (float)Math.Cos(this.myLightAngle), 1
+                                    };
+
+            float[] translateMatrix = new float[16], rotateMatrix = new float[16], modelMatrix = new float[16], invModelMatrix = new float[16],
+                    viewMatrix = new float[16], modelViewMatrix = new float[16], modelViewProjMatrix = new float[16];
+            float[] objSpaceEyePosition = new float[4], objSpaceLightPosition = new float[4];
+
+            BuildLookAtMatrix(eyePosition[0], eyePosition[1], eyePosition[2],
+                              0, 0, 0,
+                              0, 1, 0,
+                              viewMatrix);
+
+            this.vertexProgram.Bind();
+            CgGL.EnableProfile(this.vertexProfile);
+
+            this.fragmentProgram.Bind();
+            CgGL.EnableProfile(this.fragmentProfile);
+
+            this.setBrassMaterial();
+
+            /* modelView = rotateMatrix * translateMatrix */
+            MakeRotateMatrix(70, 1, 1, 1, rotateMatrix);
+            MakeTranslateMatrix(2, 0, 0, translateMatrix);
+            MultMatrix(modelMatrix, translateMatrix, rotateMatrix);
+
+            /* invModelMatrix = inverse(modelMatrix) */
+            InvertMatrix(invModelMatrix, modelMatrix);
+
+            /* Transform world-space eye and light positions to sphere's object-space. */
+            Transform(objSpaceEyePosition, invModelMatrix, eyePosition);
+            this.myCgFragmentParam_eyePosition.Set3(objSpaceEyePosition);
+            Transform(objSpaceLightPosition, invModelMatrix, lightPosition);
+            this.myCgFragmentParam_lightPosition.Set3(objSpaceLightPosition);
+
+            /* modelViewMatrix = viewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, viewMatrix, modelMatrix);
+
+            /* modelViewProj = projectionMatrix * modelViewMatrix */
+            MultMatrix(modelViewProjMatrix, this.myProjectionMatrix, modelViewMatrix);
+
+            /* Set matrix parameter with row-major matrix. */
+            this.myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            this.vertexProgram.UpdateParameters();
+            this.fragmentProgram.UpdateParameters();
+            NativeMethods.glutSolidSphere(2.0, 40, 40);
+
+            /*** Render red plastic solid cone ***/
+
+            this.setRedPlasticMaterial();
+
+            /* modelView = viewMatrix * translateMatrix */
+            MakeTranslateMatrix(-2, -1.5f, 0, translateMatrix);
+            MakeRotateMatrix(90, 1, 0, 0, rotateMatrix);
+            MultMatrix(modelMatrix, translateMatrix, rotateMatrix);
+
+            /* invModelMatrix = inverse(modelMatrix) */
+            InvertMatrix(invModelMatrix, modelMatrix);
+
+            /* Transform world-space eye and light positions to sphere's object-space. */
+            Transform(objSpaceEyePosition, invModelMatrix, eyePosition);
+            this.myCgFragmentParam_eyePosition.Set3(objSpaceEyePosition);
+            Transform(objSpaceLightPosition, invModelMatrix, lightPosition);
+            this.myCgFragmentParam_lightPosition.Set3(objSpaceLightPosition);
+
+            /* modelViewMatrix = viewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, viewMatrix, modelMatrix);
+
+            /* modelViewProj = projectionMatrix * modelViewMatrix */
+            MultMatrix(modelViewProjMatrix, this.myProjectionMatrix, modelViewMatrix);
+
+            /* Set matrix parameter with row-major matrix. */
+            this.myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            this.vertexProgram.UpdateParameters();
+            this.fragmentProgram.UpdateParameters();
+            NativeMethods.glutSolidCone(1.5, 3.5, 30, 30);
+
+            /*** Render light as emissive white ball ***/
+
+            /* modelView = translateMatrix */
+            MakeTranslateMatrix(lightPosition[0], lightPosition[1], lightPosition[2],
+                                modelMatrix);
+
+            /* modelViewMatrix = viewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, viewMatrix, modelMatrix);
+
+            /* modelViewProj = projectionMatrix * modelViewMatrix */
+            MultMatrix(modelViewProjMatrix, this.myProjectionMatrix, modelViewMatrix);
+
+            this.setEmissiveLightColorOnly();
+            /* Avoid degenerate lightPosition. */
+            this.myCgFragmentParam_lightPosition.Set(0f, 0f, 0f);
+
+            /* Set matrix parameter with row-major matrix. */
+            this.myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            this.vertexProgram.UpdateParameters();
+            this.fragmentProgram.UpdateParameters();
+            NativeMethods.glutSolidSphere(0.2, 12, 12);
+
+            CgGL.DisableProfile(this.vertexProfile);
+            CgGL.DisableProfile(this.fragmentProfile);
+
+            this.SwapBuffers();
+        }
+
+        /// <summary>
+        /// Respond to resize events here.
+        /// </summary>
+        /// <param name="e">Contains information on the new GameWindow size.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnResize(EventArgs e)
+        {
+            this.Reshape(this.Width, this.Height);
+            GL.Viewport(0, 0, this.Width, this.Height);
+        }
+
+        protected override void OnUnload(EventArgs e)
+        {
+            base.OnUnload(e);
+            this.vertexProgram.Dispose();
+            this.fragmentProgram.Dispose();
+            this.CgContext.Dispose();
+        }
+
+        /// <summary>
+        /// Add your game logic here.
+        /// </summary>
+        /// <param name="e">Contains timing information.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnUpdateFrame(FrameEventArgs e)
+        {
+            this.myLightAngle += 0.008f; /* Add a small angle (in radians). */
+            if (this.myLightAngle > 2 * Pi)
+            {
+                this.myLightAngle -= 2 * Pi;
+            }
+
+            if (this.Keyboard[Key.Escape])
+                this.Exit();
+        }
+
+        #endregion Protected Methods
+
+        #region Private Methods
+
+        private void Reshape(int width, int height)
+        {
+            double aspectRatio = (float)width / height;
+            const double FieldOfView = 40.0;
+
+            /* Build projection matrix once. */
+            BuildPerspectiveMatrix(FieldOfView, aspectRatio,
+                                   1.0, 20.0, /* Znear and Zfar */
+                                   this.myProjectionMatrix);
+        }
+
+        private void setBrassMaterial()
+        {
+            float[] brassEmissive = { 0.0f, 0.0f, 0.0f },
+                    brassAmbient = { 0.33f, 0.22f, 0.03f },
+                    brassDiffuse = { 0.78f, 0.57f, 0.11f },
+                    brassSpecular = { 0.99f, 0.91f, 0.81f };
+            float brassShininess = 27.8f;
+
+            this.myCgFragmentParam_Ke.Set(brassEmissive);
+            this.myCgFragmentParam_Ka.Set(brassAmbient);
+            this.myCgFragmentParam_Kd.Set(brassDiffuse);
+            this.myCgFragmentParam_Ks.Set(brassSpecular);
+            this.myCgFragmentParam_shininess.Set(brassShininess);
+        }
+
+        private void setEmissiveLightColorOnly()
+        {
+            float[] zero = { 0.0f, 0.0f, 0.0f };
+
+            this.myCgFragmentParam_Ke.Set(this.myLightColor);
+            this.myCgFragmentParam_Ka.Set(zero);
+            this.myCgFragmentParam_Kd.Set(zero);
+            this.myCgFragmentParam_Ks.Set(zero);
+            this.myCgFragmentParam_shininess.Set(0);
+        }
+
+        private void setRedPlasticMaterial()
+        {
+            float[] redPlasticEmissive = { 0.0f, 0.0f, 0.0f },
+                    redPlasticAmbient = { 0.0f, 0.0f, 0.0f },
+                    redPlasticDiffuse = { 0.5f, 0.0f, 0.0f },
+                    redPlasticSpecular = { 0.7f, 0.6f, 0.6f };
+            float redPlasticShininess = 32.0f;
+
+            this.myCgFragmentParam_Ke.Set(redPlasticEmissive);
+            this.myCgFragmentParam_Ka.Set(redPlasticAmbient);
+            this.myCgFragmentParam_Kd.Set(redPlasticDiffuse);
+            this.myCgFragmentParam_Ks.Set(redPlasticSpecular);
+            this.myCgFragmentParam_shininess.Set(redPlasticShininess);
+        }
+
+        #endregion Private Methods
+
+        #endregion Methods
+    }
+}

ExampleBrowser/Examples/OpenTK/Basic/ParticleSystem.cs

 
     using EnableCap = global::OpenTK.Graphics.OpenGL.EnableCap;
 
-    using ExampleBrowser.Examples.OpenTK;
-
     using GL = global::OpenTK.Graphics.OpenGL.GL;
 
     using global::OpenTK;

ExampleBrowser/Examples/OpenTK/Basic/ProjectiveTexturing.cs

+namespace ExampleBrowser.Examples.OpenTK.Basic
+{
+    using System;
+
+    using All = global::OpenTK.Graphics.OpenGL.All;
+
+    using BeginMode = global::OpenTK.Graphics.OpenGL.BeginMode;
+
+    using CgNet;
+    using CgNet.GL;
+
+    using ClearBufferMask = global::OpenTK.Graphics.OpenGL.ClearBufferMask;
+
+    using EnableCap = global::OpenTK.Graphics.OpenGL.EnableCap;
+
+    using ExampleBrowser.Data;
+
+    using GL = global::OpenTK.Graphics.OpenGL.GL;
+
+    using global::OpenTK;
+    using global::OpenTK.Graphics;
+    using global::OpenTK.Input;
+
+    using MatrixMode = global::OpenTK.Graphics.OpenGL.MatrixMode;
+
+    using PixelStoreParameter = global::OpenTK.Graphics.OpenGL.PixelStoreParameter;
+
+    using TextureParameterName = global::OpenTK.Graphics.OpenGL.TextureParameterName;
+
+    using TextureTarget = global::OpenTK.Graphics.OpenGL.TextureTarget;
+
+    [ExampleDescription(NodePath = "OpenTK/Basic/27 Projective Texturing")]
+    public class ProjectiveTexturing : Example
+    {
+        #region Fields
+
+        private float eyeAngle = 0.53f; /* Angle in radians eye rotates around scene. */
+        private float eyeHeight = 5.0f; /* Vertical height of light. */
+        private float[] Kd = { 1, 1, 1 }; /* White. */
+        private float lightAngle = -0.4f; /* Angle light rotates around scene. */
+        private float lightHeight = 2.0f; /* Vertical height of light. */
+        private Parameter myCgVertexParam_modelViewProj, myCgVertexParam_Kd, myCgVertexParam_lightPosition, myCgVertexParam_textureMatrix;
+        string myVertexProgramFileName = "Data/C9E5v_projTex.cg",
+            /* Page 254 */    myVertexProgramName = "C9E5v_projTex",
+                          myFragmentProgramFileName = "Data/C9E6f_projTex.cg",
+            /* Page 254 */    myFragmentProgramName = "C9E6f_projTex";
+        private float[] projectionMatrix = new float[16];
+        private ProfileType vertexProfile, fragmentProfile;
+        private Program vertexProgram, fragmentProgram;
+
+        #endregion Fields
+
+        #region Constructors
+
+        public ProjectiveTexturing()
+            : base("27_projective_texturing", 600, 400)
+        {
+        }
+
+        #endregion Constructors
+
+        #region Methods
+
+        #region Protected Methods
+
+        /// <summary>
+        /// Setup OpenGL and load resources here.
+        /// </summary>
+        /// <param name="e">Not used.</param>
+        protected override void OnLoad(EventArgs e)
+        {
+            GL.ClearColor(0.1f, 0.1f, 0.1f, 0.0f);
+            GL.Enable(EnableCap.DepthTest);
+            this.CgContext = CgNet.Context.Create();
+
+            CgGL.SetDebugMode(false);
+            this.CgContext.ParameterSettingMode = ParameterSettingMode.Deferred;
+            this.CgContext.SetManageTextureParameters(true);
+
+            this.vertexProfile = CgGL.GetLatestProfile(ProfileClass.Vertex);
+            CgGL.SetOptimalOptions(this.vertexProfile);
+
+            this.vertexProgram =
+                this.CgContext.CreateProgramFromFile(
+                    ProgramType.Source, /* Program in human-readable form */
+                    this.myVertexProgramFileName, /* Name of file containing program */
+                    this.vertexProfile, /* Profile: OpenGL ARB vertex program */
+                    this.myVertexProgramName, /* Entry function name */
+                    null); /* No extra compiler options */
+            this.vertexProgram.Load();
+
+            this.myCgVertexParam_modelViewProj = this.vertexProgram.GetNamedParameter("modelViewProj");
+            this.myCgVertexParam_lightPosition = this.vertexProgram.GetNamedParameter("lightPosition");
+            this.myCgVertexParam_Kd = this.vertexProgram.GetNamedParameter("Kd");
+            this.myCgVertexParam_textureMatrix = this.vertexProgram.GetNamedParameter("textureMatrix");
+
+            /* Set light source color parameters once. */
+            this.myCgVertexParam_Kd.Set(this.Kd);
+
+            this.fragmentProfile = CgGL.GetLatestProfile(ProfileClass.Fragment);
+            CgGL.SetOptimalOptions(this.fragmentProfile);
+
+            /* Specify "color passthrough" fragment program with a string. */
+            this.fragmentProgram =
+                this.CgContext.CreateProgramFromFile(
+                    ProgramType.Source, /* Program in human-readable form */
+                    myFragmentProgramFileName,
+                    this.fragmentProfile, /* Profile: latest fragment profile */
+                    myFragmentProgramName, /* Entry function name */
+                    null); /* No extra compiler options */
+            this.fragmentProgram.Load();
+            setupDemonSampler();
+        }
+
+        /// <summary>
+        /// Add your game rendering code here.
+        /// </summary>
+        /// <param name="e">Contains timing information.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnRenderFrame(FrameEventArgs e)
+        {
+            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
+
+            /* World-space positions for light and eye. */
+            float[] eyePosition = {
+                                      10*(float)Math.Sin(eyeAngle),
+                                      eyeHeight,
+                                      10*(float)Math.Cos(eyeAngle),
+                                      1
+                                  };
+            float[] lightPosition = {
+                                        4.5f * (float)Math.Sin(this.lightAngle),
+                                        lightHeight,
+                                        4.5f * (float)Math.Cos(this.lightAngle),
+                                        1
+                                    };
+            float[] center = { 0, 0, 0 };  /* eye and light point at the origin */
+            float[] up = { 0, 1, 0 };      /* up is positive Y direction */
+
+            float[] translateMatrix = new float[16],
+                rotateMatrix = new float[16],
+                modelMatrix = new float[16],
+                invModelMatrix = new float[16],
+                    eyeViewMatrix = new float[16],
+                    modelViewMatrix = new float[16],
+                    modelViewProjMatrix = new float[16],
+                    textureMatrix = new float[16],
+                    lightViewMatrix = new float[16];
+            float[] objSpaceLightPosition = new float[4];
+
+            BuildLookAtMatrix(eyePosition[0], eyePosition[1], eyePosition[2],
+                              center[0], center[1], center[2],
+                              up[0], up[1], up[2],
+                              eyeViewMatrix);
+
+            BuildLookAtMatrix(lightPosition[0], lightPosition[1], lightPosition[2],
+                  center[0], center[1], center[2],
+                  up[0], -up[1], up[2],
+                  lightViewMatrix);
+
+            this.vertexProgram.Bind();
+            CgGL.EnableProfile(this.vertexProfile);
+
+            this.fragmentProgram.Bind();
+            CgGL.EnableProfile(this.fragmentProfile);
+
+            /* modelView = rotateMatrix * translateMatrix */
+            MakeRotateMatrix(70, 1, 1, 1, rotateMatrix);
+            MakeTranslateMatrix(2, 0, 0, translateMatrix);
+            MultMatrix(modelMatrix, translateMatrix, rotateMatrix);
+
+            /* invModelMatrix = inverse(modelMatrix) */
+            InvertMatrix(invModelMatrix, modelMatrix);
+
+            /* Transform world-space light positions to sphere's object-space. */
+            Transform(objSpaceLightPosition, invModelMatrix, lightPosition);
+            this.myCgVertexParam_lightPosition.Set3(objSpaceLightPosition);
+
+            /* modelViewMatrix = viewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, eyeViewMatrix, modelMatrix);
+
+            /* modelViewProj = projectionMatrix * modelViewMatrix */
+            MultMatrix(modelViewProjMatrix, projectionMatrix, modelViewMatrix);
+
+            buildTextureMatrix(lightViewMatrix, modelMatrix, textureMatrix);
+
+            /* Set matrix parameter with row-major matrix. */
+            this.myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            myCgVertexParam_textureMatrix.SetMatrix(textureMatrix);
+            this.vertexProgram.UpdateParameters();
+            this.fragmentProgram.UpdateParameters();
+            NativeMethods.glutSolidSphere(2.0, 40, 40);
+
+            /*** Render solid cube ***/
+
+            /* modelView = viewMatrix * translateMatrix */
+            MakeTranslateMatrix(-2, -1.0f, 0, translateMatrix);
+            MakeRotateMatrix(90, 1, 0, 0, rotateMatrix);
+            MultMatrix(modelMatrix, translateMatrix, rotateMatrix);
+
+            /* invModelMatrix = inverse(modelMatrix) */
+            InvertMatrix(invModelMatrix, modelMatrix);
+
+            /* Transform world-space eye and light positions to sphere's object-space. */
+            Transform(objSpaceLightPosition, invModelMatrix, lightPosition);
+            this.myCgVertexParam_lightPosition.Set3(objSpaceLightPosition);
+
+            /* modelViewMatrix = viewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, eyeViewMatrix, modelMatrix);
+
+            /* modelViewProj = projectionMatrix * modelViewMatrix */
+            MultMatrix(modelViewProjMatrix, this.projectionMatrix, modelViewMatrix);
+
+            buildTextureMatrix(lightViewMatrix, modelMatrix, textureMatrix);
+
+            /* Set matrix parameter with row-major matrix. */
+            this.myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            this.myCgVertexParam_textureMatrix.SetMatrix(textureMatrix);
+            this.vertexProgram.UpdateParameters();
+            this.fragmentProgram.UpdateParameters();
+            NativeMethods.glutSolidCube(2.5);
+
+            /*** Render floor ***/
+
+            /* modelView = translateMatrix */
+            MultMatrix(modelViewProjMatrix, projectionMatrix, eyeViewMatrix);
+            MakeTranslateMatrix(0, 0, 0, modelMatrix);
+            buildTextureMatrix(lightViewMatrix, modelMatrix, textureMatrix);
+
+            myCgVertexParam_lightPosition.Set3(lightPosition);
+            myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            myCgVertexParam_textureMatrix.SetMatrix(textureMatrix);
+
+            vertexProgram.UpdateParameters();
+            fragmentProgram.UpdateParameters();
+
+            GL.Enable(EnableCap.CullFace);
+
+            GL.Begin(BeginMode.Quads);
+            {
+                GL.Normal3(0f, 1f, 0f);
+                GL.Vertex3(12f, -2, -12);
+                GL.Vertex3(-12f, -2, -12);
+                GL.Vertex3(-12f, -2, 12);
+                GL.Vertex3(12f, -2, 12);
+
+                GL.Normal3(0f, 0, 1);
+                GL.Vertex3(-12f, -2, -12);
+                GL.Vertex3(12f, -2, -12);
+                GL.Vertex3(12f, 10, -12);
+                GL.Vertex3(-12f, 10, -12);
+
+                GL.Normal3(0f, 0, -1);
+                GL.Vertex3(12f, -2, 12);
+                GL.Vertex3(-12f, -2, 12);
+                GL.Vertex3(-12f, 10, 12);
+                GL.Vertex3(12f, 10, 12);
+
+                GL.Normal3(0f, -1, 0);
+                GL.Vertex3(-12f, 10, -12);
+                GL.Vertex3(12f, 10, -12);
+                GL.Vertex3(12f, 10, 12);
+                GL.Vertex3(-12f, 10, 12);
+
+                GL.Normal3(1f, 0, 0);
+                GL.Vertex3(-12f, -2, 12);
+                GL.Vertex3(-12f, -2, -12);
+                GL.Vertex3(-12f, 10, -12);
+                GL.Vertex3(-12f, 10, 12);
+
+                GL.Normal3(-1f, 0, 0);
+                GL.Vertex3(12f, -2, -12);
+                GL.Vertex3(12f, -2, 12);
+                GL.Vertex3(12f, 10, 12);
+                GL.Vertex3(12f, 10, -12);
+            }
+            GL.End();
+
+            GL.Disable(EnableCap.CullFace);
+
+            CgGL.DisableProfile(vertexProfile);
+
+            CgGL.DisableProfile(fragmentProfile);
+
+            /*** Render light as white cone ***/
+
+            /* modelView = translateMatrix */
+            MakeTranslateMatrix(lightPosition[0], lightPosition[1], lightPosition[2],
+              modelMatrix);
+
+            /* modelViewMatrix = eyeViewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, eyeViewMatrix, modelMatrix);
+
+            GL.PushMatrix();
+            /* glLoadMatrixf expects a column-major matrix but Cg matrices are
+               row-major (matching C/C++ arrays) so used loadMVP to transpose
+               the Cg version. */
+            loadMVP(modelViewMatrix);
+            GL.Color3(1f, 1, 1); /* make sure current color is white */
+            NativeMethods.glutSolidSphere(0.15, 30, 30);
+            GL.PopMatrix();
+
+            this.SwapBuffers();
+        }
+
+        /// <summary>
+        /// Respond to resize events here.
+        /// </summary>
+        /// <param name="e">Contains information on the new GameWindow size.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnResize(EventArgs e)
+        {
+            this.Reshape(this.Width, this.Height);
+            GL.Viewport(0, 0, this.Width, this.Height);
+        }
+
+        protected override void OnUnload(EventArgs e)
+        {
+            base.OnUnload(e);
+            this.vertexProgram.Dispose();
+            this.fragmentProgram.Dispose();
+            this.CgContext.Dispose();
+        }
+
+        /// <summary>
+        /// Add your game logic here.
+        /// </summary>
+        /// <param name="e">Contains timing information.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnUpdateFrame(FrameEventArgs e)
+        {
+            this.lightAngle += 0.008f; /* Add a small angle (in radians). */
+            if (this.lightAngle > 2 * Pi)
+            {
+                this.lightAngle -= 2 * Pi;
+            }
+
+            if (this.Keyboard[Key.Escape])
+                this.Exit();
+        }
+
+        #endregion Protected Methods
+
+        #region Private Static Methods
+
+        static void makeClipToTextureMatrix(float[] m)
+        {
+            m[0] = 0.5f; m[1] = 0; m[2] = 0; m[3] = 0.5f;
+            m[4] = 0; m[5] = 0.5f; m[6] = 0; m[7] = 0.5f;
+            m[8] = 0; m[9] = 0; m[10] = 0.5f; m[11] = 0.5f;
+            m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1;
+        }
+
+        #endregion Private Static Methods
+
+        #region Private Methods
+
+        void buildTextureMatrix(float[] viewMatrix, float[] modelMatrix, float[] textureMatrix)
+        {
+            float[] eyeToClipMatrix = new float[16];
+            float[] modelViewMatrix = new float[16];
+
+            const float fieldOfView = 50.0f;
+            const float aspectRatio = 1;
+            float[] textureProjectionMatrix = new float[16];
+            float[] clipToTextureMatrix = new float[16];
+
+            /* Build texture projection matrix once. */
+            BuildPerspectiveMatrix(fieldOfView, aspectRatio,
+                                   0.25, 20.0,  /* Znear and Zfar */
+                                   textureProjectionMatrix);
+
+            makeClipToTextureMatrix(clipToTextureMatrix);
+
+            /* eyeToClip = clipToTexture * textureProjection */
+            MultMatrix(eyeToClipMatrix,
+               clipToTextureMatrix, textureProjectionMatrix);
+
+            /* modelView = view * model */
+            MultMatrix(modelViewMatrix, viewMatrix, modelMatrix);
+            /* texture = eyeToClip * modelView */
+            MultMatrix(textureMatrix, eyeToClipMatrix, modelViewMatrix);
+        }
+
+        void loadMVP(float[] modelView)
+        {
+            float[] transpose = new float[16];
+            int i, j;
+
+            for (i = 0; i < 4; i++)
+            {
+                for (j = 0; j < 4; j++)
+                {
+                    transpose[i * 4 + j] = modelView[j * 4 + i];
+                }
+            }
+            GL.LoadMatrix(transpose);
+        }
+
+        private void Reshape(int width, int height)
+        {
+            double aspectRatio = (float)width / height;
+            const double FieldOfView = 40.0;
+
+            /* Build projection matrix once. */
+            BuildPerspectiveMatrix(FieldOfView, aspectRatio,
+                                   1.0, 50.0, /* Znear and Zfar */
+                                   this.projectionMatrix);
+
+            /* Light drawn with fixed-function transformation so also load same
+               projection matrix in fixed-function projection matrix. */
+            GL.MatrixMode(MatrixMode.Projection);
+            GL.LoadIdentity();
+            Glu.Perspective(FieldOfView, aspectRatio,
+                           1.0, 50.0);  /* Znear and Zfar */
+            GL.MatrixMode(MatrixMode.Modelview);
+        }
+
+        void setupDemonSampler()
+        {
+            int texobj = 666;
+            Parameter sampler;
+
+            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1); /* Tightly packed texture data. */
+
+            GL.BindTexture(TextureTarget.Texture2D, texobj);
+            /* Load demon decal texture with mipmaps. */
+
+            global::OpenTK.Graphics.Glu.Build2DMipmap(global::OpenTK.Graphics.TextureTarget.Texture2D, (int)All.Rgb8,
+              128, 128, global::OpenTK.Graphics.PixelFormat.Rgb, global::OpenTK.Graphics.PixelType.UnsignedByte, ImageDemon.Array);
+            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear);
+
+            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor, Kd);
+            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, 0x812D);
+            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, 0x812D);
+
+            sampler = fragmentProgram.GetNamedParameter("projectiveMap");
+            sampler.SetTexture(texobj);
+        }
+
+        #endregion Private Methods
+
+        #endregion Methods
+    }
+}

ExampleBrowser/Examples/OpenTK/Basic/TextureSampling.cs

     using CgNet.GL;
 
     using ExampleBrowser.Data;
-    using ExampleBrowser.Examples.OpenTK;
 
     using global::OpenTK;
     using global::OpenTK.Graphics.OpenGL;

ExampleBrowser/Examples/OpenTK/Basic/TwoTextureAccesses.cs

     using CgNet.GL;
 
     using ExampleBrowser.Data;
-    using ExampleBrowser.Examples.OpenTK;
 
     using global::OpenTK;
     using global::OpenTK.Graphics.OpenGL;

ExampleBrowser/Examples/OpenTK/Basic/UniformParameter.cs

     using CgNet;
     using CgNet.GL;
 
-    using ExampleBrowser.Examples.OpenTK;
-
     using global::OpenTK;
     using global::OpenTK.Graphics.OpenGL;
     using global::OpenTK.Input;

ExampleBrowser/Examples/OpenTK/Basic/VaryingParameter.cs

     using CgNet;
     using CgNet.GL;
 
-    using ExampleBrowser.Examples.OpenTK;
-
     using global::OpenTK;
     using global::OpenTK.Graphics.OpenGL;
     using global::OpenTK.Input;
 
             this.vertexProgram =
                 this.CgContext.CreateProgramFromFile(
-                  ProgramType.Source, /* Program in human-readable form */
+                    ProgramType.Source, /* Program in human-readable form */
                     MyVertexProgramFileName, /* Name of file containing program */
                     this.vertexProfile, /* Profile: OpenGL ARB vertex program */
                     MyVertexProgramName, /* Entry function name */
             CgGL.SetOptimalOptions(this.fragmentProfile);
 
             this.fragmentProgram =
-               this.CgContext.CreateProgramFromFile(
+                this.CgContext.CreateProgramFromFile(
                     ProgramType.Source, /* Program in human-readable form */
                     MyFragmentProgramFileName, /* Name of file containing program */
                     this.fragmentProfile, /* Profile: OpenGL ARB vertex program */

ExampleBrowser/Examples/OpenTK/Basic/VertexAndFragmentProgram.cs

     using CgNet;
     using CgNet.GL;
 
-    using ExampleBrowser.Examples.OpenTK;
-
     using global::OpenTK;
     using global::OpenTK.Graphics.OpenGL;
     using global::OpenTK.Input;

ExampleBrowser/Examples/OpenTK/Basic/VertexLighting.cs

+namespace ExampleBrowser.Examples.OpenTK.Basic
+{
+    using System;
+
+    using CgNet;
+    using CgNet.GL;
+
+    using global::OpenTK;
+    using global::OpenTK.Graphics.OpenGL;
+    using global::OpenTK.Input;
+
+    [ExampleDescription(NodePath = "OpenTK/Basic/09 Vertex Lighting")]
+    public class VertexLighting : Example
+    {
+        #region Fields
+
+        private readonly float[] myGlobalAmbient = { 0.1f, 0.1f, 0.1f }; /* Dim */
+        private readonly float[] myLightColor = { 0.95f, 0.95f, 0.95f }; /* White */
+        private readonly float[] myProjectionMatrix = new float[16];
+
+        private Parameter myCgVertexParam_modelViewProj, myCgVertexParam_globalAmbient, myCgVertexParam_lightColor, myCgVertexParam_lightPosition, myCgVertexParam_eyePosition, myCgVertexParam_Ke, myCgVertexParam_Ka, myCgVertexParam_Kd, myCgVertexParam_Ks, myCgVertexParam_shininess;
+        private double myLightAngle = -0.4f; /* Angle light rotates around scene. */
+        private string myVertexProgramFileName = "Data/C5E1v_basicLight.cg",
+                       /* Page 111 */ myVertexProgramName = "C5E1v_basicLight";
+        private ProfileType vertexProfile, fragmentProfile;
+        private Program vertexProgram, fragmentProgram;
+
+        #endregion Fields
+
+        #region Constructors
+
+        public VertexLighting()
+            : base("09_vertex_lighting")
+        {
+        }
+
+        #endregion Constructors
+
+        #region Methods
+
+        #region Protected Methods
+
+        /// <summary>
+        /// Setup OpenGL and load resources here.
+        /// </summary>
+        /// <param name="e">Not used.</param>
+        protected override void OnLoad(EventArgs e)
+        {
+            GL.ClearColor(0.1f, 0.3f, 0.6f, 0.0f); /* Blue background */
+            GL.Enable(EnableCap.DepthTest);
+            this.CgContext = CgNet.Context.Create();
+
+            CgGL.SetDebugMode(false);
+            this.CgContext.ParameterSettingMode = ParameterSettingMode.Deferred;
+
+            vertexProfile = CgGL.GetLatestProfile(ProfileClass.Vertex);
+            CgGL.SetOptimalOptions(vertexProfile);
+
+            vertexProgram =
+                CgContext.CreateProgramFromFile(
+                    ProgramType.Source, /* Program in human-readable form */
+                    myVertexProgramFileName, /* Name of file containing program */
+                    vertexProfile, /* Profile: OpenGL ARB vertex program */
+                    myVertexProgramName, /* Entry function name */
+                    null); /* No extra compiler options */
+            vertexProgram.Load();
+
+            myCgVertexParam_modelViewProj = vertexProgram.GetNamedParameter("modelViewProj");
+            myCgVertexParam_globalAmbient = vertexProgram.GetNamedParameter("globalAmbient");
+            myCgVertexParam_lightColor = vertexProgram.GetNamedParameter("lightColor");
+            myCgVertexParam_lightPosition = vertexProgram.GetNamedParameter("lightPosition");
+            myCgVertexParam_eyePosition = vertexProgram.GetNamedParameter("eyePosition");
+            myCgVertexParam_Ke = vertexProgram.GetNamedParameter("Ke");
+            myCgVertexParam_Ka = vertexProgram.GetNamedParameter("Ka");
+            myCgVertexParam_Kd = vertexProgram.GetNamedParameter("Kd");
+            myCgVertexParam_Ks = vertexProgram.GetNamedParameter("Ks");
+            myCgVertexParam_shininess = vertexProgram.GetNamedParameter("shininess");
+
+            /* Set light source color parameters once. */
+            myCgVertexParam_globalAmbient.Set(myGlobalAmbient);
+            myCgVertexParam_lightColor.Set(myLightColor);
+
+            fragmentProfile = CgGL.GetLatestProfile(ProfileClass.Fragment);
+            CgGL.SetOptimalOptions(fragmentProfile);
+
+            /* Specify "color passthrough" fragment program with a string. */
+            fragmentProgram =
+                CgContext.CreateProgram(
+                    ProgramType.Source, /* Program in human-readable form */
+                    "float4 main(float4 c : COLOR) : COLOR { return c; }",
+                    fragmentProfile, /* Profile: latest fragment profile */
+                    "main", /* Entry function name */
+                    null); /* No extra compiler options */
+            fragmentProgram.Load();
+        }
+
+        /// <summary>
+        /// Add your game rendering code here.
+        /// </summary>
+        /// <param name="e">Contains timing information.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnRenderFrame(FrameEventArgs e)
+        {
+            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
+
+            /* World-space positions for light and eye. */
+            float[] eyePosition = { 0, 0, 13, 1 };
+            float[] lightPosition = {
+                                        5 * (float)Math.Sin(myLightAngle),
+                                        1.5f,
+                                        5 * (float)Math.Cos(myLightAngle), 1
+                                    };
+
+            float[] translateMatrix = new float[16], rotateMatrix = new float[16], modelMatrix = new float[16], invModelMatrix = new float[16],
+                    viewMatrix = new float[16], modelViewMatrix = new float[16], modelViewProjMatrix = new float[16];
+            float[] objSpaceEyePosition = new float[4], objSpaceLightPosition = new float[4];
+
+            BuildLookAtMatrix(eyePosition[0], eyePosition[1], eyePosition[2],
+                              0, 0, 0,
+                              0, 1, 0,
+                              viewMatrix);
+
+            vertexProgram.Bind();
+            CgGL.EnableProfile(vertexProfile);
+
+            fragmentProgram.Bind();
+            CgGL.EnableProfile(fragmentProfile);
+
+            setBrassMaterial();
+
+            /* modelView = rotateMatrix * translateMatrix */
+            MakeRotateMatrix(70, 1, 1, 1, rotateMatrix);
+            MakeTranslateMatrix(2, 0, 0, translateMatrix);
+            MultMatrix(modelMatrix, translateMatrix, rotateMatrix);
+
+            /* invModelMatrix = inverse(modelMatrix) */
+            InvertMatrix(invModelMatrix, modelMatrix);
+
+            /* Transform world-space eye and light positions to sphere's object-space. */
+            Transform(objSpaceEyePosition, invModelMatrix, eyePosition);
+            myCgVertexParam_eyePosition.Set3(objSpaceEyePosition);
+            Transform(objSpaceLightPosition, invModelMatrix, lightPosition);
+            myCgVertexParam_lightPosition.Set3(objSpaceLightPosition);
+
+            /* modelViewMatrix = viewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, viewMatrix, modelMatrix);
+
+            /* modelViewProj = projectionMatrix * modelViewMatrix */
+            MultMatrix(modelViewProjMatrix, myProjectionMatrix, modelViewMatrix);
+
+            /* Set matrix parameter with row-major matrix. */
+            myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            vertexProgram.UpdateParameters();
+            NativeMethods.glutSolidSphere(2.0, 40, 40);
+
+            /*** Render red plastic solid cone ***/
+
+            setRedPlasticMaterial();
+
+            /* modelView = viewMatrix * translateMatrix */
+            MakeTranslateMatrix(-2, -1.5f, 0, translateMatrix);
+            MakeRotateMatrix(90, 1, 0, 0, rotateMatrix);
+            MultMatrix(modelMatrix, translateMatrix, rotateMatrix);
+
+            /* invModelMatrix = inverse(modelMatrix) */
+            InvertMatrix(invModelMatrix, modelMatrix);
+
+            /* Transform world-space eye and light positions to sphere's object-space. */
+            Transform(objSpaceEyePosition, invModelMatrix, eyePosition);
+            myCgVertexParam_eyePosition.Set3(objSpaceEyePosition);
+            Transform(objSpaceLightPosition, invModelMatrix, lightPosition);
+            myCgVertexParam_lightPosition.Set3(objSpaceLightPosition);
+
+            /* modelViewMatrix = viewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, viewMatrix, modelMatrix);
+
+            /* modelViewProj = projectionMatrix * modelViewMatrix */
+            MultMatrix(modelViewProjMatrix, myProjectionMatrix, modelViewMatrix);
+
+            /* Set matrix parameter with row-major matrix. */
+            myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            vertexProgram.UpdateParameters();
+            NativeMethods.glutSolidCone(1.5, 3.5, 30, 30);
+
+            /*** Render light as emissive white ball ***/
+
+            /* modelView = translateMatrix */
+            MakeTranslateMatrix(lightPosition[0], lightPosition[1], lightPosition[2],
+                                modelMatrix);
+
+            /* modelViewMatrix = viewMatrix * modelMatrix */
+            MultMatrix(modelViewMatrix, viewMatrix, modelMatrix);
+
+            /* modelViewProj = projectionMatrix * modelViewMatrix */
+            MultMatrix(modelViewProjMatrix, myProjectionMatrix, modelViewMatrix);
+
+            setEmissiveLightColorOnly();
+            /* Avoid degenerate lightPosition. */
+            myCgVertexParam_lightPosition.Set(0f, 0f, 0f);
+
+            /* Set matrix parameter with row-major matrix. */
+            myCgVertexParam_modelViewProj.SetMatrix(modelViewProjMatrix);
+            vertexProgram.UpdateParameters();
+            NativeMethods.glutSolidSphere(0.2, 12, 12);
+
+            CgGL.DisableProfile(vertexProfile);
+            CgGL.DisableProfile(fragmentProfile);
+
+            this.SwapBuffers();
+        }
+
+        /// <summary>
+        /// Respond to resize events here.
+        /// </summary>
+        /// <param name="e">Contains information on the new GameWindow size.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnResize(EventArgs e)
+        {
+            Reshape(this.Width, this.Height);
+            GL.Viewport(0, 0, this.Width, this.Height);
+        }
+
+        protected override void OnUnload(EventArgs e)
+        {
+            base.OnUnload(e);
+            this.vertexProgram.Dispose();
+            this.fragmentProgram.Dispose();
+            this.CgContext.Dispose();
+        }
+
+        /// <summary>
+        /// Add your game logic here.
+        /// </summary>
+        /// <param name="e">Contains timing information.</param>
+        /// <remarks>There is no need to call the base implementation.</remarks>
+        protected override void OnUpdateFrame(FrameEventArgs e)
+        {
+            myLightAngle += 0.008f; /* Add a small angle (in radians). */
+            if (myLightAngle > 2 * Pi)
+            {
+                myLightAngle -= 2 * Pi;
+            }
+
+            if (this.Keyboard[Key.Escape])
+                this.Exit();
+        }
+
+        #endregion Protected Methods
+
+        #region Private Methods
+
+        private void Reshape(int width, int height)
+        {
+            double aspectRatio = (float)width / height;
+            const double FieldOfView = 40.0;
+
+            /* Build projection matrix once. */
+            BuildPerspectiveMatrix(FieldOfView, aspectRatio,
+                                   1.0, 20.0, /* Znear and Zfar */
+                                   myProjectionMatrix);
+        }
+
+        private void setBrassMaterial()
+        {
+            float[] brassEmissive = { 0.0f, 0.0f, 0.0f },
+                    brassAmbient = { 0.33f, 0.22f, 0.03f },
+                    brassDiffuse = { 0.78f, 0.57f, 0.11f },
+                    brassSpecular = { 0.99f, 0.91f, 0.81f };
+            float brassShininess = 27.8f;
+
+            myCgVertexParam_Ke.Set(brassEmissive);
+            myCgVertexParam_Ka.Set(brassAmbient);
+            myCgVertexParam_Kd.Set(brassDiffuse);
+            myCgVertexParam_Ks.Set(brassSpecular);
+            myCgVertexParam_shininess.Set(brassShininess);
+        }
+
+        private void setEmissiveLightColorOnly()
+        {
+            float[] zero = { 0.0f, 0.0f, 0.0f };
+
+            myCgVertexParam_Ke.Set(myLightColor);
+            myCgVertexParam_Ka.Set(zero);
+            myCgVertexParam_Kd.Set(zero);
+            myCgVertexParam_Ks.Set(zero);
+            myCgVertexParam_shininess.Set(0);
+        }
+
+        private void setRedPlasticMaterial()
+        {
+            float[] redPlasticEmissive = { 0.0f, 0.0f, 0.0f },
+                    redPlasticAmbient = { 0.0f, 0.0f, 0.0f },
+                    redPlasticDiffuse = { 0.5f, 0.0f, 0.0f },
+                    redPlasticSpecular = { 0.7f, 0.6f, 0.6f };
+            float redPlasticShininess = 32.0f;
+
+            myCgVertexParam_Ke.Set(redPlasticEmissive);
+            myCgVertexParam_Ka.Set(redPlasticAmbient);
+            myCgVertexParam_Kd.Set(redPlasticDiffuse);
+            myCgVertexParam_Ks.Set(redPlasticSpecular);
+            myCgVertexParam_shininess.Set(redPlasticShininess);
+        }
+
+        #endregion Private Methods
+
+        #endregion Methods
+    }
+}

ExampleBrowser/Examples/OpenTK/Basic/VertexProgram.cs

         private const string VertexProgramFileName = "Data/C2E1v_green.cg";
         private const string VertexProgramName = "C2E1v_green";
 
-        private ProfileType cgVertexProfile;
-        private Program cgVertexProgram;
+        private ProfileType vertexProfile;
+        private Program vertexProgram;
 
         #endregion Fields
 
         /// <param name="e">Not used.</param>
         protected override void OnLoad(EventArgs e)
         {
-            GL.ClearColor(0.1f, 0.3f, 0.6f, 0.0f);  /* Blue background */
+            GL.ClearColor(0.1f, 0.3f, 0.6f, 0.0f); /* Blue background */
 
             this.CgContext = CgNet.Context.Create();
 
             CgGL.SetDebugMode(false);
             this.CgContext.ParameterSettingMode = ParameterSettingMode.Deferred;
 
-            this.cgVertexProfile = CgGL.GetLatestProfile(ProfileClass.Vertex);
-            CgGL.SetOptimalOptions(this.cgVertexProfile);
+            this.vertexProfile = CgGL.GetLatestProfile(ProfileClass.Vertex);
+            CgGL.SetOptimalOptions(this.vertexProfile);
 
-            this.cgVertexProgram =
+            this.vertexProgram =
                 CgContext.CreateProgramFromFile(
-                    ProgramType.Source,                /* Program in human-readable form */
-                    VertexProgramFileName,  /* Name of file containing program */
-                    this.cgVertexProfile,        /* Profile: OpenGL ARB vertex program */
-                    VertexProgramName,      /* Entry function name */
-                    null);                    /* No extra compiler options */
+                    ProgramType.Source, /* Program in human-readable form */
+                    VertexProgramFileName, /* Name of file containing program */
+                    this.vertexProfile, /* Profile: OpenGL ARB vertex program */
+                    VertexProgramName, /* Entry function name */
+                    null); /* No extra compiler options */
 
-            this.cgVertexProgram.Load();
+            this.vertexProgram.Load();
         }
 
         /// <summary>
         {
             GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
-            this.cgVertexProgram.Bind();
+            this.vertexProgram.Bind();
 
-            CgGL.EnableProfile(this.cgVertexProfile);
+            CgGL.EnableProfile(this.vertexProfile);
 
             /* Rendering code verbatim from Chapter 1, Section 2.4.1 "Rendering
                a Triangle with OpenGL" (page 57). */
             GL.Vertex2(0.0f, -0.8f);
             GL.End();
 
-            CgGL.DisableProfile(this.cgVertexProfile);
+            CgGL.DisableProfile(this.vertexProfile);
             this.SwapBuffers();
         }
 
         protected override void OnUnload(EventArgs e)
         {
             base.OnUnload(e);
-            this.cgVertexProgram.Dispose();
+            this.vertexProgram.Dispose();
             this.CgContext.Dispose();
         }
 

ExampleBrowser/Examples/OpenTK/Basic/VertexTransform.cs

 namespace ExampleBrowser.Examples.OpenTK.Basic
 {
     using System;
-    using System.Runtime.InteropServices;
 
     using CgNet;
     using CgNet.GL;
 
-    using ExampleBrowser.Examples.OpenTK;
-
     using global::OpenTK;
     using global::OpenTK.Graphics.OpenGL;
     using global::OpenTK.Input;
                     null); /* No extra compiler options */
             this.vertexProgram.Load();
 
-            this.vertexParamModelViewProj =vertexProgram.GetNamedParameter("modelViewProj");
+            this.vertexParamModelViewProj = vertexProgram.GetNamedParameter("modelViewProj");
 
             fragmentProfile = CgGL.GetLatestProfile(ProfileClass.Fragment);
             CgGL.SetOptimalOptions(fragmentProfile);
             this.fragmentParamC.Set(0.1f, 0.7f, 0.1f, 1f); /* Green */
             vertexProgram.UpdateParameters();
             fragmentProgram.UpdateParameters();
-            glutWireSphere(2.0, 30, 30);
+            NativeMethods.glutWireSphere(2.0, 30, 30);
 
             /*** Render red wireframe cone ***/
             MakeTranslateMatrix(-2, -1.5f, 0, translateMatrix);
             this.fragmentParamC.Set(0.8f, 0.1f, 0.1f, 1); /* Red */
             vertexProgram.UpdateParameters();
             fragmentProgram.UpdateParameters();
-            glutWireCone(1.5, 3.5, 20, 20);
+            NativeMethods.glutWireCone(1.5, 3.5, 20, 20);
 
             CgGL.DisableProfile(vertexProfile);
 
         protected override void OnUpdateFrame(FrameEventArgs e)
         {
             myEyeAngle += 0.008f; /* Add a small angle (in radians). */
-            if (myEyeAngle > 2 * MyPi)
+            if (myEyeAngle > 2 * Pi)
             {
-                myEyeAngle -= (float)(2 * MyPi);
+                myEyeAngle -= (2 * Pi);
             }
 
             if (this.Keyboard[Key.Escape])
 
         /* Build a row-major (C-style) 4x4 matrix transform based on the
            parameters for gluLookAt. */
-        [DllImport("glut32.dll")]
-        private static extern void glutWireCone(double bse, double height, int slices, int stacks);
-
-        [DllImport("glut32.dll")]
-        private static extern void glutWireSphere(double radius, int slices, int stacks);
-
         private static void Reshape(int width, int height)
         {
             double aspectRatio = (float)width / height;

ExampleBrowser/Examples/OpenTK/Basic/VertexTwisting.cs

     using CgNet;
     using CgNet.GL;
 
-    using ExampleBrowser.Examples.OpenTK;
-
     using global::OpenTK;
     using global::OpenTK.Graphics.OpenGL;
     using global::OpenTK.Input;
             CgGL.SetOptimalOptions(this.fragmentProfile);
 
             this.fragmentProgram =
-                 this.CgContext.CreateProgramFromFile(
+                this.CgContext.CreateProgramFromFile(
                     ProgramType.Source, /* Program in human-readable form */
                     MyFragmentProgramFileName, /* Name of file containing program */
                     this.fragmentProfile, /* Profile: OpenGL ARB vertex program */

ExampleBrowser/Examples/OpenTK/Example.cs