From 0e96615b329676db7fd83accda8ef818477caf53 Mon Sep 17 00:00:00 2001 From: Troispoils Date: Wed, 28 Feb 2024 17:07:45 +0100 Subject: [PATCH] Add Normal in vertex. And add light in shader. --- .../AtlasMaker/AtlasBuilder.cs | 10 +---- .../AtlasMaker/TerrainAtlasManager.cs | 38 ++++++++++++++---- .../AtlasMaker/TerrainInfo/SubTerrain.cs | 15 +++++++ .../AtlasMaker/TerrainInfo/Terrain.cs | 22 +++++++++++ .../AtlasMaker/TexturesImage.cs | 27 ++++++------- LandblockExtraction/DatEngine/DatEngine.cs | 5 --- .../DatEngine/TextureEngine.cs | 6 +-- .../LandBlockExtractor/LandBlockExtrator.cs | 25 ++++++++---- .../Utilities/BlockStruct.cs | 6 +-- .../Utilities/VerticesStruct.cs | 39 +++++++++++-------- LandblockExtraction/Tools/MathOperations.cs | 17 ++++++++ Map3DRendering/MapRender.cs | 16 +++++--- Map3DRendering/Shaders/shader.frag | 11 +++++- Map3DRendering/Shaders/shader.vert | 13 ++++--- Map3DRendering/Window.cs | 29 ++++++++++++-- .../AtlasMaker/TerrainInfo/TerrainTests.cs | 27 +++++++++++++ 16 files changed, 221 insertions(+), 85 deletions(-) create mode 100644 LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs create mode 100644 LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs create mode 100644 Test_LandblockExtraction/AtlasMaker/TerrainInfo/TerrainTests.cs diff --git a/LandblockExtraction/AtlasMaker/AtlasBuilder.cs b/LandblockExtraction/AtlasMaker/AtlasBuilder.cs index b4547e5..03a719a 100644 --- a/LandblockExtraction/AtlasMaker/AtlasBuilder.cs +++ b/LandblockExtraction/AtlasMaker/AtlasBuilder.cs @@ -1,12 +1,6 @@ using AC2RE.Definitions; using ImageMagick; using LandblockExtraction.DatEngine; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using System.Text; -using System.Threading.Tasks; namespace LandblockExtraction.AtlasMaker; public class AtlasBuilder : IDisposable { @@ -21,7 +15,7 @@ public class AtlasBuilder : IDisposable { public bool AddTexture(int index, DataId matId) { var img = texturesImage.GetImage(matId); - if(img == null) return false; + if (img == null) return false; textures.Add(index, img); return true; } @@ -41,7 +35,7 @@ public class AtlasBuilder : IDisposable { int x = (index % count) * (int)TEXTURESIZE; int y = (index / count) * (int)TEXTURESIZE; atlas.Composite(kvp.Value, x, y); - + index++; if (index >= count * count) break; } diff --git a/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs b/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs index 8964ecb..a2fbffe 100644 --- a/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs +++ b/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs @@ -1,13 +1,7 @@ using AC2RE.Definitions; using ImageMagick; using LandblockExtraction.DatEngine; -using System; -using System.Collections.Generic; -using System.Linq; using System.Numerics; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; namespace LandblockExtraction.AtlasMaker; public class TerrainAtlasManager { @@ -15,10 +9,14 @@ public class TerrainAtlasManager { public Dictionary textureCoord; public Dictionary terrainTexture; + public Dictionary terrains; public TerrainAtlasManager(PortalEngine portalEngine) { this.portalEngine = portalEngine; textureCoord = new Dictionary(); terrainTexture = new Dictionary(); + terrains = new Dictionary(); + + testGenerateList(); } public void ExtractTexture() { using (var atlasBuilder = new AtlasBuilder(portalEngine)) { @@ -26,22 +24,46 @@ public class TerrainAtlasManager { foreach (var surface in portalEngine.cSurfaceDesc.surfaces) { if (surface.surfIndex == terrain.surfaceInfo) { atlasBuilder.AddTexture((int)terrain.terrainIndex, surface.terrainMaterials.First().baseMaterials.First().materialDid); + //terrains.Add((int)terrain.terrainIndex, GenerateSubTerrain((int)terrain.terrainIndex, surface.terrainMaterials)); } } textureCoord.Add((int)terrain.terrainIndex, Vector2.Zero); } atlasBuilder.GenerateAtlas(); - foreach(var img in atlasBuilder.textures) { + foreach (var img in atlasBuilder.textures) { terrainTexture.Add(img.Key, img.Value); } } } + /*private Terrain GenerateSubTerrain(int index, List materialGroups) { + Terrain tmpTerrain = new Terrain(index); + foreach(var material in materialGroups) { + int indexTer = GetIndexBySurface(material.s) + SubTerrain subTerrain = new(ma) + } + }*/ + private void testGenerateList() { + foreach(var surface in portalEngine.cSurfaceDesc.surfaces) { + foreach(var terrain in portalEngine.cTerrainDesc.terrains) { + if(surface.surfIndex == terrain.surfaceInfo) { + Console.WriteLine($"[{terrain.terrainIndex}:{terrain.terrainName}]\t- Surface: {surface.surfIndex:X8} | Base: {surface.terrainMaterials.First().baseMaterials.First().materialDid}"); + } + } + } + } + private int? GetIndexBySurface(uint surfaceIndex) { + foreach(var terrain in portalEngine.cTerrainDesc.terrains) { + if (terrain.surfaceInfo == surfaceIndex) + return (int)terrain.terrainIndex; + } + return null; + } public void GenerateUV() { int count = (int)Math.Ceiling(Math.Sqrt(textureCoord.Count)); int atlasSize = (int)64 * count; int index = 0; - foreach(var terrain in textureCoord) { + foreach (var terrain in textureCoord) { int x = (index % count) * (int)64; int y = (index / count) * (int)64; textureCoord[index] = new Vector2(x, y); diff --git a/LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs b/LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs new file mode 100644 index 0000000..65c5457 --- /dev/null +++ b/LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs @@ -0,0 +1,15 @@ +namespace LandblockExtraction.AtlasMaker; + +public class SubTerrain { + public int terrainIndex { get; set; } + public float minPitch { get; set; } + public float maxPitch { get; set; } + public SubTerrain(int terrainIndex, float minPitch, float maxPitch) { + this.terrainIndex = terrainIndex; + this.minPitch = minPitch; + this.maxPitch = maxPitch; + } + public bool MatchesPitch(float pitch) { + return pitch >= minPitch && pitch <= maxPitch; + } +} \ No newline at end of file diff --git a/LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs b/LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs new file mode 100644 index 0000000..8e6c097 --- /dev/null +++ b/LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs @@ -0,0 +1,22 @@ +namespace LandblockExtraction.AtlasMaker; +public class Terrain { + public int terrainIndex { get; set; } + public List subTerrains { get; set; } + + public Terrain(int index) { + terrainIndex = index; + subTerrains = new List(); + } + public void AddSubTerrain(SubTerrain sousTerrain) { + subTerrains.Add(sousTerrain); + } + + public int DetermineSubTerrain(float pitch) { + foreach (var terrain in subTerrains) { + if (terrain.MatchesPitch(pitch)) { + return terrain.terrainIndex; + } + } + return terrainIndex; + } +} diff --git a/LandblockExtraction/AtlasMaker/TexturesImage.cs b/LandblockExtraction/AtlasMaker/TexturesImage.cs index 7e16d37..18ca924 100644 --- a/LandblockExtraction/AtlasMaker/TexturesImage.cs +++ b/LandblockExtraction/AtlasMaker/TexturesImage.cs @@ -2,11 +2,6 @@ using ImageMagick; using LandblockExtraction.DatEngine; using LandblockExtraction.Tools; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace LandblockExtraction.AtlasMaker; public class TexturesImage { @@ -19,10 +14,10 @@ public class TexturesImage { } public MagickImage? GetImage(DataId matId) { - if(!portalEngine.datReader.contains(matId)) return null; - using(var data = portalEngine.datReader.getFileReader(matId)) { + if (!portalEngine.datReader.contains(matId)) return null; + using (var data = portalEngine.datReader.getFileReader(matId)) { var materialLayer = new RenderMaterial(data); - if(materialLayer == null) return null; + if (materialLayer == null) return null; return ImageInMaterial(materialLayer); } @@ -30,10 +25,10 @@ public class TexturesImage { private MagickImage? ImageInMaterial(RenderMaterial materialLayer) { var vdescId = materialLayer.layers.First().stages.First().textureID; - if(!portalEngine.datReader.contains(new(vdescId))) return null; - using(var data = portalEngine.datReader.getFileReader(new(vdescId))) { + if (!portalEngine.datReader.contains(new(vdescId))) return null; + using (var data = portalEngine.datReader.getFileReader(new(vdescId))) { var texture = new RenderTexture(data); - if(texture == null) return null; + if (texture == null) return null; return TextureInRender(texture); } @@ -41,13 +36,13 @@ public class TexturesImage { private MagickImage? TextureInRender(RenderTexture texture) { MagickImage magickImage; - foreach(var img in texture.levelSurfaceDids) { - if(!portalEngine.datReader.contains(img)) continue; - using(var data = portalEngine.datReader.getFileReader(img)) { + foreach (var img in texture.levelSurfaceDids) { + if (!portalEngine.datReader.contains(img)) continue; + using (var data = portalEngine.datReader.getFileReader(img)) { var image = new RenderSurface(data); - if(image.width != 64) continue; + if (image.width != 64) continue; var dataImg = DDSHeader.Generate(image); - using(MagickImage realImg = new MagickImage(dataImg)) { + using (MagickImage realImg = new MagickImage(dataImg)) { magickImage = new(realImg); } return magickImage; diff --git a/LandblockExtraction/DatEngine/DatEngine.cs b/LandblockExtraction/DatEngine/DatEngine.cs index 37646cf..f53314d 100644 --- a/LandblockExtraction/DatEngine/DatEngine.cs +++ b/LandblockExtraction/DatEngine/DatEngine.cs @@ -1,9 +1,4 @@ using AC2RE.Definitions; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace LandblockExtraction.DatEngine; public class DatEngine { diff --git a/LandblockExtraction/DatEngine/TextureEngine.cs b/LandblockExtraction/DatEngine/TextureEngine.cs index 2488b48..8ecb98b 100644 --- a/LandblockExtraction/DatEngine/TextureEngine.cs +++ b/LandblockExtraction/DatEngine/TextureEngine.cs @@ -1,9 +1,7 @@ -using AC2RE.Definitions; - -namespace LandblockExtraction.DatEngine { +namespace LandblockExtraction.DatEngine { public class TextureEngine : DatEngine { public TextureEngine() : base(@"X:\DatFiles\highres.dat") { - + } } } diff --git a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs index 26b22fc..d6597d3 100644 --- a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs +++ b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs @@ -2,7 +2,6 @@ using LandblockExtraction.AtlasMaker; using LandblockExtraction.DatEngine; using LandblockExtraction.Tools; -using System.Dynamic; using System.Numerics; namespace LandblockExtraction.LandBlockExtractor { @@ -25,11 +24,11 @@ namespace LandblockExtraction.LandBlockExtractor { } public BlockStruct? GetBlock(int landX, int landY) { - CellId landBlockId = new CellId((byte)landX, (byte)landY, 0xFF, 0xFF); - var landBlock = cellEngine.GetLandBlockData(landBlockId.id); - if (landBlock == null) return null; + CellId landBlockId = new CellId((byte)landX, (byte)landY, 0xFF, 0xFF); + var landBlock = cellEngine.GetLandBlockData(landBlockId.id); + if (landBlock == null) return null; - return GenerateBlockStructByData(landBlock, landX, landY); + return GenerateBlockStructByData(landBlock, landX, landY); } public BlockStruct GenerateBlockStructByData(CLandBlockData blockData, int landX, int landY) { @@ -120,6 +119,7 @@ namespace LandblockExtraction.LandBlockExtractor { //TEST public void DoubleEdgeVertices(BlockStruct blockStruct) { List newPositions = new List(); + List newNormals = new List(); List newColors = new List(); List newFarColors = new List(); List newTexCoord = new List(); @@ -131,7 +131,7 @@ namespace LandblockExtraction.LandBlockExtractor { // Doubler les sommets sur le bord supérieur et inférieur - for(int i = 0; i < originalIndicesCount; i = i + 6) { + for (int i = 0; i < originalIndicesCount; i = i + 6) { var one = blockStruct.indices[i + 0]; var two = blockStruct.indices[i + 1]; @@ -179,11 +179,22 @@ namespace LandblockExtraction.LandBlockExtractor { newTexCoord.Add(new(0, 1)); newTexCoord.Add(new(1, 0)); newTexCoord.Add(new(1, 1)); + + var normal = MathOperations.CalculateQuadNormal(blockStruct.verticesStruct.position[one], + blockStruct.verticesStruct.position[two], + blockStruct.verticesStruct.position[three], + blockStruct.verticesStruct.position[foor]); + + newNormals.Add(normal); + newNormals.Add(normal); + newNormals.Add(normal); + newNormals.Add(normal); } // Ajouter les nouveaux sommets à la structure BlockStruct (étape 2) // Vous devez également mettre à jour les indices dans blockStruct.indices blockStruct.verticesStruct.position = newPositions.ToArray(); + blockStruct.verticesStruct.normal = newNormals.ToArray(); blockStruct.verticesStruct.color = newColors.ToArray(); blockStruct.verticesStruct.farcolor = newFarColors.ToArray(); blockStruct.verticesStruct.texturecoord = newTexCoord.ToArray(); @@ -195,7 +206,7 @@ namespace LandblockExtraction.LandBlockExtractor { private int[] GenerateNewsIndices(int count) { List indices = new List(); - for(int i = 0; i < count; i = i + 4) { + for (int i = 0; i < count; i = i + 4) { indices.Add(i); indices.Add(i + 1); indices.Add(i + 2); diff --git a/LandblockExtraction/LandBlockExtractor/Utilities/BlockStruct.cs b/LandblockExtraction/LandBlockExtractor/Utilities/BlockStruct.cs index e329c48..062a40d 100644 --- a/LandblockExtraction/LandBlockExtractor/Utilities/BlockStruct.cs +++ b/LandblockExtraction/LandBlockExtractor/Utilities/BlockStruct.cs @@ -1,8 +1,4 @@ - -using System; -using System.Numerics; - -namespace LandblockExtraction.LandBlockExtractor { +namespace LandblockExtraction.LandBlockExtractor { public class BlockStruct { public readonly static int BlockSize = 17; diff --git a/LandblockExtraction/LandBlockExtractor/Utilities/VerticesStruct.cs b/LandblockExtraction/LandBlockExtractor/Utilities/VerticesStruct.cs index cf02b41..938ab64 100644 --- a/LandblockExtraction/LandBlockExtractor/Utilities/VerticesStruct.cs +++ b/LandblockExtraction/LandBlockExtractor/Utilities/VerticesStruct.cs @@ -3,6 +3,7 @@ namespace LandblockExtraction.LandBlockExtractor { public class VerticesStruct { public Vector3[] position { get; set; } + public Vector3[] normal { get; set; } public Vector4[] color { get; set; } public Vector4[] farcolor { get; set; } public Vector2[] texturecoord { get; set; } @@ -11,6 +12,7 @@ namespace LandblockExtraction.LandBlockExtractor { public VerticesStruct(int blockSize) { position = new Vector3[blockSize * blockSize]; + normal = new Vector3[blockSize * blockSize]; color = new Vector4[blockSize * blockSize]; farcolor = new Vector4[blockSize * blockSize]; texturecoord = new Vector2[blockSize * blockSize]; @@ -20,26 +22,29 @@ namespace LandblockExtraction.LandBlockExtractor { public float[] Vertices() { int length = position.Length; - float[] vertices = new float[length * 18]; // 3 pour position, 4 pour color, et 4 pour farcolor - for (int i = 0, vi = 0; i < length; i++, vi += 18) { + float[] vertices = new float[length * 21]; // 3 pour position, 4 pour color, et 4 pour farcolor + for (int i = 0, vi = 0; i < length; i++, vi += 21) { vertices[vi] = position[i].X; vertices[vi + 1] = position[i].Y; vertices[vi + 2] = position[i].Z; - vertices[vi + 3] = color[i].X; - vertices[vi + 4] = color[i].Y; - vertices[vi + 5] = color[i].Z; - vertices[vi + 6] = color[i].W; - vertices[vi + 7] = farcolor[i].X; - vertices[vi + 8] = farcolor[i].Y; - vertices[vi + 9] = farcolor[i].Z; - vertices[vi + 10] = farcolor[i].W; - vertices[vi + 11] = texturecoord[i].X; - vertices[vi + 12] = texturecoord[i].Y; - vertices[vi + 13] = terraintype[i].X; - vertices[vi + 14] = terraintype[i].Y; - vertices[vi + 15] = terraintype[i].Z; - vertices[vi + 16] = terraintype[i].W; - vertices[vi + 17] = realtype[i]; + vertices[vi + 3] = normal[i].X; + vertices[vi + 4] = normal[i].Y; + vertices[vi + 5] = normal[i].Z; + vertices[vi + 6] = color[i].X; + vertices[vi + 7] = color[i].Y; + vertices[vi + 8] = color[i].Z; + vertices[vi + 9] = color[i].W; + vertices[vi + 10] = farcolor[i].X; + vertices[vi + 11] = farcolor[i].Y; + vertices[vi + 12] = farcolor[i].Z; + vertices[vi + 13] = farcolor[i].W; + vertices[vi + 14] = texturecoord[i].X; + vertices[vi + 15] = texturecoord[i].Y; + vertices[vi + 16] = terraintype[i].X; + vertices[vi + 17] = terraintype[i].Y; + vertices[vi + 18] = terraintype[i].Z; + vertices[vi + 19] = terraintype[i].W; + vertices[vi + 20] = realtype[i]; } return vertices; } diff --git a/LandblockExtraction/Tools/MathOperations.cs b/LandblockExtraction/Tools/MathOperations.cs index f72301a..71f5064 100644 --- a/LandblockExtraction/Tools/MathOperations.cs +++ b/LandblockExtraction/Tools/MathOperations.cs @@ -24,5 +24,22 @@ namespace LandblockExtraction.Tools { public static Vector4 RGBAColorToVector4(RGBAColor color) { return new Vector4(color.r, color.g, color.b, color.a); } + // Calcule la normale d'un triangle à partir de trois points + public static Vector3 CalculateTriangleNormal(Vector3 A, Vector3 B, Vector3 C) { + Vector3 AB = B - A; + Vector3 AC = C - A; + Vector3 normal = Vector3.Cross(AB, AC); + normal = Vector3.Normalize(normal); + return normal; + } + + // Calcule la normale moyenne d'un quadrilatère en utilisant deux de ses triangles + public static Vector3 CalculateQuadNormal(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + Vector3 normal1 = CalculateTriangleNormal(A, B, C); + Vector3 normal2 = CalculateTriangleNormal(C, B, D); + + Vector3 averageNormal = (normal1 + normal2) / 2; + return Vector3.Normalize(averageNormal); + } } } diff --git a/Map3DRendering/MapRender.cs b/Map3DRendering/MapRender.cs index 25c15e9..769fd34 100644 --- a/Map3DRendering/MapRender.cs +++ b/Map3DRendering/MapRender.cs @@ -70,7 +70,7 @@ namespace Map3DRendering { } } private void InitializeBlock(int x, int y, BlockStruct block, Shader _shader) { - int lenghPacket = 18; + int lenghPacket = 21; // Initialisez le VAO, VBO et EBO pour le bloc à (x, y)... // Utilisez le code de votre méthode OnLoad originale pour configurer le VAO, VBO et EBO. int tempVertexArray = GL.GenVertexArray(); @@ -93,25 +93,29 @@ namespace Map3DRendering { GL.EnableVertexAttribArray(vertexLocation); GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 0); + var vertexNormal = _shader.GetAttribLocation("aNormal"); + GL.EnableVertexAttribArray(vertexNormal); + GL.VertexAttribPointer(vertexNormal, 3, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 3 * sizeof(float)); + var colorLocation = _shader.GetAttribLocation("aColor"); GL.EnableVertexAttribArray(colorLocation); - GL.VertexAttribPointer(colorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 3 * sizeof(float)); + GL.VertexAttribPointer(colorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 6 * sizeof(float)); var farcolorLocation = _shader.GetAttribLocation("aColorFar"); GL.EnableVertexAttribArray(farcolorLocation); - GL.VertexAttribPointer(farcolorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 7 * sizeof(float)); + GL.VertexAttribPointer(farcolorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 10 * sizeof(float)); var texturecoordLocation = _shader.GetAttribLocation("aTexCoord"); GL.EnableVertexAttribArray(texturecoordLocation); - GL.VertexAttribPointer(texturecoordLocation, 2, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 11 * sizeof(float)); + GL.VertexAttribPointer(texturecoordLocation, 2, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 14 * sizeof(float)); var terraintypeLocation = _shader.GetAttribLocation("aTexType"); GL.EnableVertexAttribArray(terraintypeLocation); - GL.VertexAttribPointer(terraintypeLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 13 * sizeof(float)); + GL.VertexAttribPointer(terraintypeLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 16 * sizeof(float)); var realterraintypeLocation = _shader.GetAttribLocation("aRealTexType"); GL.EnableVertexAttribArray(realterraintypeLocation); - GL.VertexAttribPointer(realterraintypeLocation, 1, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 13 * sizeof(float)); + GL.VertexAttribPointer(realterraintypeLocation, 1, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 20 * sizeof(float)); } public void Render(Shader shader) { for (int y = startY; y <= endY; y++) { diff --git a/Map3DRendering/Shaders/shader.frag b/Map3DRendering/Shaders/shader.frag index f34e6ec..0e12667 100644 --- a/Map3DRendering/Shaders/shader.frag +++ b/Map3DRendering/Shaders/shader.frag @@ -3,9 +3,12 @@ out vec4 outputColor; uniform vec3 viewPos; +uniform vec3 lightPos; +uniform vec3 lightColor; uniform sampler2D texture0; in vec4 FarColor; +in vec3 Normal; in vec3 FragPos; in vec2 TexCoord; in vec4 TexType; @@ -41,8 +44,14 @@ void main() vec4 mix2 = mix(blendedColor[1], blendedColor[3], weightX); vec4 finalColor = mix(mix1, mix2, weightY); + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(lightPos - FragPos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = diff * lightColor; + + vec4 litColor = vec4(diffuse, 1.0) * finalColor; float distance = length(viewPos - FragPos); float interpolationFactor = clamp(distance / 1000.0, 0.0, 1.0); - outputColor = mix(finalColor, FarColor, interpolationFactor); + outputColor = mix(litColor, FarColor, interpolationFactor); } \ No newline at end of file diff --git a/Map3DRendering/Shaders/shader.vert b/Map3DRendering/Shaders/shader.vert index 1e03adc..7f31960 100644 --- a/Map3DRendering/Shaders/shader.vert +++ b/Map3DRendering/Shaders/shader.vert @@ -1,16 +1,18 @@ #version 330 core layout (location = 0) in vec3 aPos; -layout (location = 1) in vec4 aColor; -layout (location = 2) in vec4 aColorFar; -layout (location = 3) in vec2 aTexCoord; -layout (location = 4) in vec4 aTexType; -layout (location = 5) in float aRealTexType; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec4 aColor; +layout (location = 3) in vec4 aColorFar; +layout (location = 4) in vec2 aTexCoord; +layout (location = 5) in vec4 aTexType; +layout (location = 6) in float aRealTexType; uniform mat4 model; uniform mat4 view; uniform mat4 projection; out vec3 FragPos; +out vec3 Normal; out vec4 Color; out vec4 FarColor; out vec2 TexCoord; @@ -21,6 +23,7 @@ void main() { gl_Position = vec4(aPos, 1.0) * model * view * projection; FragPos = vec3(vec4(aPos, 1.0) * model); + Normal = aNormal; Color = aColor; FarColor = aColorFar; TexCoord = aTexCoord; diff --git a/Map3DRendering/Window.cs b/Map3DRendering/Window.cs index d5052d7..39ff51c 100644 --- a/Map3DRendering/Window.cs +++ b/Map3DRendering/Window.cs @@ -22,6 +22,7 @@ namespace Map3DRendering { private bool _firstMove = true; private Vector2 _lastPos; + private Vector3 _lightPosVec; private double _time; @@ -44,6 +45,10 @@ namespace Map3DRendering { GL.Enable(EnableCap.DepthTest); + int maxTextureUnits; + GL.GetInteger(GetPName.MaxTextureImageUnits, out maxTextureUnits); + Console.WriteLine($"Maximum number of texture units for fragment shaders: {maxTextureUnits}"); + _shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag"); _shader.Use(); @@ -58,6 +63,7 @@ namespace Map3DRendering { //CursorState = CursorState.Grabbed; GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); + _lightPosVec = Vector3.UnitY * 1000; } protected override void OnRenderFrame(FrameEventArgs e) { @@ -73,10 +79,10 @@ namespace Map3DRendering { _shader.SetMatrix4("view", _camera.GetViewMatrix()); _shader.SetMatrix4("projection", _camera.GetProjectionMatrix()); - /*_shader.SetVector3("objectColor", new Vector3(0.5f, 0.5f, 0.5f)); + //_shader.SetVector3("objectColor", new Vector3(0.5f, 0.5f, 0.5f)); _shader.SetVector3("lightColor", new Vector3(1.0f, 1.0f, 1.0f)); - _shader.SetVector3("lightPos", _camera.Position); - _shader.SetVector3("viewPos", _camera.Position);*/ + _shader.SetVector3("lightPos", _lightPosVec); + //_shader.SetVector3("viewPos", _camera.Position); GL.LineWidth(5.0f); @@ -103,6 +109,23 @@ namespace Map3DRendering { Close(); } + // Vitesse de déplacement de la lumière + float lightSpeed = 300.0f; // Ajustez cette valeur selon les besoins + + // Mise à jour de la position de la lumière en fonction des entrées utilisateur + if (input.IsKeyDown(Keys.Down)) { + _lightPosVec += Vector3.UnitX * lightSpeed * (float)e.Time; // Déplace la lumière vers le bas + } + if (input.IsKeyDown(Keys.Up)) { + _lightPosVec -= Vector3.UnitX * lightSpeed * (float)e.Time; // Déplace la lumière vers le haut + } + if (input.IsKeyDown(Keys.Left)) { + _lightPosVec -= Vector3.UnitZ * lightSpeed * (float)e.Time; // Déplace la lumière vers la gauche + } + if (input.IsKeyDown(Keys.Right)) { + _lightPosVec += Vector3.UnitZ * lightSpeed * (float)e.Time; // Déplace la lumière vers la droite + } + const float sensitivity = 0.2f; if (input.IsKeyDown(Keys.W)) { diff --git a/Test_LandblockExtraction/AtlasMaker/TerrainInfo/TerrainTests.cs b/Test_LandblockExtraction/AtlasMaker/TerrainInfo/TerrainTests.cs new file mode 100644 index 0000000..8e07b77 --- /dev/null +++ b/Test_LandblockExtraction/AtlasMaker/TerrainInfo/TerrainTests.cs @@ -0,0 +1,27 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using LandblockExtraction.AtlasMaker; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LandblockExtraction.AtlasMaker.Tests; + +[TestClass()] +public class TerrainTests { + [TestMethod()] + public void TerrainTest() { + Assert.Fail(); + } + + [TestMethod()] + public void AddSubTerrainTest() { + Assert.Fail(); + } + + [TestMethod()] + public void DetermineSubTerrainTest() { + Assert.Fail(); + } +} \ No newline at end of file