public override void RenderFrame(GraphicsPipeline3D pipeline, MoleculeRenderingScheme scheme, bool alphaPass)
        {
            Matrix wvp = pipeline.WorldMatrix * pipeline.ViewMatrix * pipeline.ProjectionMatrix;

            effect.SetValue("worldViewProj", wvp);

            // do passes
            effect.Technique = technique;
            effect.Begin(FX.None);
            effect.BeginPass(0);

            // render everything from the scheme
            scheme.RenderAtoms(false, false);
            scheme.RenderBonds(false, false);

            effect.EndPass();
            effect.End();
        }
        public override void RenderFrame(GraphicsPipeline3D pipeline, MoleculeRenderingScheme scheme, bool alphaPass)
        {
            Effect cEffect;

            if (lod == 0)
            {
                cEffect = effect1;
            }
            else if (lod == 1)
            {
                cEffect = effect2;
            }
            else
            {
                return;
            }

            Matrix wvp = pipeline.WorldMatrix * pipeline.ViewMatrix * pipeline.ProjectionMatrix;
            Matrix iv  = Matrix.Invert(pipeline.ViewMatrix);

            cEffect.SetValue("worldViewProj", wvp);
            cEffect.SetValue("viewInverse", iv);
            cEffect.SetValue("worldInverseTranspose", Matrix.TransposeMatrix(iv));
            cEffect.SetValue("shininess", 3);
            cEffect.SetValue("diffuseTexture", testTexture);

            if (alphaPass)
            {
                cEffect.Technique = alphaTechnique;
                cEffect.Begin(FX.None);
                cEffect.BeginPass(0);

                scheme.RenderAtoms(false, false);

                cEffect.EndPass();
                cEffect.End();
            }
            else
            {
                // do light passes
                cEffect.Technique = technique;
                cEffect.Begin(FX.None);
                cEffect.BeginPass(0);
                //effect.SetValue("diffuseTexture", texture);
                foreach (NuGenSVisualLib.Rendering.Lighting.Light light in setup.lights)
                {
                    if (!light.Enabled)
                    {
                        continue;
                    }
                    if (light is DirectionalLight)
                    {
                        DirectionalLight dLight = (DirectionalLight)light;
                        cEffect.SetValue("lightDir", new Vector4(dLight.Direction.X, dLight.Direction.Y, dLight.Direction.Z, 1.0f));
                        cEffect.SetValue("lightColor", ColorValue.FromColor(dLight.Clr));

                        // render everything from the scheme
                        scheme.RenderAtoms(false, false);
                        if (scheme.LightBonds)
                        {
                            scheme.RenderBonds(false, false);
                        }
                    }
                }
                cEffect.EndPass();
                cEffect.End();
            }
            //if (!scheme.LightBonds)
            //{
            //    effect.Technique = lineTechnique;
            //    effect.Begin(FX.None);
            //    effect.BeginPass(0);

            //    scheme.RenderBonds();

            //    effect.EndPass();
            //    effect.End();
            //}

            //device.SetTexture(0, null);
        }
        public override void RenderFrame(GraphicsPipeline3D pipeline, MoleculeRenderingScheme scheme, bool alphaPass)
        {
            Surface rt0 = device.GetRenderTarget(0);
            Surface ds0 = device.DepthStencilSurface;

            // draw shadow to rt1 and ds1
            rt1 = new Texture(device, 1024, 1024, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
            Surface ds1 = device.CreateDepthStencilSurface(1024, 1024, DepthFormat.D16, MultiSampleType.None, 0, true);

            device.SetRenderTarget(0, rt1.GetSurfaceLevel(0));
            device.DepthStencilSurface = ds1;

            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1, 0);

            effect.Technique = renderShadow;

            // calc projection needed for scene
            float   radius   = scheme.SceneRadius + 0.3f;
            Vector3 lightDir = new Vector3(-0.3f, -1, 0);
            float   distance = (float)(radius / Math.Tan(Math.PI / 4)) * 2;
            Vector3 lightPos = (-lightDir * distance) + scheme.SceneOrigin;

            Matrix lightProj = Matrix.PerspectiveFovLH((float)(Math.PI / 4),
                                                       (float)rt0.Description.Width / (float)rt0.Description.Height,
                                                       distance - radius, distance + radius);
            Matrix lightView = Matrix.LookAtLH(lightPos, scheme.SceneOrigin, new Vector3(0, 1, 0));
            Matrix lightViewProjectionMatrix = /*Matrix.Invert(pipeline.WorldMatrix * pipeline.ViewMatrix) **/ lightView * lightProj;

            effect.SetValue("xWorldViewProjection", pipeline.WorldMatrix * pipeline.ViewMatrix * pipeline.ProjectionMatrix);
            effect.SetValue("xWorld", Matrix.Identity);
            effect.SetValue("xMaxDepth", distance + radius);
            effect.SetValue("xLightPos", new Vector4(lightPos.X, lightPos.Y, lightPos.Z, 1));
            effect.SetValue("xLightDir", new Vector4(lightDir.X, lightDir.Y, lightDir.Z, 1));
            effect.SetValue("xLightWorldViewProjection", lightViewProjectionMatrix);
            effect.SetValue("g_fCosTheta", (float)Math.PI / 4);

            Matrix iv = Matrix.Invert(pipeline.WorldMatrix * pipeline.ViewMatrix);

            effect.SetValue("viewInverse", iv);

            /*effect.SetValue("g_mWorldView", lightView);
             * effect.SetValue("g_mProj", lightProj);*/


            //device.Clear(ClearFlags.ZBuffer, 0x000000ff, 1, 0);
            //device.RenderState.CullMode = Cull.Clockwise;
            device.RenderState.ZBufferEnable = true;

            effect.Begin(FX.None);
            effect.BeginPass(0);

            // render everything from the scheme
            scheme.RenderAtoms(false, true);
            //scheme.RenderBonds(false, true);

            /*CustomVertex.PositionNormal[] floor = new CustomVertex.PositionNormal[4];
             * floor[0] = new CustomVertex.PositionNormal(new Vector3(6, -3, 6), new Vector3(0, 1, 0));
             * floor[2] = new CustomVertex.PositionNormal(new Vector3(-6, -3, 6), new Vector3(0, 1, 0));
             * floor[1] = new CustomVertex.PositionNormal(new Vector3(6, -3, -6), new Vector3(0, 1, 0));
             * floor[3] = new CustomVertex.PositionNormal(new Vector3(-6, -3, -6), new Vector3(0, 1, 0));*/

            /*device.VertexFormat = CustomVertex.PositionNormal.Format;
             * device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, floor);*/

            effect.EndPass();
            effect.End();

            device.SetRenderTarget(0, rt0);
            device.DepthStencilSurface = ds0;


            // render scene mapped with shadow
            //device.RenderState.CullMode = Cull.CounterClockwise;
            effect.Technique = renderScene;
            effect.SetValue("xShadowMap", rt1);

            /*effect.SetValue("xWorld", pipeline.WorldMatrix);
             * effect.SetValue("xLightWorldViewProjection", pipeline.WorldMatrix * lightViewProjectionMatrix);*/

            /*effect.SetValue("g_mWorldView", pipeline.WorldMatrix * pipeline.ViewMatrix);
             * effect.SetValue("g_mProj", pipeline.ProjectionMatrix);
             * effect.SetValue("g_mViewToLightProj", lightViewProjectionMatrix);
             * effect.SetValue("g_vLightPos", new Vector4(lightPos.X, lightPos.Y, lightPos.Z, 1));
             * effect.SetValue("g_vLightDir", new Vector4(lightDir.X, lightDir.Y, lightDir.Z, 1));
             * effect.SetValue("g_fCosTheta", (float)Math.PI / 4);*/

            effect.Begin(FX.None);
            effect.BeginPass(0);

            // render everything from the scheme
            scheme.RenderAtoms(false, true);

            if (scheme.LightBonds)
            {
                scheme.RenderBonds(false, false);
            }

            // draw floor

            /*device.VertexFormat = CustomVertex.PositionNormal.Format;
             * device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, floor);*/

            effect.EndPass();
            effect.End();

            effect.Technique = renderLines;

            effect.Begin(FX.None);
            effect.BeginPass(0);

            scheme.RenderBonds(false, true);

            effect.EndPass();
            effect.End();

            //TextureLoader.Save("c:/shadow.dds", ImageFileFormat.Dds, rt1);

            rt1.Dispose();
            ds1.Dispose();
        }
