Example #1
0
        void drawModel(
            GraphicsContext graphics,
            Camera camera,
            LightModel light,
            Model model
            )
        {
            Matrix4 world         = model.Posture;
            Matrix4 worldViewProj = camera.Projection * camera.View * world;

            shaderBumpMap.SetUniformValue(shaderBumpMap.FindUniform("WorldViewProj"), ref worldViewProj);

            Matrix4 worldInverse   = world.Inverse();
            Vector4 localLightPos4 = worldInverse * (new Vector4(light.Position, 1.0f));
            Vector3 localLightPos  = new Vector3(localLightPos4.X, localLightPos4.Y, localLightPos4.Z);

            shaderBumpMap.SetUniformValue(shaderBumpMap.FindUniform("LocalLightPosition"), ref localLightPos);

            graphics.SetTexture(0, texNormalMap);
            graphics.SetTexture(1, texColorMap);
            graphics.SetShaderProgram(shaderBumpMap);

            graphics.SetVertexBuffer(0, vbTeapot);

            graphics.DrawArrays(model.Mesh.Prim, 0, model.Mesh.IndexCount);
        }
Example #2
0
        void drawModel(
            GraphicsContext graphics,
            Camera camera,
            LightModel light,
            Model model
            )
        {
            Matrix4 world         = model.Posture;
            Matrix4 worldViewProj = camera.Projection * camera.View * world;

            shaderToon.SetUniformValue(shaderToon.FindUniform("WorldViewProj"), ref worldViewProj);

            // model local Light Direction
            Matrix4 worldInverse = world.Inverse();

            Vector4 localLightPos4 = worldInverse * (new Vector4(light.Position, 1.0f));
            Vector3 localLightDir  = new Vector3(-localLightPos4.X, -localLightPos4.Y, -localLightPos4.Z);

            localLightDir = localLightDir.Normalize();

            shaderToon.SetUniformValue(shaderToon.FindUniform("LocalLightDirection"), ref localLightDir);

            // model local eye
            Vector4 localEye4 = worldInverse * (new Vector4(camera.Position, 1.0f));
            Vector3 localEye  = new Vector3(localEye4.X, localEye4.Y, localEye4.Z);

            shaderToon.SetUniformValue(shaderToon.FindUniform("EyePosition"), ref localEye);

            graphics.SetShaderProgram(shaderToon);
            graphics.SetVertexBuffer(0, vbTeapot);
            graphics.SetTexture(0, texToon);
            graphics.DrawArrays(model.Mesh.Prim, 0, model.Mesh.IndexCount);
        }
Example #3
0
    public void Render( 
                       GraphicsContext graphics,
                       Camera camera,
                       LightModel light,
                       Model model,
                       BgModel bg
                      )
    {
        // setup a camera aligned with the  light source
        float aspect = offWidth / (float)offHeight;
        float fov = 45.0f * (float)(FMath.PI / 180.0f);
        Matrix4 proj = Matrix4.Perspective( fov,
                                            aspect,
                                            1.0f,
                                            10000.0f );
        Matrix4 view = Matrix4.LookAt( light.Position, model.Position, new Vector3( 0.0f, 1.0f, 0.0f ) );
 		Matrix4 world = model.Posture;
        Matrix4 lightVP = proj * view;

        // pass 1
        renderShadowMap( graphics, lightVP * world, model );

        // pass 2
		graphics.Clear();

        drawModel( graphics, camera, lightVP, model );
        light.Render( graphics, camera );

        // test
        texRenderer.Begin();
        int width = texShadow.Width / 2;
        int height = texShadow.Height / 2;
        texRenderer.Render( texShadow, ShaderCatalog.DspWidth - width, 0, 0, 0, width, height );
        texRenderer.End();
    }
