From 7c7dddde5e0add0b6e7286c8f6ad04daeaf880ed Mon Sep 17 00:00:00 2001 From: Troispoils Date: Sun, 3 Mar 2024 19:07:02 +0100 Subject: [PATCH] Test not positif --- .../AtlasMaker/TerrainAtlasManager.cs | 12 +- .../LandBlockExtractor/LandBlockExtrator.cs | 36 ++-- LandblockExtraction/Tools/MathOperations.cs | 15 ++ .../WorldMap/VertexInfo/VertexMap.cs | 12 ++ LandblockExtraction/WorldMap/WorldMap.cs | 165 ++++++++++++++++++ Map3DRendering/Common/Camera.cs | 2 +- Map3DRendering/Program.cs | 3 +- Map3DRendering/Window.cs | 6 +- Map3DRendering/WorldMapRender.cs | 67 +++++++ 9 files changed, 286 insertions(+), 32 deletions(-) create mode 100644 LandblockExtraction/WorldMap/VertexInfo/VertexMap.cs create mode 100644 LandblockExtraction/WorldMap/WorldMap.cs create mode 100644 Map3DRendering/WorldMapRender.cs diff --git a/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs b/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs index d62bead..b0c1149 100644 --- a/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs +++ b/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs @@ -46,11 +46,11 @@ public class TerrainAtlasManager { foreach(var surface in portalEngine.cSurfaceDesc.surfaces) { if(terrain.surfaceInfo == surface.surfIndex) { foreach(var mat in surface.terrainMaterials) { - tmpTer.subTerrains.Add(new(GetIndex(mat.baseMaterials.First().materialDid), - mat.minPitch, - mat.maxPitch, - MathOperations.RGBAColorToVector4(mat.vertexColor.First().vertexColor), - MathOperations.RGBAColorToVector4(mat.vertexColor.First().farVertexColor))); + tmpTer.AddSubTerrain(new(GetIndex(mat.baseMaterials.First().materialDid), + mat.minPitch, + mat.maxPitch, + MathOperations.RGBAColorToVector4(mat.vertexColor.First().vertexColor), + MathOperations.RGBAColorToVector4(mat.vertexColor.First().farVertexColor))); } } } @@ -87,7 +87,7 @@ public class TerrainAtlasManager { } private int GetIndex(DataId id) { foreach(var mat in mapTerrain) { - if(mat.Value == id) return mat.Key; + if(mat.Value.id == id.id) return mat.Key; } return 0; } diff --git a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs index 664456f..fa33ee7 100644 --- a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs +++ b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs @@ -65,23 +65,25 @@ namespace LandblockExtraction.LandBlockExtractor { Vector3 vertex4 = blockStruct.verticesStruct.position[index4]; var normal = MathOperations.CalculateQuadNormal(vertex1, vertex2, vertex3, vertex4); + var test = MathOperations.CalculateInclination(normal); - var sub1 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index1].X].DetermineSubTerrain(1 - normal.Y); - var sub2 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index2].X].DetermineSubTerrain(1 - normal.Y); - var sub3 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index3].X].DetermineSubTerrain(1 - normal.Y); - var sub4 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index4].X].DetermineSubTerrain(1 - normal.Y); + var sub1 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index1].X].DetermineSubTerrain(test); + var sub2 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index2].X].DetermineSubTerrain(test); + var sub3 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index3].X].DetermineSubTerrain(test); + var sub4 = terrainAtlasManager.terrains[(int)blockStruct.verticesStruct.terraintype[index4].X].DetermineSubTerrain(test); - InitColorFarColorTerrain(index1, sub1, blockStruct); - InitColorFarColorTerrain(index2, sub2, blockStruct); - InitColorFarColorTerrain(index3, sub3, blockStruct); - InitColorFarColorTerrain(index4, sub4, blockStruct); + InitColorFarColorTerrain(index1, normal, sub1, blockStruct); + InitColorFarColorTerrain(index2, normal, sub2, blockStruct); + InitColorFarColorTerrain(index3, normal, sub3, blockStruct); + InitColorFarColorTerrain(index4, normal, sub4, blockStruct); } } - private void InitColorFarColorTerrain(int index, SubTerrain terrain, BlockStruct blockStruct) { + private void InitColorFarColorTerrain(int index, Vector3 normal, SubTerrain terrain, BlockStruct blockStruct) { blockStruct.verticesStruct.terraintype[index].X = terrain.terrainIndex; blockStruct.verticesStruct.color[index] = terrain.Color; blockStruct.verticesStruct.farcolor[index] = terrain.farColor; + blockStruct.verticesStruct.normal[index] = normal; } @@ -180,42 +182,34 @@ namespace LandblockExtraction.LandBlockExtractor { newPositions.Add(blockStruct.verticesStruct.position[one]); newColors.Add(blockStruct.verticesStruct.color[one]); newFarColors.Add(blockStruct.verticesStruct.farcolor[one]); + newNormals.Add(blockStruct.verticesStruct.normal[one]); newTerrainTypes.Add(terrainType); newPositions.Add(blockStruct.verticesStruct.position[two]); newColors.Add(blockStruct.verticesStruct.color[two]); newFarColors.Add(blockStruct.verticesStruct.farcolor[two]); + newNormals.Add(blockStruct.verticesStruct.normal[two]); newTerrainTypes.Add(terrainType); newPositions.Add(blockStruct.verticesStruct.position[three]); newColors.Add(blockStruct.verticesStruct.color[three]); newFarColors.Add(blockStruct.verticesStruct.farcolor[three]); + newNormals.Add(blockStruct.verticesStruct.normal[three]); newTerrainTypes.Add(terrainType); newPositions.Add(blockStruct.verticesStruct.position[foor]); newColors.Add(blockStruct.verticesStruct.color[foor]); newFarColors.Add(blockStruct.verticesStruct.farcolor[foor]); + newNormals.Add(blockStruct.verticesStruct.normal[foor]); newTerrainTypes.Add(terrainType); newTexCoord.Add(new(0, 0)); 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(); diff --git a/LandblockExtraction/Tools/MathOperations.cs b/LandblockExtraction/Tools/MathOperations.cs index 71f5064..374a04c 100644 --- a/LandblockExtraction/Tools/MathOperations.cs +++ b/LandblockExtraction/Tools/MathOperations.cs @@ -41,5 +41,20 @@ namespace LandblockExtraction.Tools { Vector3 averageNormal = (normal1 + normal2) / 2; return Vector3.Normalize(averageNormal); } + public static float CalculateInclination(Vector3 normal) { + Vector3 up = new Vector3(0, 0, 1); // Vecteur vertical de référence + + normal = Vector3.Normalize(normal); // Normaliser la normale + + float dotProduct = Vector3.Dot(normal, up); // Produit scalaire entre la normale et le vecteur vertical + + // Calculer le cosinus de l'angle + float cosTheta = dotProduct; + + // Calculer le sinus de l'angle à partir du cosinus + float sinTheta = (float)Math.Sqrt(1 - cosTheta * cosTheta); + + return sinTheta; // Retourner l'angle d'inclinaison variant de 0 à 1 + } } } diff --git a/LandblockExtraction/WorldMap/VertexInfo/VertexMap.cs b/LandblockExtraction/WorldMap/VertexInfo/VertexMap.cs new file mode 100644 index 0000000..327a47b --- /dev/null +++ b/LandblockExtraction/WorldMap/VertexInfo/VertexMap.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace LandblockExtraction.WorldMap; +public struct VertexMap { + public Vector3 Position; + public Vector4 Color; +} diff --git a/LandblockExtraction/WorldMap/WorldMap.cs b/LandblockExtraction/WorldMap/WorldMap.cs new file mode 100644 index 0000000..0b868f9 --- /dev/null +++ b/LandblockExtraction/WorldMap/WorldMap.cs @@ -0,0 +1,165 @@ +using AC2RE.Definitions; +using LandblockExtraction.AtlasMaker; +using LandblockExtraction.DatEngine; +using LandblockExtraction.Tools; +using System.Numerics; + +namespace LandblockExtraction.WorldMap; +public class WorldMap { + private readonly int NumberLandBlocks = 254; + private readonly int BlockSize = 17; + private readonly int cellSize = 8; + + private PortalEngine portalEngine; + private CellEngine cellEngine; + private TerrainAtlasManager terrainAtlasManager; + + private VertexMap[,] vertexMaps; + + public WorldMap(PortalEngine portalEngine, CellEngine cellEngine) { + this.portalEngine = portalEngine; + this.cellEngine = cellEngine; + + terrainAtlasManager = new TerrainAtlasManager(portalEngine); + + int globalSize = NumberLandBlocks * (BlockSize - 1); + vertexMaps = new VertexMap[globalSize, globalSize]; + } + private void InitializeBaseMap() { + for (int landY = 0; landY <= NumberLandBlocks; landY++) { + for (int landX = 0; landX <= NumberLandBlocks; landX++) { + InitializeCellMap(landY, landX); + } + } + } + public void LoadRegion(int start, int end) { + for (int landY = start; landY <= end; landY++) { + for (int landX = start; landX <= end; landX++) { + InitializeCellMap(landY, landX); + } + } + } + private void InitializeCellMap(int landx, int landy) { + CellId landBlockId = new CellId((byte)landx, (byte)landy, 0xFF, 0xFF); + var landBlock = cellEngine.GetLandBlockData(landBlockId.id); + + for (int y = 0; y < BlockSize; y++) { + for (int x = 0; x < BlockSize; x++) { + float height = 0; + Vector4 color = Vector4.Zero; + + // Calculer les indices globaux en prenant en compte la position du land + int indiceX = landx * (BlockSize - 1) + y; + int indiceY = landy * (BlockSize - 1) + x; + + if (landBlock != null) { + height = GetHeightInLandBlock(landBlock, x, y); + color = GetColorVertex(landBlock, x, y); + } + + // Vérifier si les indices ne dépassent pas la taille globale de la grille + if (indiceX < NumberLandBlocks * (BlockSize - 1) && indiceY < NumberLandBlocks * (BlockSize - 1)) { + vertexMaps[indiceX, indiceY] = new VertexMap() { + Position = GenerateVertexPosition(landx, landy, x, y, height), + Color = color + }; + } + } + } + } + + private float GetHeightInLandBlock(CLandBlockData block, int x, int y) { + var indice = y * BlockSize + x; + return block.heights[indice]; + } + private Vector4 GetColorVertex(CLandBlockData block, int x, int y) { + var indice = y * BlockSize + x; + var terrain = MathOperations.GetTerrainInCellInfo(block.cellInfos[indice]); + return terrainAtlasManager.terrains[(int)terrain].subTerrains.First().Color; + } + public float[] GetAllVertices() { + List verticesList = new List(); + + int rowLength = NumberLandBlocks * (BlockSize - 1); + for (int y = 0; y < rowLength; y++) { + for (int x = 0; x < rowLength; x++) { + + // Ajouter la position du vertex à la liste + verticesList.Add(vertexMaps[x, y].Position.X); + verticesList.Add(vertexMaps[x, y].Position.Y); + verticesList.Add(vertexMaps[x, y].Position.Z); + + // Ajouter la couleur si nécessaire + verticesList.Add(vertexMaps[x, y].Color.X); + verticesList.Add(vertexMaps[x, y].Color.Y); + verticesList.Add(vertexMaps[x, y].Color.Z); + verticesList.Add(vertexMaps[x, y].Color.W); + } + } + + return verticesList.ToArray(); + } + public int[] GenerateRegionIndices(int start, int end) { + List indices = new List(); + + // Calcul de la longueur de la ligne pour la région spécifiée + int rowLength = (end - start) * (BlockSize - 1); + + // Parcours de chaque "cellule" de la grille pour la région spécifiée + for (int y = 0; y < rowLength; y++) { + for (int x = 0; x < rowLength; x++) { + int baseX = start * (BlockSize - 1); + int baseY = start * (BlockSize - 1); + + int topLeft = (y + baseY) * (NumberLandBlocks * (BlockSize - 1)) + (x + baseX); + int topRight = topLeft + 1; + int bottomLeft = topLeft + (NumberLandBlocks * (BlockSize - 1)); + int bottomRight = bottomLeft + 1; + + indices.Add(topLeft); + indices.Add(bottomLeft); + indices.Add(bottomRight); + + indices.Add(bottomLeft); + indices.Add(bottomRight); + indices.Add(topRight); + } + } + + return indices.ToArray(); + } + public int[] GenerateIndices() { + List indices = new List(); + + int rowLength = NumberLandBlocks * (BlockSize - 1); + + // Parcourir chaque "cellule" de la grille, sauf la dernière colonne et la dernière ligne + for (int y = 0; y < rowLength - 1; y++) { + for (int x = 0; x < rowLength - 1; x++) { + // Calculer les indices des 4 sommets du carré courant + int topLeft = y * rowLength + x; + int topRight = topLeft + 1; + int bottomLeft = topLeft + rowLength; + int bottomRight = bottomLeft + 1; + + // Ajouter les indices pour former les deux triangles + indices.Add(topLeft); + indices.Add(bottomLeft); + indices.Add(bottomRight); + + indices.Add(bottomLeft); + indices.Add(bottomRight); + indices.Add(topRight); + } + } + + return indices.ToArray(); + } + private Vector3 GenerateVertexPosition(int landx, int landy, int x, int y, float height) { + int tmpx = (landx * BlockSize + y) * cellSize; + int tmpy = (BlockSize * NumberLandBlocks * cellSize) - ((landy * BlockSize + x) * cellSize) - 1; + var newX = (tmpx - (NumberLandBlocks * BlockSize * cellSize / 2)) - landx * cellSize;// (tmpx - (NumberLandBlocks * BlockSize * cellSize / 2)); + var newY = (tmpy - ((NumberLandBlocks * BlockSize * cellSize) - (NumberLandBlocks * BlockSize * cellSize / 2) - 1)) + landy * cellSize; //(tmpy - ((NumberLandBlocks * BlockSize * cellSize) - (NumberLandBlocks * BlockSize * cellSize / 2) - 1)); + return new Vector3(newX + 1020, height, newY - 1020); + } +} diff --git a/Map3DRendering/Common/Camera.cs b/Map3DRendering/Common/Camera.cs index 8ab50aa..6084f37 100644 --- a/Map3DRendering/Common/Camera.cs +++ b/Map3DRendering/Common/Camera.cs @@ -83,7 +83,7 @@ namespace Map3DRendering.Common { // Get the projection matrix using the same method we have used up until this point public Matrix4 GetProjectionMatrix() { - return Matrix4.CreatePerspectiveFieldOfView(_fov, AspectRatio, 0.01f, 1000f); + return Matrix4.CreatePerspectiveFieldOfView(_fov, AspectRatio, 0.01f, 5000f); } // This function is going to update the direction vertices using some of the math learned in the web tutorials. diff --git a/Map3DRendering/Program.cs b/Map3DRendering/Program.cs index 4df39d6..8ab14ec 100644 --- a/Map3DRendering/Program.cs +++ b/Map3DRendering/Program.cs @@ -6,8 +6,9 @@ namespace Map3DRendering { public static class Program { private static void Main() { var nativeWindowSettings = new NativeWindowSettings() { - Size = new Vector2i(800, 600), + ClientSize = new Vector2i(800, 600), Title = "LearnOpenTK - Map AC2", + Vsync = VSyncMode.On, // This is needed to run on macos Flags = ContextFlags.ForwardCompatible, }; diff --git a/Map3DRendering/Window.cs b/Map3DRendering/Window.cs index d8beae7..8d5b2ea 100644 --- a/Map3DRendering/Window.cs +++ b/Map3DRendering/Window.cs @@ -10,7 +10,7 @@ namespace Map3DRendering { private readonly Vector3 _lightPos = new Vector3(0x10, 0, 0x10); - private MapRender mapRender; + private WorldMapRender mapRender; private AxesGizmo axesGizmo; private Shader _shader; @@ -33,7 +33,7 @@ namespace Map3DRendering { public Window(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(gameWindowSettings, nativeWindowSettings) { - mapRender = new MapRender(); + mapRender = new WorldMapRender(); GL.GetInteger(GetPName.MaxTextureImageUnits, out maxTextures); } @@ -88,7 +88,7 @@ namespace Map3DRendering { GL.LineWidth(5.0f); _shader.SetVector3("viewPos", _camera.Position); - mapRender.UpdateBlocks(_camera.Position, _shader); + //mapRender.UpdateBlocks(_camera.Position, _shader); mapRender.Render(_shader); axesGizmo.Render(Size.X, Size.Y, _camera); diff --git a/Map3DRendering/WorldMapRender.cs b/Map3DRendering/WorldMapRender.cs new file mode 100644 index 0000000..ce92ee1 --- /dev/null +++ b/Map3DRendering/WorldMapRender.cs @@ -0,0 +1,67 @@ +using LandblockExtraction.DatEngine; +using LandblockExtraction.WorldMap; +using Map3DRendering.Common; +using OpenTK.Graphics.OpenGL4; +using OpenTK.Mathematics; + + +namespace Map3DRendering; +public class WorldMapRender { + private PortalEngine portalEngine; + private CellEngine cellEngine; + private WorldMap worldMap; + + public int _vertexArrayObject; + public int _vertexBufferObject; + public int _elementBufferObject; + + public int indicesLength; + + public WorldMapRender() { + portalEngine = new PortalEngine(); + cellEngine = new CellEngine(); + + worldMap = new WorldMap(portalEngine, cellEngine); + worldMap.LoadRegion(0x5F, 0x9F); + } + public void OnLoad(Shader _shader) { + InitializeMap(_shader); + } + private void InitializeMap(Shader _shader) { + int lenghPacket = 7; + var vertices = worldMap.GetAllVertices(); + var indices = worldMap.GenerateRegionIndices(0x5F, 0x9F); + indicesLength = indices.Length; + // 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(); + GL.BindVertexArray(tempVertexArray); + _vertexArrayObject = tempVertexArray; + + int tmpVertexBuffer = GL.GenBuffer(); + GL.BindBuffer(BufferTarget.ArrayBuffer, tmpVertexBuffer); + GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw); + _vertexBufferObject = tmpVertexBuffer; + + GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 0); + + int tmpElementBuffer = GL.GenBuffer(); + GL.BindBuffer(BufferTarget.ElementArrayBuffer, tmpElementBuffer); + GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(int), indices, BufferUsageHint.StaticDraw); + _elementBufferObject = tmpElementBuffer; + + var vertexLocation = _shader.GetAttribLocation("aPos"); + GL.EnableVertexAttribArray(vertexLocation); + GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 0); + + var colorLocation = _shader.GetAttribLocation("aColor"); + GL.EnableVertexAttribArray(colorLocation); + GL.VertexAttribPointer(colorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 6 * sizeof(float)); + } + public void Render(Shader shader) { + var model = Matrix4.Identity;//CreateTranslation(x * BlockSize, 0, y * BlockSize); // Ajustez selon votre système de coordonnées + shader.SetMatrix4("model", model); + GL.BindVertexArray(_vertexArrayObject); + GL.DrawElements(PrimitiveType.Triangles, indicesLength, DrawElementsType.UnsignedInt, 0); + } +}