示例#4
0
        public override void RenderSceneFrame(GraphicsPipeline3D pipeline, int width, int height)
        {
            //pipeline.PushAll();

            //pipeline.PopAll();

            //pipeline.ProjectionMatrix = Matrix.OrthoLH(width, height, 0f, 2f);
            //pipeline.ViewMatrix = Matrix.Identity;
            //pipeline.WorldMatrix = Matrix.Identity;

            if (ppEffect != null)
            {
                // do alpha pass first
                ppEffect.PreFramePass(-1);
                if (ppEffect.RenderScene(-1))
                {
                    // render scene w/effect
                    device.RenderState.Lighting = false;
                    // update frame data
                    scheme.UpdateFrameViewData(pipeline);
                    if (effect != null)
                    {
                        if (effect is GeometryRenderingEffect)
                        {
                            GeometryRenderingEffect gEffect = (GeometryRenderingEffect)effect;
                            gEffect.RenderFrame(pipeline, scheme, true);
                        }

                        /*else if (effect is ScreenSpaceRenderingEffect)
                         * {
                         *  ScreenSpaceRenderingEffect sEffect = (ScreenSpaceRenderingEffect)effect;
                         *  sEffect.UpdateFrameData(scheme.atomsBufferData, pipeline);
                         *
                         *  sEffect.RenderFrame(pipeline);
                         *  return;
                         * }*/
                    }

                    /*device.RenderState.Lighting = false;
                     * scheme.RenderAtoms(true, effect == null);
                     * scheme.RenderBonds(true, (effect == null || !scheme.LightBonds));*/
                }
                ppEffect.PostFramePass(-1);

                for (int i = 0; i < ppEffect.NumPassesRequired; i++)
                {
                    ppEffect.PreFramePass(i);
                    if (ppEffect.RenderScene(i))
                    {
                        // render scene w/effect
                        device.RenderState.Lighting = true;
                        // update frame data
                        scheme.UpdateFrameViewData(pipeline);
                        if (effect != null)
                        {
                            if (effect is GeometryRenderingEffect)
                            {
                                GeometryRenderingEffect gEffect = (GeometryRenderingEffect)effect;
                                gEffect.RenderFrame(pipeline, scheme, false);
                            }
                            else if (effect is ScreenSpaceRenderingEffect)
                            {
                                ScreenSpaceRenderingEffect sEffect = (ScreenSpaceRenderingEffect)effect;
                                sEffect.UpdateFrameData(scheme.atomsBufferData, pipeline);

                                sEffect.RenderFrame(pipeline);
                                return;
                            }
                        }
                        device.RenderState.Lighting = false;
                        scheme.RenderAtoms(true, effect == null);
                        scheme.RenderBonds(true, (effect == null || !scheme.LightBonds));
                    }
                    ppEffect.PostFramePass(i);
                }
            }
            else
            {
                device.RenderState.Lighting = true;

                // update frame data
                scheme.UpdateFrameViewData(pipeline);

                if (effect != null)
                {
                    if (effect is GeometryRenderingEffect)
                    {
                        GeometryRenderingEffect gEffect = (GeometryRenderingEffect)effect;
                        gEffect.RenderFrame(pipeline, scheme, false);
                    }
                    else if (effect is ScreenSpaceRenderingEffect)
                    {
                        ScreenSpaceRenderingEffect sEffect = (ScreenSpaceRenderingEffect)effect;
                        sEffect.UpdateFrameData(scheme.atomsBufferData, pipeline);

                        sEffect.RenderFrame(pipeline);
                        return;
                    }
                    //else if (effect.EfxType == RenderingEffect.EffectType.ScreenSpace)
                    //{
                    //    // TODO: Check for matrix changes for update check
                    //    effect.UpdateFrameData(scheme.atomsBufferData, pipeline);
                    //    effect.RenderFrame(pipeline, scheme);
                    //}
                }

                // render as fixed pipeline (/ do FP pass)

                // TODO: Setup lights

                device.RenderState.Lighting = false;

                scheme.RenderAtoms(true, effect == null);
                scheme.RenderBonds(true, (effect == null || !scheme.LightBonds));
            }

            // render post-scene world entities
            foreach (IEntity entity in postSceneWorldEntities)
            {
                entity.Render();
            }

            Matrix wvMat = pipeline.WorldMatrix * pipeline.ViewMatrix;

            foreach (ViewSpaceEntity entity in postSceneViewEntities)
            {
                entity.UpdateView(pipeline.WorldMatrix, pipeline.ViewMatrix);
                // z-compare
                float z = 1000 - Vector3.Transform(entity.BoundingBox.Centre, wvMat).Z;
                try
                {
                    zCompareViewEntities.Add(z, entity);
                }
                catch { zCompareViewEntities.Add(z - 0.001f, entity); } // TODO: more robust feature?
            }

            foreach (KeyValuePair <float, ViewSpaceEntity> entity in zCompareViewEntities)
            {
                entity.Value.Render();
            }
            zCompareViewEntities.Clear();

            // render post-scene screen entities
            foreach (IScreenSpaceEntity entity in screenEntities)
            {
                // TODO: Detect matrix changes
                entity.Update(pipeline.WorldMatrix, pipeline.ViewMatrix, pipeline.ProjectionMatrix);
                entity.Render();
            }
        }