Example #4
0
        void drawGround(
            GraphicsContext graphics,
            Camera camera,
            LightModel light
            )
        {
            Matrix4 world         = Matrix4.Translation(new Vector3(0.0f, 0.0f, 0.0f));
            Matrix4 worldViewProj = camera.Projection * camera.View * world;

            // uniform value
            shaderBumpMap.SetUniformValue(shaderBumpMap.FindUniform("WorldViewProj"), ref worldViewProj);

            Matrix4 worldInverse   = world.Inverse();
            Vector4 localLightPos4 = worldInverse * (new Vector4(light.Position, 1.0f));
            Vector3 localLightPos  = new Vector3(localLightPos4.X, localLightPos4.Y, localLightPos4.Z);

            shaderBumpMap.SetUniformValue(shaderBumpMap.FindUniform("LocalLightPosition"), ref localLightPos);

            graphics.SetShaderProgram(shaderBumpMap);
            graphics.SetTexture(0, texNormalMap);
            graphics.SetTexture(1, texColorMap);

            graphics.SetVertexBuffer(0, vbGrid);
            graphics.DrawArrays(meshGrid.Prim, 0, meshGrid.IndexCount);
        }
Example #5
0
        void drawModel(
            GraphicsContext graphics,
            Camera camera,
            LightModel light,
            Model model
            )
        {
            Matrix4 world         = model.Posture;
            Matrix4 worldViewProj = camera.Projection * camera.View * world;

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("WorldViewProj"), ref worldViewProj);

            // model local Light Direction
            Matrix4 worldInverse = world.Inverse();

            Vector4 localLightDir4 = worldInverse * (new Vector4(light.Position, 0));
            Vector3 localLightDir  = new Vector3(-localLightDir4.X, -localLightDir4.Y, -localLightDir4.Z);

            localLightDir = localLightDir.Normalize();

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("LocalLightDirection"), ref localLightDir);

            // model local eye
            Vector4 localEye4 = worldInverse * (new Vector4(camera.Position, 1.0f));
            Vector3 localEye  = new Vector3(localEye4.X, localEye4.Y, localEye4.Z);

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("EyePosition"), ref localEye);

            // light
            Vector4 i_a = light.Ambient;

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("IAmbient"), ref i_a);

            Vector4 i_d = light.Color;

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("IDiffuse"), ref i_d);

            // material
            Vector4 k_a = model.AmbientColor;

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("KAmbient"), ref k_a);

            Vector4 k_d = model.DiffuseColor;

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("KDiffuse"), ref k_d);

            Vector4 k_s = model.SpecularColor;

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("KSpecular"), ref k_s);

            shaderGlossMap.SetUniformValue(shaderGlossMap.FindUniform("Shininess"), 100.0f);

            graphics.SetShaderProgram(shaderGlossMap);
            graphics.SetTexture(0, glossmap);
            graphics.SetVertexBuffer(0, vbTeapot);
            graphics.DrawArrays(model.Mesh.Prim, 0, model.Mesh.IndexCount);
        }
Example #6
0
        static void Init()
        {
            stopwatch = new Stopwatch();
            graphics  = new GraphicsContext();
            SampleDraw.Init(graphics);

            DspWidth  = graphics.Screen.Width;
            DspHeight = graphics.Screen.Height;

            sceneList = new List <IScene>();
            sceneList.Add(new SceneSimpleShader());
            sceneList.Add(new SceneVertexLightingShader());
            sceneList.Add(new SceneGouraudShader());
            sceneList.Add(new ScenePhongShader());
            sceneList.Add(new SceneGlossMapShader());
            sceneList.Add(new SceneTextureShader());
            sceneList.Add(new SceneMultiTextureShader());
            sceneList.Add(new SceneToonShader());
            sceneList.Add(new SceneFogShader());
            sceneList.Add(new SceneBumpMapShader());
            sceneList.Add(new SceneProjectionShadow());

            // graphics context
            graphics.SetViewport(0, 0, DspWidth, DspHeight);
            graphics.SetClearColor(0.0f, 0.5f, 1.0f, 1.0f);
            graphics.Enable(EnableMode.DepthTest);
            graphics.Enable(EnableMode.CullFace);
            graphics.SetCullFace(CullFaceMode.Back, CullFaceDirection.Ccw);

            // camera
            float aspect = DspWidth / (float)DspHeight;
            float fov    = FMath.Radians(45.0f);

            camera = new Camera(fov, aspect, 10.0f, 100.0f);

            // light
            light         = new LightModel();
            light.Color   = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
            light.Ambient = new Vector4(0.3f, 0.3f, 0.3f, 1.0f);

            // model
//        model = new Model( BasicMeshFactory.CreateSphere( 4.0f, 20 ) );
            model               = new Model(BasicMeshFactory.CreateTorus(3.0f, 1.0f, 40, 12));
            model.Position      = new Vector3(0.0f, 4.0f, 0.0f);
            model.DiffuseColor  = new Vector4(0.5f, 0.5f, 1.0f, 1.0f);
            model.AmbientColor  = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
            model.SpecularColor = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);

            // Bg
            modelBg = new BgModel(40.0f, 40.0f, 10, 10);

            id = 0;
            sceneList[id].Setup(graphics, model);

            stopwatch.Start();
        }
