示例#1
0
        GameObject GetPBRModel(string meshFileName, string albedoMapFile, string normalMapFile, string metallicMapFile, string roughnessMapFile)
        {
            DefaultMesh mesh = contentLoader.Load <DefaultMesh>(meshFileName);
            VAO         geom = VAOLoader.FromMesh(mesh, renderer.GetPBRShader());
            GameObject  go   = new GameObject();
            PBRMaterial mat  = new PBRMaterial();

            go.mesh     = geom;
            go.material = mat;
            //mat.metal = 1.0f;
            //mat.metal = 0f;
            mat.roughness = 0;
            if (albedoMapFile != null)
            {
                ITexture2D albedoMap = contentLoader.Load <ITexture2D>(albedoMapFile);
                mat.albedoMap = albedoMap;
            }
            if (normalMapFile != null && normalMapFile != "")
            {
                ITexture2D normalMap = contentLoader.Load <ITexture2D>(normalMapFile);
                mat.normalMap = normalMap;
            }
            if (metallicMapFile != null)
            {
                ITexture2D metallicMap = contentLoader.Load <ITexture2D>(metallicMapFile);
                mat.metallicMap = metallicMap;
            }
            if (roughnessMapFile != null)
            {
                ITexture2D roughnessMap = contentLoader.Load <ITexture2D>(roughnessMapFile);
                mat.roughnessMap = roughnessMap;
            }
            return(go);
        }
示例#2
0
        public MainVisual(IRenderState renderState, IContentLoader contentLoader)
        {
            renderState.Set(BoolState <IDepthState> .Enabled);
            renderState.Set(BoolState <IBackfaceCullingState> .Enabled);
            renderState.Set(BlendStates.AlphaBlend);

            shaderProgram = contentLoader.Load <IShaderProgram>("phong.*");
            var mesh     = new DefaultMesh();
            var roomSize = 8;
            //off-center plane
            var plane = Meshes.CreatePlane(roomSize, roomSize, 2, 2).Transform(new Translation3D(0, -roomSize / 2, 0));

            mesh.Add(plane);
            //rotate plane to create box
            mesh.Add(plane.Transform(new Rotation3D(Axis.Z, 90f)));
            mesh.Add(plane.Transform(new Rotation3D(Axis.Z, 180f)));
            mesh.Add(plane.Transform(new Rotation3D(Axis.Z, 270f)));
            mesh.Add(plane.Transform(new Rotation3D(Axis.X, 90f)));
            mesh.Add(plane.Transform(new Rotation3D(Axis.X, -90f)));

            var sphere = Meshes.CreateSphere(1);

            sphere.SetConstantUV(new Vector2(0, 0));
            mesh.Add(sphere);
            var suzanne = contentLoader.Load <DefaultMesh>("suzanne");

            mesh.Add(suzanne.Transform(new Translation3D(2, 2, -2)));
            geometry = VAOLoader.FromMesh(mesh, shaderProgram);
        }
        public void GetCubeIBLMapsFromSphereMap(ITexture2D sphereTexture, ref uint cubeMap, ref uint irradianceMap, ref uint prefilterMap, ref uint integrationMap)
        {
            GL.CullFace(CullFaceMode.Front);
            DefaultMesh cubeMesh = Meshes.CreateCubeWithNormals();

            //Set up Projection Matrix and View Matrix for rendering into Cubemap
            Matrix4x4 captureProjection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), 1.0f, 0.1f, 10.0f);

            Matrix4x4[] captureView =
            {
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(1.0f,   0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(-1.0f,  0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   1.0f,  0.0f), new Vector3(0.0f,  0.0f,  1.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,  -1.0f,  0.0f), new Vector3(0.0f,  0.0f, -1.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   0.0f,  1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   0.0f, -1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
            };
            int captureFBO = GL.GenFramebuffer();

            //Basic Skybox/IBL CubeMap
            cubeMap = CreateEnvironmentMap(captureFBO, captureProjection, captureView, sphereTexture);
            //Irradiance map, which is presampled map of irradiance of IBL Cubemap
            irradianceMap = CreateIrradianceMap(captureFBO, captureProjection, captureView, cubeMap);

            prefilterMap   = CreatePrefilteredMap(captureFBO, captureProjection, captureView, cubeMap);
            integrationMap = CreateIntegratedMap(captureFBO);

            /*
             *
             * Create BDRF Integration Map
             *
             */

            GL.CullFace(CullFaceMode.Back);
        }
        uint CreateIrradianceMap(int frameBuffer, Matrix4x4 projection, Matrix4x4[] viewMatrices, uint cubeMap)
        {
            DefaultMesh cubeMesh           = Meshes.CreateCubeWithNormals();
            VAO         renderIrradMapCube = VAOLoader.FromMesh(cubeMesh, irradianceMapShader);
            //Create Irradiance Texture
            int fbIrrWidth  = 32;
            int fbIrrHeight = 32;
            int irradMap    = CreateCubeMap(32, 32);

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            //GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMap, irradMap, 0);
            //Render
            //Irradiance Map
            irradianceMapShader.Activate();
            SetSampler(irradianceMapShader.ProgramID, 0, "environmentMap", cubeMap, TextureTarget.TextureCubeMap);
            irradianceMapShader.Uniform("projection", projection, true);

            GL.Viewport(0, 0, fbIrrWidth, fbIrrHeight);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            for (int i = 0; i < 6; i++)
            {
                irradianceMapShader.Uniform("view", viewMatrices[i], true);
                GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                        TextureTarget.TextureCubeMapPositiveX + i, irradMap, 0);

                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                renderIrradMapCube.Draw();
            }

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            return((uint)irradMap);
        }
