Skip to content
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
2 changes: 1 addition & 1 deletion Examples/MinecraftClone/Block/BlockBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected BlockBase(Vector3 position, string name, string diffuseMapFile, string

private void Initialize(Vector3 position, string name, string diffuseMapFile, string specularMapFile, string vertShaderFile, string fragShaderFile)
{
var cube = PrimitiveFactory.Create(PrimitiveType.Cube, position, diffuseMapFile, specularMapFile, vertShaderFile, fragShaderFile);
var cube = PrimitiveFactory.Create(PrimitiveType.Cube, (SharpEngine.Core.Numerics.Vector3)position, diffuseMapFile, specularMapFile, vertShaderFile, fragShaderFile);
AssignProperties(cube, name);
}

Expand Down
24 changes: 12 additions & 12 deletions Examples/MinecraftClone/Minecraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@
_lightsNode = _scene.Root.AddChild("lights");
_blocksNode = _scene.Root.AddChild("blocks");

var gridLayout = new GridLayout<UIElement>();
// var gridLayout = new GridLayout<UIElement>();

Check warning on line 78 in Examples/MinecraftClone/Minecraft.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF74b5VpPISvuiaW&open=AZ8PHF74b5VpPISvuiaW&pullRequest=136

// TODO: #89 Fix UI renderer
_uiElem = new UIElement("uiElement");
_uiElem = new UIElement(Window.GetGL(), "uiElement");
_scene.UIElements.Add(_uiElem);

var uiElem2 = new UIElement("uiElement");
var uiElem2 = new UIElement(Window.GetGL(), "uiElement");
uiElem2.Transform.Scale = new SharpEngine.Core.Numerics.Vector2(0.2f, 0.2f);
uiElem2.Transform.Position = new SharpEngine.Core.Numerics.Vector2(30, 0);

gridLayout.AddChild(_uiElem, uiElem2);
// gridLayout.AddChild(_uiElem, uiElem2);

Check warning on line 88 in Examples/MinecraftClone/Minecraft.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF74b5VpPISvuiaX&open=AZ8PHF74b5VpPISvuiaX&pullRequest=136
_scene.UIElements.Add(_uiElem);
_scene.UIElements.Add(uiElem2);

_scene.UIElements.Add(gridLayout);
// _scene.UIElements.Add(gridLayout);

Check warning on line 92 in Examples/MinecraftClone/Minecraft.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF74b5VpPISvuiaY&open=AZ8PHF74b5VpPISvuiaY&pullRequest=136

InitializeWorld();
}
Expand Down Expand Up @@ -172,17 +172,17 @@
_lightsNode.AddChild(new DirectionalLight());

_lightsNode.AddChild(
new PointLight(new Vector3(0.7f, 0.2f, 2.0f), 0),
new PointLight(new Vector3(2.3f, -3.3f, -4.0f), 1),
new PointLight(new Vector3(-4.0f, 2.0f, -12.0f), 2),
new PointLight(new Vector3(0.0f, 0.0f, -3.0f), 3)
new PointLight(new SharpEngine.Core.Numerics.Vector3(0.7f, 0.2f, 2.0f), 0),
new PointLight(new SharpEngine.Core.Numerics.Vector3(2.3f, -3.3f, -4.0f), 1),
new PointLight(new SharpEngine.Core.Numerics.Vector3(-4.0f, 2.0f, -12.0f), 2),
new PointLight(new SharpEngine.Core.Numerics.Vector3(0.0f, 0.0f, -3.0f), 3)
);

_lightsNode.AddChild(new SpotLight()
{
Ambient = new Vector3(0.0f, 0.0f, 0.0f),
Diffuse = new Vector3(1.0f, 1.0f, 1.0f),
Specular = new Vector3(1.0f, 1.0f, 1.0f),
Ambient = new SharpEngine.Core.Numerics.Vector3(0.0f, 0.0f, 0.0f),
Diffuse = new SharpEngine.Core.Numerics.Vector3(1.0f, 1.0f, 1.0f),
Specular = new SharpEngine.Core.Numerics.Vector3(1.0f, 1.0f, 1.0f),
});
}

