예제 #1
0
        public GraphicsDevice(Size size, WindowMode window)
        {
            Console.WriteLine("Using Cg renderer");
            windowSize = size;

            var extensions = new []
            {
                "GL_ARB_vertex_program",
                "GL_ARB_fragment_program",
                "GL_ARB_vertex_buffer_object",
            };

            surf = SdlGraphics.InitializeSdlGl(ref windowSize, window, extensions);

            cgContext = Tao.Cg.Cg.cgCreateContext();

            Tao.Cg.Cg.cgSetErrorCallback(CgErrorCallback);

            Tao.Cg.CgGl.cgGLRegisterStates(cgContext);
            Tao.Cg.CgGl.cgGLSetManageTextureParameters(cgContext, true);
            vertexProfile   = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_VERTEX);
            fragmentProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_FRAGMENT);

            Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
            ErrorHandler.CheckGlError();
            Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
            ErrorHandler.CheckGlError();

            Sdl.SDL_SetModState(0);             // i have had enough.

            input = new SdlInput(surf);
        }
        public override void RenderingConfigurationChanged(RenderingConfigurator conf)
        {
            renderingConfiguration = conf;

            if (LasDataManager.ColorPallette != null)
            {
                LasDataManager.ColorPallette.ColorMode = renderingConfiguration.colorType;
            }

            selectedTexture = (int)renderingConfiguration.pointTexture;

            CgGl.cgGLSetParameter1f(m_CGparam_sizeFactorPass1, renderingConfiguration.pointSize);
            checkForCgError("setting sizeFactor");

            CgGl.cgGLSetParameter1f(m_CGparam_sizeFactorPass2, renderingConfiguration.pointSize);
            checkForCgError("setting sizeFactor2");

            CgGl.cgGLSetParameter1f(m_CGparam_zoffsetPass1, renderingConfiguration.Zoffset);
            checkForCgError("setting zoffset");

            CgGl.cgGLSetParameter1f(m_CGparam_splatRotationCutoffDistPass1, renderingConfiguration.splatRotationCutoffDistance);
            checkForCgError("setting splatRotationCutoffDistancePass1");

            CgGl.cgGLSetParameter1f(m_CGparam_splatRotationCutoffDistPass2, renderingConfiguration.splatRotationCutoffDistance);
            checkForCgError("setting splatRotationCutoffDistancePass2");
        }
        static void display()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

            Cg.cgSetParameter1f(myCgVertexParam_twisting, myTwisting);

            CgGl.cgGLBindProgram(myCgVertexProgram);
            checkForCgError("binding vertex program");

            CgGl.cgGLEnableProfile(myCgVertexProfile);
            checkForCgError("enabling vertex profile");

            CgGl.cgGLBindProgram(myCgFragmentProgram);
            checkForCgError("binding fragment program");

            CgGl.cgGLEnableProfile(myCgFragmentProfile);
            checkForCgError("enabling fragment profile");

            drawSubDividedTriangle(5);

            CgGl.cgGLDisableProfile(myCgVertexProfile);
            checkForCgError("disabling vertex profile");

            CgGl.cgGLDisableProfile(myCgFragmentProfile);
            checkForCgError("disabling fragment profile");

            Glut.glutSwapBuffers();
        }
        static void Display()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

            CgGl.cgGLBindProgram(myCgVertexProgram);
            checkForCgError("binding vertex program");

            CgGl.cgGLEnableProfile(myCgVertexProfile);
            checkForCgError("enabling vertex profile");

            CgGl.cgGLBindProgram(myCgFragmentProgram);
            checkForCgError("binding fragment program");

            CgGl.cgGLEnableProfile(myCgFragmentProfile);
            checkForCgError("enabling fragment profile");

            DrawStars();

            CgGl.cgGLDisableProfile(myCgVertexProfile);
            checkForCgError("disabling vertex profile");

            CgGl.cgGLDisableProfile(myCgFragmentProfile);
            checkForCgError("disabling fragment profile");

            Glut.glutSwapBuffers();
        }
        private void DisableShaders()
        {
            CgGl.cgGLDisableProfile(m_CG_fragmentProfile);
            checkForCgError("disabling fragment profile");

            CgGl.cgGLDisableProfile(m_CG_vertexProfile);
            checkForCgError("disabling fragment profile");
        }
        public override void WindowResized(int width, int height, float FOV)
        {
            CgGl.cgGLSetParameter3f(m_CGparam_viewParamsPass1, width, height, FOV);
            checkForCgError("setting viewParams pass 1");

            CgGl.cgGLSetParameter3f(m_CGparam_viewParamsPass2, width, height, FOV);
            checkForCgError("setting viewParams pass 2");
        }