Example #7
0
 public void Render(
     GraphicsContext graphics,
     Camera camera,
     LightModel light,
     Model model,
     BgModel bg
     )
 {
     graphics.Clear();
     drawModel(graphics, camera, light, model);
     light.Render(graphics, camera);
     bg.Render(graphics, camera);
 }
Example #8
0
        public void Render(
            GraphicsContext graphics,
            Camera camera,
            LightModel light,
            Model model,
            BgModel bg
            )
        {
            // offscreen rendering
            FrameBuffer oldBuffer = graphics.GetFrameBuffer();

            // render the scene
            graphics.SetFrameBuffer(sceneBuffer);
            graphics.SetViewport(0, 0, sceneBuffer.Width, sceneBuffer.Height);
            graphics.SetClearColor(0.0f, 0.5f, 1.0f, 1.0f);

            renderSimpleScene(graphics, camera, light, model, bg);

            // apply gaussian along X axis
            graphics.SetFrameBuffer(gaussianXBuffer);
            graphics.SetViewport(0, 0, gaussianXBuffer.Width, gaussianXBuffer.Height);

            renderGaussianX(graphics);

            // apply gaussian along Y axis
            graphics.SetFrameBuffer(gaussianXYBuffer);
            graphics.SetViewport(0, 0, gaussianXYBuffer.Width, gaussianXYBuffer.Height);

            renderGaussianY(graphics);

            // final draw
            {
                // restore frame buffer
                graphics.SetFrameBuffer(oldBuffer);
                graphics.SetViewport(0, 0, oldBuffer.Width, oldBuffer.Height);
                graphics.SetClearColor(0.0f, 0.5f, 1.0f, 1.0f);

                graphics.Clear();

                int width  = sceneBuffer.Width / 4;
                int height = sceneBuffer.Height / 4;

                texRenderer.Begin();
                texRenderer.Render(texGaussianXY, 0, 0, 0, 0, sceneBuffer.Width, sceneBuffer.Height);

                texRenderer.Render(texScene, 0, height * 0, 0, 0, width, height);
                texRenderer.Render(texGaussianX, 0, height * 1, 0, 0, width, height);
                texRenderer.Render(texGaussianXY, 0, height * 2, 0, 0, width, height);
                texRenderer.End();
            }
        }
        void drawModel(
            GraphicsContext graphics,
            Camera camera,
            LightModel light,
            Model model
            )
        {
            Matrix4 world         = model.Posture;
            Matrix4 worldViewProj = camera.Projection * camera.View * world;

            shaderVertexLighting.SetUniformValue(shaderVertexLighting.FindUniform("WorldViewProj"), ref worldViewProj);

            Matrix4 worldInverse   = world.Inverse();
            Vector4 localLightPos4 = worldInverse * (new Vector4(light.Position, 1.0f));
            Vector3 localLightDir  = new Vector3(-localLightPos4.X, -localLightPos4.Y, -localLightPos4.Z);

            localLightDir = localLightDir.Normalize();

            shaderVertexLighting.SetUniformValue(shaderVertexLighting.FindUniform("LocalLightDirection"), ref localLightDir);

            // light
            Vector4 i_a = light.Ambient;

            shaderVertexLighting.SetUniformValue(shaderVertexLighting.FindUniform("IAmbient"), ref i_a);

            Vector4 i_d = light.Color;

            shaderVertexLighting.SetUniformValue(shaderVertexLighting.FindUniform("IDiffuse"), ref i_d);

            // material
            Vector4 k_a = model.DiffuseColor;

            shaderVertexLighting.SetUniformValue(shaderVertexLighting.FindUniform("KAmbient"), ref k_a);

            Vector4 k_d = model.DiffuseColor;

            shaderVertexLighting.SetUniformValue(shaderVertexLighting.FindUniform("KDiffuse"), ref k_d);


            graphics.SetShaderProgram(shaderVertexLighting);
            graphics.SetVertexBuffer(0, vbTeapot);
            graphics.DrawArrays(model.Mesh.Prim, 0, model.Mesh.IndexCount);
        }