示例#5
0
        public MainVisual(IRenderState renderState, IContentLoader contentLoader)
        {
            renderState.Set(new DepthTest(true));
            renderState.Set(new FaceCullingModeState(FaceCullingMode.BACK_SIDE));

            shaderProgramPhong = contentLoader.Load <IShaderProgram>("phong.*");
            var mesh     = new DefaultMesh();
            var roomSize = 8;
            //off-center plane
            var plane = Meshes.CreatePlane(roomSize, roomSize, 2, 2).Transform(Transformation.Translation(0, -roomSize / 2, 0));

            mesh.Add(plane);
            //rotate plane to create box
            mesh.Add(plane.Transform(Transformation.Rotation(90f, Axis.Z)));
            mesh.Add(plane.Transform(Transformation.Rotation(180f, Axis.Z)));
            mesh.Add(plane.Transform(Transformation.Rotation(270f, Axis.Z)));
            mesh.Add(plane.Transform(Transformation.Rotation(90f, Axis.X)));
            mesh.Add(plane.Transform(Transformation.Rotation(-90f, Axis.X)));

            var sphere = Meshes.CreateSphere(1);

            sphere.SetConstantUV(Vector2.Zero);             //all other meshes have texture coordinates
            mesh.Add(sphere);
            var suzanne = contentLoader.Load <DefaultMesh>("suzanne");

            mesh.Add(suzanne.Transform(Transformation.Translation(2, 2, -2)));
            geometryPhong = VAOLoader.FromMesh(mesh, shaderProgramPhong);

            shaderProgramToon = contentLoader.Load <IShaderProgram>("toon.*");
            geometryToon      = VAOLoader.FromMesh(suzanne.Transform(Transformation.Translation(2, 0, 0)), shaderProgramToon);
        }
        public TextureRenderer2D(Vector2 position, Vector2 size, Texture texture, Shader shader)
        {
            _texture = texture;
            _shader  = shader;

            DefaultMesh mesh = new DefaultMesh();

            mesh.Position.Add(new Vector3(position.X, position.Y, 0));                   //0
            mesh.Position.Add(new Vector3(position.X + size.X, position.Y, 0));          //1
            mesh.Position.Add(new Vector3(position.X + size.X, position.Y + size.Y, 0)); //2
            mesh.Position.Add(new Vector3(position.X, position.Y + size.Y, 0));          //3

            mesh.TexCoord.Add(new Vector2(0.0f, 0.0f));
            mesh.TexCoord.Add(new Vector2(1.0f, 0.0f));
            mesh.TexCoord.Add(new Vector2(1.0f, 1.0f));
            mesh.TexCoord.Add(new Vector2(0.0f, 1.0f));

            mesh.IDs.Add(0);
            mesh.IDs.Add(2);
            mesh.IDs.Add(1);
            mesh.IDs.Add(0);
            mesh.IDs.Add(3);
            mesh.IDs.Add(2);

            _geometry = VAOLoader.FromMesh(mesh, shader);
        }
