Example #1
0
        void SetupMaterial()
        {
            MMaterial InstanceMat = new MMaterial("InstanceGrassMaterial");
            MShader   shader      = new MShader("InstanceGrassShader");

            //shader.LoadFromString(sVertexShader, sFragmentShader);
            shader.Load("instanced_v.glsl",
                        "instanced_f.glsl",
                        "", ""
                        );
            shader.Bind();
            shader.SetFloat("Fade", 68);
            shader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
            shader.SetInt("material.specular", MShader.LOCATION_SPECULAR);
            shader.SetInt("material.multitex", MShader.LOCATION_MULTITEX);
            shader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP);
            shader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP);
            InstanceMat.AddShader(shader);
            MTexture GrassTex = Globals.TexturePool.GetTexture(TreeTexture);

            GrassTex._TextureWrapMode = TextureWrapMode.ClampToBorder;
            GrassTex.DoAssign         = true;
            InstanceMat.SetDiffuseTexture(GrassTex);
            this.SetMaterial(InstanceMat);
            grass.SetMaterial(InstanceMat);
            MScene.MaterialRoot.Add(InstanceMat);
        }
Example #2
0
        void AddBackdrop()
        {
            Helper.CheckGLError(this, "TestPoint 3a");
            MShader BackgroundShader = new MShader("BackgroundShader");

            BackgroundShader.Load("default_v.glsl",
                                  "unlit_f.glsl",
                                  "Terrain\\eval.glsl", "Terrain\\control.glsl"
                                  );

            BackgroundShader.Bind();
            BackgroundShader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
            Helper.CheckGLError(this, "TestPoint 3b");
            //SkyShader.SetInt("shadowMap", 2);

            MMaterial BGMat = new MMaterial("BackgroundMaterial");

            BGMat.AddShader(BackgroundShader);
            Helper.CheckGLError(this, "TestPoint 4a");
            MTexture BackgroundTexure = new MTexture("BackgroundT");

            Helper.CheckGLError(this, "TestPoint 4b");
            BackgroundTexure.LoadTextureData(MFileSystem.ProjectPath + "Assets\\Textures\\Planets\\8k_stars_milky_way.jpg");
            BackgroundTexure.DoAssign = true;

            MaterialRoot.Add(BGMat);
            BGMat.SetDiffuseTexture(BackgroundTexure);
            backdrop = new MSky();
            backdrop.AddMaterial(BGMat);
            UtilityRoot.Add(backdrop);
        }
Example #3
0
        public void AfterRender()
        {
            GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            GL.Disable(EnableCap.DepthTest);
            GL.ClearColor(ClearColor);

            FXShader.Bind();
            GL.BindVertexArray(quadVAO);
            GL.BindTexture(TextureTarget.Texture2D, SceneTex); // use the color attachment texture as the texture of the quad plane
            GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
        }
Example #4
0
        void SetupMaterial()
        {
            /*
             * MShader TreeShader = new MShader("TreeShader");
             * TreeShader.Load("default_v.glsl",
             * "default_f.glsl",
             * "",
             * ""
             * );
             * TreeShader.Bind();
             * TreeShader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
             * TreeShader.SetInt("material.specular", MShader.LOCATION_SPECULAR);
             * TreeShader.SetInt("material.multitex", MShader.LOCATION_MULTITEX);
             * TreeShader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP);
             * TreeShader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP);
             *
             * MMaterial Avatar1Mat = new MMaterial("TREE01M");
             * Avatar1Mat.AddShader(TreeShader);
             * Avatar1Mat.SetDiffuseTexture(Globals.TexturePool.GetTexture("Textures\\avatar01.jpg"));
             * MScene.MaterialRoot.Add(Avatar1Mat);
             */

            MMaterial InstanceMat = new MMaterial("ForestMaterialInstanced");
            MShader   shader      = (MShader)MScene.MaterialRoot.FindModuleByName("ForestShaderInstanced");

            if (shader == null)
            {
                shader = new MShader("ForestShaderInstanced");
                //shader.LoadFromString(sVertexShader, sFragmentShader);
                shader.Load("instanced_v.glsl",
                            "instanced_f.glsl",
                            "", ""
                            );
                shader.Bind();
                shader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
                shader.SetInt("material.specular", MShader.LOCATION_SPECULAR);
                shader.SetInt("material.multitex", MShader.LOCATION_MULTITEX);
                shader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP);
                shader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP);
            }
            InstanceMat.AddShader(shader);
            InstanceMat.SetDiffuseTexture(Globals.TexturePool.GetTexture(TreeTexture));
            this.SetMaterial(InstanceMat);
            tree.SetMaterial(InstanceMat);
            MScene.MaterialRoot.Add(InstanceMat);
        }
