public override void RenderScene() { var shader = GlobalShaders.GetShader("UV_WINDOW"); shader.Enable(); STGenericTexture tex = null; foreach (var render in GLFrameworkEngine.DataCache.ModelCache.Values) { if (ActiveTextureMap != null) { var brender = render; if (brender.Textures.ContainsKey(ActiveTextureMap.Name)) { tex = brender.Textures[ActiveTextureMap.Name]; } } } Vector2 aspectScale = UpdateAspectScale(Width, Height, tex); UVBackground.Draw(tex, ActiveTextureMap, Width, Height, aspectScale, Camera); DrawableUVMap.UpdateVertexBuffer(PolygonGroupIndex, UvChannelIndex, ActiveObjects, ActiveTextureMap); DrawableUVMap.Draw(Camera, aspectScale); }
public static void Draw(GLContext context) { if (ProbeMapManager.ProbeLighting == null) { return; } ProbeDebugVoxelDrawer.Draw(context); return; if (ProbeInstances.Count == 0) { Init(0); } var shader = GlobalShaders.GetShader("PROBE_DRAWER"); context.CurrentShader = shader; int programID = shader.program; shBlock.RenderBuffer(programID, "ProbeSHBuffer"); probeInfoBlock.RenderBuffer(programID, "ProbeInfo"); SphereRender.DrawInstances(context, numDraw); }
public void DrawColorPicking(GLContext control) { if (!ModelInFrustum(control) || !IsVisible) { return; } Transform.UpdateMatrix(); var shader = GlobalShaders.GetShader("PICKING"); control.CurrentShader = shader; if (control.ColorPicker.PickingMode == ColorPicker.SelectionMode.Object) { control.ColorPicker.SetPickingColor(this, shader); } foreach (BfresModelAsset model in Models) { if (model.IsVisible) { model.DrawColorPicking(control); } } }
static void LoadCubemapLevel(GLContext control, int size, int level, AglLightMap aglLightMap, EnvironmentGraphics environmentSettings, AglLightMap.LightArea lightMapEnv, int ID) { GL.Viewport(0, 0, size, size); var shader = GlobalShaders.GetShader("LIGHTMAP"); shader.Enable(); UpdateUniforms(control, shader, level, aglLightMap, environmentSettings, lightMapEnv); for (int i = 0; i < 6; i++) { GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, TextureTarget.TextureCubeMapPositiveX + i, ID, level); } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(); var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } GL.UseProgram(0); }
public static void Draw(STGenericTexture texture, int width, int height, Viewport2D.Camera2D camera, bool showAlpha) { Vector3 scale = new Vector3(1, 1, 1); scale = UpdateAspectScale(scale, width, height, texture); Init(); var shader = GlobalShaders.GetShader("IMAGE_EDITOR"); shader.Enable(); var cameraMtx = Matrix4.CreateScale(100) * camera.ProjectionMatrix; shader.SetMatrix4x4("mtxCam", ref cameraMtx); GL.Disable(EnableCap.Blend); DrawBackground(shader); cameraMtx = camera.ViewMatrix * camera.ProjectionMatrix; shader.SetMatrix4x4("mtxCam", ref cameraMtx); DrawImage(shader, texture, scale.Xy, showAlpha); }
public void Draw(GLContext control, Pass pass) { if (pass != Pass.OPAQUE || !Display) { return; } var gridShaderProgram = GlobalShaders.GetShader("GRID"); bool buffersWereInitialized = vbo_position != 0; if (!buffersWereInitialized) { Init(); UpdateVertexData(control); } if (Runtime.GridSettings.CellSize != CellSize || Runtime.GridSettings.CellAmount != CellAmount) { UpdateVertexData(control); } control.CurrentShader = gridShaderProgram; Matrix4 previewScale = Matrix4.CreateScale(1.0f); gridShaderProgram.SetMatrix4x4("previewScale", ref previewScale); Draw(control, gridShaderProgram); GL.UseProgram(0); }
public override void RenderScene() { var shader = GlobalShaders.GetShader("IMAGE_EDITOR"); shader.Enable(); ImageEditorBackground.Draw(ActiveTexture, Width, Height, Camera, DisplayAlpha); }
static void FilterProbeVolume(GLContext control, GLTexture2D normalsMap, GLTextureCube diffuseCubemap, Vector4[] shData, int lightmapTexID, int size, int mipLevel) { if (frameBuffer == null) { Init(size); } if (frameBuffer.Width != size) { frameBuffer.Resize(size, size); } frameBuffer.Bind(); GL.Viewport(0, 0, size, size); //attach face to fbo as color attachment for (int i = 0; i < 6; i++) { //Each fragment output is a cubemap face GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, TextureTarget.TextureCubeMapPositiveX + i, lightmapTexID, mipLevel); } var shader = GlobalShaders.GetShader("PROBE"); shader.Enable(); var programID = shader.program; LoadUniforms(programID, shData); GL.ActiveTexture(TextureUnit.Texture0 + 1); normalsMap.Bind(); shader.SetInt("sampler0", 1); GL.ActiveTexture(TextureUnit.Texture0 + 2); diffuseCubemap.Bind(); shader.SetInt("sampler1", 2); //Draw once with 6 fragment outputs to form a cubemap GL.ClearColor(0, 0, 0, 1); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(); var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } frameBuffer.Unbind(); GL.BindTexture(TextureTarget.TextureCubeMap, 0); GL.UseProgram(0); }
public static void Draw(STGenericTexture texture, STGenericTextureMap textureMap, int width, int height, Vector2 aspectScale, Viewport2D.Camera2D camera) { Vector2 bgscale = new Vector2(100, 100); Init(); GL.Disable(EnableCap.CullFace); var shader = GlobalShaders.GetShader("UV_WINDOW"); shader.Enable(); var cameraMtx = camera.ViewMatrix * camera.ProjectionMatrix; shader.SetMatrix4x4("mtxCam", ref cameraMtx); GL.ActiveTexture(TextureUnit.Texture1); BindTexture(texture, textureMap); shader.SetInt("uvTexture", 1); shader.SetInt("hasTexture", 1); shader.SetVector2("scale", bgscale * aspectScale); shader.SetVector2("texCoordScale", bgscale); shader.SetVector4("uColor", new Vector4(0.5f, 0.5f, 0.5f, 1.0f)); if (texture != null) { shader.SetBoolToInt("isSRGB", texture.IsSRGB); } //Draw background vao.Enable(shader); vao.Use(); GL.DrawArrays(PrimitiveType.TriangleStrip, 0, Length); //Draw main texture quad inside boundings (0, 1) shader.SetVector2("scale", aspectScale); shader.SetVector2("texCoordScale", new Vector2(1)); shader.SetVector4("uColor", new Vector4(1)); vao.Enable(shader); vao.Use(); GL.DrawArrays(PrimitiveType.TriangleStrip, 0, Length); //Draw outline of boundings (0, 1) shader.SetInt("hasTexture", 0); shader.SetVector2("scale", aspectScale); shader.SetVector2("texCoordScale", new Vector2(1)); shader.SetVector4("uColor", new Vector4(0, 0, 0, 1)); vao.Enable(shader); vao.Use(); GL.LineWidth(1); GL.DrawArrays(PrimitiveType.LineLoop, 0, Length); GL.Enable(EnableCap.CullFace); }
public static void DrawSceneLights(Camera camera, GLTexture gbuffer, GLTexture linearDepth) { return; var shader = GlobalShaders.GetShader("LIGHTPREPASS"); shader.Enable(); GL.ActiveTexture(TextureUnit.Texture0 + 1); gbuffer.Bind(); shader.SetInt("normalsTexture", 1); GL.ActiveTexture(TextureUnit.Texture0 + 2); linearDepth.Bind(); shader.SetInt("depthTexture", 2); int programID = shader.program; var projectionMatrixInverse = camera.ProjectionMatrix.Inverted(); var viewMatrixInverse = camera.ViewMatrix.Inverted(); var mtxProjView = camera.ProjectionMatrix * camera.ViewMatrix; shader.SetMatrix4x4("mtxProjInv", ref projectionMatrixInverse); shader.SetMatrix4x4("mtxViewInv", ref viewMatrixInverse); shader.SetVector3("cameraPosition", camera.TargetPosition); float projectionA = camera.ZFar / (camera.ZFar - camera.ZNear); float projectionB = (-camera.ZFar * camera.ZNear) / (camera.ZFar - camera.ZNear); shader.SetFloat("projectionA", projectionA); shader.SetFloat("projectionB", projectionB); shader.SetFloat("z_range", camera.ZFar - camera.ZNear); shader.SetFloat("fov_x", camera.Fov); shader.SetFloat("fov_y", camera.Fov); PointLight[] pointLights = new PointLight[32]; for (int i = 0; i < 32; i++) { pointLights[i] = new PointLight(); if (i == 0) { pointLights[i].Position = PointPosition; pointLights[i].Color = new Vector4(1, 0, 0, 1); } GL.Uniform4(GL.GetUniformLocation(programID, $"pointLights[{i}].uColor"), pointLights[i].Color); GL.Uniform3(GL.GetUniformLocation(programID, $"pointLights[{i}].uPosition"), pointLights[i].Position); } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(); }
public static void CreateShadowPrepassTexture(GLContext control, int shadowMap, int depthTexture, GLTexture2D output) { if (Filter == null) { Init(); } Filter.Bind(); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, output.ID, 0); GL.Viewport(0, 0, control.Width, control.Height); GL.BindTexture(TextureTarget.Texture2D, 0); var shader = GlobalShaders.GetShader("SHADOWPREPASS"); shader.Enable(); if (Filter.Width != control.Width || Filter.Height != control.Height) { Filter.Resize(control.Width, control.Height); } if (output.Width != control.Width || output.Height != control.Height) { output.Bind(); GL.TexImage2D(output.Target, 0, output.PixelInternalFormat, control.Width, control.Height, 0, output.PixelFormat, output.PixelType, IntPtr.Zero); output.Unbind(); } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.ActiveTexture(TextureUnit.Texture1); GL.BindTexture(TextureTarget.Texture2D, depthTexture); shader.SetInt("depthTexture", 1); GL.ActiveTexture(TextureUnit.Texture2); GL.BindTexture(TextureTarget.Texture2D, shadowMap); shader.SetInt("shadowMap", 2); ScreenQuadRender.Draw(); GL.Flush(); Filter.Unbind(); GL.BindTexture(TextureTarget.Texture2D, 0); GL.UseProgram(0); GL.Viewport(0, 0, control.Width, control.Height); }
private void DrawPath(GLContext context, LineRender render, List <Vector3> points, Vector4 color) { var shader = GlobalShaders.GetShader("LINE"); context.CurrentShader = shader; shader.SetVector4("color", color); render.Draw(points, new List <Vector4>(), true); context.CurrentShader = null; }
public static void PrepareMaterial(GLContext context, GLTexture indTexture, GLTexture patternTexture, GLTexture gbuffer, GLTexture linearDepth) { var shader = GlobalShaders.GetShader("LPP_CAUSTICS"); context.CurrentShader = shader; Matrix4 lightSpaceMatrix = Matrix4.LookAt(new Vector3(0, 0, 0.1f), new Vector3(0), new Vector3(0, 1, 0)); Matrix4 lightProj = Matrix4.CreateOrthographicOffCenter(-1, 1, -1, 1, 0.01f, 5.0f); Matrix4 lightVP = lightProj * lightSpaceMatrix; lightVP.Column0 = new Vector4(-0.2408499f, 0.5407615f, 0.8059581f, 0); lightVP.Column1 = new Vector4(1); lightVP.Column2 = new Vector4(1); lightVP.Column3 = new Vector4(-6000, 490, 0.2f, 0.2f); Vector2 projOffset = new Vector2(0); Vector2 projScale = new Vector2(1.0f); shader.SetTexture(indTexture, "IndirectTexture", 1); shader.SetTexture(patternTexture, "PatternTexture", 2); shader.SetTexture(gbuffer, "NormalsTexture", 3); shader.SetTexture(linearDepth, "LinearDepthTexture", 4); shader.SetVector4("viewParams", new OpenTK.Vector4(context.Height, context.Width, 0, 0)); var viewMat = context.Camera.ViewMatrix; var projMat = context.Camera.ProjectionMatrix; var viewProjMatInv = context.Camera.ViewProjectionMatrix.Inverted(); shader.SetMatrix4x4("mtxView", ref viewMat); shader.SetMatrix4x4("mtxProj", ref projMat); shader.SetMatrix4x4("mtxViewProjInv", ref viewProjMatInv); shader.SetMatrix4x4("mtxLightVP", ref lightVP); shader.SetFloat("clipRange", context.Camera.ZFar - context.Camera.ZNear); shader.SetFloat("clipDiv", context.Camera.ZNear / context.Camera.ZFar); shader.SetFloat("clipNear", context.Camera.ZNear); shader.SetFloat("clipFar", context.Camera.ZFar); shader.SetVector2("viewAspect", new OpenTK.Vector2( context.Camera.FactorX, context.Camera.FactorY)); shader.SetVector2("viewSize", new OpenTK.Vector2(context.Width, context.Height)); shader.SetVector3("cameraPos", context.Camera.GetViewPostion()); shader.SetVector3("cameraDir", context.Camera.GetViewDirection()); shader.SetVector2("projectionOffset", projOffset); shader.SetVector2("projectionScale", projScale); shader.SetFloat("clipRange", context.Camera.ZFar - context.Camera.ZNear); shader.SetFloat("fov_x", context.Camera.Fov); shader.SetFloat("fov_y", context.Camera.Fov); GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); }
public static void FilterScreen(GLContext control) { if (Filter == null) { Init(); } GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); Filter.Bind(); GL.Viewport(0, 0, control.Width, control.Height); GL.BindTexture(TextureTarget.Texture2D, 0); var shader = GlobalShaders.GetShader("SCREEN"); shader.Enable(); shader.SetInt("flipVertical", 1); var texture = (GLTexture2D)control.ScreenBuffer.Attachments[0]; if (Filter.Width != control.Width || Filter.Height != control.Height) { Filter.Resize(control.Width, control.Height); } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(shader, texture.ID); ScreenBuffer = (GLTexture2D)Filter.Attachments[0]; GL.Flush(); Filter.Unbind(); GL.BindTexture(TextureTarget.Texture2D, 0); GL.UseProgram(0); ScreenBuffer.Bind(); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 0); GL.BindTexture(TextureTarget.Texture2D, 0); control.ScreenBuffer.Bind(); GL.Viewport(0, 0, control.Width, control.Height); }
private void DrawCube(GLContext context, Vector3 position) { var shader = GlobalShaders.GetShader("BASIC"); context.CurrentShader = shader; var transform = Matrix4.CreateTranslation(position); shader.SetMatrix4x4("mtxMdl", ref transform); shader.SetVector4("color", new Vector4(1)); CubeRenderer.Draw(context, 0.5f); context.CurrentShader = null; }
public override void DrawModel(GLContext control, GLFrameworkEngine.Pass pass, Vector4 highlightColor) { if (!ModelInFrustum(control) || !IsVisible) { return; } if (ProbeDebugger.ForceUpdate) { UpdateProbeMap = true; } if (Runtime.DebugRendering != Runtime.DebugRender.Default) { control.CurrentShader = GlobalShaders.GetShader("DEBUG"); } else if (control.CurrentShader != BfresRender.DefaultShader) { control.CurrentShader = BfresRender.DefaultShader; } Transform.UpdateMatrix(); foreach (BfresModelAsset model in Models) { if (model.IsVisible) { model.Draw(control, pass, this); } } if (Runtime.DisplayBones) { DrawSkeleton(control); } if (Runtime.RenderBoundingBoxes) { DrawBoundings(control); } }
private void DrawWireframeOutline(GLContext control) { GL.Enable(EnableCap.StencilTest); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); GL.Enable(EnableCap.LineSmooth); GL.LineWidth(1.5f); var selectionShader = GlobalShaders.GetShader("PICKING"); control.CurrentShader = selectionShader; selectionShader.SetVector4("color", new Vector4(1, 1, 1, 1)); foreach (var mesh in Meshes) { DrawSolidColorMesh(selectionShader, mesh); } GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Disable(EnableCap.StencilTest); GL.Enable(EnableCap.DepthTest); }
public static void CreateColorLookupTexture(GLTexture3D output) { GL.BindTexture(output.Target, 0); int LUT_SIZE = output.Width; if (Filter == null) { Init(LUT_SIZE); } Filter.Bind(); for (int i = 0; i < 8; i++) { GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, output.ID, 0, i); } GL.Viewport(0, 0, LUT_SIZE, LUT_SIZE); GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); var shader = GlobalShaders.GetShader("COLOR_CORRECTION"); shader.Enable(); UpdateUniforms(shader); ScreenQuadRender.Draw(); Filter.Unbind(); GL.UseProgram(0); var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } }
public static void Draw(GLContext context) { if (ProbeMapManager.ProbeLighting == null) { return; } if (Vertices.Length == 0) { Init(0); } var shader = GlobalShaders.GetShader("PROBE_VOXEL"); context.CurrentShader = shader; vao.Use(); GL.PointSize(5); GL.DrawArrays(PrimitiveType.Points, 0, Vertices.Length); GL.PointSize(1); }
public static void Draw(GLTexture brightnessTexture, Framebuffer brightnessBuffer, GLContext glControl, int Width, int Height) { brightnessBuffer.Bind(); GL.Viewport(0, 0, glControl.Width, glControl.Height); //Clear out the buffer GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.BindTexture(TextureTarget.Texture2D, 0); var shader = GlobalShaders.GetShader("BLUR"); glControl.CurrentShader = shader; int amount = 8; shader.SetVector2("iResolution ", new Vector2(glControl.Width, glControl.Height)); for (int i = 0; i < amount; i++) { brightnessBuffer.Bind(); GL.Viewport(0, 0, glControl.Width, glControl.Height); var radius = (amount - i - 1) * 1; shader.SetVector2("direction", i % 2 == 0 ? new Vector2(radius, 0) : new Vector2(0, radius)); if (i == 0) { DrawBlur(glControl, brightnessTexture); } else { DrawBlur(glControl, (GLTexture)brightnessBuffer.Attachments[0]); } GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); } GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); }
private void DrawOverlayWireframe(GLContext control, ShaderProgram shader) { Draw(); var previousShader = control.CurrentShader; var selectionShader = GlobalShaders.GetShader("PICKING"); control.CurrentShader = selectionShader; selectionShader.SetVector4("color", new Vector4(1, 1, 1, 1)); GL.Enable(EnableCap.StencilTest); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); GL.Enable(EnableCap.LineSmooth); GL.LineWidth(1.5f); Draw(); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Enable(EnableCap.DepthTest); control.CurrentShader = previousShader; }
public void Draw(UVViewport.Camera2D camera, Vector2 scale) { GL.Disable(EnableCap.CullFace); var shader = GlobalShaders.GetShader("UV_WINDOW"); shader.Enable(); var cameraMtx = camera.ViewMatrix * camera.ProjectionMatrix; //shader.SetMatrix4x4("mtxMdl", ref scaleMtx); shader.SetMatrix4x4("mtxCam", ref cameraMtx); shader.SetInt("hasTexture", 0); shader.SetVector2("scale", scale); shader.SetVector4("uColor", ColorUtility.ToVector4(Runtime.UVEditor.UVColor)); vao.Enable(shader); vao.Use(); GL.DrawArrays(PrimitiveType.LineLoop, 0, Points.Count); GL.Enable(EnableCap.CullFace); }
public static GLTexture2D FilterScreen(GLContext control, GLTexture2D colorTexture) { if (Filter == null) { Init(); } Filter.Bind(); GL.Viewport(0, 0, control.Width, control.Height); GL.BindTexture(TextureTarget.Texture2D, 0); var shader = GlobalShaders.GetShader("BLOOM_EXTRACT"); shader.Enable(); shader.SetFloat("bloom_intensity", 1.0f); if (Filter.Width != control.Width || Filter.Height != control.Height) { Filter.Resize(control.Width, control.Height); } GL.ClearColor(System.Drawing.Color.Black); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(shader, colorTexture.ID); var screenBuffer = (GLTexture2D)Filter.Attachments[0]; GL.Flush(); Filter.Unbind(); GL.BindTexture(TextureTarget.Texture2D, 0); GL.UseProgram(0); GL.Viewport(0, 0, control.Width, control.Height); return(screenBuffer); }
public static int CreateTextureRender(STGenericTexture texture, int width, int height, bool displayAlpha = true) { if (texture.RenderableTex == null) { texture.LoadRenderableTexture(); } if (texture.RenderableTex == null) { return(-1); } int ID = texture.RenderableTex.ID; if (texture.Platform.OutputFormat == TexFormat.BC5_SNORM) { var reloaded = GLTexture2D.FromGeneric(texture, new ImageParameters() { UseSoftwareDecoder = (texture.Platform.OutputFormat == TexFormat.BC5_SNORM), }); ID = reloaded.ID; } var shader = GlobalShaders.GetShader("TEXTURE_ICON"); Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, width, height, PixelInternalFormat.Rgba, 1); frameBuffer.Bind(); GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Viewport(0, 0, width, height); GL.Disable(EnableCap.Blend); shader.Enable(); shader.SetBoolToInt("isSRGB", texture.IsSRGB); int[] mask = new int[4] { OpenGLHelper.GetSwizzle(texture.RedChannel), OpenGLHelper.GetSwizzle(texture.GreenChannel), OpenGLHelper.GetSwizzle(texture.BlueChannel), OpenGLHelper.GetSwizzle(displayAlpha ? texture.AlphaChannel : STChannelType.One), }; ((GLTexture)texture.RenderableTex).Bind(); GL.TexParameter(((GLTexture)texture.RenderableTex).Target, TextureParameterName.TextureSwizzleRgba, mask); //Draw the texture onto the framebuffer ScreenQuadRender.Draw(shader, ID); //Disable shader and textures GL.UseProgram(0); GL.BindTexture(TextureTarget.Texture2D, 0); var image = (GLTexture2D)frameBuffer.Attachments[0]; return(image.ID); /* //Dispose frame buffer * frameBuffer.Dispoe(); * frameBuffer.DisposeRenderBuffer();*/ }
public static void CreateCubemap(GLContext control, GLTextureCube cubemapInput, GLTexture cubemapOutput, int layer) { int size = cubemapOutput.Width; Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90), 1.0f, 0.1f, 10.0f); Matrix4[] captureViews = { Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(-1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(0.0f, -1.0f, 0.0f)), }; GL.BindTexture(TextureTarget.TextureCubeMap, 0); //Bind the cubemap's texture into a filtered quad. //Bind the drawn filter to a cubemap array layer Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, size, size, PixelInternalFormat.Rgba32f); frameBuffer.Bind(); GL.Disable(EnableCap.Blend); var cubemapFilter = GlobalShaders.GetShader("CUBEMAP_PREFILTER"); cubemapFilter.Enable(); //Allocate mipmaps cubemapOutput.Bind(); cubemapOutput.GenerateMipmaps(); cubemapOutput.Unbind(); GL.ActiveTexture(TextureUnit.Texture0 + 1); cubemapInput.Bind(); cubemapFilter.SetInt("environmentMap", 1); cubemapFilter.SetMatrix4x4("projection", ref projection); //Quick hack, draw once before rendering (first buffer not updating for some reason??) RenderTools.DrawCube(); GL.Disable(EnableCap.CullFace); for (int mip = 0; mip < cubemapOutput.MipCount; mip++) { int mipWidth = (int)(size * Math.Pow(0.5, mip)); int mipHeight = (int)(size * Math.Pow(0.5, mip)); frameBuffer.Resize(mipWidth, mipHeight); GL.Viewport(0, 0, mipWidth, mipHeight); float roughness = (float)mip / (float)(cubemapOutput.MipCount - 1); cubemapFilter.SetFloat("roughness", roughness); for (int i = 0; i < 6; i++) { //attach face to fbo as color attachment 0 if (cubemapOutput is GLTextureCubeArray) { GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, cubemapOutput.ID, mip, (layer * 6) + i); } else { GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMapPositiveX + i, cubemapOutput.ID, mip); } cubemapFilter.SetMatrix4x4("view", ref captureViews[i]); GL.ClearColor(0, 0, 0, 1); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); RenderTools.DrawCube(); } } var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.Blend); frameBuffer.Dispoe(); frameBuffer.DisposeRenderBuffer(); GL.UseProgram(0); }
public void DrawBoundings(GLContext control) { foreach (BfresModelAsset model in Models) { if (!model.IsVisible) { continue; } foreach (var mesh in model.Meshes) { if (!mesh.IsVisible || !mesh.InFrustum) { continue; } //Go through each bounding in the current displayed mesh var polygonGroup = mesh.Shape.PolygonGroups[mesh.LODMeshLevel] as MeshPolygonGroup; foreach (var bounding in polygonGroup.Boundings) { var min = bounding.Center - bounding.Extent; var max = bounding.Center + bounding.Extent; var shader = GlobalShaders.GetShader("PICKING"); control.CurrentShader = shader; control.CurrentShader.SetVector4("color", new Vector4(1)); Matrix4 transform = Matrix4.Identity; if (mesh.SkinCount == 0) { transform = model.ModelData.Skeleton.Bones[mesh.BoneIndex].Transform; control.CurrentShader.SetMatrix4x4("mtxMdl", ref transform); var bnd = mesh.BoundingNode.Box; BoundingBoxRender.Draw(control, bnd.Min, bnd.Max); } else { foreach (var boneIndex in mesh.Shape.Shape.SkinBoneIndices) { transform = model.ModelData.Skeleton.Bones[boneIndex].Transform; control.CurrentShader.SetMatrix4x4("mtxMdl", ref transform); BoundingBoxRender.Draw(control, new Vector3(min.X, min.Y, min.Z), new Vector3(max.X, max.Y, max.Z)); } } } /* * var center = mesh.BoundingNode.Center; * var radius = mesh.BoundingNode.Radius; * * GL.Enable(EnableCap.Blend); * * transform = Matrix4.CreateScale(radius) * Matrix4.CreateTranslation(center) * transform; * control.CurrentShader.SetMatrix4x4("mtxMdl", ref transform); * control.CurrentShader.SetVector4("color", new Vector4(0,0,0,0.2f)); * * SphereRender.Draw(control); * * GL.Disable(EnableCap.Blend);*/ } } control.CurrentShader = null; }
public static void CreateCubemap(GLContext control, GLTextureCube cubemapInput, GLTexture cubemapOutput, int layer, int numMips) { int size = cubemapInput.Width; Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90), 1.0f, 0.1f, 10.0f); Matrix4[] captureViews = { Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(-1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, -1.0f, 0.0f)), Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(0.0f, -1.0f, 0.0f)), }; GL.BindTexture(TextureTarget.TextureCubeMap, 0); //Bind the cubemap's texture into a filtered quad. //Bind the drawn filter to a cubemap array layer Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, size, size, PixelInternalFormat.Rgba32f); frameBuffer.Bind(); GL.Disable(EnableCap.Blend); var cubemapFilter = GlobalShaders.GetShader("CUBEMAP_HDRDECODE"); cubemapFilter.Enable(); GL.ActiveTexture(TextureUnit.Texture0 + 1); cubemapInput.Bind(); cubemapFilter.SetInt("cubemapTexture", 1); cubemapFilter.SetMatrix4x4("projection", ref projection); cubemapFilter.SetFloat("gamma", 2.2f); cubemapFilter.SetFloat("range", 1024.0f); cubemapFilter.SetFloat("scale", 4.0f); GL.Disable(EnableCap.CullFace); for (int mip = 0; mip < numMips; mip++) { int mipWidth = (int)(size * Math.Pow(0.5, mip)); int mipHeight = (int)(size * Math.Pow(0.5, mip)); frameBuffer.Resize(mipWidth, mipHeight); GL.Viewport(0, 0, mipWidth, mipHeight); cubemapFilter.SetFloat("mipLevel", mip); for (int i = 0; i < 6; i++) { cubemapFilter.SetInt("faceLevel", i); //attach face to fbo as color attachment 0 if (cubemapOutput is GLTextureCubeArray) { GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, cubemapOutput.ID, mip, (layer * 6) + i); } else { GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMapPositiveX + i, cubemapOutput.ID, mip); } cubemapFilter.SetMatrix4x4("view", ref captureViews[i]); GL.ClearColor(0, 0, 0, 1); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); RenderTools.DrawCube(); } } var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.Blend); frameBuffer.Dispoe(); frameBuffer.DisposeRenderBuffer(); GL.UseProgram(0); }