Exemple #1
0
        private bool CreateProgram(String progName, String vertName, String fragName,
                                   List <String> attributes, List <String> uniforms, Logger logger)
        {
            bool result = true;

            logger.Event("Creating shader program: " + progName);

            GLShaderProgram program = new GLShaderProgram();

            if (result == true)
            {
                result = program.Create();
            }

            if (result == true)
            {
                result = program.AttachShader(shaders[vertName].shader);
            }
            else
            {
                // We need to clean up this program since it failed.
                program.Destroy();
            }

            if (result == true)
            {
                result = program.AttachShader(shaders[fragName].shader);
            }
            else
            {
                program.Destroy();
            }

            int count = 0;

            foreach (String s in attributes)
            {
                if (result == true)
                {
                    result = program.BindAttribute(count, s);
                }
                else
                {
                    program.Destroy();
                    break;
                }

                count++;
            }

            // Link the program.
            if (result == true)
            {
                result = program.Link();
            }
            else
            {
                program.Destroy();
            }

            // Make sure the attributes were bound correctly.
            if (result == true)
            {
                result = program.VerifyAttributes(attributes);
            }
            else
            {
                program.Destroy();
            }

            foreach (String s in uniforms)
            {
                if (result == true)
                {
                    result = program.SetUniformLocation(s);
                }
                else
                {
                    program.Destroy();
                    break;
                }
            }

            // Store the shader.
            if (result == true)
            {
                programs.Add(progName, program);
            }
            else
            {
                logger.Error("Failed to create shader program: " + progName);
                program.Destroy();
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Draws the scene.
        /// </summary>
        /// <param name="camera">The camera for the scene.</param>
        public void Render(GLCamera camera)
        {
            // Clear back buffers.
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            // Enable a normal depth test without stenciling.
            GL.Enable(EnableCap.DepthTest);
            GL.Disable(EnableCap.StencilTest);
            GL.DepthFunc(DepthFunction.Less);

            GLShaderProgram program = null;

            // Add the translation in view space.  This allows the model to rotate around
            // itself instead of the origin in world space.
            Matrix4 view = camera.View;

            view.M41 += modelTranslation.X + mouseTranslation.X;
            view.M42 += modelTranslation.Y + mouseTranslation.Y;
            view.M43 += modelTranslation.Z + mouseTranslation.Z;

            //
            // Load shaders for Phong lit static models.
            //

            program = programs["phong"];
            program.Load();

            //
            // Update parameters for phong lighting.
            //

            // Vertex Shader Uniforms
            program.UpdateUniform("u_WorldView",
                                  world * view);
            program.UpdateUniform("u_WorldViewProjection",
                                  world * view * camera.Projection);

            // Fragment Shader Uniforms
            program.UpdateUniform("u_LightDirection", new Vector3(0.0f, 0.0f, 1.0f));
            program.UpdateUniform("u_LightDiffuse",
                                  new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
            program.UpdateUniform("u_KA", 0.85f);
            program.UpdateUniform("u_KD", 0.1f);
            program.UpdateUniform("u_KS", 0.05f);
            program.UpdateUniform("u_SExponent", 8.0f);

            // Draw Static Model

            // Load the model's texture for the shader.
            GL.ActiveTexture(TextureUnit.Texture0);
            program.UpdateUniform("u_Texture", 0);
            if (staticModel.TextureName != String.Empty)
            {
                textures[staticModel.TextureName].Bind(); // not checking return value
            }

            staticModel.Draw();

            GL.UseProgram(0);

            //
            // Load shaders for Phong lit rigged models.
            //

            program = programs["phongRigged"];
            program.Load();

            //
            // Update parameters for phong lighting.
            //

            // Fragment Shader Uniforms
            program.UpdateUniform("u_LightDirection", new Vector3(0.0f, 0.0f, 1.0f));
            program.UpdateUniform("u_LightDiffuse",
                                  new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
            program.UpdateUniform("u_KA", 0.85f);
            program.UpdateUniform("u_KD", 0.1f);
            program.UpdateUniform("u_KS", 0.05f);
            program.UpdateUniform("u_SExponent", 8.0f);

            // Draw Rigged Model

            //
            // Update the uniforms.
            //

            // Textures vary.
            GL.ActiveTexture(TextureUnit.Texture0);
            program.UpdateUniform("u_Texture", 0);
            if (riggedModel.TextureName != String.Empty)
            {
                textures[riggedModel.TextureName].Bind(); // not checking return value
            }

            //
            // Bone Transforms
            //

            if (IsSkinning == true)
            {
                // Get the transformations from the rig.
                boneTransforms = riggedModel.GetBoneTransformations(ref boneTransforms);
            }

            program.UpdateUniform("u_BoneTransform", boneTransforms);

            //
            // World Transform.
            //
            Matrix4 worldView = world * view;

            // Vertex Shader Uniforms
            program.UpdateUniform("u_WorldView",
                                  worldView);
            program.UpdateUniform("u_WorldViewProjection",
                                  worldView * camera.Projection);

            riggedModel.Draw();

            // Unload shaders.
            GL.UseProgram(0);
        }