Compare commits
6 commits
main
...
WPF-Render
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bfe8d3f98e | ||
|
|
9ad4bc1c69 | ||
|
|
af9c782c1e | ||
|
|
443b3a319d | ||
|
|
3aa6b5bc83 | ||
|
|
4ea0101cf6 |
30 changed files with 926 additions and 223 deletions
|
|
@ -1,23 +1,47 @@
|
||||||
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 readonly string PATHTERRAINIMG = @".\terrains";
|
|
||||||
private PortalEngine portalEngine;
|
private PortalEngine portalEngine;
|
||||||
|
private Dictionary<int, DataId> mapTerrain;
|
||||||
|
private TexturesImage texturesImage;
|
||||||
|
|
||||||
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 Dictionary<DataId, int> texturesIndex;
|
||||||
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>();
|
||||||
|
texturesImage = new TexturesImage(portalEngine);
|
||||||
|
texturesIndex = new Dictionary<DataId, int>();
|
||||||
|
|
||||||
testGenerateList();
|
GenerateDictionaryTexture();
|
||||||
|
foreach(var img in texturesIndex) {
|
||||||
|
var i = texturesImage.GetImage(img.Key);
|
||||||
|
i.Write($"{img.Value}.jpg");
|
||||||
|
}
|
||||||
|
InitialiseTerrainDic();
|
||||||
|
GenerateTerrain();
|
||||||
|
}
|
||||||
|
private void GenerateDictionaryTexture() {
|
||||||
|
int index = 0;
|
||||||
|
foreach (var surfaces in portalEngine.cSurfaceDesc.surfaces) {
|
||||||
|
foreach (var surface in surfaces.terrainMaterials) {
|
||||||
|
var did = surface.baseMaterials.First().materialDid;
|
||||||
|
if (!texturesIndex.ContainsKey(did)) {
|
||||||
|
texturesIndex.Add(did, index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public void ExtractTexture() {
|
public void ExtractTexture() {
|
||||||
using (var atlasBuilder = new AtlasBuilder(portalEngine)) {
|
using (var atlasBuilder = new AtlasBuilder(portalEngine)) {
|
||||||
|
|
@ -37,38 +61,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("--------");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void SaveAllTerrain() {
|
private void InitialiseTerrainDic() {
|
||||||
if (!Directory.Exists(PATHTERRAINIMG)) {
|
foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
|
||||||
Directory.CreateDirectory(PATHTERRAINIMG);
|
foreach (var surface in portalEngine.cSurfaceDesc.surfaces) {
|
||||||
}
|
if (terrain.surfaceInfo == surface.surfIndex) {
|
||||||
|
var id = surface.terrainMaterials.First().baseMaterials.First().materialDid;
|
||||||
foreach(var img in terrainTexture) {
|
mapTerrain.Add((int)terrain.terrainIndex, id);
|
||||||
var path = Path.Combine(PATHTERRAINIMG, img.Key.ToString());
|
break;
|
||||||
|
}
|
||||||
img.Value.Write(path + ".jpg");
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private int? GetIndexBySurface(uint surfaceIndex) {
|
private int GetIndex(DataId id) {
|
||||||
foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
|
foreach(var mat in mapTerrain) {
|
||||||
if (terrain.surfaceInfo == surfaceIndex)
|
if(mat.Value == id) return mat.Key;
|
||||||
return (int)terrain.terrainIndex;
|
|
||||||
}
|
}
|
||||||
return null;
|
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));
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,12 @@ using LandblockExtraction.Tools;
|
||||||
namespace LandblockExtraction.AtlasMaker;
|
namespace LandblockExtraction.AtlasMaker;
|
||||||
public class TexturesImage {
|
public class TexturesImage {
|
||||||
private PortalEngine portalEngine;
|
private PortalEngine portalEngine;
|
||||||
|
private TextureEngine textureEngine;
|
||||||
//private TextureEngine textureEngine;
|
//private TextureEngine textureEngine;
|
||||||
|
|
||||||
public TexturesImage(PortalEngine portalEngine) {
|
public TexturesImage(PortalEngine portalEngine) {
|
||||||
this.portalEngine = portalEngine;
|
this.portalEngine = portalEngine;
|
||||||
|
textureEngine = new TextureEngine();
|
||||||
//this.textureEngine = new();
|
//this.textureEngine = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,7 +42,18 @@ public class TexturesImage {
|
||||||
if (!portalEngine.datReader.contains(img)) continue;
|
if (!portalEngine.datReader.contains(img)) continue;
|
||||||
using (var data = portalEngine.datReader.getFileReader(img)) {
|
using (var data = portalEngine.datReader.getFileReader(img)) {
|
||||||
var image = new RenderSurface(data);
|
var image = new RenderSurface(data);
|
||||||
if (image.width != 256) continue;
|
if (image.width != 16) continue;
|
||||||
|
var dataImg = DDSHeader.Generate(image);
|
||||||
|
using (MagickImage realImg = new MagickImage(dataImg)) {
|
||||||
|
magickImage = new(realImg);
|
||||||
|
}
|
||||||
|
return magickImage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (var img in texture.levelSurfaceDids) {
|
||||||
|
if (!portalEngine.datReader.contains(img)) continue;
|
||||||
|
using (var data = portalEngine.datReader.getFileReader(img)) {
|
||||||
|
var image = new RenderSurface(data);
|
||||||
var dataImg = DDSHeader.Generate(image);
|
var dataImg = DDSHeader.Generate(image);
|
||||||
using (MagickImage realImg = new MagickImage(dataImg)) {
|
using (MagickImage realImg = new MagickImage(dataImg)) {
|
||||||
magickImage = new(realImg);
|
magickImage = new(realImg);
|
||||||
|
|
|
||||||
|
|
@ -15,14 +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();
|
||||||
terrainAtlasManager.SaveAllTerrain();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BlockStruct? GetBlock(int landX, int landY) {
|
public BlockStruct? GetBlock(int landX, int landY) {
|
||||||
|
|
@ -39,77 +40,19 @@ 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.indices = indiceBase;
|
||||||
blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]);
|
blockStruct.verticesStruct.color[indice] = GenerateVertexColor(blockData.cellInfos[indice]);
|
||||||
blockStruct.verticesStruct.farcolor[indice] = GenerateVertexFarColor(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.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>();
|
GenerateBasicNormal(blockStruct);
|
||||||
foreach (var test in blockStruct.verticesStruct.terraintype) {
|
DoubleEdgeVertices(blockStruct);
|
||||||
var type = (int)test.X;
|
|
||||||
if (testTerr.ContainsKey(type)) {
|
|
||||||
testTerr[type]++;
|
|
||||||
} else {
|
|
||||||
testTerr.Add(type, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return blockStruct;
|
return blockStruct;
|
||||||
}
|
}
|
||||||
|
private void GenerateBasicNormal(BlockStruct 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
|
|
||||||
for (int i = 0; i < blockStruct.indices.Length; i += 6) {
|
for (int i = 0; i < blockStruct.indices.Length; i += 6) {
|
||||||
int index1 = blockStruct.indices[i];
|
int index1 = blockStruct.indices[i];
|
||||||
int index2 = blockStruct.indices[i + 1];
|
int index2 = blockStruct.indices[i + 1];
|
||||||
|
|
@ -121,27 +64,14 @@ namespace LandblockExtraction.LandBlockExtractor {
|
||||||
Vector3 vertex3 = blockStruct.verticesStruct.position[index3];
|
Vector3 vertex3 = blockStruct.verticesStruct.position[index3];
|
||||||
Vector3 vertex4 = blockStruct.verticesStruct.position[index4];
|
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);
|
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
|
blockStruct.verticesStruct.normal[index1] = normal;
|
||||||
for (int i = 0; i < normals.Length; i++) {
|
blockStruct.verticesStruct.normal[index2] = normal;
|
||||||
normals[i] = Vector3.Normalize(normals[i]);
|
blockStruct.verticesStruct.normal[index3] = normal;
|
||||||
|
blockStruct.verticesStruct.normal[index4] = normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
return normals;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int[] GenerateBasicIndices() {
|
private int[] GenerateBasicIndices() {
|
||||||
List<int> indices = new List<int>();
|
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) {
|
private Vector3 GenerateVertexPosition(int landx, int landy, int x, int y, byte height) {
|
||||||
int tmpx = (landx * BlockSize + y) * cellSize;
|
int tmpx = (landx * BlockSize + y) * cellSize;
|
||||||
int tmpy = (BlockSize * NumberLandBlocks * cellSize) - ((landy * BlockSize + x) * cellSize) - 1;
|
int tmpy = (BlockSize * NumberLandBlocks * cellSize) - ((landy * BlockSize + x) * cellSize) - 1;
|
||||||
var newX = (tmpx - (NumberLandBlocks * BlockSize * cellSize / 2));
|
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));
|
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);
|
||||||
return new Vector3(newX, portalEngine.landScapeDefs.landHeightTable[height], newY);
|
|
||||||
}
|
}
|
||||||
private Vector4 GenerateVertexColor(uint cellInfo) {
|
private Vector4 GenerateVertexColor(uint cellInfo) {
|
||||||
var terrain = MathOperations.GetTerrainInCellInfo(cellInfo);
|
var terrain = MathOperations.GetTerrainInCellInfo(cellInfo);
|
||||||
|
|
@ -217,7 +146,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;
|
||||||
|
|
@ -229,59 +157,42 @@ namespace LandblockExtraction.LandBlockExtractor {
|
||||||
var one = blockStruct.indices[i + 0];
|
var one = blockStruct.indices[i + 0];
|
||||||
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 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]);
|
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,
|
newNormals.Add(blockStruct.verticesStruct.normal[one]);
|
||||||
blockStruct.verticesStruct.terraintype[two].X,
|
newTerrainTypes.Add(terrainType);
|
||||||
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,
|
newNormals.Add(blockStruct.verticesStruct.normal[two]);
|
||||||
blockStruct.verticesStruct.terraintype[two].X,
|
newTerrainTypes.Add(terrainType);
|
||||||
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,
|
newNormals.Add(blockStruct.verticesStruct.normal[three]);
|
||||||
blockStruct.verticesStruct.terraintype[two].X,
|
newTerrainTypes.Add(terrainType);
|
||||||
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[four]);
|
||||||
newColors.Add(blockStruct.verticesStruct.color[foor]);
|
newColors.Add(blockStruct.verticesStruct.color[four]);
|
||||||
newFarColors.Add(blockStruct.verticesStruct.farcolor[foor]);
|
newFarColors.Add(blockStruct.verticesStruct.farcolor[four]);
|
||||||
newTerrainTypes.Add(new Vector4(blockStruct.verticesStruct.terraintype[one].X,
|
newNormals.Add(blockStruct.verticesStruct.normal[four]);
|
||||||
blockStruct.verticesStruct.terraintype[two].X,
|
newTerrainTypes.Add(terrainType);
|
||||||
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));
|
||||||
newTexCoord.Add(new(1, 0));
|
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],
|
|
||||||
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)
|
// Ajouter les nouveaux sommets à la structure BlockStruct (étape 2)
|
||||||
|
|
@ -292,7 +203,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);
|
||||||
}
|
}
|
||||||
|
|
@ -300,12 +210,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();
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
LandblockExtraction/WorldMap/Types/Cell.cs
Normal file
17
LandblockExtraction/WorldMap/Types/Cell.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace LandblockExtraction.WorldMap.Types;
|
||||||
|
public class Cell {
|
||||||
|
public Vector3[,] position;
|
||||||
|
public Vector4[,] color;
|
||||||
|
|
||||||
|
public Cell() {
|
||||||
|
position = new Vector3[17, 17];
|
||||||
|
color = new Vector4[17, 17];
|
||||||
|
}
|
||||||
|
}
|
||||||
50
LandblockExtraction/WorldMap/Types/Land.cs
Normal file
50
LandblockExtraction/WorldMap/Types/Land.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
using LandblockExtraction.WorldMap.Types;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace LandblockExtraction.WorldMap;
|
||||||
|
public class Land {
|
||||||
|
public Cell cell;
|
||||||
|
|
||||||
|
public Land() {
|
||||||
|
cell = new Cell();
|
||||||
|
}
|
||||||
|
|
||||||
|
public float[] getVertices() {
|
||||||
|
List<float> vertices = new List<float>();
|
||||||
|
for(int y = 0; y < 10; y++) {
|
||||||
|
for(int x = 0; x < 10; x++) {
|
||||||
|
vertices.Add(cell.position[x, y].X);
|
||||||
|
vertices.Add(cell.position[x, y].Y);
|
||||||
|
vertices.Add(cell.position[x, y].Z);
|
||||||
|
|
||||||
|
vertices.Add(cell.color[x, y].X);
|
||||||
|
vertices.Add(cell.color[x, y].Y);
|
||||||
|
vertices.Add(cell.color[x, y].Z);
|
||||||
|
vertices.Add(cell.color[x, y].W);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vertices.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getIndices() {
|
||||||
|
List<int> indices = new List<int>();
|
||||||
|
for (int y = 0; y < 10 - 1; y++) {
|
||||||
|
for (int x = 0; x < 10 - 1; x++) {
|
||||||
|
var value = y * 10 + x;
|
||||||
|
|
||||||
|
indices.Add(value);
|
||||||
|
indices.Add(value + 1);
|
||||||
|
indices.Add(value + 10);
|
||||||
|
|
||||||
|
indices.Add(value + 10);
|
||||||
|
indices.Add(value + 11);
|
||||||
|
indices.Add(value + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return indices.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
40
LandblockExtraction/WorldMap/WorldMap.cs
Normal file
40
LandblockExtraction/WorldMap/WorldMap.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
using LandblockExtraction.WorldMap.Types;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace LandblockExtraction.WorldMap;
|
||||||
|
public class WorldMap {
|
||||||
|
public Land[,] lands;
|
||||||
|
|
||||||
|
public WorldMap() {
|
||||||
|
lands = new Land[10, 10];
|
||||||
|
|
||||||
|
CreatePlane();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreatePlane() {
|
||||||
|
for(int i = 0; i < 10; i++) {
|
||||||
|
for(int j = 0; j < 10; j++) {
|
||||||
|
lands[j,i] = CreateCell(i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Land CreateCell(int i, int j) {
|
||||||
|
Land land = new Land();
|
||||||
|
for(int y = 0; y < 17; y++) {
|
||||||
|
for (int x = 0; x < 17; x++) {
|
||||||
|
float rand = new Random().Next(0, 254) / 255f;
|
||||||
|
Console.WriteLine(rand);
|
||||||
|
land.cell.position[x, y] = new Vector3(x + (j * 9), 0, y + (i * 9));
|
||||||
|
land.cell.color[x, y] = new Vector4(rand, rand, rand, 1f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return land;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,14 +3,16 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.8.34330.188
|
VisualStudioVersion = 17.8.34330.188
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Map3DRendering", "Map3DRendering\Map3DRendering.csproj", "{B2ED409E-ACF9-4D6B-9632-6B9A7D0C3386}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Map3DRendering", "Map3DRendering\Map3DRendering.csproj", "{B2ED409E-ACF9-4D6B-9632-6B9A7D0C3386}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LandblockExtraction", "LandblockExtraction\LandblockExtraction.csproj", "{CE966441-7638-4137-BD1A-A8ED612A4E81}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LandblockExtraction", "LandblockExtraction\LandblockExtraction.csproj", "{CE966441-7638-4137-BD1A-A8ED612A4E81}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test_LandblockExtraction", "Test_LandblockExtraction\Test_LandblockExtraction.csproj", "{23CE2C14-661B-4544-A768-5475809641FC}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test_LandblockExtraction", "Test_LandblockExtraction\Test_LandblockExtraction.csproj", "{23CE2C14-661B-4544-A768-5475809641FC}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestUnit", "TestUnit", "{9CEDBDBC-D718-4F94-A8C7-8A10835816E3}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestUnit", "TestUnit", "{9CEDBDBC-D718-4F94-A8C7-8A10835816E3}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfMapView2D", "WpfMapView2D\WpfMapView2D.csproj", "{8D0A4BB4-23C7-4259-A614-3E9C6B0C8781}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
|
@ -29,6 +31,10 @@ Global
|
||||||
{23CE2C14-661B-4544-A768-5475809641FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{23CE2C14-661B-4544-A768-5475809641FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{23CE2C14-661B-4544-A768-5475809641FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{23CE2C14-661B-4544-A768-5475809641FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{23CE2C14-661B-4544-A768-5475809641FC}.Release|Any CPU.Build.0 = Release|Any CPU
|
{23CE2C14-661B-4544-A768-5475809641FC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{8D0A4BB4-23C7-4259-A614-3E9C6B0C8781}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{8D0A4BB4-23C7-4259-A614-3E9C6B0C8781}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{8D0A4BB4-23C7-4259-A614-3E9C6B0C8781}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{8D0A4BB4-23C7-4259-A614-3E9C6B0C8781}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ namespace Map3DRendering.Common {
|
||||||
|
|
||||||
// Get the projection matrix using the same method we have used up until this point
|
// Get the projection matrix using the same method we have used up until this point
|
||||||
public Matrix4 GetProjectionMatrix() {
|
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.
|
// 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 OpenTK.Graphics.OpenGL4;
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using OpenTK.Graphics.OpenGL4;
|
|
||||||
using OpenTK.Mathematics;
|
using OpenTK.Mathematics;
|
||||||
|
|
||||||
namespace Map3DRendering {
|
namespace Map3DRendering.Common {
|
||||||
// A simple class meant to help create shaders.
|
// A simple class meant to help create shaders.
|
||||||
public class Shader {
|
public class Shader {
|
||||||
public readonly int Handle;
|
public readonly int Handle;
|
||||||
|
|
@ -167,9 +163,18 @@ namespace Map3DRendering {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name">The name of the uniform</param>
|
/// <param name="name">The name of the uniform</param>
|
||||||
/// <param name="data">The data to set</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) {
|
public void SetVector3(string name, Vector3 data) {
|
||||||
GL.UseProgram(Handle);
|
GL.UseProgram(Handle);
|
||||||
GL.Uniform3(_uniformLocations[name], data);
|
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 OpenTK.Graphics.OpenGL4;
|
||||||
using System.Drawing;
|
|
||||||
using System.Drawing.Imaging;
|
|
||||||
using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
|
|
||||||
using StbImageSharp;
|
using StbImageSharp;
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace Map3DRendering {
|
namespace Map3DRendering.Common {
|
||||||
// A helper class, much like Shader, meant to simplify loading textures.
|
// A helper class, much like Shader, meant to simplify loading textures.
|
||||||
public class Texture {
|
public class Texture {
|
||||||
public readonly int Handle;
|
public readonly int Handle;
|
||||||
|
|
@ -105,6 +101,7 @@ namespace Map3DRendering {
|
||||||
return new Texture(handle);
|
return new Texture(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Texture(int glHandle) {
|
public Texture(int glHandle) {
|
||||||
Handle = glHandle;
|
Handle = glHandle;
|
||||||
}
|
}
|
||||||
|
|
@ -121,5 +118,11 @@ namespace Map3DRendering {
|
||||||
GL.ActiveTexture(unit);
|
GL.ActiveTexture(unit);
|
||||||
GL.BindTexture(TextureTarget.Texture2DArray, Handle);
|
GL.BindTexture(TextureTarget.Texture2DArray, Handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Assign(int shader, int i) {
|
||||||
|
int location = GL.GetUniformLocation(shader, "textures[" + i.ToString() + "]");
|
||||||
|
GL.Uniform1(location, i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,9 @@
|
||||||
<None Update="Shaders\lighting.frag">
|
<None Update="Shaders\lighting.frag">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="Shaders\shadertest.frag">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Update="Shaders\shader.frag">
|
<None Update="Shaders\shader.frag">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,13 @@ namespace Map3DRendering {
|
||||||
private PortalEngine portalEngine;
|
private PortalEngine portalEngine;
|
||||||
private CellEngine cellEngine;
|
private CellEngine cellEngine;
|
||||||
private LandBlockExtrator landblockExtraction;
|
private LandBlockExtrator landblockExtraction;
|
||||||
|
private TerrainAtlasManager terrainAtlasManager;
|
||||||
|
|
||||||
private readonly int NumberLandBlocks = 255;
|
private readonly int NumberLandBlocks = 255;
|
||||||
private readonly int BlockSize = 17;
|
private readonly int BlockSize = 17;
|
||||||
private readonly int allBlocks = 255 * 17 * 255 * 17;
|
private readonly int allBlocks = 255 * 17 * 255 * 17;
|
||||||
private readonly int cellSize = 8;
|
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[,] _vertexArrayObject;
|
||||||
public int[,] _vertexBufferObject;
|
public int[,] _vertexBufferObject;
|
||||||
|
|
@ -31,6 +32,7 @@ namespace Map3DRendering {
|
||||||
public MapRender() {
|
public MapRender() {
|
||||||
portalEngine = new PortalEngine();
|
portalEngine = new PortalEngine();
|
||||||
cellEngine = new CellEngine();
|
cellEngine = new CellEngine();
|
||||||
|
terrainAtlasManager = new(portalEngine);
|
||||||
|
|
||||||
landblockExtraction = new(portalEngine, cellEngine);
|
landblockExtraction = new(portalEngine, cellEngine);
|
||||||
|
|
||||||
|
|
@ -44,7 +46,6 @@ namespace Map3DRendering {
|
||||||
|
|
||||||
CalculeRadius(currentLandBlockX, currentLandBlockY);
|
CalculeRadius(currentLandBlockX, currentLandBlockY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetIndiceLenght() {
|
public int GetIndiceLenght() {
|
||||||
return (17 - 1) * (17 - 1) * 6; //Always that.
|
return (17 - 1) * (17 - 1) * 6; //Always that.
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +71,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 +113,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++) {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ namespace Map3DRendering {
|
||||||
public static class Program {
|
public static class Program {
|
||||||
private static void Main() {
|
private static void Main() {
|
||||||
var nativeWindowSettings = new NativeWindowSettings() {
|
var nativeWindowSettings = new NativeWindowSettings() {
|
||||||
ClientSize = new Vector2i(800, 600),
|
Size = new Vector2i(800, 600),
|
||||||
Title = "LearnOpenTK - Map AC2",
|
Title = "LearnOpenTK - Map AC2",
|
||||||
// This is needed to run on macos
|
// This is needed to run on macos
|
||||||
Flags = ContextFlags.ForwardCompatible,
|
Flags = ContextFlags.ForwardCompatible,
|
||||||
|
|
|
||||||
|
|
@ -2,33 +2,38 @@
|
||||||
|
|
||||||
out vec4 outputColor;
|
out vec4 outputColor;
|
||||||
|
|
||||||
uniform sampler2DArray texture0;
|
|
||||||
|
|
||||||
uniform vec3 viewPos;
|
uniform vec3 viewPos;
|
||||||
uniform vec3 lightPos;
|
uniform vec3 lightPos;
|
||||||
uniform vec3 lightColor;
|
uniform vec3 lightColor;
|
||||||
|
uniform sampler2DArray texture0;
|
||||||
|
|
||||||
in vec4 Color;
|
|
||||||
in vec4 FarColor;
|
in vec4 FarColor;
|
||||||
in vec3 Normal;
|
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()
|
||||||
{
|
{
|
||||||
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);
|
vec3 norm = normalize(Normal);
|
||||||
|
|
||||||
vec4 finalColor = color0; //mix(color0, color1, norm.y);
|
|
||||||
|
|
||||||
vec3 lightDir = normalize(lightPos - FragPos);
|
vec3 lightDir = normalize(lightPos - FragPos);
|
||||||
float diff = max(dot(norm, lightDir), 0.0);
|
float diff = max(dot(norm, lightDir), 0.0);
|
||||||
vec3 diffuse = diff * lightColor;
|
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 distance = length(viewPos - FragPos);
|
||||||
float interpolationFactor = clamp(distance / 1000.0, 0.0, 1.0);
|
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 = 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;
|
|
||||||
}
|
}
|
||||||
13
Map3DRendering/Shaders/shadertest.frag
Normal file
13
Map3DRendering/Shaders/shadertest.frag
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
#version 330
|
||||||
|
|
||||||
|
out vec4 outputColor;
|
||||||
|
|
||||||
|
uniform vec3 viewPos;
|
||||||
|
uniform sampler2DArray texture0;
|
||||||
|
|
||||||
|
in vec4 Color;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
outputColor = Color;
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,7 @@ namespace Map3DRendering {
|
||||||
private readonly Vector3 _lightPos = new Vector3(0x10, 0, 0x10);
|
private readonly Vector3 _lightPos = new Vector3(0x10, 0, 0x10);
|
||||||
|
|
||||||
private MapRender mapRender;
|
private MapRender mapRender;
|
||||||
|
private WorldMapRender _render;
|
||||||
private AxesGizmo axesGizmo;
|
private AxesGizmo axesGizmo;
|
||||||
|
|
||||||
private Shader _shader;
|
private Shader _shader;
|
||||||
|
|
@ -34,6 +35,7 @@ namespace Map3DRendering {
|
||||||
: base(gameWindowSettings, nativeWindowSettings) {
|
: base(gameWindowSettings, nativeWindowSettings) {
|
||||||
|
|
||||||
mapRender = new MapRender();
|
mapRender = new MapRender();
|
||||||
|
_render = new WorldMapRender();
|
||||||
|
|
||||||
GL.GetInteger(GetPName.MaxTextureImageUnits, out maxTextures);
|
GL.GetInteger(GetPName.MaxTextureImageUnits, out maxTextures);
|
||||||
}
|
}
|
||||||
|
|
@ -49,26 +51,23 @@ namespace Map3DRendering {
|
||||||
GL.GetInteger(GetPName.MaxTextureImageUnits, out maxTextureUnits);
|
GL.GetInteger(GetPName.MaxTextureImageUnits, out maxTextureUnits);
|
||||||
Console.WriteLine($"Maximum number of texture units for fragment shaders: {maxTextureUnits}");
|
Console.WriteLine($"Maximum number of texture units for fragment shaders: {maxTextureUnits}");
|
||||||
|
|
||||||
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
|
_shader = new Shader("Shaders/shader.vert", "Shaders/shadertest.frag");
|
||||||
_shader.Use();
|
_shader.Use();
|
||||||
|
|
||||||
mapRender.OnLoad(_shader);
|
//mapRender.OnLoad(_shader);
|
||||||
|
_render.OnLoad(_shader);
|
||||||
|
|
||||||
var file = Directory.EnumerateFiles(@"./terrains");
|
var file = Directory.EnumerateFiles(@"./terrains");
|
||||||
|
|
||||||
_texture = Texture.LoadFromArray(file.ToArray());
|
_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);
|
_texture.UseArray(TextureUnit.Texture0);
|
||||||
|
|
||||||
|
|
||||||
axesGizmo = new AxesGizmo();
|
axesGizmo = new AxesGizmo();
|
||||||
|
|
||||||
_camera = new Camera(Vector3.UnitY * 300, Size.X / (float)Size.Y);
|
_camera = new Camera(Vector3.UnitY * 300, Size.X / (float)Size.Y);
|
||||||
_camera.Fov = 60;
|
_camera.Fov = 60;
|
||||||
//CursorState = CursorState.Grabbed;
|
//CursorState = CursorState.Grabbed;
|
||||||
//GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
|
||||||
//GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
|
||||||
_lightPosVec = Vector3.UnitY * 1000;
|
_lightPosVec = Vector3.UnitY * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,23 +78,22 @@ namespace Map3DRendering {
|
||||||
|
|
||||||
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||||
|
|
||||||
_texture.UseArray(TextureUnit.Texture0);
|
|
||||||
_shader.Use();
|
_shader.Use();
|
||||||
|
_texture.UseArray(TextureUnit.Texture0);
|
||||||
|
|
||||||
_shader.SetMatrix4("view", _camera.GetViewMatrix());
|
_shader.SetMatrix4("view", _camera.GetViewMatrix());
|
||||||
_shader.SetMatrix4("projection", _camera.GetProjectionMatrix());
|
_shader.SetMatrix4("projection", _camera.GetProjectionMatrix());
|
||||||
|
|
||||||
//_shader.SetVector3("objectColor", new Vector3(0.5f, 0.5f, 0.5f));
|
//_shader.SetVector3("objectColor", new Vector3(0.5f, 0.5f, 0.5f));
|
||||||
_shader.SetVector3("lightColor", new Vector3(1.0f, 1.0f, 1.0f));
|
//_shader.SetVector3("lightColor", new Vector3(1.0f, 1.0f, 1.0f));
|
||||||
_shader.SetVector3("lightPos", _lightPosVec);
|
//_shader.SetVector3("lightPos", _camera.Position);
|
||||||
|
|
||||||
//_shader.SetVector3("viewPos", _camera.Position);
|
|
||||||
|
|
||||||
GL.LineWidth(5.0f);
|
GL.LineWidth(5.0f);
|
||||||
|
|
||||||
_shader.SetVector3("viewPos", _camera.Position);
|
//_shader.SetVector3("viewPos", _camera.Position);
|
||||||
mapRender.UpdateBlocks(_camera.Position, _shader);
|
//mapRender.UpdateBlocks(_camera.Position, _shader);
|
||||||
mapRender.Render(_shader);
|
//mapRender.Render(_shader);
|
||||||
|
_render.Render(_shader);
|
||||||
|
|
||||||
axesGizmo.Render(Size.X, Size.Y, _camera);
|
axesGizmo.Render(Size.X, Size.Y, _camera);
|
||||||
|
|
||||||
|
|
@ -149,10 +147,10 @@ namespace Map3DRendering {
|
||||||
_camera.Position += _camera.Right * cameraSpeed * (float)e.Time; // Right
|
_camera.Position += _camera.Right * cameraSpeed * (float)e.Time; // Right
|
||||||
}
|
}
|
||||||
if (input.IsKeyDown(Keys.Space)) {
|
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)) {
|
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
|
// Get the mouse state
|
||||||
|
|
|
||||||
77
Map3DRendering/WorldMapRender.cs
Normal file
77
Map3DRendering/WorldMapRender.cs
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
using LandblockExtraction.AtlasMaker;
|
||||||
|
using LandblockExtraction.DatEngine;
|
||||||
|
using LandblockExtraction.LandBlockExtractor;
|
||||||
|
using LandblockExtraction.WorldMap;
|
||||||
|
using Map3DRendering.Common;
|
||||||
|
using OpenTK.Graphics.OpenGL4;
|
||||||
|
using OpenTK.Mathematics;
|
||||||
|
|
||||||
|
namespace Map3DRendering {
|
||||||
|
public class WorldMapRender {
|
||||||
|
|
||||||
|
private WorldMap worldMap;
|
||||||
|
private int[,] _vertexArrayObject;
|
||||||
|
private int[,] _vertexBufferObject;
|
||||||
|
private int[,] _elementBufferObject;
|
||||||
|
private int[,] _indiceLength;
|
||||||
|
|
||||||
|
public WorldMapRender() {
|
||||||
|
worldMap = new WorldMap();
|
||||||
|
_vertexArrayObject = new int[10, 10];
|
||||||
|
_vertexBufferObject = new int[10, 10];
|
||||||
|
_elementBufferObject = new int[10, 10];
|
||||||
|
_indiceLength = new int[10, 10];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnLoad(Shader _shader) {
|
||||||
|
for(int i = 0; i < 10; i++) {
|
||||||
|
for(int j = 0; j < 10; j++) {
|
||||||
|
InitializeBlock(j, i, worldMap.lands[j, i], _shader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void InitializeBlock(int x, int y, Land block, Shader _shader) {
|
||||||
|
int lenghPacket = 7;
|
||||||
|
|
||||||
|
var vertices = block.getVertices();
|
||||||
|
var indices = block.getIndices();
|
||||||
|
_indiceLength[x, y] = 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[x, y] = tempVertexArray;
|
||||||
|
|
||||||
|
int tmpVertexBuffer = GL.GenBuffer();
|
||||||
|
GL.BindBuffer(BufferTarget.ArrayBuffer, tmpVertexBuffer);
|
||||||
|
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);
|
||||||
|
_vertexBufferObject[x, y] = 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[x, y] = 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), 3 * sizeof(float));
|
||||||
|
}
|
||||||
|
public void Render(Shader shader) {
|
||||||
|
for (int y = 0; y < 10; y++) {
|
||||||
|
for (int x = 0; x < 10; x++) {
|
||||||
|
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[x, y]);
|
||||||
|
GL.DrawElements(PrimitiveType.Triangles, _indiceLength[x, y], DrawElementsType.UnsignedInt, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
WpfMapView2D/App.xaml
Normal file
9
WpfMapView2D/App.xaml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
<Application x:Class="WpfMapView2D.App"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:local="clr-namespace:WpfMapView2D"
|
||||||
|
StartupUri="MainWindow.xaml">
|
||||||
|
<Application.Resources>
|
||||||
|
|
||||||
|
</Application.Resources>
|
||||||
|
</Application>
|
||||||
11
WpfMapView2D/App.xaml.cs
Normal file
11
WpfMapView2D/App.xaml.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
using System.Configuration;
|
||||||
|
using System.Data;
|
||||||
|
using System.Windows;
|
||||||
|
|
||||||
|
namespace WpfMapView2D;
|
||||||
|
/// <summary>
|
||||||
|
/// Interaction logic for App.xaml
|
||||||
|
/// </summary>
|
||||||
|
public partial class App : Application {
|
||||||
|
}
|
||||||
|
|
||||||
10
WpfMapView2D/AssemblyInfo.cs
Normal file
10
WpfMapView2D/AssemblyInfo.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
using System.Windows;
|
||||||
|
|
||||||
|
[assembly: ThemeInfo(
|
||||||
|
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||||
|
//(used if a resource is not found in the page,
|
||||||
|
// or application resource dictionaries)
|
||||||
|
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||||
|
//(used if a resource is not found in the page,
|
||||||
|
// app, or any theme specific resource dictionaries)
|
||||||
|
)]
|
||||||
106
WpfMapView2D/Common/Camera.cs
Normal file
106
WpfMapView2D/Common/Camera.cs
Normal file
|
|
@ -0,0 +1,106 @@
|
||||||
|
using OpenTK.Mathematics;
|
||||||
|
|
||||||
|
namespace WpfMapView2D.Common {
|
||||||
|
// This is the camera class as it could be set up after the tutorials on the website.
|
||||||
|
// It is important to note there are a few ways you could have set up this camera.
|
||||||
|
// For example, you could have also managed the player input inside the camera class,
|
||||||
|
// and a lot of the properties could have been made into functions.
|
||||||
|
|
||||||
|
// TL;DR: This is just one of many ways in which we could have set up the camera.
|
||||||
|
// Check out the web version if you don't know why we are doing a specific thing or want to know more about the code.
|
||||||
|
public class Camera {
|
||||||
|
// Those vectors are directions pointing outwards from the camera to define how it rotated.
|
||||||
|
private Vector3 _front = -Vector3.UnitZ;
|
||||||
|
|
||||||
|
private Vector3 _up = Vector3.UnitY;
|
||||||
|
|
||||||
|
private Vector3 _right = Vector3.UnitX;
|
||||||
|
|
||||||
|
// Rotation around the X axis (radians)
|
||||||
|
private float _pitch;
|
||||||
|
|
||||||
|
// Rotation around the Y axis (radians)
|
||||||
|
private float _yaw = -MathHelper.PiOver2; // Without this, you would be started rotated 90 degrees right.
|
||||||
|
|
||||||
|
// The field of view of the camera (radians)
|
||||||
|
private float _fov = MathHelper.PiOver2;
|
||||||
|
|
||||||
|
public Camera(Vector3 position, float aspectRatio) {
|
||||||
|
Position = position;
|
||||||
|
AspectRatio = aspectRatio;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The position of the camera
|
||||||
|
public Vector3 Position { get; set; }
|
||||||
|
|
||||||
|
// This is simply the aspect ratio of the viewport, used for the projection matrix.
|
||||||
|
public float AspectRatio { private get; set; }
|
||||||
|
|
||||||
|
public Vector3 Front => _front;
|
||||||
|
|
||||||
|
public Vector3 Up => _up;
|
||||||
|
|
||||||
|
public Vector3 Right => _right;
|
||||||
|
|
||||||
|
// We convert from degrees to radians as soon as the property is set to improve performance.
|
||||||
|
public float Pitch {
|
||||||
|
get => MathHelper.RadiansToDegrees(_pitch);
|
||||||
|
set {
|
||||||
|
// We clamp the pitch value between -89 and 89 to prevent the camera from going upside down, and a bunch
|
||||||
|
// of weird "bugs" when you are using euler angles for rotation.
|
||||||
|
// If you want to read more about this you can try researching a topic called gimbal lock
|
||||||
|
var angle = MathHelper.Clamp(value, -89f, 89f);
|
||||||
|
_pitch = MathHelper.DegreesToRadians(angle);
|
||||||
|
UpdateVectors();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We convert from degrees to radians as soon as the property is set to improve performance.
|
||||||
|
public float Yaw {
|
||||||
|
get => MathHelper.RadiansToDegrees(_yaw);
|
||||||
|
set {
|
||||||
|
_yaw = MathHelper.DegreesToRadians(value);
|
||||||
|
UpdateVectors();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The field of view (FOV) is the vertical angle of the camera view.
|
||||||
|
// This has been discussed more in depth in a previous tutorial,
|
||||||
|
// but in this tutorial, you have also learned how we can use this to simulate a zoom feature.
|
||||||
|
// We convert from degrees to radians as soon as the property is set to improve performance.
|
||||||
|
public float Fov {
|
||||||
|
get => MathHelper.RadiansToDegrees(_fov);
|
||||||
|
set {
|
||||||
|
var angle = MathHelper.Clamp(value, 1f, 90f);
|
||||||
|
_fov = MathHelper.DegreesToRadians(angle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the view matrix using the amazing LookAt function described more in depth on the web tutorials
|
||||||
|
public Matrix4 GetViewMatrix() {
|
||||||
|
return Matrix4.LookAt(Position, Position + _front, _up);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, 5000f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is going to update the direction vertices using some of the math learned in the web tutorials.
|
||||||
|
private void UpdateVectors() {
|
||||||
|
// First, the front matrix is calculated using some basic trigonometry.
|
||||||
|
_front.X = MathF.Cos(_pitch) * MathF.Cos(_yaw);
|
||||||
|
_front.Y = MathF.Sin(_pitch);
|
||||||
|
_front.Z = MathF.Cos(_pitch) * MathF.Sin(_yaw);
|
||||||
|
|
||||||
|
// We need to make sure the vectors are all normalized, as otherwise we would get some funky results.
|
||||||
|
_front = Vector3.Normalize(_front);
|
||||||
|
|
||||||
|
// Calculate both the right and the up vector using cross product.
|
||||||
|
// Note that we are calculating the right from the global up; this behaviour might
|
||||||
|
// not be what you need for all cameras so keep this in mind if you do not want a FPS camera.
|
||||||
|
_right = Vector3.Normalize(Vector3.Cross(_front, Vector3.UnitY));
|
||||||
|
_up = Vector3.Normalize(Vector3.Cross(_right, _front));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
181
WpfMapView2D/Common/Shader.cs
Normal file
181
WpfMapView2D/Common/Shader.cs
Normal file
|
|
@ -0,0 +1,181 @@
|
||||||
|
using OpenTK.Graphics.OpenGL4;
|
||||||
|
using OpenTK.Mathematics;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace WpfMapView2D.Common {
|
||||||
|
// A simple class meant to help create shaders.
|
||||||
|
public class Shader {
|
||||||
|
public readonly int Handle;
|
||||||
|
|
||||||
|
private readonly Dictionary<string, int> _uniformLocations;
|
||||||
|
|
||||||
|
// This is how you create a simple shader.
|
||||||
|
// Shaders are written in GLSL, which is a language very similar to C in its semantics.
|
||||||
|
// The GLSL source is compiled *at runtime*, so it can optimize itself for the graphics card it's currently being used on.
|
||||||
|
// A commented example of GLSL can be found in shader.vert.
|
||||||
|
public Shader(string vertPath, string fragPath) {
|
||||||
|
// There are several different types of shaders, but the only two you need for basic rendering are the vertex and fragment shaders.
|
||||||
|
// The vertex shader is responsible for moving around vertices, and uploading that data to the fragment shader.
|
||||||
|
// The vertex shader won't be too important here, but they'll be more important later.
|
||||||
|
// The fragment shader is responsible for then converting the vertices to "fragments", which represent all the data OpenGL needs to draw a pixel.
|
||||||
|
// The fragment shader is what we'll be using the most here.
|
||||||
|
|
||||||
|
// Load vertex shader and compile
|
||||||
|
var shaderSource = File.ReadAllText(vertPath);
|
||||||
|
|
||||||
|
// GL.CreateShader will create an empty shader (obviously). The ShaderType enum denotes which type of shader will be created.
|
||||||
|
var vertexShader = GL.CreateShader(ShaderType.VertexShader);
|
||||||
|
|
||||||
|
// Now, bind the GLSL source code
|
||||||
|
GL.ShaderSource(vertexShader, shaderSource);
|
||||||
|
|
||||||
|
// And then compile
|
||||||
|
CompileShader(vertexShader);
|
||||||
|
|
||||||
|
// We do the same for the fragment shader.
|
||||||
|
shaderSource = File.ReadAllText(fragPath);
|
||||||
|
var fragmentShader = GL.CreateShader(ShaderType.FragmentShader);
|
||||||
|
GL.ShaderSource(fragmentShader, shaderSource);
|
||||||
|
CompileShader(fragmentShader);
|
||||||
|
|
||||||
|
// These two shaders must then be merged into a shader program, which can then be used by OpenGL.
|
||||||
|
// To do this, create a program...
|
||||||
|
Handle = GL.CreateProgram();
|
||||||
|
|
||||||
|
// Attach both shaders...
|
||||||
|
GL.AttachShader(Handle, vertexShader);
|
||||||
|
GL.AttachShader(Handle, fragmentShader);
|
||||||
|
|
||||||
|
// And then link them together.
|
||||||
|
LinkProgram(Handle);
|
||||||
|
|
||||||
|
// When the shader program is linked, it no longer needs the individual shaders attached to it; the compiled code is copied into the shader program.
|
||||||
|
// Detach them, and then delete them.
|
||||||
|
GL.DetachShader(Handle, vertexShader);
|
||||||
|
GL.DetachShader(Handle, fragmentShader);
|
||||||
|
GL.DeleteShader(fragmentShader);
|
||||||
|
GL.DeleteShader(vertexShader);
|
||||||
|
|
||||||
|
// The shader is now ready to go, but first, we're going to cache all the shader uniform locations.
|
||||||
|
// Querying this from the shader is very slow, so we do it once on initialization and reuse those values
|
||||||
|
// later.
|
||||||
|
|
||||||
|
// First, we have to get the number of active uniforms in the shader.
|
||||||
|
GL.GetProgram(Handle, GetProgramParameterName.ActiveUniforms, out var numberOfUniforms);
|
||||||
|
|
||||||
|
// Next, allocate the dictionary to hold the locations.
|
||||||
|
_uniformLocations = new Dictionary<string, int>();
|
||||||
|
|
||||||
|
// Loop over all the uniforms,
|
||||||
|
for (var i = 0; i < numberOfUniforms; i++) {
|
||||||
|
// get the name of this uniform,
|
||||||
|
var key = GL.GetActiveUniform(Handle, i, out _, out _);
|
||||||
|
|
||||||
|
// get the location,
|
||||||
|
var location = GL.GetUniformLocation(Handle, key);
|
||||||
|
|
||||||
|
// and then add it to the dictionary.
|
||||||
|
_uniformLocations.Add(key, location);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CompileShader(int shader) {
|
||||||
|
// Try to compile the shader
|
||||||
|
GL.CompileShader(shader);
|
||||||
|
|
||||||
|
// Check for compilation errors
|
||||||
|
GL.GetShader(shader, ShaderParameter.CompileStatus, out var code);
|
||||||
|
if (code != (int)All.True) {
|
||||||
|
// We can use `GL.GetShaderInfoLog(shader)` to get information about the error.
|
||||||
|
var infoLog = GL.GetShaderInfoLog(shader);
|
||||||
|
throw new Exception($"Error occurred whilst compiling Shader({shader}).\n\n{infoLog}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void LinkProgram(int program) {
|
||||||
|
// We link the program
|
||||||
|
GL.LinkProgram(program);
|
||||||
|
|
||||||
|
// Check for linking errors
|
||||||
|
GL.GetProgram(program, GetProgramParameterName.LinkStatus, out var code);
|
||||||
|
if (code != (int)All.True) {
|
||||||
|
// We can use `GL.GetProgramInfoLog(program)` to get information about the error.
|
||||||
|
throw new Exception($"Error occurred whilst linking Program({program})");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A wrapper function that enables the shader program.
|
||||||
|
public void Use() {
|
||||||
|
GL.UseProgram(Handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The shader sources provided with this project use hardcoded layout(location)-s. If you want to do it dynamically,
|
||||||
|
// you can omit the layout(location=X) lines in the vertex shader, and use this in VertexAttribPointer instead of the hardcoded values.
|
||||||
|
public int GetAttribLocation(string attribName) {
|
||||||
|
return GL.GetAttribLocation(Handle, attribName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uniform setters
|
||||||
|
// Uniforms are variables that can be set by user code, instead of reading them from the VBO.
|
||||||
|
// You use VBOs for vertex-related data, and uniforms for almost everything else.
|
||||||
|
|
||||||
|
// Setting a uniform is almost always the exact same, so I'll explain it here once, instead of in every method:
|
||||||
|
// 1. Bind the program you want to set the uniform on
|
||||||
|
// 2. Get a handle to the location of the uniform with GL.GetUniformLocation.
|
||||||
|
// 3. Use the appropriate GL.Uniform* function to set the uniform.
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set a uniform int on this shader.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">The name of the uniform</param>
|
||||||
|
/// <param name="data">The data to set</param>
|
||||||
|
public void SetInt(string name, int data) {
|
||||||
|
GL.UseProgram(Handle);
|
||||||
|
GL.Uniform1(_uniformLocations[name], data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set a uniform float on this shader.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">The name of the uniform</param>
|
||||||
|
/// <param name="data">The data to set</param>
|
||||||
|
public void SetFloat(string name, float data) {
|
||||||
|
GL.UseProgram(Handle);
|
||||||
|
GL.Uniform1(_uniformLocations[name], data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set a uniform Matrix4 on this shader
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">The name of the uniform</param>
|
||||||
|
/// <param name="data">The data to set</param>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// The matrix is transposed before being sent to the shader.
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
public void SetMatrix4(string name, Matrix4 data) {
|
||||||
|
GL.UseProgram(Handle);
|
||||||
|
GL.UniformMatrix4(_uniformLocations[name], true, ref data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set a uniform Vector3 on this shader.
|
||||||
|
/// </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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
129
WpfMapView2D/Common/Texture.cs
Normal file
129
WpfMapView2D/Common/Texture.cs
Normal file
|
|
@ -0,0 +1,129 @@
|
||||||
|
using OpenTK.Graphics.OpenGL4;
|
||||||
|
using StbImageSharp;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace WpfMapView2D.Common {
|
||||||
|
// A helper class, much like Shader, meant to simplify loading textures.
|
||||||
|
public class Texture {
|
||||||
|
public readonly int Handle;
|
||||||
|
|
||||||
|
public static Texture LoadFromFile(string path) {
|
||||||
|
// Generate handle
|
||||||
|
int handle = GL.GenTexture();
|
||||||
|
|
||||||
|
// Bind the handle
|
||||||
|
GL.ActiveTexture(TextureUnit.Texture0);
|
||||||
|
GL.BindTexture(TextureTarget.Texture2D, handle);
|
||||||
|
|
||||||
|
// For this example, we're going to use .NET's built-in System.Drawing library to load textures.
|
||||||
|
|
||||||
|
// OpenGL has it's texture origin in the lower left corner instead of the top left corner,
|
||||||
|
// so we tell StbImageSharp to flip the image when loading.
|
||||||
|
StbImage.stbi_set_flip_vertically_on_load(1);
|
||||||
|
|
||||||
|
// Here we open a stream to the file and pass it to StbImageSharp to load.
|
||||||
|
using (Stream stream = File.OpenRead(path)) {
|
||||||
|
ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
|
||||||
|
|
||||||
|
// Now that our pixels are prepared, it's time to generate a texture. We do this with GL.TexImage2D.
|
||||||
|
// Arguments:
|
||||||
|
// The type of texture we're generating. There are various different types of textures, but the only one we need right now is Texture2D.
|
||||||
|
// Level of detail. We can use this to start from a smaller mipmap (if we want), but we don't need to do that, so leave it at 0.
|
||||||
|
// Target format of the pixels. This is the format OpenGL will store our image with.
|
||||||
|
// Width of the image
|
||||||
|
// Height of the image.
|
||||||
|
// Border of the image. This must always be 0; it's a legacy parameter that Khronos never got rid of.
|
||||||
|
// The format of the pixels, explained above. Since we loaded the pixels as RGBA earlier, we need to use PixelFormat.Rgba.
|
||||||
|
// Data type of the pixels.
|
||||||
|
// And finally, the actual pixels.
|
||||||
|
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, image.Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now that our texture is loaded, we can set a few settings to affect how the image appears on rendering.
|
||||||
|
|
||||||
|
// First, we set the min and mag filter. These are used for when the texture is scaled down and up, respectively.
|
||||||
|
// Here, we use Linear for both. This means that OpenGL will try to blend pixels, meaning that textures scaled too far will look blurred.
|
||||||
|
// You could also use (amongst other options) Nearest, which just grabs the nearest pixel, which makes the texture look pixelated if scaled too far.
|
||||||
|
// NOTE: The default settings for both of these are LinearMipmap. If you leave these as default but don't generate mipmaps,
|
||||||
|
// your image will fail to render at all (usually resulting in pure black instead).
|
||||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
|
||||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
|
||||||
|
|
||||||
|
// Now, set the wrapping mode. S is for the X axis, and T is for the Y axis.
|
||||||
|
// We set this to Repeat so that textures will repeat when wrapped. Not demonstrated here since the texture coordinates exactly match
|
||||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
|
||||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
|
||||||
|
|
||||||
|
// Next, generate mipmaps.
|
||||||
|
// Mipmaps are smaller copies of the texture, scaled down. Each mipmap level is half the size of the previous one
|
||||||
|
// Generated mipmaps go all the way down to just one pixel.
|
||||||
|
// OpenGL will automatically switch between mipmaps when an object gets sufficiently far away.
|
||||||
|
// This prevents moiré effects, as well as saving on texture bandwidth.
|
||||||
|
// Here you can see and read about the morié effect https://en.wikipedia.org/wiki/Moir%C3%A9_pattern
|
||||||
|
// Here is an example of mips in action https://en.wikipedia.org/wiki/File:Mipmap_Aliasing_Comparison.png
|
||||||
|
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Activate texture
|
||||||
|
// Multiple textures can be bound, if your shader needs more than just one.
|
||||||
|
// If you want to do that, use GL.ActiveTexture to set which slot GL.BindTexture binds to.
|
||||||
|
// The OpenGL standard requires that there be at least 16, but there can be more depending on your graphics card.
|
||||||
|
public void Use(TextureUnit unit) {
|
||||||
|
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() + "]");
|
||||||
|
GL.Uniform1(location, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
WpfMapView2D/MainWindow.xaml
Normal file
20
WpfMapView2D/MainWindow.xaml
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<Window x:Class="WpfMapView2D.MainWindow"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:glWpfControl="clr-namespace:OpenTK.Wpf;assembly=GLWpfControl"
|
||||||
|
xmlns:local="clr-namespace:WpfMapView2D"
|
||||||
|
mc:Ignorable="d"
|
||||||
|
Title="MainWindow" Height="450" Width="800">
|
||||||
|
<Grid>
|
||||||
|
<Grid Margin="0,0,0,20">
|
||||||
|
<glWpfControl:GLWpfControl
|
||||||
|
x:Name="OpenTkControl"
|
||||||
|
Render="OpenTkControl_OnRender" MouseLeftButtonDown="OpenTkControl_MouseLeftButtonDown"
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
<StatusBar Name="statusBarData" Height="20" VerticalAlignment="Bottom"/>
|
||||||
|
|
||||||
|
</Grid>
|
||||||
|
</Window>
|
||||||
42
WpfMapView2D/MainWindow.xaml.cs
Normal file
42
WpfMapView2D/MainWindow.xaml.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
using OpenTK.Graphics.OpenGL4;
|
||||||
|
using OpenTK.Mathematics;
|
||||||
|
using OpenTK.Wpf;
|
||||||
|
using System.Text;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Data;
|
||||||
|
using System.Windows.Documents;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
using System.Windows.Navigation;
|
||||||
|
using System.Windows.Shapes;
|
||||||
|
using WpfMapView2D.Common;
|
||||||
|
|
||||||
|
namespace WpfMapView2D;
|
||||||
|
/// <summary>
|
||||||
|
/// Interaction logic for MainWindow.xaml
|
||||||
|
/// </summary>
|
||||||
|
public partial class MainWindow : Window {
|
||||||
|
public Camera _camera;
|
||||||
|
public MainWindow() {
|
||||||
|
InitializeComponent();
|
||||||
|
var settings = new GLWpfControlSettings {
|
||||||
|
MajorVersion = 4,
|
||||||
|
MinorVersion = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
_camera = new Camera(Vector3.UnitY * 300, (float)this.Width / (float)this.Height);
|
||||||
|
_camera.Fov = 60;
|
||||||
|
|
||||||
|
OpenTkControl.Start(settings);
|
||||||
|
}
|
||||||
|
private void OpenTkControl_OnRender(TimeSpan delta) {
|
||||||
|
GL.ClearColor(Color4.Blue);
|
||||||
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OpenTkControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
16
WpfMapView2D/WpfMapView2D.csproj
Normal file
16
WpfMapView2D/WpfMapView2D.csproj
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
<TargetFramework>net7.0-windows</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<UseWPF>true</UseWPF>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="OpenTK.GLWpfControl" Version="4.2.3" />
|
||||||
|
<PackageReference Include="StbImageSharp" Version="2.27.13" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue