//    public override void RenderFrame(GraphicsPipeline3D pipeline, BufferedGeometryData[] gBuffers)
    //    {
    //        Matrix wvp = pipeline.WorldMatrix * pipeline.ViewMatrix * pipeline.ProjectionMatrix;
    //        Matrix iv = Matrix.Invert(pipeline.ViewMatrix);
    //        effect.SetValue("worldViewProj", wvp);
    //        effect.SetValue("viewInverse", iv);
    //        effect.SetValue("worldInverseTranspose", Matrix.TransposeMatrix(iv));

    //        effect.SetValue("diffuseTexture2", vTexture);
    //        effect.SetValue("lightDir", new float[] { 1, 1, -1, 1 });
    //        effect.SetValue("lightColor", new float[] { 1, 1, 1, 1 });
    //        effect.SetValue("materialDiffuse", new float[] { 1, 1, 1, 1 });

    //        effect.SetValue("noiseScale", 0.1f);

    //        effect.Technique = technique;

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

    //        foreach (BufferedGeometryData buffer in gBuffers)
    //        {
    //            buffer.DrawBuffers(pipeline);
    //        }

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

        public override void RenderFrame(GraphicsPipeline3D pipeline, MoleculeRenderingScheme scheme, bool alphaPass)
        {
            Matrix wvp = pipeline.WorldMatrix * pipeline.ViewMatrix * pipeline.ProjectionMatrix;
            Matrix iv = Matrix.Invert(pipeline.ViewMatrix);
            effect.SetValue("worldViewProj", wvp);
            effect.SetValue("viewInverse", iv);
            effect.SetValue("worldInverseTranspose", Matrix.TransposeMatrix(Matrix.Invert(pipeline.WorldMatrix)));

            // do light 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();
        }
        private void OnNewDevice()
        {
            if (outSettings != null)
                outSettings.CreateDevice(this, out device, out pParams);

            if (device != null)
            {
                pipeline = new GraphicsFixedPipeline3D(device);
                deviceFont = new Microsoft.DirectX.Direct3D.Font(device, Font);

                sceneManger = new MoleculeSceneManager(device, outSettings);
            }
            else
            {
            }
        }
        private void OnNewDevice()
        {
            if (outSettings != null)
                outSettings.CreateDevice(panel1, out device, out pParams);

            if (device != null)
            {
                pipeline = new GraphicsFixedPipeline3D(device);
                //deviceFont = new Microsoft.DirectX.Direct3D.Font(device, Font);

                if (previewObj != null)
                    previewObj.Dispose();
                previewObj = Mesh.Sphere(device, 2, 32, 32);

                lightObj = Mesh.Sphere(device, 1, 4, 4);

                CreateOrbitLine();

                OnResize(null);
            }
            else
            {
            }
        }
        public override void UpdateFrameData(BufferedGeometryData[] geomData, GraphicsPipeline3D pipeline)
        {
            PointVertex[] blobPoints = (PointVertex[])geomData[0].vBuffers[0].Buffer.Lock(0, LockFlags.None);
            numPoints = blobPoints.Length;

            if (needsReset)
                OnReset();

            FillBlobVB(pipeline.WorldMatrix * pipeline.ViewMatrix, pipeline.ProjectionMatrix, ref blobPoints);
            
            geomData[0].vBuffers[0].Buffer.Unlock();
        }
        public override void RenderFrame(GraphicsPipeline3D pipeline)
        {
            pEffect.SetValue("g_mWorldViewProjection", pipeline.ProjectionMatrix);

            Surface[] apSurfOldRenderTarget = new Surface[2];
            Surface pSurfOldDepthStencil = null;
            Surface[] pGBufSurf = new Surface[4];

            // Clear the render target and the zbuffer 
            //device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(0, 45, 50, 170), 1.0f, 0);
            device.BeginScene();

            // Get the initial device surfaces
            apSurfOldRenderTarget[0] = device.GetRenderTarget(0);  // Only RT 0 should've been set.
            pSurfOldDepthStencil = device.DepthStencilSurface;

            // Turn off Z
            device.RenderState.ZBufferEnable = false;
            device.DepthStencilSurface = null;

            // Clear the blank texture
            Surface pSurfBlank = pTexScratch.GetSurfaceLevel(0);
            device.SetRenderTarget(0, pSurfBlank);
            device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
            pSurfBlank.Dispose();

            // clear temp textures
            for (int i = 0; i < 2; i++)
            {
                pGBufSurf[i] = pTexGBuffer[i].GetSurfaceLevel(0);
                device.SetRenderTarget(0, pGBufSurf[i]);
                device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
            }

            for (int i = 2; i < 4; i++)
                pGBufSurf[i] = pTexGBuffer[i].GetSurfaceLevel(0);

            device.SetStreamSource(0, pBlobVB, 0, System.Runtime.InteropServices.Marshal.SizeOf(typeof(ScreenVertex)));
            device.VertexFormat = ScreenVertexFormat;

            // Render the blobs
            pEffect.Technique = hBlendTech;
            int nNumPasses;
            for (int i = 0; i < numPoints; i++)
            {
                // Copy bits off of render target into scratch surface [for blending].
                pEffect.SetValue("g_tSourceBlob", pTexScratch);
                pEffect.SetValue("g_tNormalBuffer", pTexGBuffer[0]);
                pEffect.SetValue("g_tColorBuffer", pTexGBuffer[1]);

                nNumPasses = pEffect.Begin(FX.None);
                for (int iPass = 0; iPass < nNumPasses; iPass++)
                {
                    for (int rt = 0; rt < nRtUsed; rt++)
                        device.SetRenderTarget(rt, aRTSet[iPass].apCopyRT[rt]);

                    pEffect.BeginPass(iPass);

                    device.DrawPrimitives(PrimitiveType.TriangleList, i * 6, 2);
                    pEffect.EndPass();
                }
                pEffect.End();

                // Render the blob
                pEffect.SetValue("g_tSourceBlob", pTexBlob);
                pEffect.SetValue("g_tNormalBuffer", pTexGBuffer[2]);
                pEffect.SetValue("g_tColorBuffer", pTexGBuffer[3]);

                nNumPasses = pEffect.Begin(FX.None);
                for (int iPass = 0; iPass < nNumPasses; iPass++)
                {
                    for (int rt = 0; rt < nRtUsed; rt++)
                        device.SetRenderTarget(rt, aRTSet[iPass].apBlendRT[rt]);

                    pEffect.BeginPass(iPass);

                    device.DrawPrimitives(PrimitiveType.TriangleList, i * 6, 2);
                    pEffect.EndPass();
                }

                pEffect.End();
            }

            // Restore initial device surfaces
            device.DepthStencilSurface = pSurfOldDepthStencil;

            for (int rt = 0; rt < nRtUsed; rt++)
                device.SetRenderTarget(rt, apSurfOldRenderTarget[rt]);

            // Light and composite blobs into backbuffer
            pEffect.Technique = "BlobLight";

            nNumPasses = pEffect.Begin(FX.None);

            for (int iPass = 0; iPass < nNumPasses; iPass++)
            {
                pEffect.BeginPass(iPass);

                for (int i = 0; i < numPoints; i++)
                {
                    pEffect.SetValue("g_tSourceBlob", pTexGBuffer[0]);
                    pEffect.SetValue("g_tColorBuffer", pTexGBuffer[1]);
                    pEffect.SetValue("g_tEnvMap", pEnvMap);
                    pEffect.CommitChanges();

                    RenderFullScreenQuad(1.0f);
                }
                pEffect.EndPass();
            }

            pEffect.End();
            device.EndScene();

            for (int rt = 0; rt < nRtUsed; rt++)
            {
                if (apSurfOldRenderTarget[rt] != null)
                    apSurfOldRenderTarget[rt].Dispose();
            }
            pSurfOldDepthStencil.Dispose();

            for (int i = 0; i < 4; i++)
            {
                pGBufSurf[i].Dispose();
            }
        }
        private void OnNewDevice()
        {
            if (outSettings != null)
                outSettings.CreateDevice(this, out device, out pParams);

            if (device != null)
            {
                pipeline = new GraphicsFixedPipeline3D(device);
                deviceFont = new Microsoft.DirectX.Direct3D.Font(device, Font);

                sceneManger = new MoleculeSceneManager(device, outSettings);
                BallAndStickRenderingScheme scheme = new BallAndStickRenderingScheme(device);
                sceneManger.SetScheme(scheme);
                sceneManger.SetOutputDesc(latestCoDesc);
                sceneManger.OnNewDataSource(atoms, null, new Vector3(), new Bounds3D(new Vector3(), new Vector3(), 3));
            }
            else
            {
            }
        }
 public abstract void RenderFrame(GraphicsPipeline3D pipeline);
 public virtual void UpdateFrameData(BufferedGeometryData[] geomData, GraphicsPipeline3D pipeline) { }
//        public abstract bool DesiredGeomDataFields(out DataFields[] fields, bool exclusive);
        public abstract void RenderFrame(GraphicsPipeline3D pipeline, MoleculeRenderingScheme scheme, bool alphaPass);
 public abstract void RenderSceneFrame(GraphicsPipeline3D pipeline, int width, int height);
        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();
            }
        }
        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 UpdateFrameViewData(GraphicsPipeline3D pipeline)
 {
 }
 public abstract void UpdateFrameViewData(GraphicsPipeline3D pipeline);