Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skybox #305

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion FinalEngine.Examples.Sponza/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ private static void Main()

var geometryRenderer = new GeometryRenderer(renderDevice);
var lightRenderer = new LightRenderer(renderDevice.Pipeline);
var renderingEngine = new RenderingEngine(renderDevice, geometryRenderer, lightRenderer);
var skyboxRenderer = new SkyboxRenderer(renderDevice);
var renderingEngine = new RenderingEngine(renderDevice, geometryRenderer, lightRenderer, skyboxRenderer);

var controller = new ImGuiController(window.ClientSize.Width, window.ClientSize.Height);

Expand Down
1 change: 1 addition & 0 deletions FinalEngine.Rendering.OpenGL/Buffers/OpenGLFrameBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace FinalEngine.Rendering.OpenGL.Buffers;
using FinalEngine.Rendering.OpenGL.Invocation;
using FinalEngine.Rendering.OpenGL.Textures;
using OpenTK.Graphics.OpenGL4;
using PixelFormat = Rendering.Textures.PixelFormat;

public class OpenGLFrameBuffer : IFrameBuffer, IOpenGLFrameBuffer
{
Expand Down
16 changes: 16 additions & 0 deletions FinalEngine.Rendering.OpenGL/Invocation/IOpenGLInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ void NamedBufferSubData<T3>(int buffer, IntPtr offset, int size, T3[] data)

void TextureSubImage2D(int texture, int level, int xoffset, int yoffset, int width, int height, PixelFormat format, PixelType type, IntPtr pixels);

void CopyImageSubData(int srcName,
ImageTarget srcTarget,
int srcLevel,
int srcX,
int srcY,
int srcZ,
int dstName,
ImageTarget dstTarget,
int dstLevel,
int dstX,
int dstY,
int dstZ,
int srcWidth,
int srcHeight,
int srcDepth);

void Uniform1(int location, int x);

void Uniform1(int location, float v0);
Expand Down
30 changes: 27 additions & 3 deletions FinalEngine.Rendering.OpenGL/Invocation/OpenGLInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ public int GenVertexArray()
return GL.GenVertexArray();
}

public void GetActiveUniform(int program, int index, int bufSize, out int length, out int size, out ActiveUniformType type, out string name)
public void GetActiveUniform(int program, int index, int bufSize, out int length, out int size,
out ActiveUniformType type, out string name)
{
GL.GetActiveUniform(program, index, bufSize, out length, out size, out type, out name);
}
Expand Down Expand Up @@ -357,11 +358,33 @@ public void TextureStorage2D(int texture, int levels, SizedInternalFormat intern
GL.TextureStorage2D(texture, levels, internalFormat, width, height);
}

public void TextureSubImage2D(int texture, int level, int xoffset, int yoffset, int width, int height, PixelFormat format, PixelType type, IntPtr pixels)
public void TextureSubImage2D(int texture, int level, int xoffset, int yoffset, int width, int height,
PixelFormat format, PixelType type, IntPtr pixels)
{
GL.TextureSubImage2D(texture, level, xoffset, yoffset, width, height, format, type, pixels);
}


public void CopyImageSubData(int srcName,
ImageTarget srcTarget,
int srcLevel,
int srcX,
int srcY,
int srcZ,
int dstName,
ImageTarget dstTarget,
int dstLevel,
int dstX,
int dstY,
int dstZ,
int srcWidth,
int srcHeight,
int srcDepth)
{
GL.CopyImageSubData(srcName, srcTarget, srcLevel, srcX, srcY, srcZ, dstName, dstTarget, dstLevel, dstX, dstY,
dstZ, srcWidth, srcHeight, srcDepth);
}

public void Uniform1(int location, int x)
{
GL.Uniform1(location, x);
Expand Down Expand Up @@ -412,7 +435,8 @@ public void VertexAttribBinding(int attribindex, int bindingindex)
GL.VertexAttribBinding(attribindex, bindingindex);
}