예제 #7
0
 /// <summary>
 /// Compiles and loads a Cg program</summary>
 /// <param name="id">ID string for the Cg program we want to compile and load</param>
 public static void LoadProgram(string id)
 {
     if (!Cg.cgIsProgramCompiled(m_cg_programs[id]))
     {
         Cg.cgCompileProgram(m_cg_programs[id]);
     }
     if (!CgGl.cgGLIsProgramLoaded(m_cg_programs[id]))
     {
         CgGl.cgGLLoadProgram(m_cg_programs[id]);
     }
 }
예제 #8
0
        public GraphicsDevice(Size size, WindowMode window)
            : base(size, window, requiredExtensions)
        {
            Context = Tao.Cg.Cg.cgCreateContext();

            Tao.Cg.Cg.cgSetErrorCallback(errorCallback);

            CgGl.cgGLRegisterStates(Context);
            CgGl.cgGLSetManageTextureParameters(Context, true);
            VertexProfile   = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_VERTEX);
            FragmentProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_FRAGMENT);
        }
예제 #9
0
        /// <summary>
        /// Initializes CgfxCore</summary>
        public static void Init()
        {
            Cg.cgSetErrorCallback(ErrorCallback);

            if (CgGl.cgGLIsProfileSupported(Cg.CG_PROFILE_ARBVP1))
            {
                m_cg_profiles["arbvp1"] = Cg.CG_PROFILE_ARBVP1;
            }

            if (CgGl.cgGLIsProfileSupported(Cg.CG_PROFILE_ARBFP1))
            {
                m_cg_profiles["arbfp1"] = Cg.CG_PROFILE_ARBFP1;
            }

            if (CgGl.cgGLIsProfileSupported(Cg.CG_PROFILE_VP20))
            {
                m_cg_profiles["vp20"] = Cg.CG_PROFILE_VP20;
            }

            if (CgGl.cgGLIsProfileSupported(Cg.CG_PROFILE_FP20))
            {
                m_cg_profiles["fp20"] = Cg.CG_PROFILE_FP20;
            }

            if (CgGl.cgGLIsProfileSupported(Cg.CG_PROFILE_VP30))
            {
                m_cg_profiles["vp30"] = Cg.CG_PROFILE_VP30;
            }

            if (CgGl.cgGLIsProfileSupported(Cg.CG_PROFILE_FP30))
            {
                m_cg_profiles["fp30"] = Cg.CG_PROFILE_FP30;
            }

            if (CgGl.cgGLIsProfileSupported(Cg.CG_PROFILE_VP30))
            {
                m_cg_profiles["vp40"] = Cg.CG_PROFILE_VP40;
            }

            const int FP40 = 6151;

            if (CgGl.cgGLIsProfileSupported(Cg.CG_PROFILE_FP30))
            {
                m_cg_profiles["fp40"] = FP40;
            }

            if (Cg.cgIsContext(m_cg_context) == 0)
            {
                m_cg_context = Cg.cgCreateContext();
            }
        }
        public static void Run()
        {
            string filePath       = Path.Combine("..", "..");
            string fileDirectory  = "Data";
            string vertexFileName = "C2E1v_green.cg";

            if (File.Exists(vertexFileName))
            {
                filePath      = "";
                fileDirectory = "";
            }
            else if (File.Exists(Path.Combine(fileDirectory, vertexFileName)))
            {
                filePath = "";
            }

            string myVertexProgramFileName = Path.Combine(Path.Combine(filePath, fileDirectory), vertexFileName);

            KeyboardDelegate += Keyboard;
            Glut.glutInitWindowSize(400, 400);
            Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInit();

            Glut.glutCreateWindow(myProgramName);
            Glut.glutDisplayFunc(Display);
            Glut.glutKeyboardFunc(KeyboardDelegate);

            Gl.glClearColor(0.1f, 0.3f, 0.6f, 0.0f);  /* Blue background */

            myCgContext = Cg.cgCreateContext();
            checkForCgError("creating context");

            myCgVertexProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_VERTEX);
            CgGl.cgGLSetOptimalOptions(myCgVertexProfile);
            checkForCgError("selecting vertex profile");

            myCgVertexProgram =
                Cg.cgCreateProgramFromFile(
                    myCgContext,             /* Cg runtime context */
                    Cg.CG_SOURCE,            /* Program in human-readable form */
                    myVertexProgramFileName, /* Name of file containing program */
                    myCgVertexProfile,       /* Profile: OpenGL ARB vertex program */
                    myVertexProgramName,     /* Entry function name */
                    null);                   /* No extra compiler options */
            checkForCgError("creating vertex program from file");
            CgGl.cgGLLoadProgram(myCgVertexProgram);
            checkForCgError("loading vertex program");

            Glut.glutMainLoop();
        }
        private void EnableShadersPass3()
        {
            CgGl.cgGLBindProgram(m_CGp_vertexProgramPass3);
            checkForCgError("binding vertex program3");

            CgGl.cgGLEnableProfile(m_CG_vertexProfile);
            checkForCgError("enabling vertex profile3");

            CgGl.cgGLBindProgram(m_CGp_fragmentProgramPass3);
            checkForCgError("binding fragment program3");

            CgGl.cgGLEnableProfile(m_CG_fragmentProfile);
            checkForCgError("enabling fragment profile3");
        }