示例#7
0
        public static DefaultMesh CreateTriangle()
        {
            var mesh = new DefaultMesh();

            mesh.Position.Add(new Vector3(0.0f, 1.0f, 0.0f));
            mesh.IDs.Add(0);
            mesh.Normal.Add(-Vector3.UnitZ);
            mesh.Position.Add(Vector3.Normalize(new Vector3(1.0f, -1.0f, 0.0f)));
            mesh.IDs.Add(1);
            mesh.Normal.Add(-Vector3.UnitZ);
            mesh.Position.Add(Vector3.Normalize(new Vector3(-1.0f, -1.0f, 0.0f)));
            mesh.IDs.Add(2);
            mesh.Normal.Add(-Vector3.UnitZ);

            mesh.Position.Add(new Vector3(0.0f, 1.0f, 0.0f));
            mesh.IDs.Add(3);
            mesh.Normal.Add(Vector3.UnitZ);
            mesh.Position.Add(Vector3.Normalize(new Vector3(-1.0f, -1.0f, 0.0f)));
            mesh.IDs.Add(4);
            mesh.Normal.Add(Vector3.UnitZ);
            mesh.Position.Add(Vector3.Normalize(new Vector3(1.0f, -1.0f, 0.0f)));
            mesh.IDs.Add(5);
            mesh.Normal.Add(Vector3.UnitZ);

            return(mesh);
        }
        public ProjectileGeneration(IContentLoader contentLoader, DefaultMesh triangleMesh, Enums.EntityType triangleType)
        {
            _projectileGenerationProgram = contentLoader.Load <IShaderProgram>(new[] { "ProjectileGeneration.vert", "deferred.frag" });
            _trianglesGeometry           = VAOLoader.FromMesh(triangleMesh, _projectileGenerationProgram);
            _triangleType = triangleType;

            _add = new AddWithDepthTest(contentLoader);
        }
示例#9
0
 public VoxelMesh()
 {
     DefaultMesh    = new DefaultMesh();
     TexCoord3D     = new List <Vector3>();
     MaterialAmount = new List <float>();
     MaterialId     = new List <int>();
     VoxelId        = new List <uint>();
 }
示例#10
0
 private VoxelMesh(VoxelMesh voxelMesh, DefaultMesh defaultMesh)
 {
     DefaultMesh    = defaultMesh;
     TexCoord3D     = voxelMesh.TexCoord3D;
     MaterialAmount = voxelMesh.MaterialAmount;
     MaterialId     = voxelMesh.MaterialId;
     VoxelId        = voxelMesh.VoxelId;
 }
示例#11
0
        private void UpdateRaytraceMesh()
        {
            DefaultMesh mesh = new DefaultMesh();

            mesh.Add(Meshes.CreateCubeWithNormals(_voxelSize).Transform(Matrix4x4.CreateTranslation(_raytraceVoxelPosition)));

            _raytraceGeometry = VAOLoader.FromMesh(mesh, _raytraceShader);
        }
        public PBRRenderer(IRenderState renderState, IContentLoader contentLoader)
        {
            iblMapList = new List <IBLSetup>();
            GL.Enable(EnableCap.DebugOutput);
            GL.Enable(EnableCap.TextureCubeMapSeamless);
            this.renderState = renderState;
            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.TextureCubeMap);
            renderState.Set(new DepthTest(true));
            //pbrShader = contentLoader.Load<IShaderProgram>("PBRLightingBasic.*");
            pbrShader = contentLoader.Load <IShaderProgram>("PBRLighting.*");
            //pbrShader = contentLoader.Load<IShaderProgram>("PBRReference.*");
            skyboxShader         = contentLoader.Load <IShaderProgram>("Skybox.*");
            cubeProjectionShader = contentLoader.Load <IShaderProgram>("CubeMapProjection.*");
            textureTest          = contentLoader.Load <IShaderProgram>("DisplayTexture2D.*");
            irradianceMapShader  = contentLoader.Load <IShaderProgram>("IrradianceMap.*");
            prefilterMapShader   = contentLoader.Load <IShaderProgram>("PrefilterIBLMap.*");
            integrationMapShader = contentLoader.Load <IShaderProgram>("BRDFIntegration.*");
            dLight           = new DirectionalLight();
            dLight.direction = new Vector3(0, 1, -1);
            dLight.color     = new Vector3(10);
            dLight.position  = new Vector3(0, 1, -1);

            Vector3 startPos = new Vector3(0, 0, 0.5f);

            pointLights = new PointLight[4];
            for (int i = 0; i < 4; i++)
            {
                pointLights[i]          = new PointLight();
                pointLights[i].color    = new Vector3(1);
                pointLights[i].radius   = 1;
                pointLights[i].position = startPos;
            }
            pointLights[0].position = new Vector3(-1, 1, 0.5f);
            pointLights[1].position = new Vector3(-1, -1, 0.5f);
            pointLights[2].position = new Vector3(1, -1, 0.5f);
            pointLights[3].position = new Vector3(1, 1, 0.5f);


            int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(PointLight)) * pointLights.Length;

            //Generate buffer and allocate memory
            ubo = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.UniformBuffer, ubo);
            GL.BufferData(BufferTarget.UniformBuffer, size, pointLights, BufferUsageHint.DynamicDraw);

            //Assign Buffer Block to ubo
            int uniformID = GL.GetUniformBlockIndex(pbrShader.ProgramID, "BufferPointLights");

            GL.UniformBlockBinding(pbrShader.ProgramID, uniformID, 0);
            GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, ubo);

            DefaultMesh cubeMesh = Meshes.CreateCubeWithNormals();

            unitCube = VAOLoader.FromMesh(cubeMesh, cubeProjectionShader);
            renderState.Set(new FaceCullingModeState(FaceCullingMode.BACK_SIDE));
        }