public void VertexAttribFormat(int attribindex, int size, VertexAttribType type, bool normalized, int relativeoffset)
public void VertexAttribFormat(int attribindex, int size, VertexAttribType type, bool normalized,
int relativeoffset)
{
GL.VertexAttribFormat(attribindex, size, type, normalized, relativeoffset);
}
Expand Down
7 changes: 7 additions & 0 deletions FinalEngine.Rendering.OpenGL/OpenGLGPUResourceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace FinalEngine.Rendering.OpenGL;
using FinalEngine.Rendering.Textures;
using FinalEngine.Utilities;
using OpenTK.Graphics.OpenGL4;
using static System.Runtime.InteropServices.JavaScript.JSType;
using PixelFormat = FinalEngine.Rendering.Textures.PixelFormat;

public class OpenGLGPUResourceFactory : IGPUResourceFactory
Expand Down Expand Up @@ -82,6 +83,12 @@ public ITexture2D CreateTexture2D<T>(
return result;
}

public ICubeTexture CreateCubeTexture(CubeTextureDescription description, ITexture2D right, ITexture2D left, ITexture2D top,
ITexture2D bottom, ITexture2D back, ITexture2D front, SizedFormat internalFormat = SizedFormat.Rgba8)
{
var cubeFaces = new List<ITexture2D>(){right,left,top,bottom,front,back};
return new OpenglCubeTexture(this.invoker, this.mapper, description, internalFormat, cubeFaces.Cast<IOpenGLTexture>().ToArray());
}
public IVertexBuffer CreateVertexBuffer<T>(BufferUsageType type, IReadOnlyCollection<T> data, int sizeInBytes, int stride)
where T : struct
{
Expand Down
2 changes: 2 additions & 0 deletions FinalEngine.Rendering.OpenGL/OpenGLRenderDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public OpenGLRenderDevice(IOpenGLInvoker invoker)
{ PixelType.Byte, TKPixelType.UnsignedByte },
{ PixelType.Int, TKPixelType.UnsignedInt },
{ PixelType.Short, TKPixelType.UnsignedShort },
{ PixelType.Float, TKPixelType.Float },
{ PixelFormat.R, TKPixelForamt.Red },
{ PixelFormat.Rg, TKPixelForamt.Rg },
{ PixelFormat.Rgb, TKPixelForamt.Rgb },
Expand All @@ -104,6 +105,7 @@ public OpenGLRenderDevice(IOpenGLInvoker invoker)
{ SizedFormat.Rg8, SizedInternalFormat.Rg8 },
{ SizedFormat.Rgb8, All.Rgb8 },
{ SizedFormat.Rgba8, SizedInternalFormat.Rgba8 },
{ SizedFormat.Depth, SizedInternalFormat.DepthComponent32f },
{ SizedFormat.Srgba, SizedInternalFormat.Srgb8Alpha8 },
{ BufferUsageType.Static, BufferUsageHint.StaticDraw },
{ BufferUsageType.Dynamic, BufferUsageHint.DynamicDraw },
Expand Down
2 changes: 2 additions & 0 deletions FinalEngine.Rendering.OpenGL/Textures/IOpenGLTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace FinalEngine.Rendering.OpenGL.Textures;

public interface IOpenGLTexture : ITexture
{
// TODO : delete?
int RenderId { get; }
void Attach(FramebufferAttachment type, int framebuffer);

void Bind(int unit);
Expand Down
8 changes: 8 additions & 0 deletions FinalEngine.Rendering.OpenGL/Textures/OpenGLTexture2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public OpenGLTexture2D(

protected bool IsDisposed { get; private set; }

public int RenderId
{
get
{
return this.rendererID;
}
}

public void Attach(FramebufferAttachment type, int framebuffer)
{
ObjectDisposedException.ThrowIf(this.IsDisposed, this);
Expand Down
105 changes: 105 additions & 0 deletions FinalEngine.Rendering.OpenGL/Textures/OpenglCubeTexture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
namespace FinalEngine.Rendering.OpenGL.Textures;

using System;
using FinalEngine.Rendering.OpenGL.Invocation;
using FinalEngine.Rendering.Textures;
using FinalEngine.Utilities;
using OpenTK.Graphics.OpenGL4;
using PixelFormat = FinalEngine.Rendering.Textures.PixelFormat;
using TKPixelForamt = OpenTK.Graphics.OpenGL4.PixelFormat;
using TKPixelType = OpenTK.Graphics.OpenGL4.PixelType;
using TKTextureWrapMode = OpenTK.Graphics.OpenGL4.TextureWrapMode;

public class OpenglCubeTexture : ICubeTexture, IOpenGLTexture, IDisposable
{
private readonly IOpenGLInvoker invoker;

private int rendererID;
public PixelFormat Format { get; }
public SizedFormat InternalFormat { get; }
public CubeTextureDescription Description { get; }
protected bool IsDisposed { get; private set; }

public int RenderId
{
get { return this.rendererID; }
}
public OpenglCubeTexture(
IOpenGLInvoker invoker,
IEnumMapper mapper,
CubeTextureDescription description,
SizedFormat internalFormat,
IOpenGLTexture[] data)
{
ArgumentNullException.ThrowIfNull(mapper, nameof(mapper));
this.invoker = invoker ?? throw new ArgumentNullException(nameof(invoker));
this.rendererID = invoker.CreateTexture(TextureTarget.TextureCubeMap);
this.InternalFormat = internalFormat;
this.Description = description;

int mipmap = (int)Math.Ceiling(Math.Max(Math.Log2(description.Width + 1), Math.Log2(description.Height + 1)));
invoker.TextureStorage2D(this.rendererID, mipmap, mapper.Forward<SizedInternalFormat>(this.InternalFormat),description.Width, description.Height);

invoker.TextureParameter(this.rendererID, TextureParameterName.TextureMinFilter,
(int)mapper.Forward<TextureMinFilter>(description.MinFilter));
invoker.TextureParameter(this.rendererID, TextureParameterName.TextureMagFilter,
(int)mapper.Forward<TextureMagFilter>(description.MagFilter));
invoker.TextureParameter(this.rendererID, TextureParameterName.TextureWrapS,
(int)mapper.Forward<TKTextureWrapMode>(description.WrapS));
invoker.TextureParameter(this.rendererID, TextureParameterName.TextureWrapT,
(int)mapper.Forward<TKTextureWrapMode>(description.WrapT));
invoker.TextureParameter(this.rendererID, TextureParameterName.TextureWrapR,
(int)mapper.Forward<TKTextureWrapMode>(description.WrapR));

for (int i = 0; i < data.Length; i++)
{
invoker.CopyImageSubData(data[i].RenderId,
ImageTarget.Texture2D,0,0,0,0,this.rendererID,
ImageTarget.TextureCubeMap,0,0,0,i,description.Width,description.Height,1);

if (description.GenerateMipmaps)
{
invoker.GenerateTextureMipmap(this.rendererID);
}
}
}

public void Attach(FramebufferAttachment type, int framebuffer)
{
// todo
throw new NotImplementedException();
}

public void Bind(int unit)
{
ObjectDisposedException.ThrowIf(this.IsDisposed, this);
this.invoker.BindTextureUnit(unit, this.rendererID);
}

~OpenglCubeTexture()
{
this.Dispose(false);
}

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (this.IsDisposed)
{
return;
}

if (disposing && this.rendererID != -1)
{
this.invoker.DeleteTexture(this.rendererID);
this.rendererID = -1;
}

this.IsDisposed = true;
}
}
31 changes: 31 additions & 0 deletions FinalEngine.Rendering/FinalEngine.Rendering.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
<None Update="Resources\Shaders\Lighting\lighting.point.frag">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Shaders\skybox.fesp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Shaders\skybox.frag">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Shaders\skybox.vert">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Shaders\Batching\sprite-geometry.fesp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -100,5 +109,27 @@
<None Update="Resources\Textures\default_specular.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Textures\skybox\back.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Textures\skybox\bottom.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Textures\skybox\front.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Textures\skybox\left.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Textures\skybox\right.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Textures\skybox\top.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Skybox\" />
</ItemGroup>
</Project>
11 changes: 9 additions & 2 deletions FinalEngine.Rendering/IGPUResourceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@
IFrameBuffer CreateFrameBuffer(IReadOnlyCollection<ITexture2D>? colorTargets, ITexture2D? depthTarget = null);

IIndexBuffer CreateIndexBuffer<T>(BufferUsageType type, IReadOnlyCollection<T> data, int sizeInBytes)
where T : struct;
where T : struct;

IInputLayout CreateInputLayout(IReadOnlyCollection<InputElement> elements);

IShader CreateShader(PipelineTarget target, string sourceCode);

IShaderProgram CreateShaderProgram(IReadOnlyCollection<IShader> shaders);

ITexture2D CreateTexture2D<T>(Texture2DDescription description, IReadOnlyCollection<T>? data, PixelFormat format = PixelFormat.Rgba, SizedFormat internalFormat = SizedFormat.Rgba8);
ITexture2D CreateTexture2D<T>(Texture2DDescription description, IReadOnlyCollection<T>? data,
PixelFormat format = PixelFormat.Rgba, SizedFormat internalFormat = SizedFormat.Rgba8);

// The descriptions and internal formats of all ITexture2D must match those of CubeTexture
ICubeTexture CreateCubeTexture(CubeTextureDescription description,
ITexture2D right, ITexture2D left,
ITexture2D top, ITexture2D bottom,
ITexture2D back, ITexture2D front,SizedFormat internalFormat = SizedFormat.Rgba8);

Check warning on line 32 in FinalEngine.Rendering/IGPUResourceFactory.cs

View workflow job for this annotation

GitHub Actions / build_editor_common

Check warning on line 32 in FinalEngine.Rendering/IGPUResourceFactory.cs

View workflow job for this annotation

GitHub Actions / build_rendering_opengl

Check warning on line 32 in FinalEngine.Rendering/IGPUResourceFactory.cs

View workflow job for this annotation

GitHub Actions / build_editor_view_models

Check warning on line 32 in FinalEngine.Rendering/IGPUResourceFactory.cs

View workflow job for this annotation

GitHub Actions / build_runtime

Check warning on line 32 in FinalEngine.Rendering/IGPUResourceFactory.cs

View workflow job for this annotation

GitHub Actions / build_rendering

Check warning on line 32 in FinalEngine.Rendering/IGPUResourceFactory.cs

View workflow job for this annotation

GitHub Actions / build_editor_desktop


IVertexBuffer CreateVertexBuffer<T>(BufferUsageType type, IReadOnlyCollection<T> data, int sizeInBytes, int stride)
where T : struct;
Expand Down
9 changes: 9 additions & 0 deletions FinalEngine.Rendering/Renderers/ISkyboxRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FinalEngine.Rendering.Renderers;

Check warning on line 1 in FinalEngine.Rendering/Renderers/ISkyboxRenderer.cs

View workflow job for this annotation

GitHub Actions / build_editor_common

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check warning on line 1 in FinalEngine.Rendering/Renderers/ISkyboxRenderer.cs

View workflow job for this annotation

GitHub Actions / build_rendering_opengl

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check warning on line 1 in FinalEngine.Rendering/Renderers/ISkyboxRenderer.cs

View workflow job for this annotation

GitHub Actions / build_editor_view_models

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check warning on line 1 in FinalEngine.Rendering/Renderers/ISkyboxRenderer.cs

View workflow job for this annotation

GitHub Actions / build_runtime

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

Check warning on line 1 in FinalEngine.Rendering/Renderers/ISkyboxRenderer.cs

View workflow job for this annotation

GitHub Actions / build_rendering

The file header is missing or not located at the top of the file. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md)

using FinalEngine.Rendering.Core;
using Textures;

public interface ISkyboxRenderer
{
void Render(ICubeTexture texture, ICamera camera);
}
Loading
Loading