Exemplo n.º 1
0
 public void CopyTo(MPointLight p)
 {
     base.CopyTo(p);
     p.Ambient  = Ambient;
     p.Diffuse  = Diffuse;
     p.Specular = Specular;
 }
Exemplo n.º 2
0
        /**
         * During the Rendering phase all matrices are pre-multiplied (to bring coordinates back to local 32bit space)
         * and all offsets and set relative to the camera, to aid with shadow calculations -
         * */
        public void Render()
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            Globals.ShaderOverride   = null;
            Globals.DrawCalls        = 0;
            Globals.Index            = 0;
            Globals.GlobalOffset     = Camera.transform.Position;
            Globals.GlobalOffsetCalc = Camera.transform.Position;
            Matrix4d offsetmat   = Matrix4d.CreateTranslation(-Globals.GlobalOffset);
            Matrix4d lightmatrix = light.GetLightSpaceMatrix();

            Matrix4d view       = Camera.GetViewMatrix();
            Matrix4d projection = Camera.GetProjection(true);
            Matrix4d viewproj   = view * projection;

            // GL.Viewport(0, 0, MScreen.Width, MScreen.Height);
            //============================
            // Globals.GlobalOffset = Vector3d.Zero;

            // GL.CullFace(CullFaceMode.Back);
            // reset viewport
            // 2. render scene as normal using the generated depth/shadow map
            // --------------------------------------------------------------

            for (int i = 0; i < MaterialRoot.Modules.Count; i++)
            {
                MMaterial mat = (MMaterial)MaterialRoot.Modules[i];
                if (mat.IsUsed == false)
                {
                    continue;
                }
                mat.shader.Bind();
                mat.shader.SetMat4("view", MTransform.GetFloatMatrix(view));
                mat.shader.SetMat4("projection", MTransform.GetFloatMatrix(projection));
                mat.shader.SetVec4("Tweak", new Vector4(Settings.Tweak1, Settings.Tweak2, Settings.Tweak3, Settings.Tweak4));

                // set light uniforms
                Fog.Bind(mat.shader);

                //TODO: use nearest lights
                mat.shader.SetInt("NumLights", LightRoot.Modules.Count);
                //TODO sort lights closest to furthest (because there is max lights =10 in shader)

                for (int il = 0; il < LightRoot.Modules.Count; il++)
                {
                    if (il >= MAXLIGHTS)
                    {
                        continue;
                    }
                    //pointLightPositions[0]
                    MPointLight p = (MPointLight)LightRoot.Modules[il];
                    p.Bind(mat, il);
                }

                mat.shader.SetVec3("viewPos", MTransform.GetVector3(Camera.transform.Position - Globals.GlobalOffset));
                mat.shader.SetVec3("sunPos", MTransform.GetVector3(light.transform.Position - Globals.GlobalOffset));
                mat.shader.SetInt("Closeup", Closeup);
                light.Bind(mat);
            }

            /////////////////////// DEPTH LIGHT FOR SHADOWS ///////////////////////
            GL.Viewport(0, 0, MScreen.Width, MScreen.Height);

