Compare commits
4 commits
main
...
Save-Doubl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af9c782c1e | ||
|
|
443b3a319d | ||
|
|
3aa6b5bc83 | ||
|
|
4ea0101cf6 |
14 changed files with 147 additions and 214 deletions
|
|
@ -1,23 +1,26 @@
|
|||
using AC2RE.Definitions;
|
||||
using ImageMagick;
|
||||
using LandblockExtraction.DatEngine;
|
||||
using LandblockExtraction.Tools;
|
||||
using System.Numerics;
|
||||
|
||||
namespace LandblockExtraction.AtlasMaker;
|
||||
public class TerrainAtlasManager {
|
||||
private readonly string PATHTERRAINIMG = @".\terrains";
|
||||
private PortalEngine portalEngine;
|
||||
private Dictionary<int, DataId> mapTerrain;
|
||||
|
||||
public Dictionary<int, Vector2> textureCoord;
|
||||
public Dictionary<int, MagickImage> terrainTexture;
|
||||
public Dictionary<int, Terrain> terrains;
|
||||
public TerrainAtlasManager(PortalEngine portalEngine) {
|
||||
this.portalEngine = portalEngine;
|
||||
mapTerrain = new();
|
||||
textureCoord = new Dictionary<int, Vector2>();
|
||||
terrainTexture = new Dictionary<int, MagickImage>();
|
||||
terrains = new Dictionary<int, Terrain>();
|
||||
|
||||
testGenerateList();
|
||||
InitialiseTerrainDic();
|
||||
GenerateTerrain();
|
||||
}
|
||||
public void ExtractTexture() {
|
||||
using (var atlasBuilder = new AtlasBuilder(portalEngine)) {
|
||||
|
|
@ -37,38 +40,56 @@ public class TerrainAtlasManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
/*private Terrain GenerateSubTerrain(int index, List<CSurfaceDesc.MaterialGroup> 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) {
|
||||
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("--------");
|
||||
}
|
||||
}
|
||||
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) {
|
||||
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));
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 != 256) continue;
|
||||
if (image.width != 64) continue;
|
||||
var dataImg = DDSHeader.Generate(image);
|
||||
using (MagickImage realImg = new MagickImage(dataImg)) {
|
||||
magickImage = new(realImg);
|
||||
|
|
|
|||
|
|
@ -15,14 +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();
|
||||
terrainAtlasManager.SaveAllTerrain();
|
||||
|
||||
indiceBase = GenerateBasicIndices();
|
||||
}
|
||||
|
||||
public BlockStruct? GetBlock(int landX, int landY) {
|
||||
|
|
@ -39,77 +40,19 @@ 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.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.verticesStruct.texturecoord[indice] = GenerateUVForSubTile(x, y);
|
||||
blockStruct.indices = GenerateBasicIndices();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//blockStruct.verticesStruct.texturecoord = GenerateBasicUVTest(blockStruct);
|
||||
blockStruct.verticesStruct.normal = GenerateBasicNormal(blockStruct);
|
||||
//DoubleEdgeVertices(blockStruct);
|
||||
|
||||
Dictionary<int, int> testTerr = new Dictionary<int, int>();
|
||||
foreach (var test in blockStruct.verticesStruct.terraintype) {
|
||||
var type = (int)test.X;
|
||||
if (testTerr.ContainsKey(type)) {
|
||||
testTerr[type]++;
|
||||
} else {
|
||||
testTerr.Add(type, 1);
|
||||
}
|
||||
}
|
||||
GenerateBasicNormal(blockStruct);
|
||||
DoubleEdgeVertices(blockStruct);
|
||||
|
||||
return blockStruct;
|
||||
}
|
||||
|
||||
private Vector2 GenerateBasicUV(int x, int y) {
|
||||
float u = (float)x / (BlockSize - 1) * 8;
|
||||
float v = (float)y / (BlockSize - 1) * 8;
|
||||
|
||||
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];
|
||||
|
||||
// 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
|
||||
private void GenerateBasicNormal(BlockStruct blockStruct) {
|
||||
for (int i = 0; i < blockStruct.indices.Length; i += 6) {
|
||||
int index1 = blockStruct.indices[i];
|
||||
int index2 = blockStruct.indices[i + 1];
|
||||
|
|
@ -121,27 +64,14 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
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.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
|
||||
for (int i = 0; i < normals.Length; i++) {
|
||||
normals[i] = Vector3.Normalize(normals[i]);
|
||||
blockStruct.verticesStruct.normal[index1] = normal;
|
||||
blockStruct.verticesStruct.normal[index2] = normal;
|
||||
blockStruct.verticesStruct.normal[index3] = normal;
|
||||
blockStruct.verticesStruct.normal[index4] = normal;
|
||||
}
|
||||
|
||||
return normals;
|
||||
}
|
||||
|
||||
private int[] GenerateBasicIndices() {
|
||||
List<int> indices = new List<int>();
|
||||
|
||||
|
|
@ -171,10 +101,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);
|
||||
|
|
@ -217,7 +146,6 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
List<Vector4> newFarColors = new List<Vector4>();
|
||||
List<Vector2> newTexCoord = new List<Vector2>();
|
||||
List<Vector4> newTerrainTypes = new List<Vector4>();
|
||||
List<float> newRealTerrainType = new List<float>();
|
||||
|
||||
int originalVertexCount = blockStruct.verticesStruct.position.Length;
|
||||
int originalIndicesCount = blockStruct.indices.Length;
|
||||
|
|
@ -229,59 +157,42 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
var one = blockStruct.indices[i + 0];
|
||||
var two = blockStruct.indices[i + 1];
|
||||
var three = blockStruct.indices[i + 2];
|
||||
var foor = blockStruct.indices[i + 5];
|
||||
var four = 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[four].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);
|
||||
newNormals.Add(blockStruct.verticesStruct.normal[one]);
|
||||
newTerrainTypes.Add(terrainType);
|
||||
|
||||
/*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,
|
||||
blockStruct.verticesStruct.terraintype[two].X,
|
||||
blockStruct.verticesStruct.terraintype[three].X,
|
||||
blockStruct.verticesStruct.terraintype[foor].X));
|
||||
newRealTerrainType.Add(blockStruct.verticesStruct.terraintype[two].X);
|
||||
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]);
|
||||
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);
|
||||
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]);
|
||||
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);
|
||||
newPositions.Add(blockStruct.verticesStruct.position[four]);
|
||||
newColors.Add(blockStruct.verticesStruct.color[four]);
|
||||
newFarColors.Add(blockStruct.verticesStruct.farcolor[four]);
|
||||
newNormals.Add(blockStruct.verticesStruct.normal[four]);
|
||||
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);
|
||||
newTexCoord.Add(new(1, 1));
|
||||
}
|
||||
|
||||
// Ajouter les nouveaux sommets à la structure BlockStruct (étape 2)
|
||||
|
|
@ -292,7 +203,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);
|
||||
}
|
||||
|
|
@ -300,12 +210,12 @@ namespace LandblockExtraction.LandBlockExtractor {
|
|||
private int[] GenerateNewsIndices(int count) {
|
||||
List<int> indices = new List<int>();
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,7 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using OpenTK.Mathematics;
|
||||
|
||||
namespace Map3DRendering {
|
||||
namespace Map3DRendering.Common {
|
||||
// A simple class meant to help create shaders.
|
||||
public class Shader {
|
||||
public readonly int Handle;
|
||||
|
|
@ -167,9 +163,18 @@ namespace Map3DRendering {
|
|||
/// </summary>
|
||||
/// <param name="name">The name of the uniform</param>
|
||||
/// <param name="data">The data to set</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,7 @@
|
|||
using OpenTK.Graphics.OpenGL4;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
|
||||
using StbImageSharp;
|
||||
using System.IO;
|
||||
|
||||
namespace Map3DRendering {
|
||||
namespace Map3DRendering.Common {
|
||||
// A helper class, much like Shader, meant to simplify loading textures.
|
||||
public class Texture {
|
||||
public readonly int Handle;
|
||||
|
|
@ -105,6 +101,7 @@ namespace Map3DRendering {
|
|||
return new Texture(handle);
|
||||
}
|
||||
|
||||
|
||||
public Texture(int glHandle) {
|
||||
Handle = glHandle;
|
||||
}
|
||||
|
|
@ -121,5 +118,11 @@ namespace Map3DRendering {
|
|||
GL.ActiveTexture(unit);
|
||||
GL.BindTexture(TextureTarget.Texture2DArray, Handle);
|
||||
}
|
||||
|
||||
|
||||
public void Assign(int shader, int i) {
|
||||
int location = GL.GetUniformLocation(shader, "textures[" + i.ToString() + "]");
|
||||
GL.Uniform1(location, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ namespace Map3DRendering {
|
|||
private readonly int BlockSize = 17;
|
||||
private readonly int allBlocks = 255 * 17 * 255 * 17;
|
||||
private readonly int cellSize = 8;
|
||||
private readonly int radius = 0x5; // Rayon du voisinage
|
||||
private readonly int radius = 0x10; // Rayon du voisinage
|
||||
|
||||
public int[,] _vertexArrayObject;
|
||||
public int[,] _vertexBufferObject;
|
||||
|
|
@ -44,7 +44,6 @@ namespace Map3DRendering {
|
|||
|
||||
CalculeRadius(currentLandBlockX, currentLandBlockY);
|
||||
}
|
||||
|
||||
public int GetIndiceLenght() {
|
||||
return (17 - 1) * (17 - 1) * 6; //Always that.
|
||||
}
|
||||
|
|
@ -70,7 +69,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 +111,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++) {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ namespace Map3DRendering {
|
|||
public static class Program {
|
||||
private static void Main() {
|
||||
var nativeWindowSettings = new NativeWindowSettings() {
|
||||
ClientSize = new Vector2i(800, 600),
|
||||
Size = new Vector2i(800, 600),
|
||||
Title = "LearnOpenTK - Map AC2",
|
||||
// This is needed to run on macos
|
||||
Flags = ContextFlags.ForwardCompatible,
|
||||
|
|
|
|||
|
|
@ -2,33 +2,38 @@
|
|||
|
||||
out vec4 outputColor;
|
||||
|
||||
uniform sampler2DArray texture0;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform vec3 lightPos;
|
||||
uniform vec3 lightColor;
|
||||
uniform sampler2DArray texture0;
|
||||
|
||||
in vec4 Color;
|
||||
in vec4 FarColor;
|
||||
in vec3 Normal;
|
||||
in vec3 FragPos;
|
||||
in vec2 TexCoord;
|
||||
in vec4 TexType;
|
||||
in float RealType;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 color0 = texture(texture0, vec3(TexCoord, TexType.x));
|
||||
vec4 blendedColor[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
float type = TexType[i];
|
||||
blendedColor[i] = texture(texture0, vec3(TexCoord, type));
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
vec3 norm = normalize(Normal);
|
||||
|
||||
vec4 finalColor = color0; //mix(color0, color1, norm.y);
|
||||
|
||||
vec3 lightDir = normalize(lightPos - FragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * lightColor;
|
||||
|
||||
vec4 litColor = vec4(diffuse, 1.0) * finalColor * Color;
|
||||
vec4 litColor = vec4(diffuse, 1.0) * finalColor;
|
||||
|
||||
float distance = length(viewPos - FragPos);
|
||||
float interpolationFactor = clamp(distance / 1000.0, 0.0, 1.0);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -55,20 +55,16 @@ namespace Map3DRendering {
|
|||
mapRender.OnLoad(_shader);
|
||||
|
||||
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.UseArray(TextureUnit.Texture0);
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -79,17 +75,15 @@ namespace Map3DRendering {
|
|||
|
||||
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||
|
||||
_texture.UseArray(TextureUnit.Texture0);
|
||||
_shader.Use();
|
||||
_texture.UseArray(TextureUnit.Texture0);
|
||||
|
||||
_shader.SetMatrix4("view", _camera.GetViewMatrix());
|
||||
_shader.SetMatrix4("projection", _camera.GetProjectionMatrix());
|
||||
|
||||
//_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);
|
||||
_shader.SetVector3("lightPos", _camera.Position);
|
||||
|
||||
GL.LineWidth(5.0f);
|
||||
|
||||
|
|
@ -149,10 +143,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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue