Example #1
0
 public static CollisionShape CreateBoundingBoxShape(Object3dManager info)
 {
     info.UpdateBoundingBox();
     return new BoxShape(info.AABB.Maximum - info.AABB.Minimum);
 }
Example #2
0
        public Renderer(int initialWidth, int initialHeight, int samples)
        {
            GlareTexture = new Texture(Media.Get("glaretex.png"));

            Voxelizer = new Voxel3dTextureWriter(256, 256, 256, new Vector3(22, 22, 22), new Vector3(0, 8, 0));

            CubeMapSphere = new Object3dInfo(Object3dManager.LoadFromObjSingle(Media.Get("cubemapsphere.obj")).Vertices);

            var cubeMapTexDefault = new CubeMapTexture(Media.Get("posx.jpg"), Media.Get("posy.jpg"), Media.Get("posz.jpg"),
                                                       Media.Get("negx.jpg"), Media.Get("negy.jpg"), Media.Get("negz.jpg"));

            CubeMaps.Add(new CubeMapInfo()
            {
                Texture      = cubeMapTexDefault,
                FalloffScale = 99999999.0f,
                Position     = Vector3.Zero
            });

            Width  = initialWidth;
            Height = initialHeight;


            Samples = samples;

            CreateBuffers();

            HDRShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "HDR.fragment.glsl");
            HDRShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                HDRShader.SetGlobal("USE_MSAA", "");
            }

            VXGIShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "VXGI.fragment.glsl");
            VXGIShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                VXGIShader.SetGlobal("USE_MSAA", "");
            }

            AmbientOcclusionShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "AmbientOcclusion.fragment.glsl");
            AmbientOcclusionShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                AmbientOcclusionShader.SetGlobal("USE_MSAA", "");
            }

            FogShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Fog.fragment.glsl");
            FogShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                FogShader.SetGlobal("USE_MSAA", "");
            }

            DeferredShader = ShaderProgram.Compile("PostProcessPerspective.vertex.glsl", "Deferred.fragment.glsl");
            DeferredShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                DeferredShader.SetGlobal("USE_MSAA", "");
            }

            EnvLightShader = ShaderProgram.Compile("PostProcessPerspective.vertex.glsl", "EnvironmentLight.fragment.glsl");
            EnvLightShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                EnvLightShader.SetGlobal("USE_MSAA", "");
            }

            CombinerShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Combiner.fragment.glsl");
            CombinerShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                CombinerShader.SetGlobal("USE_MSAA", "");
            }

            CombinerSecondShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "CombinerSecond.fragment.glsl");
            CombinerSecondShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                CombinerSecondShader.SetGlobal("USE_MSAA", "");
            }

            MotionBlurShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "MotionBlur.fragment.glsl");
            MotionBlurShader.SetGlobal("MSAA_SAMPLES", samples.ToString());
            if (samples > 1)
            {
                MotionBlurShader.SetGlobal("USE_MSAA", "");
            }

            BloomShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Bloom.fragment.glsl");

            PostProcessingMesh          = new Object3dInfo(VertexInfo.FromFloatArray(postProcessingPlaneVertices));
            PostProcessingMesh.DrawMode = PrimitiveType.TriangleStrip;
        }
Example #3
0
 public void Append(Object3dManager info)
 {
     Vertices.AddRange(info.Vertices);
 }
Example #4
0
 public static CollisionShape CreateConvexCollisionShape(Object3dManager info)
 {
     return info.GetConvexHull();
 }
Example #5
0
 public static CollisionShape CreateBoundingBoxShape(Object3dManager info)
 {
     info.UpdateBoundingBox();
     return(new BoxShape(info.AABB.Maximum - info.AABB.Minimum));
 }
