From: Tim R. <ti...@us...> - 2004-09-11 14:27:36
|
Update of /cvsroot/csdopenglnet/csdOpenGL/Cg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19432/Cg Modified Files: Cg.build Added Files: demoCg.cs Removed Files: demo.cs Log Message: Cg Demo is now build by nant --- NEW FILE: demoCg.cs --- using csDragons.OpenGL; using System; using System.Diagnostics; namespace csDragons { namespace OpenGL { namespace Cg { public class Demo : Cg { protected IntPtr context, vertexProgram, fragmentProgram; protected CGprofile vertexProfile, fragmentProfile; protected int curTime = 0; protected bool haveLoadedPrograms = false; protected int res=512; protected float[] P = null; protected float[] N = null; protected float[] uv = null; protected uint[] indices = null; public Demo() { Debug.Indent(); Debug.WriteLine( "Entering Demo()" ); Debug.WriteLine( "Initializing GLUT" ); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); glutInitWindowSize( res, res ); glutCreateWindow("Cg Demo"); glutDisplayFunc( new cb0_glutDisplayFunc( draw ) ); glutKeyboardFunc( new cb0_glutKeyboardFunc(keyboard) ); Debug.WriteLine( "Initializing Cg" ); cgSetErrorCallback( new CgErrorEvent( errorHandler ) ); context = cgCreateContext(); Debug.WriteLine( "Start main loop" ); glutMainLoop(); Debug.WriteLine( "Exiting Demo()" ); Debug.Unindent(); } protected void errorHandler() { Debug.Indent(); Debug.WriteLine( "Entering Demo.errorHandler()" ); CGerror err = cgGetError(); Console.Write( "Cg error: " ); Console.Write( err ); Console.WriteLine( cgGetErrorString( err ) ); System.Environment.Exit( 3 ); Debug.WriteLine( "Exiting Demo.errorHandler()" ); Debug.Unindent(); } protected void draw() { Debug.Indent(); Debug.WriteLine( "Entering Demo.draw()" ); if (!haveLoadedPrograms) { Debug.WriteLine( "Initiize GL/Cg" ); ChooseProfiles(); LoadCgPrograms(); LoadTextures(); glEnable( GL_DEPTH_TEST ); haveLoadedPrograms = true; } Debug.WriteLine( "Reset GL" ); glClearColor( 0.25f, 0.25f, 0.25f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); Debug.WriteLine( "Set projection matrix" ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 30.0, 1.0, 0.1, 100.0 ); Debug.WriteLine( "Set modelview matrix" ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 4.0, 4.0, -4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); glRotatef( curTime, 0, 1, 0 ); Debug.WriteLine( "Bind Cg programs" ); cgGLBindProgram( vertexProgram ); cgGLBindProgram( fragmentProgram ); Debug.WriteLine( "Bind uniform parameters to vertex shader" ); Debug.WriteLine( "==> ModelViewProj" ); cgGLSetStateMatrixParameter(cgGetNamedParameter(vertexProgram, "ModelViewProj"), CGGLenum.CG_GL_MODELVIEW_PROJECTION_MATRIX, CGGLenum.CG_GL_MATRIX_IDENTITY); Debug.WriteLine( "==> ModelView" ); cgGLSetStateMatrixParameter(cgGetNamedParameter(vertexProgram, "ModelView"), CGGLenum.CG_GL_MODELVIEW_MATRIX, CGGLenum.CG_GL_MATRIX_IDENTITY); Debug.WriteLine( "==> ModelViewIT" ); cgGLSetStateMatrixParameter(cgGetNamedParameter(vertexProgram, "ModelViewIT"), CGGLenum.CG_GL_MODELVIEW_MATRIX, CGGLenum.CG_GL_MATRIX_INVERSE_TRANSPOSE); Debug.WriteLine( "Bind varying parameters to vertex shader" ); float[] Kd = { 0.7f, 0.2f, 0.2f }; float[] Ks = { 0.9f, 0.9f, 0.9f }; cgGLSetParameter3fv(cgGetNamedParameter(vertexProgram, "diffuse"), Kd); cgGLSetParameter3fv(cgGetNamedParameter(vertexProgram, "specular"), Ks); Debug.WriteLine( "Bind uniform parameters to fragment shader" ); float[] lightPos = { 3.0f, 2.0f, -3.0f }; float[] lightColor = { 1.0f, 1.0f, 1.0f }; Debug.WriteLine( "==> Plight" ); cgGLSetParameter3fv(cgGetNamedParameter(fragmentProgram, "Plight"), lightPos); Debug.WriteLine( "==> lightColor" ); cgGLSetParameter3fv(cgGetNamedParameter(fragmentProgram, "lightColor"), lightColor); Debug.WriteLine( "==> shininess" ); cgGLSetParameter1f(cgGetNamedParameter(fragmentProgram, "shininess"), 40.0f ); Debug.WriteLine( "Enable approprate texture for fragment shader" ); cgGLEnableTextureParameter(cgGetNamedParameter(fragmentProgram, "diffuseMap")); Debug.WriteLine( "Draw scene" ); DrawGeometry(); Debug.WriteLine( "Disable texture" ); cgGLDisableTextureParameter(cgGetNamedParameter(fragmentProgram, "diffuseMap")); glutSwapBuffers(); ++curTime; glutPostRedisplay(); Debug.WriteLine( "Exiting Demo.draw()" ); Debug.Unindent(); } protected void DrawGeometry() { Debug.Indent(); Debug.WriteLine( "Entering Demo.DrawGeometry()" ); int nu = 30; int nv = 30; int nTris = 2*(nu-1)*(nv-1); int nVerts = nu*nv; int pp = 0; int np = 0; int uvp = 0; IntPtr param; if (P==null) { int u,v; P = new float[3*nVerts]; N = new float[3*nVerts]; uv = new float[2*nVerts]; Debug.WriteLine( "Fill position, normal and texture coordinate arrays" ); for ( v=0; v<nv; ++v ) { float fv = ((float)v)/((float)(nv-1)); for (u=0; u<nu; ++u ) { float fu= ((float)u) /((float)(nu-1)); uv[uvp] = fu; uv[uvp+1] = fv; parametricEval( fu, fv, pp, np ); pp += 3; np += 3; uvp += 2; } } Debug.WriteLine( "Fill in the vertex index arrays" ); indices = new uint[3*nTris]; int ip = 0; for ( v=0; v<nv-1; ++v ) { for ( u=0; u<nu-1; ++u) { indices[ip++] = (uint)(u+v*nu); indices[ip++] = (uint)((u+1)+v*nu); indices[ip++] = (uint)((u+1)+(v+1)*nu); indices[ip++] = (uint)(u+v*nu); indices[ip++] = (uint)((u+1)+(v+1)*nu); indices[ip++] = (uint)(u+(v+1)*nu); } } Debug.WriteLine( "Associate data 'pointers' with vertex shader" ); Debug.WriteLine( "==> Pobject" ); param = cgGetNamedParameter( vertexProgram, "Pobject" ); cgGLSetParameterPointer( param, 3, GL_FLOAT, 0, P ); Debug.WriteLine( "==> Nobject "); param = cgGetNamedParameter( vertexProgram, "Nobject" ); cgGLSetParameterPointer( param, 3, GL_FLOAT, 0, N ); Debug.WriteLine( "==> TexUV" ); param = cgGetNamedParameter( vertexProgram, "TexUV" ); cgGLSetParameterPointer( param, 2, GL_FLOAT, 0, uv ); } Debug.WriteLine( "Enable bindings to parameters" ); Debug.WriteLine( "==> Pobject" ); param = cgGetNamedParameter( vertexProgram, "Pobject" ); cgGLEnableClientState( param ); Debug.WriteLine( "==> Nobject" ); param = cgGetNamedParameter( vertexProgram, "Nobject" ); cgGLEnableClientState( param ); Debug.WriteLine( "==> TexUV" ); param = cgGetNamedParameter( vertexProgram, "TexUV" ); cgGLEnableClientState( param ); Debug.WriteLine( "==> diffuseMap" ); param = cgGetNamedParameter( fragmentProgram, "diffuseMap" ); cgGLEnableTextureParameter( param ); Debug.WriteLine( "Draw geometry" ); glDrawElements( GL_TRIANGLES, 3*nTris, GL_UNSIGNED_INT, indices ); Debug.WriteLine( "Disable bindings to parameters" ); Debug.WriteLine( "==> Pobject" ); param = cgGetNamedParameter( vertexProgram, "Pobject" ); cgGLDisableClientState( param ); Debug.WriteLine( "==> Nobject" ); param = cgGetNamedParameter( vertexProgram, "Nobject" ); cgGLDisableClientState( param ); Debug.WriteLine( "==> TexUV" ); param = cgGetNamedParameter( vertexProgram, "TexUV" ); cgGLDisableClientState( param ); Debug.WriteLine( "==> diffuseMap" ); param = cgGetNamedParameter( fragmentProgram, "diffuseMap" ); cgGLDisableTextureParameter( param ); Debug.WriteLine( "Exiting Demo.DrawGeometry()" ); Debug.Unindent(); } protected void parametricEval( float u, float v, int pp, int np ) { Debug.Indent(); Debug.WriteLine( "Entering Demo.parametricEval(float,float,int,int)" ); double theta = System.Math.PI * u; double phi = 2.0 * System.Math.PI * v; P[pp] = (float)(System.Math.Sin(theta) * System.Math.Sin(phi)); P[pp+1] = (float)(System.Math.Sin(theta) * System.Math.Cos(phi)); P[pp+2] = (float)(System.Math.Cos(theta)); N[np] = P[pp]; N[np+1] = P[pp+1]; N[np+2] = P[pp+2]; Debug.WriteLine( "Exiting Demo.parametricEval(float,float,int,int)" ); Debug.Unindent(); } protected void ChooseProfiles() { Debug.Indent(); Debug.WriteLine( "Entering Demo.ChooseProfiles()" ); if ( CG_TRUE==cgGLIsProfileSupported( CGprofile.CG_PROFILE_ARBVP1 ) ) { vertexProfile = CGprofile.CG_PROFILE_ARBVP1; Console.WriteLine( "Vertex Profile: ARBVP1" ); } else if ( CG_TRUE==cgGLIsProfileSupported( CGprofile.CG_PROFILE_VP30 ) ) { vertexProfile = CGprofile.CG_PROFILE_VP30; Console.WriteLine( "Vertex Profile: VP30" ); } else { Console.WriteLine( "Neither arbvp1 or vp30 vertex profiles supported on this system!" ); System.Environment.Exit(1); } if ( CG_TRUE==cgGLIsProfileSupported( CGprofile.CG_PROFILE_ARBFP1 ) ) { fragmentProfile = CGprofile.CG_PROFILE_ARBFP1; Console.WriteLine( "Fargment Profile: ARBFP1" ); } else if ( CG_TRUE==cgGLIsProfileSupported( CGprofile.CG_PROFILE_FP30 ) ) { fragmentProfile = CGprofile.CG_PROFILE_FP30; Console.WriteLine( "Fragmen Profile: FP30" ); } else { Console.WriteLine( "Netiher arbfp1 or fp30 fragment profiles supported on this system!" ); System.Environment.Exit(2); } Debug.WriteLine( "Exiting Demo.ChooseProfiles()" ); Debug.Unindent(); } protected void LoadCgPrograms() { Debug.Indent(); Debug.WriteLine( "Entering Demo.LoadCgPrograms()" ); Debug.Assert( CG_TRUE==cgIsContext( context ) ); vertexProgram = cgCreateProgramFromFile( context, CGenum.CG_SOURCE, "demo_vert.cg", vertexProfile, null, null ); if (CG_FALSE==cgIsProgramCompiled(vertexProgram)) cgCompileProgram(vertexProgram); cgGLEnableProfile( vertexProfile ); cgGLLoadProgram( vertexProgram ); fragmentProgram = cgCreateProgramFromFile( context, CGenum.CG_SOURCE, "demo_frag.cg", fragmentProfile, null, null ); if ( CG_FALSE==cgIsProgramCompiled( fragmentProgram ) ) cgCompileProgram( fragmentProgram ); cgGLEnableProfile( fragmentProfile ); cgGLLoadProgram( fragmentProgram ); Debug.WriteLine( "Exiting Demo.LoadCgPrograms()" ); Debug.Unindent(); } protected void LoadTextures() { Debug.Indent(); Debug.WriteLine( "Entering Demo.LoadTextures()" ); uint handle ; uint[] h = new uint[1]; int dp=0; glGenTextures( 1, h ); handle = h[0]; glBindTexture( GL_TEXTURE_2D, handle ); glTexParameterf(GL_TEXTURE_2D, 0x8191, GL_TRUE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); float[] data = new float[res*res*4]; for ( int i=0; i<res; i++ ) { for ( int j=0; j<res; j++ ) { if ( (i/32+j/32)%2==0 ) { data[dp++] = 0.7f; data[dp++] = 0.7f; data[dp++] = 0.7f; } else { data[dp++] = 0.1f; data[dp++] = 0.1f; data[dp++] = 0.1f; } data[dp++] = 0.1f; } } glTexImage2D( GL_TEXTURE_2D, 0, 4, res, res, 0, GL_RGBA, GL_FLOAT, data ); cgGLSetTextureParameter( cgGetNamedParameter( fragmentProgram, "diffuseMap" ), handle ); Debug.WriteLine( "Exiting Demo.LoadTextures()" ); Debug.Unindent(); } protected void keyboard( byte key, int x, int y ) { if ( (key==(byte)'q') || (key==(byte)'Q') || (key==27) ) { cgDestroyContext( context ); System.Environment.Exit(0); } } public static void Main( string[] args) { Debug.Listeners.Add( new TextWriterTraceListener( Console.Out ) ); Debug.AutoFlush = true; Demo demo = new Demo(); } } } } } Index: Cg.build =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/Cg.build,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Cg.build 11 Sep 2004 12:27:21 -0000 1.1 --- Cg.build 11 Sep 2004 14:27:25 -0000 1.2 *************** *** 5,8 **** --- 5,9 ---- <delete file="${build.dir}/csDragons.OpenGL.Cg.dll" failonerror="false" /> <delete file="${build.dir}/csDragons.OpenGL.Cg.dll.config" failonerror="false" /> + <delete file="${build.dir}/demoCg.exe" failonerror="false" /> <delete> <fileset> *************** *** 39,41 **** --- 40,56 ---- <copy file="csDragons.OpenGL.Cg.dll.config" todir="${build.dir}" /> </target> + <target name="samples" description="build demos"> + <csc target="exe" output="${build.dir}/demoCg.exe"> + <sources> + <include name="demoCg.cs" /> + </sources> + <references> + <lib> + <include name="${build.dir}" /> + </lib> + <include name="csDragons.OpenGL.dll" /> + <include name="csDragons.OpenGL.Cg.dll" /> + </references> + </csc> + </target> </project> \ No newline at end of file --- demo.cs DELETED --- |