From 3a7d169b30c0b2a20b89bef030b19573c6fad00f Mon Sep 17 00:00:00 2001 From: Troispoils Date: Thu, 29 Feb 2024 16:51:41 +0100 Subject: [PATCH 1/7] Test with new technique texturing --- .../AtlasMaker/TerrainAtlasManager.cs | 12 +++ .../AtlasMaker/TexturesImage.cs | 2 +- .../LandBlockExtractor/LandBlockExtrator.cs | 73 +++++++++++++++++-- Map3DRendering/Common/Shader.cs | 19 ++--- Map3DRendering/Common/Texture.cs | 13 ++-- Map3DRendering/Program.cs | 2 +- Map3DRendering/Shaders/shader.frag | 41 +++-------- Map3DRendering/Window.cs | 24 +++++- 8 files changed, 125 insertions(+), 61 deletions(-) diff --git a/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs b/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs index 9a169e2..cf211c2 100644 --- a/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs +++ b/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs @@ -5,6 +5,7 @@ using System.Numerics; namespace LandblockExtraction.AtlasMaker; public class TerrainAtlasManager { + private readonly string PATHTERRAINIMG = @".\terrains"; private PortalEngine portalEngine; public Dictionary textureCoord; @@ -51,6 +52,17 @@ public class TerrainAtlasManager { } } } + public void SaveAllTerrain() { + if (!Directory.Exists(PATHTERRAINIMG)) { + Directory.CreateDirectory(PATHTERRAINIMG); + } + + foreach(var img in terrainTexture) { + var path = Path.Combine(PATHTERRAINIMG, img.Key.ToString()); + + img.Value.Write(path + ".jpg"); + } + } private int? GetIndexBySurface(uint surfaceIndex) { foreach(var terrain in portalEngine.cTerrainDesc.terrains) { if (terrain.surfaceInfo == surfaceIndex) diff --git a/LandblockExtraction/AtlasMaker/TexturesImage.cs b/LandblockExtraction/AtlasMaker/TexturesImage.cs index 18ca924..cea78c1 100644 --- a/LandblockExtraction/AtlasMaker/TexturesImage.cs +++ b/LandblockExtraction/AtlasMaker/TexturesImage.cs @@ -40,7 +40,7 @@ public class TexturesImage { 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 != 256) continue; var dataImg = DDSHeader.Generate(image); using (MagickImage realImg = new MagickImage(dataImg)) { magickImage = new(realImg); diff --git a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs index d6597d3..d04368e 100644 --- a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs +++ b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs @@ -2,6 +2,7 @@ using LandblockExtraction.AtlasMaker; using LandblockExtraction.DatEngine; using LandblockExtraction.Tools; +using System; using System.Numerics; namespace LandblockExtraction.LandBlockExtractor { @@ -21,6 +22,7 @@ namespace LandblockExtraction.LandBlockExtractor { terrainAtlasManager = new TerrainAtlasManager(portalEngine); terrainAtlasManager.ExtractTexture(); terrainAtlasManager.GenerateUV(); + terrainAtlasManager.SaveAllTerrain(); } public BlockStruct? GetBlock(int landX, int landY) { @@ -40,15 +42,72 @@ namespace LandblockExtraction.LandBlockExtractor { blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]); blockStruct.verticesStruct.farcolor[indice] = GenerateVertexFarColor(blockData.cellInfos[indice]); blockStruct.verticesStruct.terraintype[indice] = GenerateVertexTerrainType(blockData.cellInfos[indice]); + blockStruct.verticesStruct.texturecoord[indice] = GenerateBasicUV(x, y); blockStruct.indices = GenerateBasicIndices(); + + } + } + blockStruct.verticesStruct.normal = GenerateBasicNormal(blockStruct); + //DoubleEdgeVertices(blockStruct); + + Dictionary testTerr = new Dictionary(); + foreach (var test in blockStruct.verticesStruct.terraintype) { + var type = (int)test.X; + if (testTerr.ContainsKey(type)) { + testTerr[type]++; + } else { + testTerr.Add(type, 1); } } - DoubleEdgeVertices(blockStruct); - return blockStruct; } + private Vector2 GenerateBasicUV(int x, int y) { + float u = (float)x / (BlockSize - 1) * 16; + float v = (float)y / (BlockSize - 1) * 16; + + return new Vector2(u, v); + } + + private Vector3[] GenerateBasicNormal(BlockStruct blockStruct) { + Vector3[] normals = new Vector3[blockStruct.verticesStruct.position.Length]; + + // Initialise tous les vecteurs normaux à zéro + for (int i = 0; i < normals.Length; i++) { + normals[i] = new Vector3(0, 0, 0); + } + + // Parcourt tous les indices et calcule les normales pour chaque triangle + for (int i = 0; i < blockStruct.indices.Length; i += 3) { + int index1 = blockStruct.indices[i]; + int index2 = blockStruct.indices[i + 1]; + int index3 = blockStruct.indices[i + 2]; + + Vector3 vertex1 = blockStruct.verticesStruct.position[index1]; + Vector3 vertex2 = blockStruct.verticesStruct.position[index2]; + Vector3 vertex3 = blockStruct.verticesStruct.position[index3]; + + // Calcule la normale du triangle + /*Vector3 edge1 = vertex2 - vertex1; + Vector3 edge2 = vertex3 - vertex1; + Vector3 normal = Vector3.Cross(edge1, edge2); + normal = Vector3.Normalize(normal);*/ + var normal = MathOperations.CalculateTriangleNormal(vertex1, vertex2, vertex3); + // Ajoute la normale du triangle aux normales des sommets du triangle + normals[index1] += normal; + normals[index2] += normal; + normals[index3] += normal; + } + + // Normalise toutes les normales de sommets pour qu'elles soient de longueur unitaire + for (int i = 0; i < normals.Length; i++) { + normals[i] = Vector3.Normalize(normals[i]); + } + + return normals; + } + private int[] GenerateBasicIndices() { List indices = new List(); @@ -147,7 +206,7 @@ namespace LandblockExtraction.LandBlockExtractor { blockStruct.verticesStruct.terraintype[foor].X)); newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[one].X); - newPositions.Add(blockStruct.verticesStruct.position[two]); + /*newPositions.Add(blockStruct.verticesStruct.position[two]); newColors.Add(blockStruct.verticesStruct.color[two]); newFarColors.Add(blockStruct.verticesStruct.farcolor[two]); newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, @@ -178,7 +237,7 @@ namespace LandblockExtraction.LandBlockExtractor { newTexCoord.Add(new(0, 0)); newTexCoord.Add(new(0, 1)); newTexCoord.Add(new(1, 0)); - newTexCoord.Add(new(1, 1)); + newTexCoord.Add(new(1, 1));*/ var normal = MathOperations.CalculateQuadNormal(blockStruct.verticesStruct.position[one], blockStruct.verticesStruct.position[two], @@ -186,9 +245,9 @@ namespace LandblockExtraction.LandBlockExtractor { blockStruct.verticesStruct.position[foor]); newNormals.Add(normal); - newNormals.Add(normal); - newNormals.Add(normal); - newNormals.Add(normal); + //newNormals.Add(normal); + //newNormals.Add(normal); + //newNormals.Add(normal); } // Ajouter les nouveaux sommets à la structure BlockStruct (étape 2) diff --git a/Map3DRendering/Common/Shader.cs b/Map3DRendering/Common/Shader.cs index d16e150..dd7900a 100644 --- a/Map3DRendering/Common/Shader.cs +++ b/Map3DRendering/Common/Shader.cs @@ -1,7 +1,11 @@ -using OpenTK.Graphics.OpenGL4; +using System; +using System.IO; +using System.Text; +using System.Collections.Generic; +using OpenTK.Graphics.OpenGL4; using OpenTK.Mathematics; -namespace Map3DRendering.Common { +namespace Map3DRendering { // A simple class meant to help create shaders. public class Shader { public readonly int Handle; @@ -163,18 +167,9 @@ namespace Map3DRendering.Common { /// /// The name of the uniform /// The data to set - public void SetVector2(string name, Vector2 data) { - GL.UseProgram(Handle); - GL.Uniform2(_uniformLocations[name], data); - } public void SetVector3(string name, Vector3 data) { GL.UseProgram(Handle); GL.Uniform3(_uniformLocations[name], data); } - - public void SetVector4(string name, Vector4 data) { - GL.UseProgram(Handle); - GL.Uniform4(_uniformLocations[name], data); - } } -} +} \ No newline at end of file diff --git a/Map3DRendering/Common/Texture.cs b/Map3DRendering/Common/Texture.cs index d0cf265..2032c28 100644 --- a/Map3DRendering/Common/Texture.cs +++ b/Map3DRendering/Common/Texture.cs @@ -1,7 +1,11 @@ using OpenTK.Graphics.OpenGL4; +using System.Drawing; +using System.Drawing.Imaging; +using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat; using StbImageSharp; +using System.IO; -namespace Map3DRendering.Common { +namespace Map3DRendering { // A helper class, much like Shader, meant to simplify loading textures. public class Texture { public readonly int Handle; @@ -77,10 +81,5 @@ namespace Map3DRendering.Common { GL.ActiveTexture(unit); GL.BindTexture(TextureTarget.Texture2D, Handle); } - - public void Assign(int shader, int i) { - int location = GL.GetUniformLocation(shader, "textures[" + i.ToString() + "]"); - GL.Uniform1(location, i); - } } -} +} \ No newline at end of file diff --git a/Map3DRendering/Program.cs b/Map3DRendering/Program.cs index 4df39d6..88992d4 100644 --- a/Map3DRendering/Program.cs +++ b/Map3DRendering/Program.cs @@ -6,7 +6,7 @@ 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", // This is needed to run on macos Flags = ContextFlags.ForwardCompatible, diff --git a/Map3DRendering/Shaders/shader.frag b/Map3DRendering/Shaders/shader.frag index 0e12667..b2e8c63 100644 --- a/Map3DRendering/Shaders/shader.frag +++ b/Map3DRendering/Shaders/shader.frag @@ -2,11 +2,14 @@ out vec4 outputColor; +uniform sampler2D texture0; +uniform sampler2D texture1; + uniform vec3 viewPos; uniform vec3 lightPos; uniform vec3 lightColor; -uniform sampler2D texture0; +in vec4 Color; in vec4 FarColor; in vec3 Normal; in vec3 FragPos; @@ -16,40 +19,20 @@ in float RealType; void main() { - float tileSize = 64.0 / 512.0; - - float baseWeight = 0.2; - float dominantWeight = 0.4; - - vec4 weights = vec4(baseWeight); - vec2 uvOffsets[4]; - for (int i = 0; i < 4; i++) { - float type = TexType[i]; - uvOffsets[i] = vec2(mod(type, 8.0), floor(type / 8.0)) * tileSize; - if (type == RealType) { - weights[i] = dominantWeight; - } - } - - vec4 blendedColor[4]; - for (int i = 0; i < 4; i++) { - vec2 uv = TexCoord * tileSize + uvOffsets[i]; - blendedColor[i] = texture(texture0, uv); - } - - float weightX = TexCoord.x; - float weightY = TexCoord.y; - - vec4 mix1 = mix(blendedColor[0], blendedColor[2], weightX); - vec4 mix2 = mix(blendedColor[1], blendedColor[3], weightX); - vec4 finalColor = mix(mix1, mix2, weightY); + vec4 color0 = texture(texture0, TexCoord); + vec4 color1 = texture(texture1, TexCoord); vec3 norm = normalize(Normal); + + float inclineFactor = abs(norm.y); + + vec4 finalColor = mix(color0, color1, inclineFactor); + vec3 lightDir = normalize(lightPos - FragPos); float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * lightColor; - vec4 litColor = vec4(diffuse, 1.0) * finalColor; + vec4 litColor = vec4(diffuse, 1.0) * finalColor * Color; float distance = length(viewPos - FragPos); float interpolationFactor = clamp(distance / 1000.0, 0.0, 1.0); diff --git a/Map3DRendering/Window.cs b/Map3DRendering/Window.cs index 39ff51c..8040ec4 100644 --- a/Map3DRendering/Window.cs +++ b/Map3DRendering/Window.cs @@ -16,6 +16,7 @@ namespace Map3DRendering { private Shader _shader; private Texture _texture; + private Texture _texture2; private Camera _camera; @@ -53,16 +54,29 @@ namespace Map3DRendering { _shader.Use(); mapRender.OnLoad(_shader); - _texture = Texture.LoadFromFile("atlas.jpg"); + + _texture = Texture.LoadFromFile("terrains/0.jpg"); + // Texture units are explained in Texture.cs, at the Use function. + // First texture goes in texture unit 0. _texture.Use(TextureUnit.Texture0); + // This is helpful because System.Drawing reads the pixels differently than OpenGL expects. + _texture2 = Texture.LoadFromFile("terrains/1.jpg"); + // Then, the second goes in texture unit 1. + _texture2.Use(TextureUnit.Texture1); + + // Next, we must setup the samplers in the shaders to use the right textures. + // The int we send to the uniform indicates which texture unit the sampler should use. + _shader.SetInt("texture0", 0); + _shader.SetInt("texture1", 1); + axesGizmo = new AxesGizmo(); _camera = new Camera(Vector3.UnitY * 300, Size.X / (float)Size.Y); _camera.Fov = 60; //CursorState = CursorState.Grabbed; - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); + //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); + //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); _lightPosVec = Vector3.UnitY * 1000; } @@ -73,8 +87,9 @@ namespace Map3DRendering { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - _shader.Use(); _texture.Use(TextureUnit.Texture0); + _texture2.Use(TextureUnit.Texture1); + _shader.Use(); _shader.SetMatrix4("view", _camera.GetViewMatrix()); _shader.SetMatrix4("projection", _camera.GetProjectionMatrix()); @@ -82,6 +97,7 @@ namespace Map3DRendering { //_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", _lightPosVec); + //_shader.SetVector3("viewPos", _camera.Position); GL.LineWidth(5.0f); From 2b62d3b9e4f21a454d016b255b05b6d86fcedacd Mon Sep 17 00:00:00 2001 From: Troispoils Date: Thu, 29 Feb 2024 17:09:27 +0100 Subject: [PATCH 2/7] Update Normal calcul --- .../LandBlockExtractor/LandBlockExtrator.cs | 11 +++++++---- Map3DRendering/Shaders/shader.frag | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs index d04368e..cf65fac 100644 --- a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs +++ b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs @@ -64,8 +64,8 @@ namespace LandblockExtraction.LandBlockExtractor { } private Vector2 GenerateBasicUV(int x, int y) { - float u = (float)x / (BlockSize - 1) * 16; - float v = (float)y / (BlockSize - 1) * 16; + float u = (float)x / (BlockSize - 1) * 8; + float v = (float)y / (BlockSize - 1) * 8; return new Vector2(u, v); } @@ -79,25 +79,28 @@ namespace LandblockExtraction.LandBlockExtractor { } // Parcourt tous les indices et calcule les normales pour chaque triangle - for (int i = 0; i < blockStruct.indices.Length; i += 3) { + for (int i = 0; i < blockStruct.indices.Length; i += 6) { int index1 = blockStruct.indices[i]; int index2 = blockStruct.indices[i + 1]; int index3 = blockStruct.indices[i + 2]; + int index4 = blockStruct.indices[i + 5]; Vector3 vertex1 = blockStruct.verticesStruct.position[index1]; Vector3 vertex2 = blockStruct.verticesStruct.position[index2]; Vector3 vertex3 = blockStruct.verticesStruct.position[index3]; + Vector3 vertex4 = blockStruct.verticesStruct.position[index4]; // Calcule la normale du triangle /*Vector3 edge1 = vertex2 - vertex1; Vector3 edge2 = vertex3 - vertex1; Vector3 normal = Vector3.Cross(edge1, edge2); normal = Vector3.Normalize(normal);*/ - var normal = MathOperations.CalculateTriangleNormal(vertex1, vertex2, vertex3); + var normal = MathOperations.CalculateQuadNormal(vertex1, vertex2, vertex3, vertex4); // Ajoute la normale du triangle aux normales des sommets du triangle normals[index1] += normal; normals[index2] += normal; normals[index3] += normal; + normals[index4] += normal; } // Normalise toutes les normales de sommets pour qu'elles soient de longueur unitaire diff --git a/Map3DRendering/Shaders/shader.frag b/Map3DRendering/Shaders/shader.frag index b2e8c63..8de05a0 100644 --- a/Map3DRendering/Shaders/shader.frag +++ b/Map3DRendering/Shaders/shader.frag @@ -26,7 +26,7 @@ void main() float inclineFactor = abs(norm.y); - vec4 finalColor = mix(color0, color1, inclineFactor); + vec4 finalColor = mix(color0, color1, norm.y); vec3 lightDir = normalize(lightPos - FragPos); float diff = max(dot(norm, lightDir), 0.0); From 51325d86508320d2338e2273c85d9eb13d68ace3 Mon Sep 17 00:00:00 2001 From: Troispoils Date: Fri, 1 Mar 2024 14:03:04 +0100 Subject: [PATCH 3/7] Test array texture --- .../LandBlockExtractor/LandBlockExtrator.cs | 33 ++++++++++++++- Map3DRendering/Common/Texture.cs | 40 +++++++++++++++++++ Map3DRendering/Shaders/shader.frag | 10 ++--- Map3DRendering/Window.cs | 19 +++------ 4 files changed, 80 insertions(+), 22 deletions(-) diff --git a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs index cf65fac..a9f41a5 100644 --- a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs +++ b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs @@ -42,11 +42,12 @@ namespace LandblockExtraction.LandBlockExtractor { blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]); blockStruct.verticesStruct.farcolor[indice] = GenerateVertexFarColor(blockData.cellInfos[indice]); blockStruct.verticesStruct.terraintype[indice] = GenerateVertexTerrainType(blockData.cellInfos[indice]); - blockStruct.verticesStruct.texturecoord[indice] = GenerateBasicUV(x, y); + blockStruct.verticesStruct.texturecoord[indice] = GenerateUVForSubTile(x, y); blockStruct.indices = GenerateBasicIndices(); } } + //blockStruct.verticesStruct.texturecoord = GenerateBasicUVTest(blockStruct); blockStruct.verticesStruct.normal = GenerateBasicNormal(blockStruct); //DoubleEdgeVertices(blockStruct); @@ -69,7 +70,37 @@ namespace LandblockExtraction.LandBlockExtractor { return new Vector2(u, v); } + private Vector2 GenerateUVForSubTile(int x, int y) { + // Taille d'une "mini-tuile" en termes de coordonnées UV + float miniTileSize = 1f / BlockSize - 1; // Comme la sous-grille est 5x5 + // Calcul des coordonnées UV basées sur la position (x, y) dans la sous-grille + float u = x * miniTileSize; + float v = y * miniTileSize; + + return new Vector2(u, v); + } + private Vector2[] GenerateBasicUVTest(BlockStruct blockStruct) { + Vector2[] uvs = new Vector2[blockStruct.verticesStruct.position.Length]; + + for (int i = 0; i < uvs.Length; i++) { + uvs[i] = new Vector2(0, 0); + } + + for (int i = 0; i < blockStruct.indices.Length; i += 6) { + int index1 = blockStruct.indices[i]; + int index2 = blockStruct.indices[i + 1]; + int index3 = blockStruct.indices[i + 2]; + int index4 = blockStruct.indices[i + 5]; + + uvs[index1] = new(0, 0); + uvs[index2] = new(0, 1); + uvs[index3] = new(1, 0); + uvs[index4] = new(1, 1); + } + + return uvs; + } private Vector3[] GenerateBasicNormal(BlockStruct blockStruct) { Vector3[] normals = new Vector3[blockStruct.verticesStruct.position.Length]; diff --git a/Map3DRendering/Common/Texture.cs b/Map3DRendering/Common/Texture.cs index 2032c28..96e397c 100644 --- a/Map3DRendering/Common/Texture.cs +++ b/Map3DRendering/Common/Texture.cs @@ -68,6 +68,42 @@ namespace Map3DRendering { return new Texture(handle); } + public static Texture LoadFromArray(string[] paths) { + // Générer un identifiant de texture + int handle = GL.GenTexture(); + + // Activer la texture + GL.ActiveTexture(TextureUnit.Texture0); + GL.BindTexture(TextureTarget.Texture2DArray, handle); + + // Ici, nous supposons que toutes les images ont les mêmes dimensions et le même format + // Charger la première image pour obtenir les dimensions + ImageResult firstImage = ImageResult.FromStream(File.OpenRead(paths[0]), ColorComponents.RedGreenBlueAlpha); + int width = firstImage.Width; + int height = firstImage.Height; + + // Initialiser la texture 2D array sans lui passer de données pour l'instant + GL.TexImage3D(TextureTarget.Texture2DArray, 0, PixelInternalFormat.Rgba, width, height, paths.Length, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); + + // Charger chaque texture dans l'array + for (int i = 0; i < paths.Length; i++) { + using (Stream stream = File.OpenRead(paths[i])) { + ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); + GL.TexSubImage3D(TextureTarget.Texture2DArray, 0, 0, 0, i, width, height, 1, PixelFormat.Rgba, PixelType.UnsignedByte, image.Data); + } + } + + // Paramètres de texture + GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); + GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); + GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); + GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); + + // Générer des mipmaps pour la texture array + GL.GenerateMipmap(GenerateMipmapTarget.Texture2DArray); + + return new Texture(handle); + } public Texture(int glHandle) { Handle = glHandle; @@ -81,5 +117,9 @@ namespace Map3DRendering { GL.ActiveTexture(unit); GL.BindTexture(TextureTarget.Texture2D, Handle); } + public void UseArray(TextureUnit unit) { + GL.ActiveTexture(unit); + GL.BindTexture(TextureTarget.Texture2DArray, Handle); + } } } \ No newline at end of file diff --git a/Map3DRendering/Shaders/shader.frag b/Map3DRendering/Shaders/shader.frag index 8de05a0..a559a77 100644 --- a/Map3DRendering/Shaders/shader.frag +++ b/Map3DRendering/Shaders/shader.frag @@ -2,8 +2,7 @@ out vec4 outputColor; -uniform sampler2D texture0; -uniform sampler2D texture1; +uniform sampler2DArray texture0; uniform vec3 viewPos; uniform vec3 lightPos; @@ -19,14 +18,11 @@ in float RealType; void main() { - vec4 color0 = texture(texture0, TexCoord); - vec4 color1 = texture(texture1, TexCoord); + vec4 color0 = texture(texture0, vec3(TexCoord, TexType.x)); vec3 norm = normalize(Normal); - float inclineFactor = abs(norm.y); - - vec4 finalColor = mix(color0, color1, norm.y); + vec4 finalColor = color0; //mix(color0, color1, norm.y); vec3 lightDir = normalize(lightPos - FragPos); float diff = max(dot(norm, lightDir), 0.0); diff --git a/Map3DRendering/Window.cs b/Map3DRendering/Window.cs index 8040ec4..8958671 100644 --- a/Map3DRendering/Window.cs +++ b/Map3DRendering/Window.cs @@ -16,7 +16,6 @@ namespace Map3DRendering { private Shader _shader; private Texture _texture; - private Texture _texture2; private Camera _camera; @@ -55,20 +54,13 @@ namespace Map3DRendering { mapRender.OnLoad(_shader); - _texture = Texture.LoadFromFile("terrains/0.jpg"); + var file = Directory.EnumerateFiles(@"./terrains"); + + _texture = Texture.LoadFromArray(file.ToArray()); // Texture units are explained in Texture.cs, at the Use function. // First texture goes in texture unit 0. - _texture.Use(TextureUnit.Texture0); + _texture.UseArray(TextureUnit.Texture0); - // This is helpful because System.Drawing reads the pixels differently than OpenGL expects. - _texture2 = Texture.LoadFromFile("terrains/1.jpg"); - // Then, the second goes in texture unit 1. - _texture2.Use(TextureUnit.Texture1); - - // Next, we must setup the samplers in the shaders to use the right textures. - // The int we send to the uniform indicates which texture unit the sampler should use. - _shader.SetInt("texture0", 0); - _shader.SetInt("texture1", 1); axesGizmo = new AxesGizmo(); @@ -87,8 +79,7 @@ namespace Map3DRendering { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - _texture.Use(TextureUnit.Texture0); - _texture2.Use(TextureUnit.Texture1); + _texture.UseArray(TextureUnit.Texture0); _shader.Use(); _shader.SetMatrix4("view", _camera.GetViewMatrix()); From 4ea0101cf6cdb237075adde13369648c2dc8994d Mon Sep 17 00:00:00 2001 From: Troispoils Date: Fri, 1 Mar 2024 15:03:45 +0100 Subject: [PATCH 4/7] Update loc of Vertex. --- .../LandBlockExtractor/LandBlockExtrator.cs | 7 ++-- Map3DRendering/Common/Texture.cs | 42 +++++++++++++++++++ Map3DRendering/Shaders/shader.frag | 21 ++-------- Map3DRendering/Window.cs | 12 +++--- 4 files changed, 55 insertions(+), 27 deletions(-) diff --git a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs index d6597d3..4c12d42 100644 --- a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs +++ b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs @@ -78,10 +78,9 @@ namespace LandblockExtraction.LandBlockExtractor { private Vector3 GenerateVertexPosition(int landx, int landy, int x, int y, byte height) { int tmpx = (landx * BlockSize + y) * cellSize; int tmpy = (BlockSize * NumberLandBlocks * cellSize) - ((landy * BlockSize + x) * cellSize) - 1; - var newX = (tmpx - (NumberLandBlocks * BlockSize * cellSize / 2)); - var newY = (tmpy - ((NumberLandBlocks * BlockSize * cellSize) - (NumberLandBlocks * BlockSize * cellSize / 2) - 1)); - - return new Vector3(newX, portalEngine.landScapeDefs.landHeightTable[height], newY); + 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, portalEngine.landScapeDefs.landHeightTable[height], newY - 1020); } private Vector4 GenerateVertexColor(uint cellInfo) { var terrain = MathOperations.GetTerrainInCellInfo(cellInfo); diff --git a/Map3DRendering/Common/Texture.cs b/Map3DRendering/Common/Texture.cs index d0cf265..7a52e63 100644 --- a/Map3DRendering/Common/Texture.cs +++ b/Map3DRendering/Common/Texture.cs @@ -64,6 +64,43 @@ namespace Map3DRendering.Common { return new Texture(handle); } + public static Texture LoadFromArray(string[] paths) { + // Générer un identifiant de texture + int handle = GL.GenTexture(); + + // Activer la texture + GL.ActiveTexture(TextureUnit.Texture0); + GL.BindTexture(TextureTarget.Texture2DArray, handle); + + // Ici, nous supposons que toutes les images ont les mêmes dimensions et le même format + // Charger la première image pour obtenir les dimensions + ImageResult firstImage = ImageResult.FromStream(File.OpenRead(paths[0]), ColorComponents.RedGreenBlueAlpha); + int width = firstImage.Width; + int height = firstImage.Height; + + // Initialiser la texture 2D array sans lui passer de données pour l'instant + GL.TexImage3D(TextureTarget.Texture2DArray, 0, PixelInternalFormat.Rgba, width, height, paths.Length, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); + + // Charger chaque texture dans l'array + for (int i = 0; i < paths.Length; i++) { + using (Stream stream = File.OpenRead(paths[i])) { + ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); + GL.TexSubImage3D(TextureTarget.Texture2DArray, 0, 0, 0, i, width, height, 1, PixelFormat.Rgba, PixelType.UnsignedByte, image.Data); + } + } + + // Paramètres de texture + GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); + GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); + GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); + GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); + + // Générer des mipmaps pour la texture array + GL.GenerateMipmap(GenerateMipmapTarget.Texture2DArray); + + return new Texture(handle); + } + public Texture(int glHandle) { Handle = glHandle; @@ -77,6 +114,11 @@ namespace Map3DRendering.Common { GL.ActiveTexture(unit); GL.BindTexture(TextureTarget.Texture2D, Handle); } + public void UseArray(TextureUnit unit) { + GL.ActiveTexture(unit); + GL.BindTexture(TextureTarget.Texture2DArray, Handle); + } + public void Assign(int shader, int i) { int location = GL.GetUniformLocation(shader, "textures[" + i.ToString() + "]"); diff --git a/Map3DRendering/Shaders/shader.frag b/Map3DRendering/Shaders/shader.frag index 0e12667..29f168c 100644 --- a/Map3DRendering/Shaders/shader.frag +++ b/Map3DRendering/Shaders/shader.frag @@ -5,7 +5,7 @@ out vec4 outputColor; uniform vec3 viewPos; uniform vec3 lightPos; uniform vec3 lightColor; -uniform sampler2D texture0; +uniform sampler2DArray texture0; in vec4 FarColor; in vec3 Normal; @@ -16,25 +16,10 @@ in float RealType; void main() { - float tileSize = 64.0 / 512.0; - - float baseWeight = 0.2; - float dominantWeight = 0.4; - - vec4 weights = vec4(baseWeight); - vec2 uvOffsets[4]; - for (int i = 0; i < 4; i++) { - float type = TexType[i]; - uvOffsets[i] = vec2(mod(type, 8.0), floor(type / 8.0)) * tileSize; - if (type == RealType) { - weights[i] = dominantWeight; - } - } - vec4 blendedColor[4]; for (int i = 0; i < 4; i++) { - vec2 uv = TexCoord * tileSize + uvOffsets[i]; - blendedColor[i] = texture(texture0, uv); + float type = TexType[i]; + blendedColor[i] = texture(texture0, vec3(TexCoord, type)); } float weightX = TexCoord.x; diff --git a/Map3DRendering/Window.cs b/Map3DRendering/Window.cs index 39ff51c..30fba6e 100644 --- a/Map3DRendering/Window.cs +++ b/Map3DRendering/Window.cs @@ -53,8 +53,10 @@ namespace Map3DRendering { _shader.Use(); mapRender.OnLoad(_shader); - _texture = Texture.LoadFromFile("atlas.jpg"); - _texture.Use(TextureUnit.Texture0); + + var file = Directory.EnumerateFiles(@"./terrains"); + _texture = Texture.LoadFromArray(file.ToArray()); + _texture.UseArray(TextureUnit.Texture0); axesGizmo = new AxesGizmo(); @@ -74,7 +76,7 @@ namespace Map3DRendering { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); _shader.Use(); - _texture.Use(TextureUnit.Texture0); + _texture.UseArray(TextureUnit.Texture0); _shader.SetMatrix4("view", _camera.GetViewMatrix()); _shader.SetMatrix4("projection", _camera.GetProjectionMatrix()); @@ -142,10 +144,10 @@ namespace Map3DRendering { _camera.Position += _camera.Right * cameraSpeed * (float)e.Time; // Right } if (input.IsKeyDown(Keys.Space)) { - _camera.Position += _camera.Up * cameraSpeed * (float)e.Time; // Up + _camera.Position += Vector3.UnitY * cameraSpeed * (float)e.Time; // Up } if (input.IsKeyDown(Keys.LeftShift)) { - _camera.Position -= _camera.Up * cameraSpeed * (float)e.Time; // Down + _camera.Position -= Vector3.UnitY * cameraSpeed * (float)e.Time; // Down } // Get the mouse state From 3aa6b5bc830b9e44bcab3ec00d2b023bf8d408e6 Mon Sep 17 00:00:00 2001 From: Troispoils Date: Fri, 1 Mar 2024 16:15:05 +0100 Subject: [PATCH 5/7] Update Window.cs --- Map3DRendering/Window.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Map3DRendering/Window.cs b/Map3DRendering/Window.cs index 30fba6e..d2af5e8 100644 --- a/Map3DRendering/Window.cs +++ b/Map3DRendering/Window.cs @@ -84,7 +84,6 @@ namespace Map3DRendering { //_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", _lightPosVec); - //_shader.SetVector3("viewPos", _camera.Position); GL.LineWidth(5.0f); From 443b3a319d5b38a2f6586812657ec9e357c79de7 Mon Sep 17 00:00:00 2001 From: Troispoils Date: Fri, 1 Mar 2024 21:56:18 +0100 Subject: [PATCH 6/7] better system for texture terrain. --- .../AtlasMaker/TerrainAtlasManager.cs | 65 ++++++++++---- .../AtlasMaker/TerrainInfo/SubTerrain.cs | 10 ++- .../AtlasMaker/TerrainInfo/Terrain.cs | 6 +- .../LandBlockExtractor/LandBlockExtrator.cs | 89 ++++++++++++------- .../Utilities/VerticesStruct.cs | 7 +- Map3DRendering/MapRender.cs | 6 +- Map3DRendering/Shaders/shader.frag | 1 - Map3DRendering/Shaders/shader.vert | 3 - Map3DRendering/Window.cs | 2 +- 9 files changed, 120 insertions(+), 69 deletions(-) diff --git a/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs b/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs index 9a169e2..d62bead 100644 --- a/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs +++ b/LandblockExtraction/AtlasMaker/TerrainAtlasManager.cs @@ -1,22 +1,26 @@ using AC2RE.Definitions; using ImageMagick; using LandblockExtraction.DatEngine; +using LandblockExtraction.Tools; using System.Numerics; namespace LandblockExtraction.AtlasMaker; public class TerrainAtlasManager { private PortalEngine portalEngine; + private Dictionary mapTerrain; public Dictionary textureCoord; public Dictionary terrainTexture; public Dictionary terrains; public TerrainAtlasManager(PortalEngine portalEngine) { this.portalEngine = portalEngine; + mapTerrain = new(); textureCoord = new Dictionary(); terrainTexture = new Dictionary(); terrains = new Dictionary(); - testGenerateList(); + InitialiseTerrainDic(); + GenerateTerrain(); } public void ExtractTexture() { using (var atlasBuilder = new AtlasBuilder(portalEngine)) { @@ -36,27 +40,56 @@ public class TerrainAtlasManager { } } } - /*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 GenerateTerrain() { + foreach(var terrain in portalEngine.cTerrainDesc.terrains) { + Terrain tmpTer = new((int)terrain.terrainIndex); + 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))); + } + } + } + terrains.Add((int)terrain.terrainIndex, tmpTer); + Console.WriteLine("Init: " + terrain.terrainIndex); } - }*/ - private void testGenerateList() { - foreach(var surfaces in portalEngine.cSurfaceDesc.surfaces) { - Console.WriteLine($"{surfaces.surfIndex} :"); - foreach( var surface in surfaces.terrainMaterials) { - Console.WriteLine($"\t[{surface.minPitch} < {surface.maxPitch}]({surface.baseMaterials.First().materialDid}) - Vec3: {surface.vertexColor.First().vertexColor}"); + } + private void testGenerateList() { + foreach (var terrain in portalEngine.cTerrainDesc.terrains) { + foreach (var surfaces in portalEngine.cSurfaceDesc.surfaces) { + if (surfaces.surfIndex != terrain.surfaceInfo) continue; + foreach (var surface in surfaces.terrainMaterials) { + + uint test = 0; + //foreach(var m in map) { if (m.Key == surface.baseMaterials.First().materialDid) test = m.Value; } + + Console.Write($"({test})[{terrain.terrainName}]{surfaces.surfIndex} :"); + Console.WriteLine($"\t[{surface.minPitch} < {surface.maxPitch}]({surface.baseMaterials.First().materialDid}) - Vec3: {surface.vertexColor.First().vertexColor}"); + } + Console.WriteLine("--------"); } } } - private int? GetIndexBySurface(uint surfaceIndex) { + private void InitialiseTerrainDic() { foreach(var terrain in portalEngine.cTerrainDesc.terrains) { - if (terrain.surfaceInfo == surfaceIndex) - return (int)terrain.terrainIndex; + foreach (var surface in portalEngine.cSurfaceDesc.surfaces) { + if (terrain.surfaceInfo == surface.surfIndex) { + var id = surface.terrainMaterials.First().baseMaterials.First().materialDid; + mapTerrain.Add((int)terrain.terrainIndex, id); + break; + } + } } - return null; + } + private int GetIndex(DataId id) { + foreach(var mat in mapTerrain) { + if(mat.Value == id) return mat.Key; + } + return 0; } public void GenerateUV() { int count = (int)Math.Ceiling(Math.Sqrt(textureCoord.Count)); diff --git a/LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs b/LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs index 65c5457..55dd4f9 100644 --- a/LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs +++ b/LandblockExtraction/AtlasMaker/TerrainInfo/SubTerrain.cs @@ -1,13 +1,19 @@ -namespace LandblockExtraction.AtlasMaker; +using System.Numerics; + +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) { + public Vector4 Color { get; set; } + public Vector4 farColor { get; set; } + public SubTerrain(int terrainIndex, float minPitch, float maxPitch, Vector4 color, Vector4 farcolor) { this.terrainIndex = terrainIndex; this.minPitch = minPitch; this.maxPitch = maxPitch; + this.Color = color; + this.farColor = farcolor; } public bool MatchesPitch(float pitch) { return pitch >= minPitch && pitch <= maxPitch; diff --git a/LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs b/LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs index 8e6c097..5f5fc19 100644 --- a/LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs +++ b/LandblockExtraction/AtlasMaker/TerrainInfo/Terrain.cs @@ -11,12 +11,12 @@ public class Terrain { subTerrains.Add(sousTerrain); } - public int DetermineSubTerrain(float pitch) { + public SubTerrain DetermineSubTerrain(float pitch) { foreach (var terrain in subTerrains) { if (terrain.MatchesPitch(pitch)) { - return terrain.terrainIndex; + return terrain; } } - return terrainIndex; + return subTerrains.First(); } } diff --git a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs index 4c12d42..664456f 100644 --- a/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs +++ b/LandblockExtraction/LandBlockExtractor/LandBlockExtrator.cs @@ -2,6 +2,7 @@ using LandblockExtraction.AtlasMaker; using LandblockExtraction.DatEngine; using LandblockExtraction.Tools; +using System; using System.Numerics; namespace LandblockExtraction.LandBlockExtractor { @@ -14,13 +15,15 @@ namespace LandblockExtraction.LandBlockExtractor { private readonly int BlockSize = 17; private readonly int cellSize = 8; + private int[] indiceBase; + public LandBlockExtrator(PortalEngine portalEngine, CellEngine cellEngine) { this.portalEngine = portalEngine; this.cellEngine = cellEngine; terrainAtlasManager = new TerrainAtlasManager(portalEngine); - terrainAtlasManager.ExtractTexture(); - terrainAtlasManager.GenerateUV(); + + indiceBase = GenerateBasicIndices(); } public BlockStruct? GetBlock(int landX, int landY) { @@ -37,17 +40,50 @@ namespace LandblockExtraction.LandBlockExtractor { for (int x = 0; x < BlockSize; x++) { var indice = y * BlockSize + x; blockStruct.verticesStruct.position[indice] = GenerateVertexPosition(landX, landY, x, y, blockData.heights[indice]); - blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]); - blockStruct.verticesStruct.farcolor[indice] = GenerateVertexFarColor(blockData.cellInfos[indice]); + blockStruct.indices = indiceBase; + //blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]); + //blockStruct.verticesStruct.farcolor[indice] = GenerateVertexFarColor(blockData.cellInfos[indice]); blockStruct.verticesStruct.terraintype[indice] = GenerateVertexTerrainType(blockData.cellInfos[indice]); - blockStruct.indices = GenerateBasicIndices(); } } + GenerateBasicNormalandRighTexture(blockStruct); DoubleEdgeVertices(blockStruct); return blockStruct; } + private void GenerateBasicNormalandRighTexture(BlockStruct blockStruct) { + for (int i = 0; i < blockStruct.indices.Length; i += 6) { + int index1 = blockStruct.indices[i]; + int index2 = blockStruct.indices[i + 1]; + int index3 = blockStruct.indices[i + 2]; + int index4 = blockStruct.indices[i + 5]; + + Vector3 vertex1 = blockStruct.verticesStruct.position[index1]; + Vector3 vertex2 = blockStruct.verticesStruct.position[index2]; + Vector3 vertex3 = blockStruct.verticesStruct.position[index3]; + Vector3 vertex4 = blockStruct.verticesStruct.position[index4]; + + var normal = MathOperations.CalculateQuadNormal(vertex1, vertex2, vertex3, vertex4); + + 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); + + InitColorFarColorTerrain(index1, sub1, blockStruct); + InitColorFarColorTerrain(index2, sub2, blockStruct); + InitColorFarColorTerrain(index3, sub3, blockStruct); + InitColorFarColorTerrain(index4, sub4, blockStruct); + } + } + + private void InitColorFarColorTerrain(int index, SubTerrain terrain, BlockStruct blockStruct) { + blockStruct.verticesStruct.terraintype[index].X = terrain.terrainIndex; + blockStruct.verticesStruct.color[index] = terrain.Color; + blockStruct.verticesStruct.farcolor[index] = terrain.farColor; + } + private int[] GenerateBasicIndices() { List indices = new List(); @@ -123,7 +159,6 @@ namespace LandblockExtraction.LandBlockExtractor { List newFarColors = new List(); List newTexCoord = new List(); List newTerrainTypes = new List(); - List newRealTerrainType = new List(); int originalVertexCount = blockStruct.verticesStruct.position.Length; int originalIndicesCount = blockStruct.indices.Length; @@ -136,43 +171,32 @@ namespace LandblockExtraction.LandBlockExtractor { var two = blockStruct.indices[i + 1]; var three = blockStruct.indices[i + 2]; var foor = blockStruct.indices[i + 5]; + Vector4 terrainType = new Vector4(blockStruct.verticesStruct.terraintype[one].X, + blockStruct.verticesStruct.terraintype[two].X, + blockStruct.verticesStruct.terraintype[three].X, + blockStruct.verticesStruct.terraintype[foor].X); + newPositions.Add(blockStruct.verticesStruct.position[one]); newColors.Add(blockStruct.verticesStruct.color[one]); newFarColors.Add(blockStruct.verticesStruct.farcolor[one]); - newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, - blockStruct.verticesStruct.terraintype[two].X, - blockStruct.verticesStruct.terraintype[three].X, - blockStruct.verticesStruct.terraintype[foor].X)); - newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[one].X); + newTerrainTypes.Add(terrainType); newPositions.Add(blockStruct.verticesStruct.position[two]); newColors.Add(blockStruct.verticesStruct.color[two]); newFarColors.Add(blockStruct.verticesStruct.farcolor[two]); - newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, - blockStruct.verticesStruct.terraintype[two].X, - blockStruct.verticesStruct.terraintype[three].X, - blockStruct.verticesStruct.terraintype[foor].X)); - newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[two].X); + newTerrainTypes.Add(terrainType); newPositions.Add(blockStruct.verticesStruct.position[three]); newColors.Add(blockStruct.verticesStruct.color[three]); newFarColors.Add(blockStruct.verticesStruct.farcolor[three]); - newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, - blockStruct.verticesStruct.terraintype[two].X, - blockStruct.verticesStruct.terraintype[three].X, - blockStruct.verticesStruct.terraintype[foor].X)); - newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[three].X); + newTerrainTypes.Add(terrainType); newPositions.Add(blockStruct.verticesStruct.position[foor]); newColors.Add(blockStruct.verticesStruct.color[foor]); newFarColors.Add(blockStruct.verticesStruct.farcolor[foor]); - newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, - blockStruct.verticesStruct.terraintype[two].X, - blockStruct.verticesStruct.terraintype[three].X, - blockStruct.verticesStruct.terraintype[foor].X)); - newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[foor].X); + newTerrainTypes.Add(terrainType); newTexCoord.Add(new(0, 0)); newTexCoord.Add(new(0, 1)); @@ -198,7 +222,6 @@ namespace LandblockExtraction.LandBlockExtractor { blockStruct.verticesStruct.farcolor = newFarColors.ToArray(); blockStruct.verticesStruct.texturecoord = newTexCoord.ToArray(); blockStruct.verticesStruct.terraintype = newTerrainTypes.ToArray(); - blockStruct.verticesStruct.realtype = newRealTerrainType.ToArray(); blockStruct.indices = GenerateNewsIndices(newPositions.Count); } @@ -206,12 +229,12 @@ namespace LandblockExtraction.LandBlockExtractor { private int[] GenerateNewsIndices(int count) { List indices = new List(); for (int i = 0; i < count; i = i + 4) { - indices.Add(i); - indices.Add(i + 1); - indices.Add(i + 2); - indices.Add(i + 2); - indices.Add(i + 1); - indices.Add(i + 3); + indices.Add(i); //A + indices.Add(i + 1); //B + indices.Add(i + 2); //C + indices.Add(i + 2); //C + indices.Add(i + 1); //B + indices.Add(i + 3); //D } return indices.ToArray(); diff --git a/LandblockExtraction/LandBlockExtractor/Utilities/VerticesStruct.cs b/LandblockExtraction/LandBlockExtractor/Utilities/VerticesStruct.cs index 938ab64..342de34 100644 --- a/LandblockExtraction/LandBlockExtractor/Utilities/VerticesStruct.cs +++ b/LandblockExtraction/LandBlockExtractor/Utilities/VerticesStruct.cs @@ -8,7 +8,6 @@ namespace LandblockExtraction.LandBlockExtractor { public Vector4[] farcolor { get; set; } public Vector2[] texturecoord { get; set; } public Vector4[] terraintype { get; set; } - public float[] realtype { get; set; } public VerticesStruct(int blockSize) { position = new Vector3[blockSize * blockSize]; @@ -17,13 +16,12 @@ namespace LandblockExtraction.LandBlockExtractor { farcolor = new Vector4[blockSize * blockSize]; texturecoord = new Vector2[blockSize * blockSize]; terraintype = new Vector4[blockSize * blockSize]; - realtype = new float[blockSize * blockSize]; } public float[] Vertices() { int length = position.Length; - 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) { + float[] vertices = new float[length * 20]; + for (int i = 0, vi = 0; i < length; i++, vi += 20) { vertices[vi] = position[i].X; vertices[vi + 1] = position[i].Y; vertices[vi + 2] = position[i].Z; @@ -44,7 +42,6 @@ namespace LandblockExtraction.LandBlockExtractor { 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/Map3DRendering/MapRender.cs b/Map3DRendering/MapRender.cs index 769fd34..1434eaf 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 = 21; + int lenghPacket = 20; // 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(); @@ -112,10 +112,6 @@ namespace Map3DRendering { var terraintypeLocation = _shader.GetAttribLocation("aTexType"); GL.EnableVertexAttribArray(terraintypeLocation); 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), 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 29f168c..3ee40c0 100644 --- a/Map3DRendering/Shaders/shader.frag +++ b/Map3DRendering/Shaders/shader.frag @@ -12,7 +12,6 @@ in vec3 Normal; in vec3 FragPos; in vec2 TexCoord; in vec4 TexType; -in float RealType; void main() { diff --git a/Map3DRendering/Shaders/shader.vert b/Map3DRendering/Shaders/shader.vert index 7f31960..084f70a 100644 --- a/Map3DRendering/Shaders/shader.vert +++ b/Map3DRendering/Shaders/shader.vert @@ -5,7 +5,6 @@ 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; @@ -17,7 +16,6 @@ out vec4 Color; out vec4 FarColor; out vec2 TexCoord; out vec4 TexType; -out float RealType; void main() { @@ -28,5 +26,4 @@ void main() FarColor = aColorFar; TexCoord = aTexCoord; TexType = aTexType; - RealType = aRealTexType; } \ No newline at end of file diff --git a/Map3DRendering/Window.cs b/Map3DRendering/Window.cs index d2af5e8..d8beae7 100644 --- a/Map3DRendering/Window.cs +++ b/Map3DRendering/Window.cs @@ -83,7 +83,7 @@ namespace Map3DRendering { //_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", _lightPosVec); + _shader.SetVector3("lightPos", _camera.Position); GL.LineWidth(5.0f); From 7c7dddde5e0add0b6e7286c8f6ad04daeaf880ed Mon Sep 17 00:00:00 2001 From: Troispoils Date: Sun, 3 Mar 2024 19:07:02 +0100 Subject: [PATCH 7/7] 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); + } +}