示例#13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultMesh"/> class.
        /// </summary>

        public TBNMesh(DefaultMesh mesh)
        {
            Position.AddRange(mesh.Position);
            Normal.AddRange(mesh.Normal);
            TexCoord.AddRange(mesh.TexCoord);
            IDs.AddRange(mesh.IDs);

            Tangent   = AddAttribute <Vector3>(TangentName);
            Bitangent = AddAttribute <Vector3>(BitangentName);
            CalcTangentsAndBitangents();
        }
示例#14
0
        public static Mesh CreateteTrahedron()
        {
            DefaultMesh tetrahedron = new DefaultMesh();

            //bottom
            tetrahedron.Pos.Add(new Vector3(0.5f, -0.289f, 0.289f));
            tetrahedron.Normal.Add(new Vector3(0f, -1f, 0f));
            tetrahedron.Pos.Add(new Vector3(-0.5f, -0.289f, 0.289f));
            tetrahedron.Normal.Add(new Vector3(0f, -1f, 0f));
            tetrahedron.Pos.Add(new Vector3(0f, -0.289f, -0.577f));
            tetrahedron.Normal.Add(new Vector3(0f, -1f, 0f));
            tetrahedron.IDs.Add(0);
            tetrahedron.IDs.Add(1);
            tetrahedron.IDs.Add(2);

            //front
            tetrahedron.Pos.Add(new Vector3(-0.5f, -0.289f, 0.289f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(0f, 0.289f, 0.816f)));
            tetrahedron.Pos.Add(new Vector3(0.5f, -0.289f, 0.289f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(0f, 0.289f, 0.816f)));
            tetrahedron.Pos.Add(new Vector3(0f, 0.816f - 0.289f, 0f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(0f, 0.289f, 0.816f)));
            tetrahedron.IDs.Add(3);
            tetrahedron.IDs.Add(4);
            tetrahedron.IDs.Add(5);

            //right
            tetrahedron.Pos.Add(new Vector3(0f, 0f - 0.289f, -0.577f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(0.707f, 0.289f, -0.408f)));
            tetrahedron.Pos.Add(new Vector3(0.5f, 0f - 0.289f, 0.289f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(0.707f, 0.289f, -0.408f)));
            tetrahedron.Pos.Add(new Vector3(0f, 0.816f - 0.289f, 0f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(0.707f, 0.289f, -0.408f)));
            tetrahedron.IDs.Add(6);
            tetrahedron.IDs.Add(7);
            tetrahedron.IDs.Add(8);

            //left
            tetrahedron.Pos.Add(new Vector3(-0.5f, -0.289f, 0.289f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(-0.707f, 0.289f, -0.408f)));
            tetrahedron.Pos.Add(new Vector3(0f, -0.289f, -0.577f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(-0.707f, 0.289f, -0.408f)));
            tetrahedron.Pos.Add(new Vector3(0f, 0.816f - 0.289f, 0f));
            tetrahedron.Normal.Add(System.Numerics.Vector3.Normalize(new Vector3(-0.707f, 0.289f, -0.408f)));
            tetrahedron.IDs.Add(9);
            tetrahedron.IDs.Add(10);
            tetrahedron.IDs.Add(11);

            return(tetrahedron);
        }