Example #6
0
        public static List <Mesh3d> LoadSceneFromObj(string objfile, string mtlfile, float scale = 1.0f)
        {
            string[]      lines  = File.ReadAllLines(Media.Get(objfile));
            var           objs   = ParseOBJString(lines);
            var           mtllib = LoadMaterialsFromMtl(Media.Get(mtlfile));
            List <Mesh3d> meshes = new List <Mesh3d>();
            Dictionary <string, GenericMaterial>          texCache   = new Dictionary <string, GenericMaterial>();
            Dictionary <Vector3, GenericMaterial>         colorCache = new Dictionary <Vector3, GenericMaterial>();
            Dictionary <GenericMaterial, MaterialInfo>    mInfos     = new Dictionary <GenericMaterial, MaterialInfo>();
            Dictionary <GenericMaterial, Object3dManager> linkCache  = new Dictionary <GenericMaterial, Object3dManager>();
            var colorPink = new GenericMaterial(Color.White);

            foreach (var obj in objs)
            {
                Console.WriteLine("Loading " + obj.Name);
                Console.WriteLine("GC MEMORY USED {0} MB", GC.GetTotalMemory(true) / 1024.0 / 1024);
                var             mat      = mtllib.ContainsKey(obj.MaterialName) ? mtllib[obj.MaterialName] : new MaterialInfo();
                GenericMaterial material = colorPink;
                if (mat != null && mat.TextureName.Length > 0)
                {
                    var m = new GenericMaterial();
                    m.SetDiffuseTexture(Path.GetFileName(mat.TextureName));
                    m.SpecularTexture = m.DiffuseTexture;
                    // m.NormalMapScale = 10;
                    material = m;

                    material.Name    = obj.MaterialName;
                    mInfos[material] = mat;
                    // texCache.Add(mat.TextureName + mat.AlphaMask, material);
                    // material = colorPink;
                }
                else if (mat != null)
                {
                    material         = new GenericMaterial(mat.DiffuseColor);
                    mInfos[material] = mat;
                }
                else
                {
                    material         = colorPink;
                    mInfos[material] = mat;
                }

                for (int i = 0; i < obj.VBO.Count; i++)
                {
                    obj.VBO[i].Position *= scale;
                }
                var o3di = new Object3dManager(obj.VBO);
                o3di.Name = obj.Name;
                // if(!linkCache.ContainsKey(material))
                linkCache.Add(material, o3di);
                // else
                //     linkCache[material].Add(o3di);
            }
            foreach (var kv in linkCache)
            {
                Object3dManager o3di = kv.Value;

                // var trans = o3di.GetAverageTranslationFromZero();
                // o3di.OriginToCenter();
                //o3di.CorrectFacesByNormals();
                // o3di.CorrectFacesByNormals();
                var oi = new Object3dInfo(o3di.Vertices);
                oi.Manager = o3di;
                // GC.Collect();
                Mesh3d mesh = Mesh3d.Create(oi, kv.Key);
                //kv.Key.SpecularComponent = 1.0f - mInfos[kv.Key].SpecularStrength + 0.01f;
                kv.Key.Roughness     = mInfos[kv.Key].SpecularStrength;
                kv.Key.DiffuseColor  = mInfos[kv.Key].DiffuseColor;
                kv.Key.SpecularColor = mInfos[kv.Key].SpecularColor;
                // kv.Key.ReflectionStrength = 1.0f - (mInfos[kv.Key].SpecularStrength);
                //kv.Key.DiffuseComponent = mInfos[kv.Key].DiffuseColor.GetBrightness() + 0.01f;
                var kva = kv.Key;
                if (!mInfos.ContainsKey(kva))
                {
                    kva = mInfos.Keys.First();
                }
                if (mInfos[kva].BumpMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetBumpTexture(mInfos[kv.Key].BumpMapName);
                }
                if (mInfos[kva].RoughnessMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetRoughnessTexture(mInfos[kv.Key].RoughnessMapName);
                }
                if (mInfos[kva].NormapMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetNormalsTexture(mInfos[kv.Key].NormapMapName);
                }
                if (mInfos[kva].TextureName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetDiffuseTexture(mInfos[kv.Key].TextureName);
                }
                // mesh.SpecularComponent = kv.Key.SpecularStrength;
                //  mesh.GetInstance(0).Translate(trans);
                // mesh.SetCollisionShape(o3di.GetConvexHull(mesh.Transformation.GetPosition(),
                // 1.0f, 1.0f));
                meshes.Add(mesh);
            }
            return(meshes);
        }