Expand Down
2 changes: 1 addition & 1 deletion Examples/Tutorial 4.1 - Model Loading/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
private static void OnLoad()
{
if (window == null)
throw new NullReferenceException("Window is not initialized.");

Check warning on line 69 in Examples/Tutorial 4.1 - Model Loading/Program.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'System.NullReferenceException' should not be thrown by user code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF8Fb5VpPISvuiaa&open=AZ8PHF8Fb5VpPISvuiaa&pullRequest=136

IInputContext input = window.CreateInput();
primaryKeyboard = input.Keyboards[0];
Expand All @@ -82,7 +82,7 @@

Gl = GL.GetApi(window);

Shader = new Shader(Gl, PathExtensions.GetAssemblyPath("shader2.vert"), Default.LightShader, "test").Initialize();
Shader = new Shader(Gl, PathExtensions.GetAssemblyPath("shader2.vert"), Default.LightShader, "test");
Texture = new Texture(Gl, "silk.png");

var model = ObjLoaderFactory.Load(Gl, "Untitled2.obj");
Expand All @@ -94,7 +94,7 @@
private static void OnUpdate(double deltaTime)
{
if (primaryKeyboard == null)
throw new NullReferenceException("Primary keyboard is not initialized.");

Check warning on line 97 in Examples/Tutorial 4.1 - Model Loading/Program.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'System.NullReferenceException' should not be thrown by user code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF8Fb5VpPISvuiaZ&open=AZ8PHF8Fb5VpPISvuiaZ&pullRequest=136

var moveSpeed = 2.5f * (float) deltaTime;

Expand All @@ -118,10 +118,10 @@
private static void OnRender(double deltaTime)
{
if (Gl == null)
throw new NullReferenceException("OpenGL context is not initialized.");

Check warning on line 121 in Examples/Tutorial 4.1 - Model Loading/Program.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'System.NullReferenceException' should not be thrown by user code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF8Fb5VpPISvuiae&open=AZ8PHF8Fb5VpPISvuiae&pullRequest=136

if (window == null)
throw new NullReferenceException("Window is not initialized.");

Check warning on line 124 in Examples/Tutorial 4.1 - Model Loading/Program.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'System.NullReferenceException' should not be thrown by user code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF8Fb5VpPISvuiaf&open=AZ8PHF8Fb5VpPISvuiaf&pullRequest=136

Gl.Enable(EnableCap.DepthTest);
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Expand Down Expand Up @@ -150,13 +150,13 @@
private static void RenderModel(Model model, Matrix4x4 modelMatrix, Matrix4x4 view, Matrix4x4 projection)
{
if (Texture == null)
throw new NullReferenceException("Texture is not initialized.");

Check warning on line 153 in Examples/Tutorial 4.1 - Model Loading/Program.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'System.NullReferenceException' should not be thrown by user code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF8Fb5VpPISvuiab&open=AZ8PHF8Fb5VpPISvuiab&pullRequest=136

if (Shader == null)
throw new NullReferenceException("Shader is not initialized.");

Check warning on line 156 in Examples/Tutorial 4.1 - Model Loading/Program.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'System.NullReferenceException' should not be thrown by user code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF8Fb5VpPISvuiac&open=AZ8PHF8Fb5VpPISvuiac&pullRequest=136

if (Gl == null)
throw new NullReferenceException("OpenGL context is not initialized.");

Check warning on line 159 in Examples/Tutorial 4.1 - Model Loading/Program.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'System.NullReferenceException' should not be thrown by user code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF8Fb5VpPISvuiad&open=AZ8PHF8Fb5VpPISvuiad&pullRequest=136

foreach (var mesh in model.Meshes)
{
Expand Down
18 changes: 9 additions & 9 deletions ObjLoader/Loaders/MaterialLoader/MaterialLibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ public MaterialLibraryLoader(IMaterialDataStore dataStore, IFileStreamFactory fi

AddParseAction("illum", i => _currentMaterial.IlluminationModel = i.ParseInvariantInt());

AddParseAction("map_Ka", m => _currentMaterial.AmbientTextureMap = m);
AddParseAction("map_Kd", m => _currentMaterial.DiffuseTextureMap = m);
AddParseAction("map_Ka", m => _currentMaterial.AmbientTextureMap = new TextureDto(m));
AddParseAction("map_Kd", m => _currentMaterial.DiffuseMap = new TextureDto(m));

AddParseAction("map_Ks", m => _currentMaterial.SpecularTextureMap = m);
AddParseAction("map_Ns", m => _currentMaterial.SpecularHighlightTextureMap = m);
AddParseAction("map_Ks", m => _currentMaterial.SpecularMap = new TextureDto(m));
AddParseAction("map_Ns", m => _currentMaterial.SpecularHighlightTextureMap = new TextureDto(m));

AddParseAction("map_d", m => _currentMaterial.AlphaTextureMap = m);
AddParseAction("map_d", m => _currentMaterial.AlphaTextureMap = new TextureDto(m));

AddParseAction("map_bump", m => _currentMaterial.BumpMap = m);
AddParseAction("bump", m => _currentMaterial.BumpMap = m);
AddParseAction("map_bump", m => _currentMaterial.BumpMap = new TextureDto(m));
AddParseAction("bump", m => _currentMaterial.BumpMap = new TextureDto(m));

AddParseAction("disp", m => _currentMaterial.DisplacementMap = m);
AddParseAction("disp", m => _currentMaterial.DisplacementMap = new TextureDto(m));

AddParseAction("decal", m => _currentMaterial.StencilDecalMap = m);
AddParseAction("decal", m => _currentMaterial.StencilDecalMap = new TextureDto(m));
}

private void AddParseAction(string key, Action<string> action)
Expand Down
119 changes: 50 additions & 69 deletions SharpEngine.Core.Components/Properties/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,68 @@

namespace SharpEngine.Core.Components.Properties;

public record TextureDto
{
public TextureDto(string path, Texture? texture = null)
{
Path = path;
Texture = texture;
}

public string Path { get; set; }
public Texture? Texture { get; set; }
}

public enum TextureUnitIndex : int

Check warning on line 20 in SharpEngine.Core.Components/Properties/Material.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'int' should not be explicitly used as the underlying type.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF80b5VpPISvuiag&open=AZ8PHF80b5VpPISvuiag&pullRequest=136
{
/// <summary>The texture unit for the diffuse map.</summary>
DIFFUSE_UNIT = 0,

/// <summary>The texture unit for the specular map.</summary>
SPECULAR_UNIT = 1
}

/// <summary>
/// Represents the material rendered onto a game object.
/// </summary>
public class Material : ICloneable, IEquatable<Material>
public class Material
{
/// <summary>
/// Initializes a new instance of <see cref="Material"/>.
/// </summary>
/// <param name="materialName">The name assigned to the new material.</param>
public Material(string materialName)
: this(materialName, diffuseMap: null, specularMap: null) { }

/// <summary>
/// Initializes a new instance of <see cref="Material"/>.
/// </summary>
/// <param name="materialName">The name assigned to the new material.</param>
/// <param name="diffuseMap">The diffuse map texture of the material.</param>
/// <param name="specularMap">The specular map texture of the material. Defaults to the diffuse map if not provided.</param>
public Material(string materialName, Texture? diffuseMap = null, Texture? specularMap = null) : this(materialName)
public Material(string materialName, Texture? diffuseMap = null, Texture? specularMap = null)

Check failure on line 47 in SharpEngine.Core.Components/Properties/Material.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This method signature overlaps the one defined on line 38, the default parameter value can only be used with named arguments.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF80b5VpPISvuiai&open=AZ8PHF80b5VpPISvuiai&pullRequest=136
{
DiffuseMap = diffuseMap;
SpecularMap = specularMap ?? diffuseMap;
DiffuseMap = new TextureDto(diffuseMap?.Path ?? string.Empty, diffuseMap);
SpecularMap = new TextureDto(specularMap?.Path ?? string.Empty, specularMap);

Specular = new(0.5f, 0.5f, 0.5f);
Shininess = 32.0f;
}

/// <summary>
/// Initializes a new instance of <see cref="Material"/>.
/// </summary>
/// <param name="materialName">The name assigned to the new material.</param>
public Material(string materialName)
{
Name = materialName;
}

// Resolve dependency issue with GL to fix this.
// public Shader Shader { get; init; }

Check warning on line 59 in SharpEngine.Core.Components/Properties/Material.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=Av3boy_SharpEngine&issues=AZ8PHF80b5VpPISvuiah&open=AZ8PHF80b5VpPISvuiah&pullRequest=136
public Shader Shader { get; set; }

/// <summary>Gets or sets the name of the material.</summary>
public string Name { get; set; }

/// <summary>Gets or sets the diffuse map texture.</summary>
public Texture? DiffuseMap { get; set; }

/// <summary>Gets or sets the path to the diffuse texture map.</summary>
public string? DiffuseTextureMap { get; set; }
public TextureDto? DiffuseMap { get; set; }

/// <summary>Gets or sets the specular map texture.</summary>
public Texture? SpecularMap { get; set; }

/// <summary>Gets or sets the path to the specular texture map.</summary>
public string? SpecularTextureMap { get; set; }

/// <summary>Gets a value indicating whether the material uses a specular map.</summary>
public bool UseSpecularMap => SpecularMap is not null && SpecularMap?.Handle != DiffuseMap?.Handle;

/// <summary>The texture unit for the diffuse map.</summary>
public const int DIFFUSE_UNIT = 0;

/// <summary>The texture unit for the specular map.</summary>
public const int SPECULAR_UNIT = 1;
public TextureDto? SpecularMap { get; set; }

/// <summary>Gets or sets the specular color of the material.</summary>
public Vector3 Specular { get; set; }
Expand All @@ -83,22 +93,22 @@
public int IlluminationModel { get; set; }

/// <summary>Gets or sets the path to the ambient texture map.</summary>
public string? AmbientTextureMap { get; set; }
public TextureDto? AmbientTextureMap { get; set; }

/// <summary>Gets or sets the path to the specular highlight texture map.</summary>
public string? SpecularHighlightTextureMap { get; set; }
public TextureDto? SpecularHighlightTextureMap { get; set; }

/// <summary>Gets or sets the path to the bump map.</summary>
public string? BumpMap { get; set; }
public TextureDto? BumpMap { get; set; }

/// <summary>Gets or sets the path to the displacement map.</summary>
public string? DisplacementMap { get; set; }
public TextureDto? DisplacementMap { get; set; }

/// <summary>Gets or sets the path to the stencil decal map.</summary>
public string? StencilDecalMap { get; set; }
public TextureDto? StencilDecalMap { get; set; }

/// <summary>Gets or sets the path to the alpha texture map.</summary>
public string? AlphaTextureMap { get; set; }
public TextureDto? AlphaTextureMap { get; set; }

/// <summary>
/// Sets the uniform values for the material in the specified shader.
Expand All @@ -110,48 +120,19 @@

if (DiffuseMap is not null)
{
DiffuseMap.Use(TextureUnit.Texture0);
shader.SetInt("material.diffuse", DIFFUSE_UNIT);
DiffuseMap.Texture?.Use(TextureUnit.Texture0);
shader.SetTextureUnit("material.diffuse", TextureUnitIndex.DIFFUSE_UNIT);
}

if (SpecularMap is not null)
{
SpecularMap.Use(TextureUnit.Texture1);
shader.SetInt("material.specular", SPECULAR_UNIT);
shader.SetFloat("material.shininess", Shininess);
SpecularMap.Texture?.Use(TextureUnit.Texture1);
shader.SetTextureUnit("material.specular", TextureUnitIndex.SPECULAR_UNIT);
}
else
{
shader.SetInt("material.specular", DIFFUSE_UNIT);
shader.SetFloat("material.shininess", 0);
}
}

/// <inheritdoc />
public object Clone()
=> MemberwiseClone();

/// <inheritdoc />
public override bool Equals(object? obj)
{
if (obj is null)
return false;
shader.SetTextureUnit("material.specular", TextureUnitIndex.DIFFUSE_UNIT);

if (ReferenceEquals(this, obj))
return true;
shader.SetFloat("material.shininess", Shininess);

if (GetType() != obj.GetType())
return false;

return true;
}

/// <inheritdoc />
public bool Equals(Material? other) => Equals(other);

/// <inheritdoc />
public override int GetHashCode()
{
throw new NotImplementedException();
}
}
37 changes: 37 additions & 0 deletions SharpEngine.Core.Components/Properties/Meshes/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ public Mesh(GL gl, float[] vertices, uint[] indices, List<Texture> textures)
SetupMesh();
}

/// <summary>
/// Initializes a new instance of the <see cref="Mesh"/> class with the specified OpenGL context, interleaved vertices, and indices.
/// </summary>
/// <param name="gl">The OpenGL context.</param>
/// <param name="vertices">The interleaved vertex data (position, normal, uv per vertex).</param>
/// <param name="indices">The indices of the mesh.</param>
public Mesh(GL gl, float[] vertices, uint[] indices)
{
GL = gl;
Vertices = vertices;
Indices = indices;
SetupMesh();
}

/// <summary>
/// Initializes a new instance of the <see cref="Mesh"/> class with the specified OpenGL context.
/// </summary>
Expand All @@ -141,6 +155,21 @@ public Mesh(GL gl)
SetupMesh();
}

/// <summary>
/// Disposes the existing GPU buffers and reinitializes them using the current <see cref="Vertices"/> and <see cref="Indices"/> data.
/// </summary>
/// <remarks>
/// Call this method after updating <see cref="Vertices"/> or <see cref="Indices"/> on a mesh that was already
/// set up, for example after in-place processing via <see cref="SharpEngine.Core.Components.Properties.Meshes.Model.ProcessMesh"/>.
/// </remarks>
public void ReinitializeGpuBuffers()
{
VAO.Dispose();
VBO.Dispose();
EBO.Dispose();
SetupMesh();
}

/// <summary>
/// Allocates the required memory for the mesh and sets up the Vertex Array Object (VAO), Vertex Buffer Object (VBO), and Element Buffer Object (EBO).
/// </summary>
Expand All @@ -166,6 +195,14 @@ public void SetupMesh()
public void Bind()
=> VAO.Bind();

public void Draw()
{
if (Indices.Length > 0)
GL.DrawElements<uint>(PrimitiveType.Triangles, (uint)Indices.Length, DrawElementsType.UnsignedInt, []);
else
GL.DrawArrays(PrimitiveType.Triangles, 0, (uint)(Vertices.Length / (VertexData.VerticesSize + VertexData.NormalsSize + VertexData.TexCoordsSize)));
}

/// <inheritdoc />
protected virtual void Dispose(bool disposing)
{
Expand Down
Loading