示例#15
0
        public VAO GetDrawable(DefaultMesh mesh, DrawableType type)
        {
            MeshAttribute uvs = mesh.GetAttribute("uv");

            int elems = uvs.ToArray().Length;

            if (elems == 0)
            {
                mesh.SetConstantUV(new Vector2(0, 0));
            }
            IShaderProgram shader = GetShader(type);
            VAO            res    = VAOLoader.FromMesh(mesh, shader);

            return(res);
        }
        public FileDrawable(string fileName, RenderDevice renderer)
        {
            byte[]      bytes = File.ReadAllBytes(fileName);
            DefaultMesh mesh  = Obj2Mesh.FromObj(bytes);

            indices = renderer.CopyToVideoRAM(mesh.IDs.ToArray());
            List <Vector3> positions = mesh.Position;
            List <Vector3> normals   = mesh.Normal;

            attributePosition = renderer.CopyToVideoRAM(positions.ToArray());

            var normalsAsColors = normals.Select(n => new Vector4(n, 1));

            attributeColor = renderer.CopyToVideoRAM(normalsAsColors.ToArray());
        }
        public static Renderable GetDefaultRenderable(DeferredRenderer renderer, DefaultMesh mesh)
        {
            IShaderProgram geometryShader  = renderer.GetShader(DeferredRenderer.DrawableType.deferredDefaultMesh);
            IShaderProgram lightViewShader = renderer.GetShader(DeferredRenderer.DrawableType.lightViewMesh);
            IShaderProgram shadowMapShader = renderer.GetShader(DeferredRenderer.DrawableType.shadowMapMesh);
            Renderable     renderable      = new Renderable();
            VAO            geometry        = renderer.GetDrawable(mesh, DeferredRenderer.DrawableType.deferredDefaultMesh);
            VAO            lightViewMesh   = renderer.GetDrawable(mesh, DeferredRenderer.DrawableType.lightViewMesh);
            VAO            shadowMapMesh   = renderer.GetDrawable(mesh, DeferredRenderer.DrawableType.shadowMapMesh);

            renderable.SetDeferredGeometryMesh(geometry, geometryShader);
            renderable.SetLightViewMesh(geometry, lightViewShader);
            renderable.SetShadowMapMesh(geometry, shadowMapShader);
            return(renderable);
        }
示例#18
0
        public static Mesh CreateTriangle()
        {
            DefaultMesh triangle = new DefaultMesh();

            triangle.Pos.Add(new Vector3(0f, 0f, 0f));
            triangle.Normal.Add(new Vector3(0f, 0f, 1f));
            triangle.Pos.Add(new Vector3(1f, 1f, 0f));
            triangle.Normal.Add(new Vector3(0f, 0f, 1f));
            triangle.Pos.Add(new Vector3(0f, 1f, 0f));
            triangle.Normal.Add(new Vector3(0f, 0f, 1f));
            triangle.IDs.Add(0);
            triangle.IDs.Add(1);
            triangle.IDs.Add(2);

            return(triangle);
        }
示例#19
0
        public void Add(VoxelMesh voxelMesh)
        {
            DefaultMesh.Add(voxelMesh.DefaultMesh);
            TexCoord3D.AddRange(voxelMesh.TexCoord3D);
            MaterialAmount.AddRange(voxelMesh.MaterialAmount);
            MaterialId.AddRange(voxelMesh.MaterialId);
            uint max = 0;

            if (VoxelId.Count > 0)
            {
                max = (uint)(VoxelId.Last() + 1);
            }
            foreach (var voxelId in voxelMesh.VoxelId)
            {
                VoxelId.Add(voxelId + max);
            }
        }
        uint CreatePrefilteredMap(int frameBuffer, Matrix4x4 projection, Matrix4x4[] viewMatrices, uint cubeMap)
        {
            DefaultMesh cubeMesh            = Meshes.CreateCubeWithNormals();
            VAO         renderPrefilterCube = VAOLoader.FromMesh(cubeMesh, prefilterMapShader);
            int         texResX             = 128;
            int         texResY             = 128;

            int prefiltMap = CreateCubeMap(texResX, texResY);

            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap);


            prefilterMapShader.Activate();

            SetSampler(prefilterMapShader.ProgramID, 0, "environmentMap", cubeMap, TextureTarget.TextureCubeMap);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);


            prefilterMapShader.Uniform("projection", projection, true);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            int maxMipLvl = 5;

            for (int i = 0; i < maxMipLvl; i++)
            {
                float mipWidth  = texResX * (float)Math.Pow(0.5, i);
                float mipHeight = texResY * (float)Math.Pow(0.5, i);
                GL.Viewport(0, 0, (int)mipWidth, (int)mipHeight);

                float roughness = (float)i / (float)(maxMipLvl - 1.0);
                prefilterMapShader.Uniform("roughness", roughness);
                for (int j = 0; j < 6; j++)
                {
                    prefilterMapShader.Uniform("view", viewMatrices[j], true);
                    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                            TextureTarget.TextureCubeMapPositiveX + j, prefiltMap, i);

                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    renderPrefilterCube.Draw();
                }
            }
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            return((uint)prefiltMap);
        }
