From: Tim R. <ti...@us...> - 2004-08-09 05:14:58
|
Update of /cvsroot/csdopenglnet/csdOpenGL/Cg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23427/Cg Modified Files: Makefile Added Files: cgGL_vertex_example.cg demo2.cs Log Message: second Cg example --- NEW FILE: cgGL_vertex_example.cg --- struct appdata { float4 position : POSITION; float3 normal : NORMAL; float3 color : DIFFUSE; float3 TestColor : SPECULAR; }; struct vfconn { float4 HPOS : POSITION; float4 COL0 : COLOR0; }; vfconn main(appdata IN, uniform float4 Kd, uniform float4x4 ModelViewProj) { vfconn OUT; OUT.HPOS = mul(ModelViewProj, IN.position); OUT.COL0.xyz = Kd.xyz * IN.TestColor.xyz; OUT.COL0.w = 1.0; return OUT; } // main --- NEW FILE: demo2.cs --- using csDragons.OpenGL; using System; using System.Diagnostics; namespace csDragons { namespace OpenGL { namespace Cg { public class Demo2 : Cg { protected Random rnd; protected const int REPEAT_INTERVAL = 4096; protected const int RepeatInterval = REPEAT_INTERVAL; protected const int Mask = REPEAT_INTERVAL - 1; protected const int NSwaps = RepeatInterval * 8; protected bool Initialized = false; protected double[] Lattice1D = new double[REPEAT_INTERVAL]; protected double[,] Lattice2D = new double[REPEAT_INTERVAL,2]; protected double[,] Lattice3D = new double[REPEAT_INTERVAL,3]; protected int[] FoldTable = new int[REPEAT_INTERVAL]; protected double[] PhaseOffsetTable = new double[REPEAT_INTERVAL]; public Demo2() { Debug.Indent(); Debug.WriteLine( "Entering Demo2()" ); rnd = new System.Random(); Debug.WriteLine( "Exiting Demo2()" ); Debug.Unindent(); } protected double nvFloatRand( double min, double max ) { Debug.Indent(); Debug.WriteLine( "Entering Demo2.nvFloatRand(double,double)" ); Debug.WriteLine( "Exiting Demo2.nvFloatRand(double,double) *" ); Debug.Unindent(); return rnd.NextDouble() * (max-min) + min; } protected int nvIntRand( int min, int max ) { Debug.Indent(); Debug.WriteLine( "Entering Demo2.nvIntRand(int,int)" ); Debug.WriteLine( "Exiting Demo2.nvIntRand(int,int) *" ); Debug.Unindent(); return rnd.Next( min, max ); } protected void normalize( ref double x, ref double y ) { Debug.Indent(); Debug.WriteLine( "Entering Demo2.normalize(ref double,ref double)" ); double Mag = System.Math.Sqrt( x*x + y*y ); x /= Mag; y /= Mag; Debug.WriteLine( "Exiting Demo2.normalize(ref double,ref double" ); Debug.Unindent(); } protected void normalize( ref double x, ref double y, ref double z ) { Debug.Indent(); Debug.WriteLine( "Entering Demo2.normalize( ref double, ref double, ref double)" ); double Mag = System.Math.Sqrt( x*x + y*y + z*z ); x /= Mag; y /= Mag; z /= Mag; Debug.WriteLine( "Exiting Demo2.normalize( ref double, ref double, ref double)" ); Debug.Unindent(); } protected void Initialize() { Debug.Indent(); Debug.WriteLine( "Entering Demo2.Initialize()" ); if (Initialized) { Debug.WriteLine( "Already initiailzed!" ); } else { int i; Debug.WriteLine( "Build the fold table" ); for ( i=0; i<RepeatInterval; ++i) FoldTable[i] = i; for ( i=0; i<NSwaps; ++i ) { int Index0 = nvIntRand( 0, RepeatInterval - 1 ); int Index1 = nvIntRand( 0, RepeatInterval - 1 ); int TempVal = FoldTable[Index0]; FoldTable[Index0] = FoldTable[Index1]; FoldTable[Index1] = TempVal; } Debug.WriteLine( "Build the 1D lattice table" ); for ( i=0; i<RepeatInterval; ++i ) Lattice1D[i] = nvFloatRand( 0.0, 1.0 ); Debug.WriteLine( "Build the 2D lattice table" ); for ( i=0; i<RepeatInterval; ++i ) { Lattice2D[i,0] = nvFloatRand( -1.0, 1.0 ); Lattice2D[i,1] = nvFloatRand( -1.0, 1.0 ); normalize( ref Lattice2D[i,0], ref Lattice2D[i,1] ); } Debug.WriteLine( "Build the 2D lattice table" ); for ( i=0; i<RepeatInterval; ++i ) { Lattice3D[i,0] = nvFloatRand( -1.0, 1.0 ); Lattice3D[i,1] = nvFloatRand( -1.0, 1.0 ); Lattice3D[i,2] = nvFloatRand( -1.0, 1.0 ); normalize( ref Lattice3D[i,0], ref Lattice3D[i,1], ref Lattice3D[i,2] ); } Debug.WriteLine( "Build the Phase Offset Table" ); for ( i=0; i<RepeatInterval; ++i ) PhaseOffsetTable[i] = nvFloatRand( 0.0, RepeatInterval ); Initialized = true; } Debug.WriteLine( "Exiting Demo2.Initialize()" ); Debug.Unindent(); } protected int MaskedIndex( int index ) { Debug.Indent(); Debug.WriteLine( "Entering MaskedIndex(int)" ); if (index<0) { index = Mask & -index; if (index>0) index = RepeatInterval-index; } else index &= Mask; Debug.WriteLine( "Exiting MaskedIndex(int) *" ); Debug.Unindent(); return index; } protected double LatticeVal( int x ) { Debug.Indent(); Debug.WriteLine( "Entering LatticeVal(int)" ); Debug.WriteLine( "Exiting LatticeVal(int) *" ); Debug.Unindent(); return (Lattice1D[MaskedIndex(x)]); } protected double LatticeVal( int x, int y ) { Debug.Indent(); Debug.WriteLine( "Entering LatticeVal(int,int)" ); int Index = FoldTable[MaskedIndex(x)]; Index = FoldTable[(Index+y)&Mask]; Debug.WriteLine( "Exiting LatticeVal(int,int) *" ); Debug.Unindent(); return Lattice2D[Index,0]; } protected double LatticeVal( int x, int y, int z ) { Debug.Indent(); Debug.WriteLine( "Entering LatticeVal(int,int,int)" ); int Index = FoldTable[MaskedIndex(x)]; Index = FoldTable[ (Index+y) & Mask ]; Index = FoldTable[ (Index+z) & Mask ]; Debug.WriteLine( "Exiting LatticeVal(int,int,int) *" ); Debug.Unindent(); return Lattice2D[Index,0]; } protected double WeightSmooth( double t ) { Debug.Indent(); Debug.WriteLine( "Entering Demo2.WeightSmooth(double)" ); Debug.WriteLine( "Exiting Demo2.WeightSmooth(double) *" ); Debug.Unindent(); return t * t * ( 3 -2 * t ); } protected double Lerp( double t, double a, double b ) { Debug.Indent(); Debug.WriteLine( "Entering Demo2.Lerp(double,double,double)" ); Debug.WriteLine( "Exiting Demo2.Lerp(double,double,double) *" ); Debug.Unindent(); return ( 1.0 - t ) * a + t * b; } protected double cgNoise( double t ) { Debug.Indent(); Debug.WriteLine( "Entering Demo2.cgNoise(double)" ); int T0 = (int)System.Math.Floor(t); double TParam = WeightSmooth( t - (double)T0 ); Initialize(); Debug.WriteLine( "Exiting Demo2.cgNoise(double) *" ); Debug.Unindent(); return Lerp( TParam, LatticeVal(T0), LatticeVal(T0+1) ); } protected double cgNoise( double u, double v ) { Debug.Indent(); Debug.WriteLine( "Entering Demo2.cgNoise(double,double)" ); double Ret; double InvUParam, InvVParam; double G00, G10, G01, G11; double F00, F10, F01, F11; int U0 = (int)System.Math.Floor( u ); int U1 = U0 + 1; double UParam = u - (double)U0; int V0 = (int)System.Math.Floor( v ); int V1 = V0 + 1; double VParam = v - (double)V0; Initialize(); UParam = WeightSmooth( UParam ); VParam = WeightSmooth( VParam ); InvUParam = 1.0 - UParam; InvVParam = 1.0 - VParam; G00 = LatticeVal( U0, V0 ); G10 = LatticeVal( U1, V0 ); G01 = LatticeVal( U0, V1 ); G11 = LatticeVal( U1, V1 ); Debug.WriteLine( "Exiting Demo2.cgNoise(double,double) *" ); Debug.Unindent(); } public static void Main( string[] args) { Debug.Listeners.Add( new TextWriterTraceListener( Console.Out ) ); Debug.AutoFlush = true; Demo2 demo = new Demo2(); } } } } } Index: Makefile =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/Makefile,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Makefile 6 Aug 2004 11:42:33 -0000 1.8 --- Makefile 9 Aug 2004 05:14:49 -0000 1.9 *************** *** 6,10 **** DEBUG=/d:DEBUG DEBUG= ! OPTS=$(DEBUG) -lib:/usr/lib/mono/gtk-sharp MCS=$(CC) $(OPTS) PACKAGE=csDragons --- 6,10 ---- DEBUG=/d:DEBUG DEBUG= ! OPTS=$(DEBUG) MCS=$(CC) $(OPTS) PACKAGE=csDragons *************** *** 40,48 **** $(GEN) cgGL.xml ! demo: demo.exe ! ! demo.exe: demo.cs csdCg.dll libcsdCg.so libcsdGL.so ! $(MCS) -r:csdCg.dll -r:csdGL.dll demo.cs -out:demo.exe test: test.exe --- 40,47 ---- $(GEN) cgGL.xml + demo: demo.exe demo2.exe ! %.exe:%.cs ! $(MCS) -r:csdCg.dll -r:csdGL.dll $^ -o $@ test: test.exe |