//      GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

            if (light.Shadows)
            {
                GL.Enable(EnableCap.CullFace);
                GL.CullFace(CullFaceMode.Back);
                //GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
                Globals.RenderPass = Globals.eRenderPass.ShadowDepth;
                //============================
                //render depthmap from light using depth shader
                light.Render(lightmatrix, offsetmat);
                simpleDepthShader.Bind();

                Globals.ShaderOverride = simpleDepthShader;
                GL.DepthFunc(DepthFunction.Less);
                GL.Enable(EnableCap.DepthTest);

                //Background.Render(lightmatrix, offsetmat);
                //GL.Clear(ClearBufferMask.DepthBufferBit);
                Background2.Render(lightmatrix, offsetmat);
                ModelRoot.Render(lightmatrix, offsetmat);
                Globals.RenderPass     = Globals.eRenderPass.Normal;
                Globals.ShaderOverride = null;
            }

            //============================
            ////////////////// NORMAL RENDER ////////////////////
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            //Helper.CheckGLError(this, "Render 1");
            //set FBO
            Renderer.Render(viewproj, offsetmat);
            //Helper.CheckGLError(this, "Render 1");

            //load the shadow map into texture slot 4
            GL.ActiveTexture(TextureUnit.Texture0 + MShader.LOCATION_SHADOWMAP);
            GL.BindTexture(TextureTarget.Texture2D, light.depthMap);
            ////////////////////////////////////////////////////////
            //  GL.PatchParameter(PatchParameterInt.PatchVertices, 16);

            RenderScene(ref view, ref offsetmat, ref projection, ref viewproj);

            //////////// OVERLAY LAYER ////////////
            //============================
            GL.Disable(EnableCap.DepthTest);
            GL.Clear(ClearBufferMask.DepthBufferBit);
            if (Overlay.Modules.Count > 0)
            {
                projection = Camera.GetOverlayProjection();
                view       = Camera.GetOverlayViewMatrix();
                viewproj   = view * projection;
                //UpdateOverlay();
                Overlay.Render(viewproj, Matrix4d.Identity);
            }

            if (Stereo == true)
            {
                GL.Viewport(0, 0, MScreen.Width / 2, MScreen.Height);
                Vector3d original = Camera.transform.Position;
                Camera.transform.Position -= Camera.transform.Right();
                view     = Camera.GetViewMatrix();
                viewproj = view * projection;
                GL.Viewport(MScreen.Width / 2, 0, MScreen.Width / 2, MScreen.Height);
                RenderScene(ref view, ref offsetmat, ref projection, ref viewproj);
                Camera.transform.Position = original;
                GL.Viewport(0, 0, MScreen.Width, MScreen.Height);
            }

            Renderer.AfterRender();

            // GL.Disable(EnableCap.DepthTest);
            // GL.Disable(EnableCap.DepthTest);
            // render Depth map to quad for visual debugging
            // ---------------------------------------------
            //GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
            if (Settings.DebugDepth == true)
            {
                debugQuad.Bind();

                debugQuad.material.shader.SetFloat("near_plane", light.NearPlane);
                debugQuad.material.shader.SetFloat("far_plane", light.FarPlane);
                GL.ActiveTexture(TextureUnit.Texture0);
                GL.BindTexture(TextureTarget.Texture2D, light.depthMap);
                GL.Clear(ClearBufferMask.DepthBufferBit);
                debugQuad.Render(lightmatrix, Matrix4d.Identity);
            }

            ///do not reset
            /// Globals.GlobalOffset = Vector3d.Zero;

            //projection = Camera.GetProjection(true);
            //GUIRoot.Render(Matrix4d.Identity, Matrix4d.Identity);



            ErrorCode err = GL.GetError();

            if (err != ErrorCode.NoError)
            {
                //Console.WriteLine("MScene Render:" + err);
            }
        }
Exemplo n.º 3
0
        public override void CopyTo(MObject m1)
        {
            base.CopyTo(m1);

            MSceneObject mo = (MSceneObject)m1;

            mo.transform.Scale          = transform.Scale;
            mo.transform.RotationOffset = transform.RotationOffset;

            //we don't want to use the template position, since these will be set by the world map
            //mo.transform.Position = transform.Position;
            //mo.transform.Rotation = transform.Rotation;
            mo.SetMaterial(material);
            mo.TemplateID     = TemplateID;
            mo.IsAvatar       = IsAvatar;
            mo.CastsShadow    = CastsShadow;
            mo.BoundingBox    = BoundingBox;
            mo.IndicesLength  = IndicesLength;
            mo.VerticesLength = VerticesLength;
            mo.IsTransparent  = IsTransparent;

            foreach (MObject m in Modules)
            {
                if (m.Type == EType.PhysicsObject)
                {
                    MPhysicsObject p1 = (MPhysicsObject)m;
                    MPhysicsObject p2 = new MPhysicsObject(mo, mo.Name, (float)p1.Mass, p1.Shape, false, p1.CreateScale);
                    p2.Name = p1.Name;
                    p2.SetDamping(p1._rigidBody.LinearDamping, p1._rigidBody.AngularDamping);
                    p2._rigidBody.AngularFactor = p1._rigidBody.AngularFactor;
                    p2._rigidBody.LinearFactor  = p1._rigidBody.LinearFactor;
                    p2._rigidBody.Restitution   = p1._rigidBody.Restitution;
                    p2.SetPosRot(mo.transform.Position, mo.transform.Rotation);
                    //p2.SetPosition(mo.transform.Position);
                    //p2.SetRotation(mo.transform.Rotation);
                    p2.StopAllMotion();
                }

                if (m.Type == EType.PointLight)
                {
                    MPointLight p = new MPointLight(m.Name);
                    MScene.LightRoot.Add(p);
                    m.CopyTo(p);
                    mo.Add(p);
                }

                if (m.Type == EType.Sound)
                {
                    MSound os = (MSound)m;
                    MSound s  = new MSound();
                    os.CopyTo(s);
                    s.Error = os.Error;
                    s.Load(os.Filename, mo);
                    mo.Add(s);
                }

                //network objects can only be owned by the user. When the user is offline the network object is dormant
                //if (mo.OwnerID.Equals(Globals.UserAccount.UserID))
                if (m.Type == EType.NetworkObject)
                {
                    MNetworkObject mn = (MNetworkObject)m.FindModuleByType(MObject.EType.NetworkObject);
                    if (mn != null)
                    {
                        mo.Add(mn);
                    }
                }

                if (m.Type == EType.NPCPlayer)
                {
                    MNPC p = new MNPC(mo, m.Name);
                    m.CopyTo(p);
                    mo.Add(p);
                }

                if (m.Type == EType.Door)
                {
                    MDoor d2 = new MDoor(mo);
                    m.CopyTo(d2);
                    mo.Add(d2);
                }
            }
        }