예제 #12
0
        /// <summary>
        /// Enables or binds a texture with a parameter and a texture ID</summary>
        /// <param name="parameter">Name of parameter used in the Cg program, usually a Simpler2D</param>
        /// <param name="texture_id">Texture ID from glTexGen()</param>
        /// <returns>True if texture enabled successfully</returns>
        public static bool EnableTexture(string parameter, int texture_id)
        {
            IntPtr param;

            try {
                m_cg_parameters.TryGetValue(parameter, out param);
                if (param == (IntPtr)0)
                {
                    return(false);
                }
                CgGl.cgGLSetTextureParameter(param, texture_id);
                CgGl.cgGLEnableTextureParameter(param);
            }
            catch { }
            return(false);
        }
예제 #13
0
        /// <summary>
        /// Disables or unbinds a specific parameter</summary>
        /// <param name="parameter">Name of parameter used in the Cg program, usually a Simpler2D</param>
        /// <returns>True if texture disabled successfully</returns>
        public static bool DisableTexture(string parameter)
        {
            IntPtr param;

            try {
                m_cg_parameters.TryGetValue(parameter, out param);
                if (param == (IntPtr)0)
                {
                    return(false);
                }
                CgGl.cgGLDisableTextureParameter(param);
                return(true);
            }
            catch { }
            return(false);
        }
        private void EnableShadersPass2(Vector3f eyeVector)
        {
            SetShaderParametersPass2(eyeVector);

            CgGl.cgGLBindProgram(m_CGp_vertexProgramPass2);
            checkForCgError("binding vertex program2");

            CgGl.cgGLEnableProfile(m_CG_vertexProfile);
            checkForCgError("enabling vertex profile2");

            CgGl.cgGLBindProgram(m_CGp_fragmentProgramPass2);
            checkForCgError("binding fragment program2");

            CgGl.cgGLEnableProfile(m_CG_fragmentProfile);
            checkForCgError("enabling fragment profile2");
        }
예제 #15
0
        /// <summary>
        /// Sets parameter used in a Cg program</summary>
        /// <param name="parameter">Name of parameter that we want to set</param>
        /// <param name="array">Float array as values that we want to set parameter with</param>
        /// <returns>True if parameter set successfully</returns>
        public static bool SetParameter(string parameter, float[] array)
        {
            if (array == null)
            {
                return(false);
            }
            IntPtr param;

            try {
                m_cg_parameters.TryGetValue(parameter, out param);
                if (param == (IntPtr)0)
                {
                    return(false);
                }
                int length = array.Length;
                switch (length)
                {
                case 1:
                    Cg.cgSetParameter1f(param, array[0]);
                    break;

                case 2:
                    Cg.cgSetParameter2f(param, array[0], array[1]);
                    break;

                case 3:
                    Cg.cgSetParameter3f(param, array[0], array[1], array[2]);
                    break;

                case 4:
                    Cg.cgSetParameter4f(param, array[0], array[1], array[2], array[3]);
                    break;

                case 16:
                    CgGl.cgGLSetMatrixParameterfc(param, array);
                    break;

                default:
                    break;
                }
            }
            catch { return(false); }
            return(true);
        }