Example #5
0
        public static MShader CreateShader(string sName)
        {
            MShader WallShader = new MShader(sName);

            WallShader.Load("default_v.glsl",
                            "default_f.glsl",
                            "Terrain\\eval.glsl",
                            "Terrain\\control.glsl"
                            );
            WallShader.Bind();
            WallShader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
            WallShader.SetInt("material.specular", MShader.LOCATION_SPECULAR);
            WallShader.SetInt("material.multitex", MShader.LOCATION_MULTITEX);
            WallShader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP);
            WallShader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP);
            return(WallShader);
        }
Example #6
0
        void CreateShaders()
        {
            MShader SkyShader = Helper.CreateShader("SkyShader");

            SkyShader.Load("default_v.glsl",
                           "sky_f.glsl",
                           "Terrain\\eval.glsl",
                           "Terrain\\control.glsl"
                           );
            SkyShader.Bind();
            SkyShader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
            SkyShader.SetInt("material.specular", MShader.LOCATION_SPECULAR);
            SkyShader.SetInt("material.multitex", MShader.LOCATION_MULTITEX);
            SkyShader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP);
            SkyShader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP);

            MSkyMaterial = new MMaterial("SKY01M");
            MSkyMaterial.AddShader(SkyShader);
            MSkyMaterial.SetDiffuseTexture(Globals.TexturePool.GetTexture(SkyTexURL));
            MScene.MaterialRoot.Add(MSkyMaterial);
        }
Example #7
0
        void SetupMaterial()
        {
            MMaterial InstanceMat = new MMaterial("InstanceMaterial");
            MShader   shader      = new MShader("InstanceShader");

            //shader.LoadFromString(sVertexShader, sFragmentShader);
            shader.Load("instanced_v.glsl",
                        "instanced_f.glsl",
                        "", ""
                        );
            shader.Bind();
            shader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
            shader.SetInt("material.specular", MShader.LOCATION_SPECULAR);
            shader.SetInt("material.multitex", MShader.LOCATION_MULTITEX);
            shader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP);
            shader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP);
            InstanceMat.AddShader(shader);
            InstanceMat.SetDiffuseTexture(Globals.TexturePool.GetTexture(MeshTexture));
            this.SetMaterial(InstanceMat);
            material = InstanceMat;
        }
Example #8
0
        public void Setup(MAstroBody b)
        {
            CurrentBody = b;

            // TerrainShader = Helper.CreateShader(MShader.TERRAIN_SHADER);

            TerrainShader = new MShader(MShader.TERRAIN_SHADER);
            TerrainShader.Load("default_v.glsl",
                               "Terrain\\terrain_f.glsl",
                               "",
                               ""
                               );
            TerrainShader.Bind();
            TerrainShader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
            TerrainShader.SetInt("material.specular", MShader.LOCATION_SPECULAR);
            TerrainShader.SetInt("material.multitex", MShader.LOCATION_MULTITEX);
            TerrainShader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP);
            TerrainShader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP);

            base.Setup();
        }
Example #9
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);
            }
        }