Example #7
0
 public static CollisionShape CreateConvexCollisionShape(Object3dManager info)
 {
     return(info.GetConvexHull());
 }
Example #8
0
        public List <Voxel> Voxelize(Object3dManager objinfo, int gridSize)
        {
            List <Voxel> voxels = new List <Voxel>();
            List <byte>  bytes  = new List <byte>();
            var          dv3    = new Vector3(gridSize, gridSize, gridSize);

            for (int x = 0; x < gridSize; x++)
            {
                for (int y = 0; y < gridSize; y++)
                {
                    for (int z = 0; z < gridSize; z++)
                    {
                        var voxel = new Voxel();
                        voxel.Minimum = (Vector3.Divide(new Vector3(x, y, z), (float)gridSize) - new Vector3(0.5f)) * 2.0f;
                        voxel.Maximum = (Vector3.Divide(new Vector3(x + 1, y + 1, z + 1), (float)gridSize) - new Vector3(0.5f)) * 2.0f;
                        voxels.Add(voxel);
                        bytes.AddRange(BitConverter.GetBytes((uint)0));
                        bytes.AddRange(BitConverter.GetBytes((int)0));
                        bytes.AddRange(BitConverter.GetBytes((int)0));
                        bytes.AddRange(BitConverter.GetBytes((int)0));
                    }
                }
            }
            BoxesSSBO.MapData(bytes.ToArray());
            // yay
            Vector3 transFromCenter = objinfo.GetAverageTranslationFromZero();

            objinfo.OriginToCenter();
            float divisor = objinfo.GetNormalizeDivisor();
            var   v3aabb  = objinfo.GetAxisAlignedBox();

            //objinfo.Normalize();
            objinfo.AxisDivide(v3aabb);
            DrawingFramebuffer.Use();
            GL.Disable(EnableCap.DepthTest);
            GL.Disable(EnableCap.CullFace);
            GL.ColorMask(true, false, false, false);
            VoxelizerShader.Use();
            //VoxelizerShader.SetUniform("NormalizationDivisor", 1.0f / divisor);
            // VoxelizerShader.SetUniform("CenterToZero", transFromCenter);
            BoxesSSBO.Use(9);
            var ob3d = new Object3dInfo(objinfo.Vertices);

            VoxelizerShader.SetUniform("Grid", gridSize);
            VoxelizerShader.SetUniform("Pass", 0);
            ob3d.Draw();/*
                         * VoxelizerShader.SetUniform("Pass", 1);
                         * ob3d.Draw();
                         * VoxelizerShader.SetUniform("Pass", 2);
                         * ob3d.Draw();*/
            GL.MemoryBarrier(MemoryBarrierFlags.ShaderStorageBarrierBit);
            var resultBytes = BoxesSSBO.Read(0, bytes.Count);
            int cursor      = 0;
            int i           = 0;
            var newVoxels   = new List <Voxel>();

            for (int x = 0; x < gridSize; x++)
            {
                for (int y = 0; y < gridSize; y++)
                {
                    for (int z = 0; z < gridSize; z++)
                    {
                        uint hit = BitConverter.ToUInt32(resultBytes, cursor);
                        int  nrx = BitConverter.ToInt32(resultBytes, cursor + 4);
                        int  nry = BitConverter.ToInt32(resultBytes, cursor + 8);
                        int  nrz = BitConverter.ToInt32(resultBytes, cursor + 12);
                        cursor += 4 * 4;
                        if (hit > 0)
                        {
                            float fnrx = (float)nrx / (float)hit;
                            float fnry = (float)nry / (float)hit;
                            float fnrz = (float)nrz / (float)hit;
                            if (hit > 65536)
                            {
                                hit = 65536;
                            }
                            float dens = (float)hit / (float)65536;
                            voxels[i].Maximum *= v3aabb;
                            voxels[i].Minimum *= v3aabb;
                            voxels[i].Maximum += transFromCenter;
                            voxels[i].Minimum += transFromCenter;
                            voxels[i].Density  = dens;
                            voxels[i].Normal   = new Vector3(fnrx / 128.0f, fnry / 128.0f, fnrz / 128.0f);
                            newVoxels.Add(voxels[i]);
                        }
                        i++;
                    }
                }
            }
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.CullFace);
            GL.ColorMask(true, true, true, true);
            return(newVoxels);
        }