예제 #16
0
        /// <summary>
        /// Sets a matrix parameter on a special semantic in a Cg program</summary>
        /// <param name="parameter">Name of parameter that we want to set</param>
        /// <param name="semantic">The semantic that is needed to set a parameter in a special way</param>
        /// <returns>True if parameter set successfully</returns>
        public static bool SetMatrixParameter(string parameter, string semantic)
        {
            IntPtr param;
            int    matrix   = 0;
            int    tranform = 0;

            try
            {
                m_cg_parameters.TryGetValue(parameter, out param);
                if (semantic == "WORLDVIEWPROJECTION")
                {
                    matrix   = CgGl.CG_GL_MODELVIEW_PROJECTION_MATRIX;
                    tranform = CgGl.CG_GL_MATRIX_IDENTITY;
                }

                CgGl.cgGLSetStateMatrixParameter(param, matrix, tranform);
                return(true);
            }
            catch { }
            return(false);
        }
        private void SetShaderParametersPass2(Vector3f eyeVect)
        {
            CgGl.cgGLSetParameter3f(m_CGparam_eyePositionPass2, ViewerPosition.x, ViewerPosition.y, ViewerPosition.z);
            checkForCgError("setting eyePos2");

            // Track the combined model-view-projection matrix
            CgGl.cgGLSetStateMatrixParameter(m_CGparam_modelViewProjPass2,
                                             CgGl.CG_GL_MODELVIEW_PROJECTION_MATRIX,
                                             CgGl.CG_GL_MATRIX_IDENTITY);
            checkForCgError("setting model view projection matrix parameter2");

            //set modelViewIT matrix
            CgGl.cgGLSetStateMatrixParameter(m_CGparam_modelViewITPass2,
                                             CgGl.CG_GL_MODELVIEW_MATRIX,
                                             CgGl.CG_GL_MATRIX_INVERSE_TRANSPOSE);
            checkForCgError("setting model view IT matrix parameter2");

            CgGl.cgGLSetStateMatrixParameter(m_CGparam_modelViewPass2,
                                             CgGl.CG_GL_MODELVIEW_MATRIX,
                                             CgGl.CG_GL_MATRIX_IDENTITY);
            checkForCgError("setting model view matrix parameter2");
        }
        static void Display()
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

            CgGl.cgGLBindProgram(myCgVertexProgram);
            checkForCgError("binding vertex program");

            CgGl.cgGLEnableProfile(myCgVertexProfile);
            checkForCgError("enabling vertex profile");

            /* Rendering code verbatim from Chapter 1, Section 2.4.1 "Rendering
             * a Triangle with OpenGL" (page 57). */
            Gl.glBegin(Gl.GL_TRIANGLES);
            Gl.glVertex2f(-0.8f, 0.8f);
            Gl.glVertex2f(0.8f, 0.8f);
            Gl.glVertex2f(0.0f, -0.8f);
            Gl.glEnd();

            CgGl.cgGLDisableProfile(myCgVertexProfile);
            checkForCgError("disabling vertex profile");

            Glut.glutSwapBuffers();
        }
        private void InitCg()
        {
            m_CG_vertexProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_VERTEX);

            CgGl.cgGLSetOptimalOptions(m_CG_vertexProfile);
            checkForCgError("selecting vertex profile");

            m_CG_fragmentProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_FRAGMENT);

            CgGl.cgGLSetOptimalOptions(m_CG_fragmentProfile);
            checkForCgError("selecting fragment profile");

            // Create the context...
            m_CGcontext = Cg.cgCreateContext();
            checkForCgError("creating context");

            string cas = Application.StartupPath;

            //load pass1
            m_CGp_vertexProgramPass1 = Cg.cgCreateProgramFromFile(m_CGcontext,
                                                                  Cg.CG_SOURCE,
                                                                  cas + "/SpriteVertexProgram.cg",
                                                                  m_CG_vertexProfile,
                                                                  "main_pass1",
                                                                  null);
            checkForCgError("creating vertex program pass1 from file SpriteVertexProgram.cg");
            CgGl.cgGLLoadProgram(m_CGp_vertexProgramPass1);
            checkForCgError("loading vertex program");

            m_CGp_fragmentProgramPass1 = Cg.cgCreateProgramFromFile(m_CGcontext,
                                                                    Cg.CG_SOURCE,
                                                                    cas + "/SpriteFragmentProgram.cg",
                                                                    m_CG_fragmentProfile,
                                                                    "main_pass1",
                                                                    null);
            checkForCgError("creating fragment program from file SpriteFragmentProgram.cg");
            CgGl.cgGLLoadProgram(m_CGp_fragmentProgramPass1);
            checkForCgError("loading fragment program");

            //load pass2
            m_CGp_vertexProgramPass2 = Cg.cgCreateProgramFromFile(m_CGcontext,
                                                                  Cg.CG_SOURCE,
                                                                  cas + "/SpriteVertexProgram.cg",
                                                                  m_CG_vertexProfile,
                                                                  "main_pass2",
                                                                  null);
            checkForCgError("creating vertex program pass2 from file SpriteVertexProgram.cg");
            CgGl.cgGLLoadProgram(m_CGp_vertexProgramPass2);
            checkForCgError("loading vertex program");

            m_CGp_fragmentProgramPass2 = Cg.cgCreateProgramFromFile(m_CGcontext,
                                                                    Cg.CG_SOURCE,
                                                                    cas + "/SpriteFragmentProgram.cg",
                                                                    m_CG_fragmentProfile,
                                                                    "main_pass2",
                                                                    null);
            checkForCgError("creating fragment program from file SpriteFragmentProgram.cg");
            CgGl.cgGLLoadProgram(m_CGp_fragmentProgramPass2);
            checkForCgError("loading fragment program");

            //load pass3
            m_CGp_vertexProgramPass3 = Cg.cgCreateProgramFromFile(m_CGcontext,
                                                                  Cg.CG_SOURCE,
                                                                  cas + "/SpriteVertexProgram.cg",
                                                                  m_CG_vertexProfile,
                                                                  "main_pass3",
                                                                  null);
            checkForCgError("creating vertex program pass3 from file SpriteVertexProgram.cg");
            CgGl.cgGLLoadProgram(m_CGp_vertexProgramPass3);
            checkForCgError("loading vertex program");

            m_CGp_fragmentProgramPass3 = Cg.cgCreateProgramFromFile(m_CGcontext,
                                                                    Cg.CG_SOURCE,
                                                                    cas + "/SpriteFragmentProgram.cg",
                                                                    m_CG_fragmentProfile,
                                                                    "main_pass3",
                                                                    null);
            checkForCgError("creating fragment program from file SpriteFragmentProgram.cg");
            CgGl.cgGLLoadProgram(m_CGp_fragmentProgramPass3);
            checkForCgError("loading fragment pass3 program");

            //pass1 parameters
            m_CGparam_modelViewProjPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "modelViewProj");
            checkForCgError("getting modal view proj");

            m_CGparam_modelViewITPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "modelViewIT");
            checkForCgError("getting modal view IT");

            m_CGparam_modelViewPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "modelView");
            checkForCgError("getting modal view");

            m_CGparam_eyePositionPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "eyePos");
            checkForCgError("getting eyePos");

            m_CGparam_sizeFactorPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "sizeFactor");
            checkForCgError("getting sizeFactor");

            m_CGparam_zoffsetPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "zoffset");
            checkForCgError("getting zoffset");

            CgGl.cgGLSetParameter1f(m_CGparam_zoffsetPass1, 0.003f);
            checkForCgError("setting zoffset");

            m_CGparam_splatRotationCutoffDistPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "splatRotationCutoffDist");
            checkForCgError("getting splatRotationCutoffDist");

            CgGl.cgGLSetParameter1f(m_CGparam_zoffsetPass1, 0.003f);
            checkForCgError("setting zoffset");


            m_CGparam_farcullPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "farcull");
            checkForCgError("getting farcull");

            CgGl.cgGLSetParameter1f(m_CGparam_farcullPass1, WorldView.farCull);
            checkForCgError("setting farcull");

            m_CGparam_viewParamsPass1 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass1, "viewParams");
            checkForCgError("getting viewParams");


            //pass2 parameters
            m_CGparam_modelViewProjPass2 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass2, "modelViewProj");
            checkForCgError("getting modal view proj2");

            m_CGparam_modelViewITPass2 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass2, "modelViewIT");
            checkForCgError("getting modal view IT2");

            m_CGparam_modelViewPass2 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass2, "modelView");
            checkForCgError("getting modal view2");

            m_CGparam_eyePositionPass2 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass2, "eyePos");
            checkForCgError("getting eyePos2");

            m_CGparam_sizeFactorPass2 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass2, "sizeFactor");
            checkForCgError("getting sizeFactor2");

            m_CGparam_splatRotationCutoffDistPass2 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass2, "splatRotationCutoffDist");
            checkForCgError("getting splatRotationCutoffDist");

            CgGl.cgGLSetParameter1f(m_CGparam_sizeFactorPass1, 1f);
            checkForCgError("setting sizeFactor");

            CgGl.cgGLSetParameter1f(m_CGparam_sizeFactorPass2, 1f);
            checkForCgError("setting sizeFactor2");

            m_CGparam_farcullPass2 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass2, "farcull");
            checkForCgError("getting farcull");

            CgGl.cgGLSetParameter1f(m_CGparam_farcullPass2, WorldView.farCull);
            checkForCgError("setting farcull");

            m_CGparam_viewParamsPass2 = Cg.cgGetNamedParameter(m_CGp_vertexProgramPass2, "viewParams");
            checkForCgError("getting viewParams");
        }
        public static void Run()
        {
            string filePath         = Path.Combine("..", "..");
            string fileDirectory    = "Data";
            string vertexFileName   = "C3E4v_twist.cg";
            string fragmentFileName = "C2E2f_passthru.cg";

            if (File.Exists(vertexFileName))
            {
                filePath      = "";
                fileDirectory = "";
            }
            else if (File.Exists(Path.Combine(fileDirectory, vertexFileName)))
            {
                filePath = "";
            }

            string myVertexProgramFileName   = Path.Combine(Path.Combine(filePath, fileDirectory), vertexFileName);
            string myFragmentProgramFileName = Path.Combine(Path.Combine(filePath, fileDirectory), fragmentFileName);

            // Callback Delegates
            keyboardDelegate += keyboard;
            menuDelegate     += menu;
            idleDelegate     += idle;

            Glut.glutInitWindowSize(400, 400);
            Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInit();

            Glut.glutCreateWindow(myProgramName);
            Glut.glutDisplayFunc(display);
            Glut.glutKeyboardFunc(keyboardDelegate);

            Gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);  /* White background */

            myCgContext = Cg.cgCreateContext();
            checkForCgError("creating context");

            myCgVertexProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_VERTEX);
            CgGl.cgGLSetOptimalOptions(myCgVertexProfile);
            checkForCgError("selecting vertex profile");

            myCgVertexProgram =
                Cg.cgCreateProgramFromFile(
                    myCgContext,             /* Cg runtime context */
                    Cg.CG_SOURCE,            /* Program in human-readable form */
                    myVertexProgramFileName, /* Name of file containing program */
                    myCgVertexProfile,       /* Profile: OpenGL ARB vertex program */
                    myVertexProgramName,     /* Entry function name */
                    null);                   /* No extra compiler options */
            checkForCgError("creating vertex program from file");
            CgGl.cgGLLoadProgram(myCgVertexProgram);
            checkForCgError("loading vertex program");

            myCgVertexParam_twisting =
                Cg.cgGetNamedParameter(myCgVertexProgram, "twisting");
            checkForCgError("could not get twisting parameter");

            myCgFragmentProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_FRAGMENT);
            CgGl.cgGLSetOptimalOptions(myCgFragmentProfile);
            checkForCgError("selecting fragment profile");

            myCgFragmentProgram =
                Cg.cgCreateProgramFromFile(
                    myCgContext,               /* Cg runtime context */
                    Cg.CG_SOURCE,              /* Program in human-readable form */
                    myFragmentProgramFileName, /* Name of file containing program */
                    myCgFragmentProfile,       /* Profile: OpenGL ARB vertex program */
                    myFragmentProgramName,     /* Entry function name */
                    null);                     /* No extra compiler options */
            checkForCgError("creating fragment program from file");
            CgGl.cgGLLoadProgram(myCgFragmentProgram);
            checkForCgError("loading fragment program");

            /* No uniform fragment program parameters expected. */

            Glut.glutCreateMenu(menuDelegate);
            Glut.glutAddMenuEntry("[ ] Animate", ' ');
            Glut.glutAddMenuEntry("[w] Wireframe", 'w');
            Glut.glutAttachMenu(Glut.GLUT_RIGHT_BUTTON);

            // Change Animation to true, so that it starts with action ;-)
            keyboard((byte)' ', 0, 0);

            Glut.glutMainLoop();
        }