Example #10
0
        /// <summary>
        /// The main setup point for the initial scene graph and tools
        /// </summary>
        public void SetupInitialObjects()
        {
            Root = new MObject(MObject.EType.Null, "Root");
            //Root.Deletable = false;

            TemplateRoot = new MObject(MObject.EType.Null, "TemplateRoot");
            Root.Add(TemplateRoot);
            TemplateRoot.Enabled = false; //disable updates

            SelectionRoot = new MObject(MObject.EType.Null, "Selection");
            Root.Add(SelectionRoot);

            UtilityRoot           = new MObject(MObject.EType.Null, "Utility");
            UtilityRoot.Deletable = false;
            Root.Add(UtilityRoot);
            time = new Time();
            UtilityRoot.Add(time);

            LightRoot = new MObject(MObject.EType.Null, "LightRoot");
            Root.Add(LightRoot);

            Globals.ScriptHost = new MScriptHost();
            UtilityRoot.Add(Globals.ScriptHost);
            MSystemScript sc = new MSystemScript("Massive.Main", Root);

            sc.SetActivator(Globals.ScriptHost.GetMainActivator());
            Root.Add(sc);

            Globals.TexturePool = new TexturePool();
            UtilityRoot.Add(Globals.TexturePool);

            Renderer = new MRenderer();
            UtilityRoot.Add(Renderer);

            DefaultShader = new MShader(MShader.DEFAULT_SHADER);
            DefaultShader.Load("default_v.glsl",
                               "default_f.glsl",
                               "Terrain\\eval.glsl", "Terrain\\control.glsl");
            DefaultShader.Bind();
            DefaultShader.SetInt("material.diffuse", MShader.LOCATION_DIFFUSE);
            DefaultShader.SetInt("material.specular", MShader.LOCATION_SPECULAR);
            DefaultShader.SetInt("material.multitex", MShader.LOCATION_MULTITEX);
            DefaultShader.SetInt("material.normalmap", MShader.LOCATION_NORMALMAP);
            DefaultShader.SetInt("material.shadowMap", MShader.LOCATION_SHADOWMAP);
            //DefaultShader.Deletable = false;
            Helper.CheckGLError(this, "TestPoint 1");

            // MShader GUIShader = new MShader("DefaultGUIShader");
            // GUIShader.Load("gui_v.glsl", "gui_f.glsl");
            //GUIShader.Bind();
            //GUIShader.Deletable = false;

            MaterialRoot = new MObject(MObject.EType.Null, "MaterialRoot");
            //MaterialRoot.Deletable = false;
            Root.Add(MaterialRoot);
            Helper.CheckGLError(this);
            if (Physics != null)
            {
                Physics.Dispose();
            }
            Physics = new MPhysics();
            Physics.Setup(); //only need to do this once
            //Physics.Deletable = false;
            UtilityRoot.Add(Physics);
            Helper.CheckGLError(this, "TestPoint 2");
            //UtilityRoot.Add(Globals.Network);
            //Globals.Network.Deletable = false;

            DistanceClipper = new MDistanceClipper();
            UtilityRoot.Add(DistanceClipper);
            FrustrumCuller = new MFrustrumCuller();
            UtilityRoot.Add(FrustrumCuller);

            Globals.Avatar = new MAvatar("Player1");
            UtilityRoot.Add(Globals.Avatar);

            Background = new MObject(MObject.EType.Null, "Background");
            Root.Add(Background);
            Background2 = new MObject(MObject.EType.Null, "Background2");
            Root.Add(Background2);

            ModelRoot = new MObject(MObject.EType.Null, "ModelRoot");
            Root.Add(ModelRoot);
            ModelRoot.Deletable = false;

            AstroRoot = new MObject(MObject.EType.Null, "AstroRoot");
            //ModelRoot.Add(AstroRoot);
            UtilityRoot.Add(AstroRoot);
            AstroRoot.Deletable = false;

            if (Settings.DrawPlanets == true)
            {
                MPlanetHandler mpi = new MPlanetHandler();
                UtilityRoot.Add(mpi);
                MGrass grass = new MGrass();
                Background2.Add(grass);
            }

            Priority1 = new MObject(MObject.EType.Null, "Priority1");
            ModelRoot.Add(Priority1);
            Priority2 = new MObject(MObject.EType.Null, "Priority2");
            ModelRoot.Add(Priority2);
            Overlay = new MObject(MObject.EType.Null, "Overlay");
            //ModelRoot.Add(Overlay); //manually drawn in render


            if (Settings.DrawBackdrop == true)
            {
                AddBackdrop();
            }

            Fog = new MFog();
            UtilityRoot.Add(Fog);

            DefaultMaterial           = new MMaterial(MMaterial.DEFAULT_MATERIAL);
            DefaultMaterial.Deletable = false;
            DefaultMaterial.AddShader(DefaultShader);
            //DefaultMaterial.SetDiffuseTexture(Globals.TexturePool.GetTexture("Textures\\default.jpg"));
            MaterialRoot.Add(DefaultMaterial);

            MTexture DefaultTexture = new MTexture(MTexture.DEFAULT_TEXTURE);

            DefaultTexture.LoadTextureData(MFileSystem.ProjectPath + "Assets\\Textures\\default.jpg");
            DefaultTexture.DoAssign = true;
            DefaultMaterial.SetDiffuseTexture(DefaultTexture);


            MMaterial GUIMat = new MMaterial("DefaultGUIMaterial");
            //GUIMat.Deletable = false;
            //GUIMat.AddShader(GUIShader);
            //GUIMat.SetDiffuseTexture(Globals.TexturePool.GetTexture("Textures\\unwrap_helper_1024.jpg"));
            //MaterialRoot.Add(GUIMat);

            MMaterial DepthMaterial = new MMaterial("Depth");

            DepthMaterial.Deletable = false;
            simpleDepthShader       = new MShader("simpleDepthShader");
            simpleDepthShader.Load("shadow_mapping_depth_v.glsl",
                                   "shadow_mapping_depth_f.glsl",
                                   "", "")
            ;
            DepthMaterial.shader = simpleDepthShader;
            DepthMaterial.Add(simpleDepthShader);
            MaterialRoot.Add(DepthMaterial);
            Helper.CheckGLError(this, "TestPoint 5");


            //UtilityRoot.Add(audioListener);
            //audioListener.Deletable = false;

            Camera                    = new MCamera("MainCam");
            Camera.OwnerID            = MObject.OWNER_SYSTEM;
            Camera.transform.Position = new Vector3d(9, 5, 9);
            UtilityRoot.Add(Camera);
            audioListener = new MAudioListener();
            Camera.Add(audioListener);
            // Camera.Deletable = false;

            light                    = new MLight("DirLight");
            light.OwnerID            = MObject.OWNER_SYSTEM;
            light.transform.Position = new Vector3d(-10, 20.0f, -10.0f);
            UtilityRoot.Add(light);
            //light.Deletable = false;

            ScreenPick = new MScreenPick();
            // ScreenPick.Setup();
            UtilityRoot.Add(ScreenPick);
            // ScreenPick.Deletable = false;

            MShader debugDepthQuad = new MShader("debugDepthQuad");

            debugDepthQuad.Load("debug_quad_v.glsl",
                                "debug_quad_f.glsl",
                                "Terrain\\eval.glsl", "Terrain\\control.glsl");
            debugDepthQuad.Bind();
            debugDepthQuad.SetInt("depthMap", 0);
            //debugDepthQuad.SetFloat("near_plane", light.NearPlane);
            //debugDepthQuad.SetFloat("far_plane", light.FarPlane);
            UtilityRoot.Add(debugDepthQuad);
            debugDepthQuad.Deletable = false;

            MMaterial debugmat = new MMaterial("DEBUG");

            debugmat.shader = debugDepthQuad;
            //debugmat.Deletable = false;
            debugQuad          = new DebugQuad("DEBUGQUAD");
            debugQuad.material = debugmat;
            UtilityRoot.Add(debugQuad);
            debugQuad.Deletable = false;

            GUIRoot = new MGUI();
            Root.Add(GUIRoot);
            Helper.CheckGLError(this, "TestPoint 6");
        }