fix #4 Sphere can be rendered with normals now. Calculation fo normals is however not working
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Gitea Actions Demo / Scan the project (push) Successful in 17s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Gitea Actions Demo / Scan the project (push) Successful in 17s
				
			This commit is contained in:
		| @ -5,7 +5,8 @@ namespace Nebulix.Rendering | |||||||
| { | { | ||||||
| 	public sealed class Mesh | 	public sealed class Mesh | ||||||
| 	{ | 	{ | ||||||
| 		public Vector3D<float>[] Vertices { get => vertices; set { vertices = value; regenerate = true; } } | 		public Vector3D<float>[] Vertices { get => vertices;  | ||||||
|  | 			set { vertices = value; regenerate = true; normals = new Vector3D<float>[vertices.Length];} } | ||||||
| 		public uint[] Indices { get => indices; set { indices = value; regenerate = true; } } | 		public uint[] Indices { get => indices; set { indices = value; regenerate = true; } } | ||||||
| 		 | 		 | ||||||
| 		private uint vao = 0, vbo = 0, ebo = 0; | 		private uint vao = 0, vbo = 0, ebo = 0; | ||||||
| @ -23,11 +24,11 @@ namespace Nebulix.Rendering | |||||||
|  |  | ||||||
| 		public void CalculateNormals() | 		public void CalculateNormals() | ||||||
| 		{ | 		{ | ||||||
| 			normals = new Vector3D<float>[vertices.Length]; | 			// normals = new Vector3D<float>[vertices.Length]; | ||||||
| 			for (int j = 0; j < vertices.Length; j++) | 			// for (int j = 0; j < vertices.Length; j++) | ||||||
| 			{ | 			// { | ||||||
| 				normals[j] = Vector3D<float>.Zero; | 			// 	normals[j] = Vector3D<float>.Zero; | ||||||
| 			} | 			// } | ||||||
|  |  | ||||||
| 			return; | 			return; | ||||||
| 			for (int i = 0; i < indices.Length; i += 3) | 			for (int i = 0; i < indices.Length; i += 3) | ||||||
| @ -72,12 +73,23 @@ namespace Nebulix.Rendering | |||||||
| 				ebo = gl.GenBuffer(); | 				ebo = gl.GenBuffer(); | ||||||
|  |  | ||||||
| 			float[] meshData = new float[vertices.Length * 3 + normals.Length * 3]; | 			float[] meshData = new float[vertices.Length * 3 + normals.Length * 3]; | ||||||
| 			vertices.ExtractComponents().CopyTo(meshData, 0); | 			for (int i = 0, insert = 0; i < vertices.Length; i++) | ||||||
| 			//meshData.AddRange(normals.ExtractComponents()); | 			{ | ||||||
|  | 				meshData[insert] = vertices[i].X; | ||||||
|  | 				meshData[insert + 1] = vertices[i].Y; | ||||||
|  | 				meshData[insert + 2] = vertices[i].Z; | ||||||
|  | 				 | ||||||
|  | 				meshData[insert + 3] = normals[i].X; | ||||||
|  | 				meshData[insert + 4] = normals[i].Y; | ||||||
|  | 				meshData[insert + 5] = normals[i].Z; | ||||||
|  | 				insert += 6; | ||||||
|  | 			} | ||||||
|  | 			// extractedVertices.CopyTo(meshData, 0); | ||||||
|  | 			// extractedNormals.CopyTo(meshData, extractedVertices.Length); | ||||||
| 			gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo); | 			gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo); | ||||||
| 			fixed(void* data = &meshData[0]) | 			fixed(void* data = &meshData[0]) | ||||||
| 			{ | 			{ | ||||||
| 				gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * 3 * sizeof(float)), data, BufferUsageARB.StaticDraw); | 				gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * sizeof(float)), data, BufferUsageARB.StaticDraw); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo); | 			gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo); | ||||||
| @ -88,10 +100,10 @@ namespace Nebulix.Rendering | |||||||
| 			 | 			 | ||||||
| 			// vertices | 			// vertices | ||||||
| 			gl.EnableVertexAttribArray(0); | 			gl.EnableVertexAttribArray(0); | ||||||
| 			gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), null); | 			gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), null); | ||||||
| 			// normals | 			// normals | ||||||
| 			//gl.EnableVertexAttribArray(1); | 			gl.EnableVertexAttribArray(1); | ||||||
| 			//gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float))); | 			gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float))); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -35,6 +35,11 @@ public class Shader | |||||||
|         _glContext.Uniform1(_glContext.GetUniformLocation(_shaderProgramId, name), value); |         _glContext.Uniform1(_glContext.GetUniformLocation(_shaderProgramId, name), value); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public void SetVector(string name, Vector3D<float> value) | ||||||
|  |     { | ||||||
|  | 	    _glContext.Uniform3(_glContext.GetUniformLocation(_shaderProgramId, name), value.X, value.Y, value.Z); | ||||||
|  |     } | ||||||
|  |      | ||||||
|     public unsafe void SetMatrix(string name, Matrix4x4 matrix) |     public unsafe void SetMatrix(string name, Matrix4x4 matrix) | ||||||
|     { |     { | ||||||
|         _glContext.UniformMatrix4(_glContext.GetUniformLocation(_shaderProgramId, name), 1, false, (float*) &matrix); |         _glContext.UniformMatrix4(_glContext.GetUniformLocation(_shaderProgramId, name), 1, false, (float*) &matrix); | ||||||
|  | |||||||
| @ -14,6 +14,7 @@ public enum MovementDirection | |||||||
|  |  | ||||||
| public class Camera | public class Camera | ||||||
| { | { | ||||||
|  | 	public Vector3D<float> Position => _position; | ||||||
| 	private readonly Vector3D<float> _worldUp; | 	private readonly Vector3D<float> _worldUp; | ||||||
| 	private Vector3D<float> _position; | 	private Vector3D<float> _position; | ||||||
| 	private Vector3D<float> _front; | 	private Vector3D<float> _front; | ||||||
|  | |||||||
| @ -29,7 +29,7 @@ | |||||||
|       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||||||
|     </None> |     </None> | ||||||
|     <None Update="Shaders\Sphere\sphere.frag"> |     <None Update="Shaders\Sphere\sphere.frag"> | ||||||
|       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |       <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||||
|     </None> |     </None> | ||||||
|     <None Update="Shaders\Sphere\sphere.vert"> |     <None Update="Shaders\Sphere\sphere.vert"> | ||||||
|       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||||||
|  | |||||||
| @ -171,7 +171,7 @@ public static class Program | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	private static unsafe void OnRender(double deltaTime) | 	private static void OnRender(double deltaTime) | ||||||
| 	{ | 	{ | ||||||
| 		_gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); | 		_gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); | ||||||
|  |  | ||||||
| @ -199,6 +199,13 @@ public static class Program | |||||||
| 		_sphereShader.SetMatrix("projectionMatrix", projectionMatrix); | 		_sphereShader.SetMatrix("projectionMatrix", projectionMatrix); | ||||||
| 		_sphereShader.SetMatrix("viewMatrix", viewMatrix); | 		_sphereShader.SetMatrix("viewMatrix", viewMatrix); | ||||||
| 		 | 		 | ||||||
|  | 		_sphereShader.SetVector("viewPosition", _cam.Position); | ||||||
|  | 		 | ||||||
|  | 		_sphereShader.SetVector("dirLight.direction", new Vector3D<float>(-0.2f, -1.0f, -0.3f)); | ||||||
|  | 		_sphereShader.SetVector("dirLight.ambient", new Vector3D<float>(0.1f)); | ||||||
|  | 		_sphereShader.SetVector("dirLight.diffuse", new Vector3D<float>(1f)); | ||||||
|  | 		_sphereShader.SetVector("dirLight.specular", new Vector3D<float>(0.5f)); | ||||||
|  |  | ||||||
| 		sphere.RenderSphere(_gl); | 		sphere.RenderSphere(_gl); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | |||||||
| @ -3,10 +3,41 @@ | |||||||
| out vec4 FragColour; | out vec4 FragColour; | ||||||
|  |  | ||||||
| in vec3 FragPos; | in vec3 FragPos; | ||||||
| //in vec3 Normal; | in vec3 Normal; | ||||||
|  |  | ||||||
|  | struct DirectionalLight { | ||||||
|  |     vec3 direction; | ||||||
|  |  | ||||||
|  |     vec3 ambient; | ||||||
|  |     vec3 diffuse; | ||||||
|  |     vec3 specular; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | uniform vec3 viewPosition; | ||||||
|  | uniform DirectionalLight dirLight; | ||||||
|  |  | ||||||
|  | vec3 CalculateDirectionalLight(DirectionalLight light, vec3 normal, vec3 viewDir) | ||||||
|  | { | ||||||
|  |     vec3 lightDir = normalize(-light.direction); | ||||||
|  |  | ||||||
|  |     float diff = max(dot(normal, lightDir), 0.0); | ||||||
|  |  | ||||||
|  |     vec3 reflectionDir = reflect(-lightDir, normal); | ||||||
|  |     float spec = pow(max(dot(viewDir, reflectionDir), 0.0), 32); | ||||||
|  |  | ||||||
|  |     vec3 ambient = light.ambient;// * vec3(texture(material.diffuseMap, TexCoords)); | ||||||
|  |     vec3 diffuse = light.diffuse * diff;// * vec3(texture(material.diffuseMap, TexCoords)); | ||||||
|  |     vec3 specular = light.specular * spec;// * vec3(texture(material.specularMap, TexCoords)); | ||||||
|  |  | ||||||
|  |     return ambient + diffuse + specular; | ||||||
|  | } | ||||||
|  |  | ||||||
| void main() | void main() | ||||||
| { | { | ||||||
|     vec3 col = vec3(1.0, 0.5, 0.2) * FragPos; |     vec3 normal = normalize(Normal); | ||||||
|  |     vec3 viewDir = normalize(viewPosition - FragPos); | ||||||
|  |  | ||||||
|  | //    vec3 col = vec3(1.0, 0.5, 0.2) * FragPos; | ||||||
|  |     vec3 col = CalculateDirectionalLight(dirLight, normal, viewDir); | ||||||
|     FragColour = vec4(col, 1); |     FragColour = vec4(col, 1); | ||||||
| } | } | ||||||
| @ -1,7 +1,7 @@ | |||||||
| #version 330 core | #version 330 core | ||||||
|  |  | ||||||
| layout (location = 0) in vec3 aPosition; | layout (location = 0) in vec3 aPosition; | ||||||
| //layout (location = 1) in vec3 normalVector; | layout (location = 1) in vec3 normalVector; | ||||||
|  |  | ||||||
|  |  | ||||||
| uniform mat4 modelMatrix; | uniform mat4 modelMatrix; | ||||||
| @ -9,13 +9,14 @@ uniform mat4 viewMatrix; | |||||||
| uniform mat4 projectionMatrix; | uniform mat4 projectionMatrix; | ||||||
|  |  | ||||||
| out vec3 FragPos; | out vec3 FragPos; | ||||||
| //out vec3 Normal; | out vec3 Normal; | ||||||
|  |  | ||||||
| void main() | void main() | ||||||
| { | { | ||||||
|     vec4 pos = vec4(aPosition, 1.0); |     vec4 pos = vec4(aPosition, 1.0); | ||||||
|     // TODO: calculate the inverse of the model matrix beforehand since "inverse()" is very costly to calculate for every vertex |     // TODO: calculate the inverse of the model matrix beforehand since "inverse()" is very costly to calculate for every vertex | ||||||
| //    Normal = mat3(transpose(inverse(modelMatrix))) * normalVector; |     Normal = mat3(transpose(inverse(modelMatrix))) * normalVector; | ||||||
|  | //    Normal = vec3(0); | ||||||
|     FragPos = vec3(modelMatrix * pos); |     FragPos = vec3(modelMatrix * pos); | ||||||
|     gl_Position = projectionMatrix * viewMatrix * modelMatrix * pos; |     gl_Position = projectionMatrix * viewMatrix * modelMatrix * pos; | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user