slightly improved sphere generation
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 24s

This commit is contained in:
Daniel 2024-02-01 20:06:53 +01:00
parent 5de28c7d7c
commit 0b0d2360d6
3 changed files with 38 additions and 26 deletions

View File

@ -24,24 +24,26 @@ namespace Nebulix.Rendering
public void CalculateNormals()
{
// normals = new Vector3D<float>[vertices.Length];
// for (int j = 0; j < vertices.Length; j++)
// {
// normals[j] = Vector3D<float>.Zero;
// }
// return;
for (int i = 0; i < indices.Length; i += 3)
{
Vector3D<float> a = vertices[indices[i]];
Vector3D<float> b = vertices[indices[i + 1]];
Vector3D<float> c = vertices[indices[i + 2]];
uint i0 = indices[i];
uint i1 = indices[i+1];
uint i2 = indices[i+2];
Vector3D<float> v0 = vertices[i0];
Vector3D<float> v1 = vertices[i1];
Vector3D<float> v2 = vertices[i2];
Vector3D<float> normal = Vector3D.Cross(b-a, c-a);
Vector3D<float> normal = Vector3D.Cross(v1-v0, v2-v0);
// Commenting this out, will result in the normals being weighted based on the triangle area
normal = Vector3D.Normalize(normal);
normals[indices[i]] = normal;
normals[indices[i + 1]] = normal;
normals[indices[i + 2]] = normal;
normals[i0] += normal;
normals[i1] += normal;
normals[i2] += normal;
}
for (int i = 0; i < normals.Length; i++)
{
normals[i] = Vector3D.Normalize(normals[i]); // smoothing for shared vertices
}
}
@ -84,8 +86,7 @@ namespace Nebulix.Rendering
meshData[insert + 5] = normals[i].Z;
insert += 6;
}
// extractedVertices.CopyTo(meshData, 0);
// extractedNormals.CopyTo(meshData, extractedVertices.Length);
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
fixed(void* data = &meshData[0])
{

View File

@ -57,7 +57,7 @@ vec3 CalculatePointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewD
vec3 ambientLight = light.ambient * attenuation;// * texture(material.diffuseMap, TexCoords).rgb;
vec3 diffuseLight = light.diffuse * diff * attenuation;// * texture(material.diffuseMap, TexCoords).rgb;
vec3 specularLight = light.specular * spec * attenuation;// * texture(material.specularMap, TexCoords).rgb;
vec3 specularLight = vec3(0);//light.specular * spec * attenuation;// * texture(material.specularMap, TexCoords).rgb;
return ambientLight + diffuseLight + specularLight;
}

View File

@ -8,7 +8,7 @@ namespace Engine_silk.NET
// also maybe make an ISphere and call this class "CubeSphere"?
public class Sphere
{
private readonly Face[] sphereFaces = new Face[6];
private readonly Mesh mesh = new();
private readonly uint resolution;
public Sphere(uint resolution)
@ -21,6 +21,7 @@ namespace Engine_silk.NET
public void CreateSphere()
{
// TODO: merge the individual meshes to one mesh
Vector3D<float>[] directions =
[
Vector3D<float>.UnitZ, -Vector3D<float>.UnitZ,
@ -28,11 +29,25 @@ namespace Engine_silk.NET
Vector3D<float>.UnitX, -Vector3D<float>.UnitX
];
for (int i = 0; i < sphereFaces.Length; i++)
List<Vector3D<float>> vertices = new();
List<uint> indices = new();
for (int i = 0; i < directions.Length; i++)
{
sphereFaces[i] = new Face(directions[i], new Mesh(), resolution);
sphereFaces[i].GenerateMesh();
Mesh m = new();
Face f = new(directions[i], m, resolution);
f.GenerateMesh();
vertices.AddRange(m.Vertices);
for (int j = 0; j < m.Indices.Length; j++)
{
indices.Add(m.Indices[j] + (uint)(m.Vertices.Length * i));
}
}
// TODO get rid of overlapping vertices maybe
mesh.Clear();
mesh.Vertices = vertices.ToArray();
mesh.Indices = indices.ToArray();
mesh.CalculateNormals();
}
/// <summary>
@ -40,11 +55,7 @@ namespace Engine_silk.NET
/// </summary>
public void RenderSphere(GL gl) // Will not be needed
{
// Use default shader etc. to render the sphere
for (int i = 0; i < 6; i++)
{
sphereFaces[i].Mesh.Render(gl);
}
mesh.Render(gl);
}
}