Update of /cvsroot/csdopenglnet/csdOpenGL/Cg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24368/Cg Modified Files: Makefile cgEnums.cs cgError.cs cgGLEnum.cs cgProfile.cs cgRessource.cs cgType.cs csdCg.cs Added Files: demo.cs demo_frag.cg demo_vert.cg Log Message: Cg Advanced Index: cgError.cs =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/cgError.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** cgError.cs 5 Aug 2004 18:37:58 -0000 1.1 --- cgError.cs 5 Aug 2004 21:13:56 -0000 1.2 *************** *** 3,7 **** namespace Cg { ! enum CgError { CG_NO_ERROR = 0, CG_COMPILER_ERROR, --- 3,7 ---- namespace Cg { ! public enum CGerror { CG_NO_ERROR = 0, CG_COMPILER_ERROR, Index: cgProfile.cs =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/cgProfile.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** cgProfile.cs 5 Aug 2004 18:37:58 -0000 1.1 --- cgProfile.cs 5 Aug 2004 21:13:56 -0000 1.2 *************** *** 3,7 **** namespace Cg { ! enum CgProfile { CG_PROFILE_START=6144, CG_PROFILE_UNKNOWN, --- 3,7 ---- namespace Cg { ! public enum CGprofile { CG_PROFILE_START=6144, CG_PROFILE_UNKNOWN, *************** *** 17,20 **** --- 17,27 ---- CG_PROFILE_PS_2_X, + CG_PROFILE_VP20 = 6146, + CG_PROFILE_FP20 = 6147, + CG_PROFILE_VP30 = 6148, + CG_PROFILE_FP30 = 6149, + CG_PROFILE_ARBVP1 = 6150, + CG_PROFILE_ARBFP1 = 7000, + CG_PROFILE_MAX=7100 } --- NEW FILE: demo_frag.cg --- /*********************************************************************NVMH3**** Copyright NVIDIA Corporation 2002 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Comments: ******************************************************************************/ // Utility functions that return the appropriate components from the vector // of lighting coefficients returned by the standard library lighting // funciton, lit(). half diffuse(half4 l) { return l.y; } half specular(half4 l) { return l.z; } // Main shader. half4 main(float3 Peye : TEXCOORD0, half3 Neye : TEXCOORD1, half2 uv : TEXCOORD2, half3 Kd : COLOR0, half3 Ks : COLOR1, uniform sampler2D diffuseMap, uniform float3 Plight, uniform half3 lightColor, uniform half3 shininess) : COLOR { // Normalize surface normal, vector to light source, and vector // to the viewer half3 N = normalize(Neye); half3 L = normalize(Plight - Peye); half3 V = normalize(-Peye); // Compute half-angle vector for specular lighting half3 H = normalize(L + V); // Compute lighting values. lit() returns the diffuse coefficient // in y (or zero, if NdotL < 0), and the specular coefficient in z // (or zero, also if NdotL < 0). half NdotL = dot(N, L), NdotH = dot(N, H); half4 lighting = lit(NdotL, NdotH, shininess); // Compute overall color for the fragment. Scale sum of diffuse // and specular contributions together and by the light color. half3 C = lightColor * (diffuse(lighting) * Kd * (half3)tex2D(diffuseMap, uv).xyz + specular(lighting) * Ks); // Always set the alpha value to 1. return half4(C, 1); } Index: cgEnums.cs =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/cgEnums.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** cgEnums.cs 5 Aug 2004 18:37:58 -0000 1.1 --- cgEnums.cs 5 Aug 2004 21:13:56 -0000 1.2 *************** *** 3,7 **** namespace Cg { ! enum CgEnum { CG_UNKNOWN = 4096, CG_IN, --- 3,7 ---- namespace Cg { ! public enum CGenum { CG_UNKNOWN = 4096, CG_IN, Index: cgGLEnum.cs =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/cgGLEnum.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** cgGLEnum.cs 5 Aug 2004 18:50:53 -0000 1.1 --- cgGLEnum.cs 5 Aug 2004 21:13:56 -0000 1.2 *************** *** 3,7 **** namespace Cg { ! enum CgGLEnum { CG_GL_MATRIX_IDENTITY = 0, CG_GL_MATRIX_TRANSPOSE = 1, --- 3,7 ---- namespace Cg { ! public enum CGGLenum { CG_GL_MATRIX_IDENTITY = 0, CG_GL_MATRIX_TRANSPOSE = 1, Index: Makefile =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/Makefile,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Makefile 5 Aug 2004 18:50:53 -0000 1.6 --- Makefile 5 Aug 2004 21:13:56 -0000 1.7 *************** *** 13,19 **** all: csdCg.dll ! csdCg.dll: $(FILES) ! $(MCS) -target:library $^ -out:$@ cg_8h.xml: ln -sf ../doxygen/cg_xml/cg_8h.xml . --- 13,28 ---- all: csdCg.dll ! csdCg.dll: $(FILES) csdGL.dll ! $(MCS) -target:library -r:csdGL.dll $(FILES) -out:$@ ! ! csdGL.dll: ! ln -sf ../GL/csdGL.dll ! ! libcsdCg.so: ! ln -sf ../cbonding/libcsdCg.so + libcsdGL.so: + ln -sf ../cbonding/libcsdGL.so + cg_8h.xml: ln -sf ../doxygen/cg_xml/cg_8h.xml . *************** *** 28,36 **** $(GEN) cgGL.xml test: test.exe ! test.exe: test.cs $(FILES) ! $(MCS) test.cs $(FILES) -out:test.exe && ./test.exe ! install: install-gac --- 37,50 ---- $(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 ! test.exe: test.cs $(FILES) ! $(MCS) -r:csdGL.dll test.cs $(FILES) -out:test.exe install: install-gac --- NEW FILE: demo_vert.cg --- /*********************************************************************NVMH3**** Copyright NVIDIA Corporation 2002 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Comments: ******************************************************************************/ void main(float4 Pobject : POSITION, float3 Nobject : NORMAL, float2 TexUV : TEXCOORD0, float3 diffuse : TEXCOORD1, float3 specular : TEXCOORD2, uniform float4x4 ModelViewProj, uniform float4x4 ModelView, uniform float4x4 ModelViewIT, out float4 HPosition : POSITION, out float3 Peye : TEXCOORD0, out float3 Neye : TEXCOORD1, out float2 uv : TEXCOORD2, out float3 Kd : COLOR0, out float3 Ks : COLOR1) { // compute homogeneous position of vertex for rasterizer HPosition = mul(ModelViewProj, Pobject); // transform position and normal from model-space to view-space Peye = mul(ModelView, Pobject).xyz; Neye = mul(ModelViewIT, float4(Nobject, 0)).xyz; // pass uv, Kd, and Ks through unchanged; if they are varying // per-vertex, however, they'll be interpolated before being // passed to the fragment program. uv = TexUV; Kd = diffuse; Ks = specular; } Index: csdCg.cs =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/csdCg.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** csdCg.cs 5 Aug 2004 18:50:53 -0000 1.4 --- csdCg.cs 5 Aug 2004 21:13:56 -0000 1.5 *************** *** 3,9 **** using System.Runtime.InteropServices; ! namespace csDragons { ! namespace OpenGL { ! namespace Cg { public delegate void CgErrorEvent(); --- 3,7 ---- using System.Runtime.InteropServices; ! namespace csDragons.OpenGL.Cg { public delegate void CgErrorEvent(); *************** *** 16,25 **** public static int CG_FALSE = 0; ! public static int CG_TRUE = 0; ! [ DllImport( "libcsdCG.so" ) ] public static extern void cgSetErrorCallback( CgErrorEvent func ); ! [ DllImport( "libcsdCG.so" ) ] public static extern CgErrorEvent cgGetErrorCallback(); --- 14,23 ---- public static int CG_FALSE = 0; ! public static int CG_TRUE = 1; ! [ DllImport( "libcsdCg.so" ) ] public static extern void cgSetErrorCallback( CgErrorEvent func ); ! [ DllImport( "libcsdCg.so" ) ] public static extern CgErrorEvent cgGetErrorCallback(); *************** *** 30,33 **** } - } - } \ No newline at end of file --- 28,29 ---- Index: cgType.cs =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/cgType.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** cgType.cs 5 Aug 2004 18:10:52 -0000 1.1 --- cgType.cs 5 Aug 2004 21:13:56 -0000 1.2 *************** *** 3,7 **** namespace Cg { ! enum CgType { CG_UNKNOWN_TYPE, CG_STRUCT, --- 3,7 ---- namespace Cg { ! public enum CGtype { CG_UNKNOWN_TYPE, CG_STRUCT, --- NEW FILE: demo.cs --- using csDragons.OpenGL; using System; using System.Diagnostics; namespace csDragons { namespace OpenGL { namespace Cg { public class Demo : Cg { IntPtr context, vertexProgram, fragmentProgram; CGprofile vertexProfile, fragmentProfile; int curTime = 0; bool haveLoadedPrograms = false; int res=512; float[] P, N, uv; uint[] indices; public Demo() { glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); glutInitWindowSize( res, res ); glutCreateWindow("Cg Demo"); glutDisplayFunc( new cb0_glutDisplayFunc( draw ) ); glutKeyboardFunc( new cb0_glutKeyboardFunc(keyboard) ); cgSetErrorCallback( new CgErrorEvent( errorHandler ) ); context = cgCreateContext(); glutMainLoop(); } protected void errorHandler() { } protected void draw() { if (!haveLoadedPrograms) { ChooseProfiles(); LoadCgPrograms(); LoadTextures(); glEnable( GL_DEPTH_TEST ); haveLoadedPrograms = true; } glClearColor( 0.25f, 0.25f, 0.25f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 30.0, 1.0, 0.1, 100.0 ); 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 ); //cgGLBindProgram( vertexProgram ); //cgGLBindProgram( fragmentProgram ); //cgGLSetStateMatrixParameter(cgGetNamedParameter(vertexProgram, "ModelViewProj"), CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY); //cgGLSetStateMatrixParameter(cgGetNamedParameter(vertexProgram, "ModelView"), CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY); //cgGLSetStateMatrixParameter(cgGetNamedParameter(vertexProgram, "ModelViewIT"), CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE); 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); float[] lightPos = { 3, 2, -3 }; //cgGLSetParameter3fv(cgGetNamedParameter(fragmentProgram, "Plight"), lightPos); float[] lightColor = { 1, 1, 1 }; //cgGLSetParameter3fv(cgGetNamedParameter(fragmentProgram, "lightColor"), lightColor); //cgGLSetParameter1f(cgGetNamedParameter(fragmentProgram, "shininess"), 40); //cgGLEnableTextureParameter(cgGetNamedParameter(fragmentProgram, "diffuseMap")); // And go ahead and draw the scene geometry DrawGeometry(); //cgGLDisableTextureParameter(cgGetNamedParameter(fragmentProgram, "diffuseMap")); glutSwapBuffers(); ++curTime; glutPostRedisplay(); } protected void 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; if (P==null) { int u,v; P = new float[3*nVerts]; N = new float[3*nVerts]; uv = new float[2*nVerts]; for ( v=0; v<nv; ++v ) { float fv = (float)(v/(nv-1)); for (u=0; u<nu; ++u ) { float fu=(float)(u/(nu-1)); uv[uvp] = fu; uv[uvp+1] = fu; parametricEval( fu, fv, pp, np ); pp += 3; np += 3; uvp += 2; } } 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); } } uint[] a3 = {3}; uint[] af = {GL_FLOAT}; uint[] a0 = {0}; IntPtr param = cgGetNamedParameter( vertexProgram, "Pobject" ); cgGLSetParameterPointer( param, a3, af, a0, P ); } } protected void parametricEval( float u, float v, int pp, int np ) { 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[pp] = P[pp]; N[pp+1] = P[pp+1]; N[pp+2] = P[pp+2]; } protected void ChooseProfiles() { if ( CG_TRUE==cgGLIsProfileSupported( CGprofile.CG_PROFILE_ARBVP1 ) ) { vertexProfile = CGprofile.CG_PROFILE_ARBVP1; } else if ( CG_TRUE==cgGLIsProfileSupported( CGprofile.CG_PROFILE_VP30 ) ) { vertexProfile = CGprofile.CG_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; } else if ( CG_TRUE==cgGLIsProfileSupported( CGprofile.CG_PROFILE_FP30 ) ) { fragmentProfile = CGprofile.CG_PROFILE_FP30; } else { Console.WriteLine( "Netiher arbfp1 or fp30 fragment profiles supported on this system!" ); System.Environment.Exit(2); } } protected void LoadCgPrograms() { Debug.Assert( CG_TRUE==cgIsContext( context ) ); vertexProgram = cgCreateProgramFromFile( context, CGenum.CG_SOURCE, "demo_vert.cg", vertexProfile, null, null ); 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 ); } protected void LoadTextures() { uint handle ; uint[] h = new uint[1]; glGenTextures( 1, h ); handle = h[0]; glBindTexture( GL_TEXTURE_2D, handle ); glTexParameterf(GL_TEXTURE_2D, 33169, 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[i*res+j*4] = 0.7f; data[i*res+j*4+1] = 0.7f; data[i*res+j*4+2] = 0.7f; data[i*res+j*4+3] = 0.7f; } else { data[i*res+j*4] = 0.1f; data[i*res+j*4+1] = 0.1f; data[i*res+j*4+2] = 0.1f; data[i*res+j*4+3] = 0.1f; } } } glTexImage2D( GL_TEXTURE_2D, 0, 4, res, res, 0, GL_RGBA, GL_FLOAT, data ); cgGLSetTextureParameter( cgGetNamedParameter( fragmentProgram, "diffuseMap" ), h ); } protected void keyboard( byte key, int x, int y ) { Console.Write( "Key ==> " ); Console.WriteLine( key ); } public static void Main( string[] args) { Demo demo = new Demo(); } } } } } Index: cgRessource.cs =================================================================== RCS file: /cvsroot/csdopenglnet/csdOpenGL/Cg/cgRessource.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** cgRessource.cs 5 Aug 2004 18:10:52 -0000 1.1 --- cgRessource.cs 5 Aug 2004 21:13:56 -0000 1.2 *************** *** 3,7 **** namespace Cg { ! enum CgRessource { CG_TEXUNIT0=2048, CG_TEXUNIT1, --- 3,7 ---- namespace Cg { ! public enum CGressource { CG_TEXUNIT0=2048, CG_TEXUNIT1, |