示例#21
0
        private void GenerateVoronoi(int sizeX, int sizeY, Vector3 scale)
        {
            Random rand = new Random(345546);

            float RandFloat() => (float)rand.NextDouble();

            float[,] heights   = new float[sizeX + 2, sizeY + 2];
            Vector2[,] centers = new Vector2[sizeX + 2, sizeY + 2];

            for (int x = 0; x < sizeX + 2; x++)
            {
                for (int y = 0; y < sizeY + 2; y++)
                {
                    heights[x, y] = RandFloat() * scale.Y;
                    centers[x, y] = new Vector2((RandFloat() + x - ((sizeX + 2)) / 2) * scale.X, (RandFloat() + y - ((sizeY + 2) / 2)) * scale.Z);
                }
            }

            for (int x = 1; x < sizeX + 1; x++)
            {
                for (int y = 1; y < sizeY + 1; y++)
                {
                    List <Vector2> neighbors = new List <Vector2>()
                    {
                        centers[x - 1, y - 1],
                        centers[x - 1, y],
                        centers[x - 1, y + 1],
                        centers[x, y - 1],
                        centers[x, y + 1],
                        centers[x + 1, y - 1],
                        centers[x + 1, y],
                        centers[x + 1, y + 1]
                    };

                    GenerateVoronoiTower(centers[x, y], neighbors, heights[x, y]);
                }
            }

            DefaultMesh plane = Meshes.CreatePlane(sizeX * scale.X, sizeY * scale.Z, 1, 1);

            plane.TexCoord.Clear();
            Mesh.Add(plane);
        }
        uint CreateEnvironmentMap(int frameBuffer, Matrix4x4 projection, Matrix4x4[] viewMatrices, ITexture2D sphereTexture)
        {
            int         fbCubeWidht       = 512;
            int         fbCubeHeight      = 512;
            DefaultMesh cubeMesh          = Meshes.CreateCubeWithNormals();
            VAO         renderCubeMapCube = VAOLoader.FromMesh(cubeMesh, cubeProjectionShader);

            //Cubemap
            int envCubeMap = CreateCubeMap(fbCubeWidht, fbCubeHeight);

            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            //FBO

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            //GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMap, envCubeMap, 0);

            //Render into Cubemap
            //Setting up shader
            cubeProjectionShader.Activate();
            cubeProjectionShader.Uniform("projection", projection, true);

            SetSampler(cubeProjectionShader.ProgramID, 0, "equirectangularMap", sphereTexture);
            GL.Viewport(0, 0, fbCubeWidht, fbCubeHeight);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            //GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
            for (int i = 0; i < 6; i++)
            {
                cubeProjectionShader.Uniform("view", viewMatrices[i], true);

                GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                        TextureTarget.TextureCubeMapPositiveX + i, envCubeMap, 0);

                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                renderCubeMapCube.Draw();
            }
            GL.BindTexture(TextureTarget.TextureCubeMap, envCubeMap);
            GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap);

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            DeactivateTexture(0);
            return((uint)envCubeMap);
        }
        public void ShowCubeMap(ITexture2D sphereTexture, int side)
        {
            GL.CullFace(CullFaceMode.Front);
            DefaultMesh cubeMesh   = Meshes.CreateCubeWithNormals();
            VAO         renderCube = VAOLoader.FromMesh(cubeMesh, cubeProjectionShader);

            //Set up Projection Matrix and View Matrix for rendering into Cubemap
            Matrix4x4 captureProjection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), 1.0f, 0.1f, 10.0f);

            Matrix4x4[] captureView =
            {
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(1.0f,   0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(-1.0f,  0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   1.0f,  0.0f), new Vector3(0.0f,  0.0f,  1.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,  -1.0f,  0.0f), new Vector3(0.0f,  0.0f, -1.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   0.0f,  1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   0.0f, -1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
            };

            //Setting up shader
            cubeProjectionShader.Activate();
            SetSampler(cubeProjectionShader.ProgramID, 0, "equirectangularMap", sphereTexture);
            cubeProjectionShader.Uniform("projection", captureProjection, true);

            GL.ActiveTexture(TextureUnit.Texture0);
            sphereTexture.Activate();

            //Render into Cubemap
            GL.Viewport(0, 0, 512, 512);
            cubeProjectionShader.Uniform("view", captureView[side], true);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            renderCube.Draw();

            sphereTexture.Deactivate();
            DeactivateTexture(0);
            GL.CullFace(CullFaceMode.Back);
        }
示例#24
0
        /// <summary>
        /// Creates a VertexArrayObject from a mesh expecting the MeshAttribute names as shader variable names for the attributes
        /// </summary>
        /// <param name="mesh">From which to load positions, indices, normals, texture coordinates</param>
        /// <param name="shaderProgram">Used for the attribute location bindings</param>
        /// <returns>A vertex array object</returns>
        public static VAO FromMesh(DefaultMesh mesh, IShaderProgram shaderProgram)
        {
            var vao = new VAO(PrimitiveType.Triangles);

            if (mesh.Position.Count > 0)
            {
                var loc = shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, DefaultMesh.PositionName);
                vao.SetAttribute(loc, mesh.Position.ToArray(), VertexAttribPointerType.Float, 3);
            }
            if (mesh.Normal.Count > 0)
            {
                var loc = shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, DefaultMesh.NormalName);
                vao.SetAttribute(loc, mesh.Normal.ToArray(), VertexAttribPointerType.Float, 3);
            }
            if (mesh.TexCoord.Count > 0)
            {
                var loc = shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, DefaultMesh.TexCoordName);
                vao.SetAttribute(loc, mesh.TexCoord.ToArray(), VertexAttribPointerType.Float, 2);
            }
            vao.SetIndex(mesh.IDs.ToArray());
            return(vao);
        }
