Пример #1
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);
            }
        }
Пример #2
0
        public void PlantTrees(MAstroBody planet, MTerrainTile tile)
        {
            //DistanceThreshold = tile.DistanceThreshold;
            Tile = tile;
            if (tile.material == null)
            {
                return;
            }
            MTexture tex = Tile.Biome;

            if (tex == null)
            {
                return;
            }

            this.transform.Position = tile.transform.Position;

            Random ran = new Random(1234);

            Matrix4d TreeRotation = Matrix4d.CreateFromQuaternion(Globals.LocalUpRotation());

            int i = 0;

            for (int z = 0; z < tile.z_res - 1; z++)
            {
                for (int x = 0; x < tile.x_res - 1; x++)
                {
                    float[] c = tex.GetPixel(x, z);
                    float   r = c[0];
                    float   g = c[1];
                    float   b = c[2];
                    float   a = c[3];

                    float t = 0;
                    //if ((b > r) && (b > g)) t = g;
                    if ((r < 0.05) && (g > 0.1) && (b < 0.05))
                    {
                        t = g;
                    }
                    else
                    {
                        continue;
                    }

                    if (ran.NextDouble() > Settings.TreeDensity)
                    {
                        continue;
                    }

                    //if (g < 0.7) continue;
                    //Console.WriteLine(c[0] + " " + c[1] + " " + c[2] + " " + c[3]);
                    if (i >= Settings.MaxTreesPerTerrain)
                    {
                        break;
                    }
                    Vector3d Treepos = new Vector3d(x,
                                                    0,
                                                    z);
                    //Vector3d PlantingPos = planet.GetNearestPointOnSphere(Treepos, 0);
                    Matrix4d TreeScale    = Matrix4d.Scale(1 + ran.NextDouble(), 1 + ran.NextDouble() * 2, 1 + ran.NextDouble());
                    Vector3d PlantingPos  = Tile.GetPointOnSurfaceFromGrid(Treepos); //; + new Vector3d(r.NextDouble()*5, r.NextDouble() * 5, r.NextDouble()*5);
                    Matrix4d TreePosition = Matrix4d.CreateTranslation(PlantingPos);
                    //find point at y with raycast
                    Matrix4 final = MTransform.GetFloatMatrix(TreeScale * TreeRotation * TreePosition);
                    mats[i] = final;
                    i++;
                }
            }

            TotalInstances = i;

            for (int j = TotalInstances; j < Settings.MaxTreesPerTerrain; j++)
            {
                Matrix4 final = Matrix4.CreateTranslation(j, 0, 0);
                mats[j] = final;
            }

            //Setup();
            //UploadBuffer();
            Planted = false;
        }
Пример #3
0
 public MAudioListener() : base(EType.AudioListener, "AudioListener")
 {
     transform = new MTransform();
     engine    = new ISoundEngine(SoundOutputDriver.AutoDetect);
 }