Example #9
0
        public List<Voxel> Voxelize(Object3dManager objinfo, int gridSize)
        {
            List<Voxel> voxels = new List<Voxel>();
            List<byte> bytes = new List<byte>();
            var dv3 = new Vector3(gridSize, gridSize, gridSize);
            for(int x = 0; x < gridSize; x++)
            {
                for(int y = 0; y < gridSize; y++)
                {
                    for(int z = 0; z < gridSize; z++)
                    {
                        var voxel = new Voxel();
                        voxel.Minimum = (Vector3.Divide(new Vector3(x, y, z), (float)gridSize) - new Vector3(0.5f)) * 2.0f;
                        voxel.Maximum = (Vector3.Divide(new Vector3(x + 1, y + 1, z + 1), (float)gridSize) - new Vector3(0.5f)) * 2.0f;
                        voxels.Add(voxel);
                        bytes.AddRange(BitConverter.GetBytes((uint)0));
                    }
                }
            }
            BoxesSSBO.MapData(bytes.ToArray());
            // yay
            var v3aabb = objinfo.GetAxisAlignedBox();
            var copy = objinfo.CopyDeep();
            copy.OriginToCenter();
            copy.Normalize();
            Vector3 transFromCenter = objinfo.GetAverageTranslationFromZero();
            float divisor = objinfo.GetDivisorFromPoint(transFromCenter);
            DrawingFramebuffer.Use();
            GL.Disable(EnableCap.DepthTest);
            GL.ColorMask(true, false, false, false);
            VoxelizerShader.Use();
            //VoxelizerShader.SetUniform("NormalizationDivisor", 1.0f / divisor);
            // VoxelizerShader.SetUniform("CenterToZero", transFromCenter);
            VoxelizerShader.SetUniform("Grid", gridSize);
            BoxesSSBO.Use(4);
            new Object3dInfo(copy.Vertices).Draw();
            GL.MemoryBarrier(MemoryBarrierFlags.ShaderStorageBarrierBit);
            var resultBytes = BoxesSSBO.Read(0, bytes.Count);
            int cursor = 0;
            int i = 0;
            var newVoxels = new List<Voxel>();

            for(int x = 0; x < gridSize; x++)
            {
                for(int y = 0; y < gridSize; y++)
                {
                    for(int z = 0; z < gridSize; z++)
                    {
                        int hit = BitConverter.ToInt32(resultBytes, cursor);
                        cursor += 4;
                        if(hit > 0)
                        {
                            if(hit > 65536)
                                hit = 65536;
                            float dens = (float)hit / (float)65536;
                            voxels[i].Maximum *= v3aabb;
                            voxels[i].Minimum *= v3aabb;
                            voxels[i].Maximum += transFromCenter;
                            voxels[i].Minimum += transFromCenter;
                            voxels[i].Density = dens;
                            newVoxels.Add(voxels[i]);
                        }
                        i++;
                    }
                }
            }
            GL.Enable(EnableCap.DepthTest);
            GL.ColorMask(true, true, true, true);
            return newVoxels;
        }
Example #10
0
 public void Append(Object3dManager info)
 {
     Vertices.AddRange(info.Vertices);
 }