示例#25
0
        private void CreateWorldGround()
        {
            DefaultMesh mesh = new DefaultMesh();

            Vector3I negativeWorldSize = ((-_worldSize / 2) * Constant.ChunkSize);

            negativeWorldSize.Y = 0;
            Vector3I positiveWorldSize = ((_worldSize / 2) * Constant.ChunkSize);

            positiveWorldSize.Y = _worldSize.Y * Constant.ChunkSizeY;

            mesh.Position.Add(new Vector3(positiveWorldSize.X - 0.5f, -0.5f, negativeWorldSize.Z - 0.5f)); //0
            mesh.Position.Add(new Vector3(positiveWorldSize.X - 0.5f, -0.5f, positiveWorldSize.Z - 0.5f)); //1
            mesh.Position.Add(new Vector3(negativeWorldSize.X - 0.5f, -0.5f, positiveWorldSize.Z - 0.5f)); //2
            mesh.Position.Add(new Vector3(negativeWorldSize.X - 0.5f, -0.5f, negativeWorldSize.Z - 0.5f)); //3

            mesh.TexCoord.Add(new Vector2(0, 0));
            mesh.TexCoord.Add(new Vector2(_worldSize.X * Constant.ChunkSizeX, 0));
            mesh.TexCoord.Add(new Vector2(_worldSize.X * Constant.ChunkSizeX, _worldSize.Z * Constant.ChunkSizeZ));
            mesh.TexCoord.Add(new Vector2(0, _worldSize.Z * Constant.ChunkSizeZ));

            mesh.Normal.Add(-Vector3.UnitY);
            mesh.Normal.Add(-Vector3.UnitY);
            mesh.Normal.Add(-Vector3.UnitY);
            mesh.Normal.Add(-Vector3.UnitY);

            mesh.IDs.Add(0);
            mesh.IDs.Add(2);
            mesh.IDs.Add(1);
            mesh.IDs.Add(0);
            mesh.IDs.Add(3);
            mesh.IDs.Add(2);

            mesh = mesh.Transform(Matrix4x4.CreateScale(_voxelSize));

            _groundGeometry      = VAOLoader.FromMesh(mesh, _geometryShader);
            _groundDepthGeometry = VAOLoader.FromMesh(mesh, _depthShader);
        }