Example #10
0
        private void renderSimpleScene(
            GraphicsContext graphics,
            Camera camera,
            LightModel light,
            Model model,
            BgModel bg
            )
        {
            // rendering
            graphics.Clear();

            Matrix4 world         = model.Posture;
            Matrix4 worldViewProj = camera.Projection * camera.View * world;

            shaderTexture.SetUniformValue(shaderTexture.FindUniform("WorldViewProj"), ref worldViewProj);
            graphics.SetShaderProgram(shaderTexture);
            graphics.SetVertexBuffer(0, vbTeapot);

            graphics.SetTexture(0, texture);
            graphics.DrawArrays(model.Mesh.Prim, 0, model.Mesh.IndexCount);

            light.Render(graphics, camera);
            bg.Render(graphics, camera);
        }
Example #11
0
        void drawModel(
            GraphicsContext graphics,
            Camera camera,
            LightModel light,
            Model model
            )
        {
            // Fog Near - Far
            float a_Near = 20.0f;
            float a_Far  = 80.0f;

            shaderFog.SetUniformValue(shaderFog.FindUniform("FogNear"), a_Far / (a_Far - a_Near));
            shaderFog.SetUniformValue(shaderFog.FindUniform("FogFar"), -1.0f / (a_Far - a_Near));

            // Fog Color
            Vector4 a_FogColor = new Vector4(1.0f, 0.0f, 0.0f, 0.0f);

            shaderFog.SetUniformValue(shaderFog.FindUniform("FogColor"), ref a_FogColor);


            // light
            Vector4 i_a = light.Ambient;

            shaderFog.SetUniformValue(shaderFog.FindUniform("IAmbient"), ref i_a);

            Vector4 i_d = light.Color;

            shaderFog.SetUniformValue(shaderFog.FindUniform("IDiffuse"), ref i_d);

            // material
            Vector4 k_a = model.AmbientColor;

            shaderFog.SetUniformValue(shaderFog.FindUniform("KAmbient"), ref k_a);

            Vector4 k_d = model.DiffuseColor;

            shaderFog.SetUniformValue(shaderFog.FindUniform("KDiffuse"), ref k_d);


            for (int z = 0; z < 5; z++)
            {
                Matrix4 world         = Matrix4.Translation(new Vector3(5.0f * z, 0.0f, -12.0f * z)) * model.Posture;
                Matrix4 worldViewProj = camera.Projection * camera.View * world;

                shaderFog.SetUniformValue(shaderFog.FindUniform("WorldViewProj"), ref worldViewProj);

                // model local Light Direction
                Matrix4 worldInverse = world.Inverse();

                Vector4 localLightPos4 = worldInverse * (new Vector4(light.Position, 1.0f));
                Vector3 localLightDir  = new Vector3(-localLightPos4.X, -localLightPos4.Y, -localLightPos4.Z);
                localLightDir = localLightDir.Normalize();

                shaderFog.SetUniformValue(shaderFog.FindUniform("LocalLightDirection"), ref localLightDir);

                // model local eye
                Vector4 localEye4 = worldInverse * (new Vector4(camera.Position, 1.0f));
                Vector3 localEye  = new Vector3(localEye4.X, localEye4.Y, localEye4.Z);
                shaderFog.SetUniformValue(shaderFog.FindUniform("EyePosition"), ref localEye);


                graphics.SetShaderProgram(shaderFog);
                graphics.SetVertexBuffer(0, vbTeapot);
                graphics.DrawArrays(model.Mesh.Prim, 0, model.Mesh.IndexCount);
            }
        }