예제 #21
0
        public static void Run()
        {
            string filePath       = Path.Combine("..", "..");
            string fileDirectory  = "Data";
            string vertexFileName = "C6E1v_bulge.cg";

            if (File.Exists(vertexFileName))
            {
                filePath      = "";
                fileDirectory = "";
            }
            else if (File.Exists(Path.Combine(fileDirectory, vertexFileName)))
            {
                filePath = "";
            }

            string myVertexProgramFileName = Path.Combine(Path.Combine(filePath, fileDirectory), vertexFileName);

            // Callback Delegates
            keyboardDelegate += keyboard;
            menuDelegate     += menu;
            idleDelegate     += idle;
            reshapeDelegate  += reshape;

            Glut.glutInitWindowSize(400, 400);
            Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInit();

            Glut.glutCreateWindow(myProgramName);
            Glut.glutDisplayFunc(display);
            Glut.glutKeyboardFunc(keyboardDelegate);
            Glut.glutReshapeFunc(reshapeDelegate);

            Gl.glClearColor(0.1f, 0.1f, 0.5f, 0f); /* Gray background. */
            Gl.glEnable(Gl.GL_DEPTH_TEST);         /* Hidden surface removal. */

            myCgContext = Cg.cgCreateContext();
            checkForCgError("creating context");

            myCgVertexProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_VERTEX);
            CgGl.cgGLSetOptimalOptions(myCgVertexProfile);
            checkForCgError("selecting vertex profile");

            myCgVertexProgram =
                Cg.cgCreateProgramFromFile(
                    myCgContext,             /* Cg runtime context */
                    Cg.CG_SOURCE,            /* Program in human-readable form */
                    myVertexProgramFileName, /* Name of file containing program */
                    myCgVertexProfile,       /* Profile: OpenGL ARB vertex program */
                    myVertexProgramName,     /* Entry function name */
                    null);                   /* No extra compiler options */
            checkForCgError("creating vertex program from file");
            CgGl.cgGLLoadProgram(myCgVertexProgram);
            checkForCgError("loading vertex program");

            GET_PARAM("modelViewProj");
            GET_PARAM("time");
            GET_PARAM("frequency");
            GET_PARAM("scaleFactor");
            GET_PARAM("Kd");
            GET_PARAM("shininess");
            GET_PARAM("eyePosition");
            GET_PARAM("lightPosition");
            GET_PARAM("lightColor");

            /* Set light source color parameters once. */
            Cg.cgSetParameter3fv(myCgVertexParam_lightColor, out myLightColor[0]);

            Cg.cgSetParameter1f(myCgVertexParam_scaleFactor, 0.3f);
            Cg.cgSetParameter1f(myCgVertexParam_frequency, 2.4f);
            Cg.cgSetParameter1f(myCgVertexParam_shininess, 35f);

            myCgFragmentProfile = CgGl.cgGLGetLatestProfile(CgGl.CG_GL_FRAGMENT);
            CgGl.cgGLSetOptimalOptions(myCgFragmentProfile);
            checkForCgError("selecting fragment profile");

            /* Specify fragment program with a string. */
            myCgFragmentProgram =
                Cg.cgCreateProgram(
                    myCgContext,         /* Cg runtime context */
                    Cg.CG_SOURCE,        /* Program in human-readable form */
                    "float4 main(float4 c : COLOR) : COLOR { return c; }",
                    myCgFragmentProfile, /* Profile: latest fragment profile */
                    "main",              /* Entry function name */
                    null);               /* No extra commyPiler options */
            checkForCgError("creating fragment program from string");
            CgGl.cgGLLoadProgram(myCgFragmentProgram);
            checkForCgError("loading fragment program");

            /* Specify vertex program for rendering the light source with a
             * string. */
            myCgLightVertexProgram =
                Cg.cgCreateProgram(
                    myCgContext,          /* Cg runtime context */
                    Cg.CG_SOURCE,         /* Program in human-readable form */
                    "void main(inout float4 p : POSITION, " +
                    "uniform float4x4 modelViewProj, " +
                    "out float4 c : COLOR) " +
                    "{ p = mul(modelViewProj, p); c = float4(1,1,0,1); }",
                    myCgVertexProfile, /* Profile: latest fragment profile */
                    "main",            /* Entry function name */
                    null);             /* No extra commyPiler options */
            checkForCgError("creating light vertex program from string");
            CgGl.cgGLLoadProgram(myCgLightVertexProgram);
            checkForCgError("loading light vertex program");

            myCgLightVertexParam_modelViewProj =
                Cg.cgGetNamedParameter(myCgLightVertexProgram, "modelViewProj");
            checkForCgError("could not get modelViewProj parameter");

            Glut.glutCreateMenu(menu);
            Glut.glutAddMenuEntry("[ ] Animate", ' ');
            Glut.glutAttachMenu(Glut.GLUT_RIGHT_BUTTON);

            // Change Animation to true, so that it starts with action ;-)
            keyboard((byte)' ', 0, 0);

            Glut.glutMainLoop();
        }
