better system for texture terrain.

This commit is contained in:
Troispoils 2024-03-01 21:56:18 +01:00
parent 3aa6b5bc83
commit 443b3a319d
9 changed files with 120 additions and 69 deletions

View file

@ -1,22 +1,26 @@
using AC2RE.Definitions; using AC2RE.Definitions;
using ImageMagick; using ImageMagick;
using LandblockExtraction.DatEngine; using LandblockExtraction.DatEngine;
using LandblockExtraction.Tools;
using System.Numerics; using System.Numerics;
namespace LandblockExtraction.AtlasMaker; namespace LandblockExtraction.AtlasMaker;
public class TerrainAtlasManager { public class TerrainAtlasManager {
private PortalEngine portalEngine; private PortalEngine portalEngine;
private Dictionary<int, DataId> mapTerrain;
public Dictionary<int, Vector2> textureCoord; public Dictionary<int, Vector2> textureCoord;
public Dictionary<int, MagickImage> terrainTexture; public Dictionary<int, MagickImage> terrainTexture;
public Dictionary<int, Terrain> terrains; public Dictionary<int, Terrain> terrains;
public TerrainAtlasManager(PortalEngine portalEngine) { public TerrainAtlasManager(PortalEngine portalEngine) {
this.portalEngine = portalEngine; this.portalEngine = portalEngine;
mapTerrain = new();
textureCoord = new Dictionary<int, Vector2>(); textureCoord = new Dictionary<int, Vector2>();
terrainTexture = new Dictionary<int, MagickImage>(); terrainTexture = new Dictionary<int, MagickImage>();
terrains = new Dictionary<int, Terrain>(); terrains = new Dictionary<int, Terrain>();
testGenerateList(); InitialiseTerrainDic();
GenerateTerrain();
} }
public void ExtractTexture() { public void ExtractTexture() {
using (var atlasBuilder = new AtlasBuilder(portalEngine)) { using (var atlasBuilder = new AtlasBuilder(portalEngine)) {
@ -36,27 +40,56 @@ public class TerrainAtlasManager {
} }
} }
} }
/*private Terrain GenerateSubTerrain(int index, List<CSurfaceDesc.MaterialGroup> materialGroups) { private void GenerateTerrain() {
Terrain tmpTerrain = new Terrain(index); foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
foreach(var material in materialGroups) { Terrain tmpTer = new((int)terrain.terrainIndex);
int indexTer = GetIndexBySurface(material.s) foreach(var surface in portalEngine.cSurfaceDesc.surfaces) {
SubTerrain subTerrain = new(ma) 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() { private void testGenerateList() {
foreach(var surfaces in portalEngine.cSurfaceDesc.surfaces) { foreach (var terrain in portalEngine.cTerrainDesc.terrains) {
Console.WriteLine($"{surfaces.surfIndex} :"); foreach (var surfaces in portalEngine.cSurfaceDesc.surfaces) {
foreach( var surface in surfaces.terrainMaterials) { if (surfaces.surfIndex != terrain.surfaceInfo) continue;
Console.WriteLine($"\t[{surface.minPitch} < {surface.maxPitch}]({surface.baseMaterials.First().materialDid}) - Vec3: {surface.vertexColor.First().vertexColor}"); 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) { foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
if (terrain.surfaceInfo == surfaceIndex) foreach (var surface in portalEngine.cSurfaceDesc.surfaces) {
return (int)terrain.terrainIndex; 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() { public void GenerateUV() {
int count = (int)Math.Ceiling(Math.Sqrt(textureCoord.Count)); int count = (int)Math.Ceiling(Math.Sqrt(textureCoord.Count));

View file

@ -1,13 +1,19 @@
namespace LandblockExtraction.AtlasMaker; using System.Numerics;
namespace LandblockExtraction.AtlasMaker;
public class SubTerrain { public class SubTerrain {
public int terrainIndex { get; set; } public int terrainIndex { get; set; }
public float minPitch { get; set; } public float minPitch { get; set; }
public float maxPitch { 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.terrainIndex = terrainIndex;
this.minPitch = minPitch; this.minPitch = minPitch;
this.maxPitch = maxPitch; this.maxPitch = maxPitch;
this.Color = color;
this.farColor = farcolor;
} }
public bool MatchesPitch(float pitch) { public bool MatchesPitch(float pitch) {
return pitch >= minPitch && pitch <= maxPitch; return pitch >= minPitch && pitch <= maxPitch;

View file

@ -11,12 +11,12 @@ public class Terrain {
subTerrains.Add(sousTerrain); subTerrains.Add(sousTerrain);
} }
public int DetermineSubTerrain(float pitch) { public SubTerrain DetermineSubTerrain(float pitch) {
foreach (var terrain in subTerrains) { foreach (var terrain in subTerrains) {
if (terrain.MatchesPitch(pitch)) { if (terrain.MatchesPitch(pitch)) {
return terrain.terrainIndex; return terrain;
} }
} }
return terrainIndex; return subTerrains.First();
} }
} }

View file

@ -2,6 +2,7 @@
using LandblockExtraction.AtlasMaker; using LandblockExtraction.AtlasMaker;
using LandblockExtraction.DatEngine; using LandblockExtraction.DatEngine;
using LandblockExtraction.Tools; using LandblockExtraction.Tools;
using System;
using System.Numerics; using System.Numerics;
namespace LandblockExtraction.LandBlockExtractor { namespace LandblockExtraction.LandBlockExtractor {
@ -14,13 +15,15 @@ namespace LandblockExtraction.LandBlockExtractor {
private readonly int BlockSize = 17; private readonly int BlockSize = 17;
private readonly int cellSize = 8; private readonly int cellSize = 8;
private int[] indiceBase;
public LandBlockExtrator(PortalEngine portalEngine, CellEngine cellEngine) { public LandBlockExtrator(PortalEngine portalEngine, CellEngine cellEngine) {
this.portalEngine = portalEngine; this.portalEngine = portalEngine;
this.cellEngine = cellEngine; this.cellEngine = cellEngine;
terrainAtlasManager = new TerrainAtlasManager(portalEngine); terrainAtlasManager = new TerrainAtlasManager(portalEngine);
terrainAtlasManager.ExtractTexture();
terrainAtlasManager.GenerateUV(); indiceBase = GenerateBasicIndices();
} }
public BlockStruct? GetBlock(int landX, int landY) { public BlockStruct? GetBlock(int landX, int landY) {
@ -37,17 +40,50 @@ namespace LandblockExtraction.LandBlockExtractor {
for (int x = 0; x < BlockSize; x++) { for (int x = 0; x < BlockSize; x++) {
var indice = y * BlockSize + x; var indice = y * BlockSize + x;
blockStruct.verticesStruct.position[indice] = GenerateVertexPosition(landX, landY, x, y, blockData.heights[indice]); blockStruct.verticesStruct.position[indice] = GenerateVertexPosition(landX, landY, x, y, blockData.heights[indice]);
blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]); blockStruct.indices = indiceBase;
blockStruct.verticesStruct.farcolor[indice] = GenerateVertexFarColor(blockData.cellInfos[indice]); //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.terraintype[indice] = GenerateVertexTerrainType(blockData.cellInfos[indice]);
blockStruct.indices = GenerateBasicIndices();
} }
} }
GenerateBasicNormalandRighTexture(blockStruct);
DoubleEdgeVertices(blockStruct); DoubleEdgeVertices(blockStruct);
return 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() { private int[] GenerateBasicIndices() {
List<int> indices = new List<int>(); List<int> indices = new List<int>();
@ -123,7 +159,6 @@ namespace LandblockExtraction.LandBlockExtractor {
List<Vector4> newFarColors = new List<Vector4>(); List<Vector4> newFarColors = new List<Vector4>();
List<Vector2> newTexCoord = new List<Vector2>(); List<Vector2> newTexCoord = new List<Vector2>();
List<Vector4> newTerrainTypes = new List<Vector4>(); List<Vector4> newTerrainTypes = new List<Vector4>();
List<float> newRealTerrainType = new List<float>();
int originalVertexCount = blockStruct.verticesStruct.position.Length; int originalVertexCount = blockStruct.verticesStruct.position.Length;
int originalIndicesCount = blockStruct.indices.Length; int originalIndicesCount = blockStruct.indices.Length;
@ -136,43 +171,32 @@ namespace LandblockExtraction.LandBlockExtractor {
var two = blockStruct.indices[i + 1]; var two = blockStruct.indices[i + 1];
var three = blockStruct.indices[i + 2]; var three = blockStruct.indices[i + 2];
var foor = blockStruct.indices[i + 5]; 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]); newPositions.Add(blockStruct.verticesStruct.position[one]);
newColors.Add(blockStruct.verticesStruct.color[one]); newColors.Add(blockStruct.verticesStruct.color[one]);
newFarColors.Add(blockStruct.verticesStruct.farcolor[one]); newFarColors.Add(blockStruct.verticesStruct.farcolor[one]);
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, newTerrainTypes.Add(terrainType);
blockStruct.verticesStruct.terraintype[two].X,
blockStruct.verticesStruct.terraintype[three].X,
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]); newColors.Add(blockStruct.verticesStruct.color[two]);
newFarColors.Add(blockStruct.verticesStruct.farcolor[two]); newFarColors.Add(blockStruct.verticesStruct.farcolor[two]);
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, newTerrainTypes.Add(terrainType);
blockStruct.verticesStruct.terraintype[two].X,
blockStruct.verticesStruct.terraintype[three].X,
blockStruct.verticesStruct.terraintype[foor].X));
newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[two].X);
newPositions.Add(blockStruct.verticesStruct.position[three]); newPositions.Add(blockStruct.verticesStruct.position[three]);
newColors.Add(blockStruct.verticesStruct.color[three]); newColors.Add(blockStruct.verticesStruct.color[three]);
newFarColors.Add(blockStruct.verticesStruct.farcolor[three]); newFarColors.Add(blockStruct.verticesStruct.farcolor[three]);
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, newTerrainTypes.Add(terrainType);
blockStruct.verticesStruct.terraintype[two].X,
blockStruct.verticesStruct.terraintype[three].X,
blockStruct.verticesStruct.terraintype[foor].X));
newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[three].X);
newPositions.Add(blockStruct.verticesStruct.position[foor]); newPositions.Add(blockStruct.verticesStruct.position[foor]);
newColors.Add(blockStruct.verticesStruct.color[foor]); newColors.Add(blockStruct.verticesStruct.color[foor]);
newFarColors.Add(blockStruct.verticesStruct.farcolor[foor]); newFarColors.Add(blockStruct.verticesStruct.farcolor[foor]);
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X, newTerrainTypes.Add(terrainType);
blockStruct.verticesStruct.terraintype[two].X,
blockStruct.verticesStruct.terraintype[three].X,
blockStruct.verticesStruct.terraintype[foor].X));
newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[foor].X);
newTexCoord.Add(new(0, 0)); newTexCoord.Add(new(0, 0));
newTexCoord.Add(new(0, 1)); newTexCoord.Add(new(0, 1));
@ -198,7 +222,6 @@ namespace LandblockExtraction.LandBlockExtractor {
blockStruct.verticesStruct.farcolor = newFarColors.ToArray(); blockStruct.verticesStruct.farcolor = newFarColors.ToArray();
blockStruct.verticesStruct.texturecoord = newTexCoord.ToArray(); blockStruct.verticesStruct.texturecoord = newTexCoord.ToArray();
blockStruct.verticesStruct.terraintype = newTerrainTypes.ToArray(); blockStruct.verticesStruct.terraintype = newTerrainTypes.ToArray();
blockStruct.verticesStruct.realtype = newRealTerrainType.ToArray();
blockStruct.indices = GenerateNewsIndices(newPositions.Count); blockStruct.indices = GenerateNewsIndices(newPositions.Count);
} }
@ -206,12 +229,12 @@ namespace LandblockExtraction.LandBlockExtractor {
private int[] GenerateNewsIndices(int count) { private int[] GenerateNewsIndices(int count) {
List<int> indices = new List<int>(); List<int> indices = new List<int>();
for (int i = 0; i < count; i = i + 4) { for (int i = 0; i < count; i = i + 4) {
indices.Add(i); indices.Add(i); //A
indices.Add(i + 1); indices.Add(i + 1); //B
indices.Add(i + 2); indices.Add(i + 2); //C
indices.Add(i + 2); indices.Add(i + 2); //C
indices.Add(i + 1); indices.Add(i + 1); //B
indices.Add(i + 3); indices.Add(i + 3); //D
} }
return indices.ToArray(); return indices.ToArray();

View file

@ -8,7 +8,6 @@ namespace LandblockExtraction.LandBlockExtractor {
public Vector4[] farcolor { get; set; } public Vector4[] farcolor { get; set; }
public Vector2[] texturecoord { get; set; } public Vector2[] texturecoord { get; set; }
public Vector4[] terraintype { get; set; } public Vector4[] terraintype { get; set; }
public float[] realtype { get; set; }
public VerticesStruct(int blockSize) { public VerticesStruct(int blockSize) {
position = new Vector3[blockSize * blockSize]; position = new Vector3[blockSize * blockSize];
@ -17,13 +16,12 @@ namespace LandblockExtraction.LandBlockExtractor {
farcolor = new Vector4[blockSize * blockSize]; farcolor = new Vector4[blockSize * blockSize];
texturecoord = new Vector2[blockSize * blockSize]; texturecoord = new Vector2[blockSize * blockSize];
terraintype = new Vector4[blockSize * blockSize]; terraintype = new Vector4[blockSize * blockSize];
realtype = new float[blockSize * blockSize];
} }
public float[] Vertices() { public float[] Vertices() {
int length = position.Length; int length = position.Length;
float[] vertices = new float[length * 21]; // 3 pour position, 4 pour color, et 4 pour farcolor float[] vertices = new float[length * 20];
for (int i = 0, vi = 0; i < length; i++, vi += 21) { for (int i = 0, vi = 0; i < length; i++, vi += 20) {
vertices[vi] = position[i].X; vertices[vi] = position[i].X;
vertices[vi + 1] = position[i].Y; vertices[vi + 1] = position[i].Y;
vertices[vi + 2] = position[i].Z; vertices[vi + 2] = position[i].Z;
@ -44,7 +42,6 @@ namespace LandblockExtraction.LandBlockExtractor {
vertices[vi + 17] = terraintype[i].Y; vertices[vi + 17] = terraintype[i].Y;
vertices[vi + 18] = terraintype[i].Z; vertices[vi + 18] = terraintype[i].Z;
vertices[vi + 19] = terraintype[i].W; vertices[vi + 19] = terraintype[i].W;
vertices[vi + 20] = realtype[i];
} }
return vertices; return vertices;
} }

View file

@ -70,7 +70,7 @@ namespace Map3DRendering {
} }
} }
private void InitializeBlock(int x, int y, BlockStruct block, Shader _shader) { 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)... // 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. // Utilisez le code de votre méthode OnLoad originale pour configurer le VAO, VBO et EBO.
int tempVertexArray = GL.GenVertexArray(); int tempVertexArray = GL.GenVertexArray();
@ -112,10 +112,6 @@ namespace Map3DRendering {
var terraintypeLocation = _shader.GetAttribLocation("aTexType"); var terraintypeLocation = _shader.GetAttribLocation("aTexType");
GL.EnableVertexAttribArray(terraintypeLocation); GL.EnableVertexAttribArray(terraintypeLocation);
GL.VertexAttribPointer(terraintypeLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 16 * 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), 20 * sizeof(float));
} }
public void Render(Shader shader) { public void Render(Shader shader) {
for (int y = startY; y <= endY; y++) { for (int y = startY; y <= endY; y++) {

View file

@ -12,7 +12,6 @@ in vec3 Normal;
in vec3 FragPos; in vec3 FragPos;
in vec2 TexCoord; in vec2 TexCoord;
in vec4 TexType; in vec4 TexType;
in float RealType;
void main() void main()
{ {

View file

@ -5,7 +5,6 @@ layout (location = 2) in vec4 aColor;
layout (location = 3) in vec4 aColorFar; layout (location = 3) in vec4 aColorFar;
layout (location = 4) in vec2 aTexCoord; layout (location = 4) in vec2 aTexCoord;
layout (location = 5) in vec4 aTexType; layout (location = 5) in vec4 aTexType;
layout (location = 6) in float aRealTexType;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
@ -17,7 +16,6 @@ out vec4 Color;
out vec4 FarColor; out vec4 FarColor;
out vec2 TexCoord; out vec2 TexCoord;
out vec4 TexType; out vec4 TexType;
out float RealType;
void main() void main()
{ {
@ -28,5 +26,4 @@ void main()
FarColor = aColorFar; FarColor = aColorFar;
TexCoord = aTexCoord; TexCoord = aTexCoord;
TexType = aTexType; TexType = aTexType;
RealType = aRealTexType;
} }

View file

@ -83,7 +83,7 @@ namespace Map3DRendering {
//_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("lightColor", new Vector3(1.0f, 1.0f, 1.0f));
_shader.SetVector3("lightPos", _lightPosVec); _shader.SetVector3("lightPos", _camera.Position);
GL.LineWidth(5.0f); GL.LineWidth(5.0f);