Example #11
0
        public static List<Mesh3d> LoadSceneFromObj(string objfile, string mtlfile, float scale = 1.0f)
        {
            string[] lines = File.ReadAllLines(Media.Get(objfile));
            var objs = ParseOBJString(lines);
            var mtllib = LoadMaterialsFromMtl(Media.Get(mtlfile));
            List<Mesh3d> meshes = new List<Mesh3d>();
            Dictionary<string, GenericMaterial> texCache = new Dictionary<string, GenericMaterial>();
            Dictionary<Color, GenericMaterial> colorCache = new Dictionary<Color, GenericMaterial>();
            Dictionary<GenericMaterial, MaterialInfo> mInfos = new Dictionary<GenericMaterial, MaterialInfo>();
            Dictionary<GenericMaterial, List<Object3dManager>> linkCache = new Dictionary<GenericMaterial, List<Object3dManager>>();
            var colorPink = new GenericMaterial(Color.Pink);
            mInfos = new Dictionary<GenericMaterial, MaterialInfo>();
            foreach(var obj in objs)
            {
                var mat = mtllib.ContainsKey(obj.MaterialName) ? mtllib[obj.MaterialName] : null;
                GenericMaterial material = null;
                if(mat != null && mat.TextureName.Length > 0)
                {
                    if(texCache.ContainsKey(mat.TextureName + mat.AlphaMask))
                    {
                        material = texCache[mat.TextureName + mat.AlphaMask];

                        material.Name = obj.MaterialName;
                        mInfos[material] = mat;
                    }
                    else
                    {
                        var m = new GenericMaterial();
                        m.SetDiffuseTexture(Path.GetFileName(mat.TextureName));
                        m.SpecularTexture = m.DiffuseTexture;
                        m.NormalMapScale = 10;
                        material = m;

                        material.Name = obj.MaterialName;
                        mInfos[material] = mat;
                        texCache.Add(mat.TextureName + mat.AlphaMask, material);
                        // material = colorPink;
                    }
                    //material = new GenericMaterial(Color.Pink);
                }
                else if(mat != null)
                {
                    if(colorCache.ContainsKey(mat.DiffuseColor))
                    {
                        material = colorCache[mat.DiffuseColor];
                        mInfos[material] = mat;
                    }
                    else
                    {
                        material = new GenericMaterial(mat.DiffuseColor);
                        mInfos[material] = mat;
                        //  colorCache.Add(mat.DiffuseColor, material);
                    }
                }
                else
                {
                    material = colorPink;
                    mInfos[material] = mat;
                }

                for(int i = 0; i < obj.VBO.Count; i ++)
                {
                    obj.VBO[i].Position *= scale;
                }
                var o3di = new Object3dManager(obj.VBO);
                o3di.Name = obj.Name;
                if(!linkCache.ContainsKey(material))
                    linkCache.Add(material, new List<Object3dManager> { o3di });
                else
                    linkCache[material].Add(o3di);
            }
            foreach(var kv in linkCache)
            {
                Object3dManager o3di = kv.Value[0];
                if(kv.Value.Count > 1)
                {
                    foreach(var e in kv.Value.Skip(1))
                        o3di.Append(e);
                }
               // var trans = o3di.GetAverageTranslationFromZero();
               // o3di.OriginToCenter();
                //o3di.CorrectFacesByNormals();
                // o3di.CorrectFacesByNormals();
                var oi = new Object3dInfo(o3di.Vertices);
                oi.Manager = o3di;
                Mesh3d mesh = Mesh3d.Create(oi, kv.Key);
                //kv.Key.SpecularComponent = 1.0f - mInfos[kv.Key].SpecularStrength + 0.01f;
                kv.Key.Roughness = (1);
                // kv.Key.ReflectionStrength = 1.0f - (mInfos[kv.Key].SpecularStrength);
                //kv.Key.DiffuseComponent = mInfos[kv.Key].DiffuseColor.GetBrightness() + 0.01f;
                var kva = kv.Key;
                if(!mInfos.ContainsKey(kva))
                    kva = mInfos.Keys.First();
                if(mInfos[kva].BumpMapName.Length > 1)
                    ((GenericMaterial)kv.Key).SetBumpTexture(mInfos[kv.Key].BumpMapName);
                // mesh.SpecularComponent = kv.Key.SpecularStrength;
              //  mesh.GetInstance(0).Translate(trans);
                // mesh.SetCollisionShape(o3di.GetConvexHull(mesh.Transformation.GetPosition(),
                // 1.0f, 1.0f));
                meshes.Add(mesh);
            }
            return meshes;
        }