예제 #22
0
        /// <summary>
        /// Unbinds a program from a Cg profile</summary>
        /// <param name="profile">ID for profile we want to unbind the Cg program with</param>
        public static void UnBindProgram(string profile)
        {
            CgGl.cgGLUnbindProgram(m_cg_profiles[profile]);
//            System.Console.WriteLine("  Cg UnBindProgram  ," + m_cg_context + "," + m_cg_program_counter + " profile=" + profile);
        }
예제 #23
0
 /// <summary>
 /// Disables a Cg profile</summary>
 /// <param name="profile">ID for profile we want to disable</param>
 public static void DisableProfile(string profile)
 {
     CgGl.cgGLDisableProfile(m_cg_profiles[profile]);
 }
예제 #24
0
        /// <summary>
        /// Binds a Cg program</summary>
        /// <param name="id">ID for the Cg program we want to bind</param>
        public static void BindProgram(string id)
        {
            CgGl.cgGLBindProgram(m_cg_programs[id]);
//            System.Console.WriteLine("  Cg BindProgram  ," + m_cg_context + "," + m_cg_program_counter + " id=" + id);
        }
예제 #25
0
        static void display()
        {
            /* World-space positions for light and eye. */
            float[] eyePosition   = { 0, 0, 8, 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];

            Cg.cgSetParameter1f(myCgVertexParam_time, myTime);

            buildLookAtMatrix(eyePosition[0], eyePosition[1], eyePosition[2],
                              0, 0, 0,
                              0, 1, 0,
                              ref viewMatrix);

            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

            CgGl.cgGLEnableProfile(myCgVertexProfile);
            checkForCgError("enabling vertex profile");

            CgGl.cgGLEnableProfile(myCgFragmentProfile);
            checkForCgError("enabling fragment profile");

            CgGl.cgGLBindProgram(myCgVertexProgram);
            checkForCgError("binding vertex program");

            CgGl.cgGLBindProgram(myCgFragmentProgram);
            checkForCgError("binding fragment program");

            /*** Render green solid bulging sphere ***/

            /* modelView = rotateMatrix * translateMatrix */
            makeRotateMatrix(70f, 1f, 1f, 1f, ref rotateMatrix);
            makeTranslateMatrix(2.2f, 1f, 0.2f, ref translateMatrix);
            multMatrix(ref modelMatrix, translateMatrix, rotateMatrix);

            /* invModelMatrix = inverse(modelMatrix) */
            invertMatrix(ref invModelMatrix, ref modelMatrix);

            /* Transform world-space eye and light positions to sphere's object-space. */
            transform(ref objSpaceEyePosition, invModelMatrix, eyePosition);
            Cg.cgSetParameter3fv(myCgVertexParam_eyePosition, out objSpaceEyePosition[0]);
            transform(ref objSpaceLightPosition, invModelMatrix, lightPosition);
            Cg.cgSetParameter3fv(myCgVertexParam_lightPosition, out objSpaceLightPosition[0]);

            /* modelViewMatrix = viewMatrix * modelMatrix */
            multMatrix(ref modelViewMatrix, viewMatrix, modelMatrix);

            /* modelViewProj = projectionMatrix * modelViewMatrix */
            multMatrix(ref modelViewProjMatrix, myProjectionMatrix, modelViewMatrix);

            /* Set matrix parameter with row-major matrix. */
            Cg.cgSetMatrixParameterfr(myCgVertexParam_modelViewProj, out modelViewProjMatrix[0]);
            Cg.cgSetParameter4f(myCgVertexParam_Kd, 0.1f, 0.7f, 0.1f, 1f);  /* Green */
            Glut.glutSolidSphere(1.0, 40, 40);

            /*** Render red solid bulging torus ***/

            /* modelView = viewMatrix * translateMatrix */
            makeTranslateMatrix(-2f, -1.5f, 0f, ref translateMatrix);
            makeRotateMatrix(55, 1, 0, 0, ref rotateMatrix);
            multMatrix(ref modelMatrix, translateMatrix, rotateMatrix);

            /* invModelMatrix = inverse(modelMatrix) */
            invertMatrix(ref invModelMatrix, ref modelMatrix);

            /* Transform world-space eye and light positions to sphere's object-space. */
            transform(ref objSpaceEyePosition, invModelMatrix, eyePosition);
            Cg.cgSetParameter3fv(myCgVertexParam_eyePosition, out objSpaceEyePosition[0]);
            transform(ref objSpaceLightPosition, invModelMatrix, lightPosition);
            Cg.cgSetParameter3fv(myCgVertexParam_lightPosition, out objSpaceLightPosition[0]);

            /* modelViewMatrix = viewMatrix * modelMatrix */
            multMatrix(ref modelViewMatrix, viewMatrix, modelMatrix);

            /* modelViewProj = projectionMatrix * modelViewMatrix */
            multMatrix(ref modelViewProjMatrix, myProjectionMatrix, modelViewMatrix);

            /* Set matrix parameter with row-major matrix. */
            Cg.cgSetMatrixParameterfr(myCgVertexParam_modelViewProj, out modelViewProjMatrix[0]);
            Cg.cgSetParameter4f(myCgVertexParam_Kd, 0.8f, 0.1f, 0.1f, 1f);  /* Red */
            Glut.glutSolidTorus(0.15, 1.7, 40, 40);

            /*** Render light as emissive yellow ball ***/

            CgGl.cgGLBindProgram(myCgLightVertexProgram);
            checkForCgError("binding light vertex program");

            /* modelView = translateMatrix */
            makeTranslateMatrix(lightPosition[0], lightPosition[1], lightPosition[2],
                                ref modelMatrix);

            /* modelViewMatrix = viewMatrix * modelMatrix */
            multMatrix(ref modelViewMatrix, viewMatrix, modelMatrix);

            /* modelViewProj = projectionMatrix * modelViewMatrix */
            multMatrix(ref modelViewProjMatrix, myProjectionMatrix, modelViewMatrix);

            /* Set matrix parameter with row-major matrix. */
            Cg.cgSetMatrixParameterfr(myCgLightVertexParam_modelViewProj,
                                      out modelViewProjMatrix[0]);
            Glut.glutSolidSphere(0.1, 12, 12);

            CgGl.cgGLDisableProfile(myCgVertexProfile);
            checkForCgError("disabling vertex profile");

            CgGl.cgGLDisableProfile(myCgFragmentProfile);
            checkForCgError("disabling fragment profile");

            Glut.glutSwapBuffers();
        }