private int TransformHdrToCubemap(int textureId, string textureName, ShaderProg shader, int framebuffer, int renderbuffer, int size = 2048) { Cube cube = new Cube(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbuffer); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, size, size); GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, renderbuffer); int cubemap = AllocateCubemap(size); Matrix4 projection = Matrix4.Identity; Matrix4[] views = new Matrix4[6]; GenerateMatricesForMapHdrToCubemap(ref projection, ref views); ShaderProg equirectangularToCubemapShader = shader; GL.UseProgram(equirectangularToCubemapShader.ProgramID); equirectangularToCubemapShader.EnableVertexAttribArrays(); GL.UniformMatrix4(equirectangularToCubemapShader.GetUniform("projection"), false, ref projection); UniformHelper helper = new UniformHelper(); bool suc; if (textureName == "equirectangularMap") { suc = helper.TryAddUniformTexture2D(textureId, textureName, equirectangularToCubemapShader, TextureUnit.Texture0); } else { suc = helper.TryAddUniformTextureCubemap(textureId, textureName, equirectangularToCubemapShader, TextureUnit.Texture0); } GL.Viewport(0, 0, size, size); GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer); GL.ActiveTexture(TextureUnit.Texture0); for (int i = 0; i < 6; ++i) { GL.UniformMatrix4(equirectangularToCubemapShader.GetUniform("view"), false, ref views[i]); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMapPositiveX + i, cubemap, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); cube.RenderCube(); // renders a 1x1 cube } equirectangularToCubemapShader.DisableVertexAttribArrays(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); return(cubemap); }
public void SetSpecificUniforms(IPipelineData pipelineData) { int texId = -1; int normId = -1; int metRoughId = -1; int metallicId = -1; int roughnessId = -1; int ambientId = -1; Textures.TryGetValue(Materials.FirstOrDefault().Value.DiffuseMap, out texId); Textures.TryGetValue(Materials.FirstOrDefault().Value.NormalMap, out normId); Textures.TryGetValue(Materials.FirstOrDefault().Value.MetallicRoughness, out metRoughId); Textures.TryGetValue(Materials.FirstOrDefault().Value.Metallic, out metallicId); Textures.TryGetValue(Materials.FirstOrDefault().Value.Roughness, out roughnessId); Textures.TryGetValue(Materials.FirstOrDefault().Value.AmbientMap, out ambientId); _uniformHelper.TryAddUniformTexture2D(texId, maintexture, shaderProg, TextureUnit.Texture0); _uniformHelper.TryAddUniformTexture2D(normId, normaltexture, shaderProg, TextureUnit.Texture1); _uniformHelper.TryAddUniformTexture2D(metRoughId, metallicRoughness, shaderProg, TextureUnit.Texture2); _uniformHelper.TryAddUniformTexture2D(metallicId, metallic, shaderProg, TextureUnit.Texture2); _uniformHelper.TryAddUniformTexture2D(roughnessId, roughness, shaderProg, TextureUnit.Texture2); _uniformHelper.TryAddUniformTexture2D(ambientId, amibenOclussionMap, shaderProg, TextureUnit.Texture3); _uniformHelper.TryAddUniformTextureCubemap(pipelineData.SkyboxComponent.IrradianceMap, irradianceMap, shaderProg, TextureUnit.Texture4); _uniformHelper.TryAddUniformTextureCubemap(pipelineData.SkyboxComponent.PrefilteredMap, prefilterMap, shaderProg, TextureUnit.Texture5); _uniformHelper.TryAddUniformTexture2D(pipelineData.SkyboxComponent.BrdfMap, brdf, shaderProg, TextureUnit.Texture6); _uniformHelper.TryAddUniform1(randomCoeff, "randomCoeff", shaderProg); _uniformHelper.TryAddUniform1(pipelineData.Lights.Count(), numberOfLights, shaderProg); Vector3[] lightpositions = new Vector3[pipelineData.Lights.Count()]; Vector3[] lightcolors = new Vector3[pipelineData.Lights.Count()]; for (int j = 0; j < pipelineData.Lights.Count; ++j) { var lightComponent = (PointLightSceneObjectComponent)pipelineData.Lights[j].components["PointLightSceneObjectComponent"]; lightcolors[j] = lightComponent.Color * lightComponent.LightStrenght; lightpositions[j] = pipelineData.Lights[j].Position; } bool suc = _uniformHelper.TryAddUniform1(lightcolors, lightColor, shaderProg); bool suc2 = _uniformHelper.TryAddUniform1(lightpositions, lightPosition, shaderProg); _uniformHelper.TryAddUniform1(pipelineData.Cam.Position, cameraPosition, shaderProg); _uniformHelper.TryAddUniform1(Materials.FirstOrDefault().Value.NormalScale, "normalScale", shaderProg); }
public void SetSpecificUniforms(IPipelineData pipelineData) { int texId; bool isTexPresent = Textures.TryGetValue(Materials.FirstOrDefault().Value.DiffuseMap, out texId); if (isTexPresent) { _uniformHelper.TryAddUniformTexture2D(texId, maintexture, shaderProg, TextureUnit.Texture0); _uniformHelper.TryAddUniform1(1, isTextured, shaderProg); } else { _uniformHelper.TryAddUniform1(0, isTextured, shaderProg); } }