示例#26
0
        List <GameObject> GetSphereSampleScene()
        {
            List <GameObject> goList = new List <GameObject>();
            DefaultMesh       plane  = Meshes.CreatePlane(10, 10, 10, 10).Transform(Transformation.Translation(new Vector3(0, -1f, 0)));
            VAO         planeMesh    = VAOLoader.FromMesh(plane, renderer.GetPBRShader());
            GameObject  planeGO      = new GameObject();
            PBRMaterial planemat     = new PBRMaterial();

            planemat.albedoColor = new Vector3(1);
            planemat.roughness   = 1;
            planemat.metal       = 0;
            planeGO.mesh         = planeMesh;
            planeGO.material     = planemat;
            //goList.Add(planeGO);
            //return goList;
            int        gridSize      = 7;
            float      sphereSize    = 0.1f;
            float      spacing       = sphereSize + sphereSize * 2f;
            float      startX        = gridSize / 2 * spacing;
            Vector3    startVector   = new Vector3(startX, startX, 0);
            float      paramSteps    = 1.0f / gridSize;
            string     texPrefix     = "rustediron2_";
            ITexture2D albedoText    = contentLoader.Load <ITexture2D>(texPrefix + "basecolor.png");
            ITexture2D metallicText  = contentLoader.Load <ITexture2D>(texPrefix + "metallic.png");
            ITexture2D normalText    = contentLoader.Load <ITexture2D>(texPrefix + "normal.png");
            ITexture2D roughnessText = contentLoader.Load <ITexture2D>(texPrefix + "roughness.png");

            DefaultMesh mesh = contentLoader.Load <DefaultMesh>("uvSphere").Transform(Transformation.Scale(0.1f));
            //DefaultMesh mesh = Meshes.CreateSphere(sphereSize, 2);
            VAO geom = VAOLoader.FromMesh(mesh, renderer.GetPBRShader());


            TangentSpaceData tangentData = GetTangentSpaceData(mesh.Position, mesh.TexCoord);
            int tangentLocation          = renderer.GetPBRShader().GetResourceLocation(ShaderResourceType.Attribute, "tangent");
            int biTangentLocation        = GL.GetAttribLocation(renderer.GetPBRShader().ProgramID, "biTangent");

            geom.SetAttribute(tangentLocation, tangentData.tangent);
            geom.SetAttribute(biTangentLocation, tangentData.biTangent);


            for (int i = 0; i < gridSize; i++)
            {
                Vector3 tmpStart = startVector;
                for (int j = 0; j < gridSize; j++)
                {
                    GameObject go = new GameObject();
                    go.transform.position   = tmpStart;
                    go.material.metal       = i * paramSteps;
                    go.material.roughness   = j * paramSteps;
                    go.material.albedoColor = new Vector3(1, 0, 0);
                    go.mesh = geom;

                    /*
                     * go.material.albedoMap = albedoText;
                     * go.material.metallicMap = metallicText;
                     * go.material.normalMap = normalText;
                     * go.material.roughnessMap = roughnessText;
                     */

                    goList.Add(go);
                    tmpStart.X -= spacing;
                }
                startVector.Y -= spacing;
            }
            return(goList);
        }
示例#27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MeshVisual"/> class.
 /// </summary>
 /// <param name="mesh">The mesh.</param>
 /// <param name="shader">The shader.</param>
 /// <param name="textureBindings">The texture bindings.</param>
 public MeshVisual(DefaultMesh mesh, IShaderProgram shader, TextureBinding[] textureBindings)
 {
     shaderProgram        = shader;
     geometry             = VAOLoader.FromMesh(mesh, shader);
     this.textureBindings = textureBindings;
 }
示例#28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MeshVisual"/> class.
 /// </summary>
 /// <param name="mesh">The mesh.</param>
 /// <param name="shader">The shader.</param>
 /// <param name="textureBindings">The texture bindings.</param>
 public MeshVisual(DefaultMesh mesh, IShaderProgram shader, IEnumerable <TextureBinding> textureBindings = null)
     : this(VAOLoader.FromMesh(mesh, shader), shader, textureBindings)
 {
 }
示例#29
0
        //public void UpdateInstanceAttribute<DATA_ELEMENT_TYPE>(string name, DATA_ELEMENT_TYPE[] data) where DATA_ELEMENT_TYPE : struct
        //{
        //	Vao.SetAttribute(GetAttributeShaderLocationAndCheckVao(name), data, VertexAttribPointerType.Float, 3, true);
        //}

        /// <summary>
        /// Updates the mesh shader.
        /// </summary>
        /// <param name="mesh">The mesh.</param>
        /// <param name="shaderProgram">The shader program.</param>
        /// <exception cref="ArgumentNullException">
        /// mesh
        /// or
        /// shaderProgram
        /// or
        /// A shaderName is required
        /// </exception>
        /// <exception cref="ArgumentException">Shader '" + shaderName + "' does not exist</exception>
        public void UpdateMeshShader(DefaultMesh mesh, IShaderProgram shaderProgram)
        {
            ShaderProgram = shaderProgram ?? throw new ArgumentNullException(nameof(shaderProgram));
            Vao           = mesh is null ? null : VAOLoader.FromMesh(mesh, ShaderProgram);
        }
示例#30
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultMesh"/> class.
        /// </summary>

        public Voronoi(int sizeX, int sizeY, Vector3 scale)
        {
            Mesh = new DefaultMesh();
            GenerateVoronoi(sizeX, sizeY, scale);
            GenerateRandomPositionsOnPlateau();
        }