Add Normal in vertex. And add light in shader.

This commit is contained in:
Troispoils 2024-02-28 17:07:45 +01:00
parent 8908ef1332
commit 0e96615b32
16 changed files with 221 additions and 85 deletions

View file

@ -1,12 +1,6 @@
using AC2RE.Definitions;
using ImageMagick;
using LandblockExtraction.DatEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace LandblockExtraction.AtlasMaker;
public class AtlasBuilder : IDisposable {
@ -21,7 +15,7 @@ public class AtlasBuilder : IDisposable {
public bool AddTexture(int index, DataId matId) {
var img = texturesImage.GetImage(matId);
if(img == null) return false;
if (img == null) return false;
textures.Add(index, img);
return true;
}

View file

@ -1,13 +1,7 @@
using AC2RE.Definitions;
using ImageMagick;
using LandblockExtraction.DatEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace LandblockExtraction.AtlasMaker;
public class TerrainAtlasManager {
@ -15,10 +9,14 @@ public class TerrainAtlasManager {
public Dictionary<int, Vector2> textureCoord;
public Dictionary<int, MagickImage> terrainTexture;
public Dictionary<int, Terrain> terrains;
public TerrainAtlasManager(PortalEngine portalEngine) {
this.portalEngine = portalEngine;
textureCoord = new Dictionary<int, Vector2>();
terrainTexture = new Dictionary<int, MagickImage>();
terrains = new Dictionary<int, Terrain>();
testGenerateList();
}
public void ExtractTexture() {
using (var atlasBuilder = new AtlasBuilder(portalEngine)) {
@ -26,22 +24,46 @@ public class TerrainAtlasManager {
foreach (var surface in portalEngine.cSurfaceDesc.surfaces) {
if (surface.surfIndex == terrain.surfaceInfo) {
atlasBuilder.AddTexture((int)terrain.terrainIndex, surface.terrainMaterials.First().baseMaterials.First().materialDid);
//terrains.Add((int)terrain.terrainIndex, GenerateSubTerrain((int)terrain.terrainIndex, surface.terrainMaterials));
}
}
textureCoord.Add((int)terrain.terrainIndex, Vector2.Zero);
}
atlasBuilder.GenerateAtlas();
foreach(var img in atlasBuilder.textures) {
foreach (var img in atlasBuilder.textures) {
terrainTexture.Add(img.Key, img.Value);
}
}
}
/*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 testGenerateList() {
foreach(var surface in portalEngine.cSurfaceDesc.surfaces) {
foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
if(surface.surfIndex == terrain.surfaceInfo) {
Console.WriteLine($"[{terrain.terrainIndex}:{terrain.terrainName}]\t- Surface: {surface.surfIndex:X8} | Base: {surface.terrainMaterials.First().baseMaterials.First().materialDid}");
}
}
}
}
private int? GetIndexBySurface(uint surfaceIndex) {
foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
if (terrain.surfaceInfo == surfaceIndex)
return (int)terrain.terrainIndex;
}
return null;
}
public void GenerateUV() {
int count = (int)Math.Ceiling(Math.Sqrt(textureCoord.Count));
int atlasSize = (int)64 * count;
int index = 0;
foreach(var terrain in textureCoord) {
foreach (var terrain in textureCoord) {
int x = (index % count) * (int)64;
int y = (index / count) * (int)64;
textureCoord[index] = new Vector2(x, y);

View file

@ -0,0 +1,15 @@
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) {
this.terrainIndex = terrainIndex;
this.minPitch = minPitch;
this.maxPitch = maxPitch;
}
public bool MatchesPitch(float pitch) {
return pitch >= minPitch && pitch <= maxPitch;
}
}

View file

@ -0,0 +1,22 @@
namespace LandblockExtraction.AtlasMaker;
public class Terrain {
public int terrainIndex { get; set; }
public List<SubTerrain> subTerrains { get; set; }
public Terrain(int index) {
terrainIndex = index;
subTerrains = new List<SubTerrain>();
}
public void AddSubTerrain(SubTerrain sousTerrain) {
subTerrains.Add(sousTerrain);
}
public int DetermineSubTerrain(float pitch) {
foreach (var terrain in subTerrains) {
if (terrain.MatchesPitch(pitch)) {
return terrain.terrainIndex;
}
}
return terrainIndex;
}
}

View file

@ -2,11 +2,6 @@
using ImageMagick;
using LandblockExtraction.DatEngine;
using LandblockExtraction.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LandblockExtraction.AtlasMaker;
public class TexturesImage {
@ -19,10 +14,10 @@ public class TexturesImage {
}
public MagickImage? GetImage(DataId matId) {
if(!portalEngine.datReader.contains(matId)) return null;
using(var data = portalEngine.datReader.getFileReader(matId)) {
if (!portalEngine.datReader.contains(matId)) return null;
using (var data = portalEngine.datReader.getFileReader(matId)) {
var materialLayer = new RenderMaterial(data);
if(materialLayer == null) return null;
if (materialLayer == null) return null;
return ImageInMaterial(materialLayer);
}
@ -30,10 +25,10 @@ public class TexturesImage {
private MagickImage? ImageInMaterial(RenderMaterial materialLayer) {
var vdescId = materialLayer.layers.First().stages.First().textureID;
if(!portalEngine.datReader.contains(new(vdescId))) return null;
using(var data = portalEngine.datReader.getFileReader(new(vdescId))) {
if (!portalEngine.datReader.contains(new(vdescId))) return null;
using (var data = portalEngine.datReader.getFileReader(new(vdescId))) {
var texture = new RenderTexture(data);
if(texture == null) return null;
if (texture == null) return null;
return TextureInRender(texture);
}
@ -41,13 +36,13 @@ public class TexturesImage {
private MagickImage? TextureInRender(RenderTexture texture) {
MagickImage magickImage;
foreach(var img in texture.levelSurfaceDids) {
if(!portalEngine.datReader.contains(img)) continue;
using(var data = portalEngine.datReader.getFileReader(img)) {
foreach (var img in texture.levelSurfaceDids) {
if (!portalEngine.datReader.contains(img)) continue;
using (var data = portalEngine.datReader.getFileReader(img)) {
var image = new RenderSurface(data);
if(image.width != 64) continue;
if (image.width != 64) continue;
var dataImg = DDSHeader.Generate(image);
using(MagickImage realImg = new MagickImage(dataImg)) {
using (MagickImage realImg = new MagickImage(dataImg)) {
magickImage = new(realImg);
}
return magickImage;

View file

@ -1,9 +1,4 @@
using AC2RE.Definitions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LandblockExtraction.DatEngine;
public class DatEngine {

View file

@ -1,6 +1,4 @@
using AC2RE.Definitions;
namespace LandblockExtraction.DatEngine {
namespace LandblockExtraction.DatEngine {
public class TextureEngine : DatEngine {
public TextureEngine() : base(@"X:\DatFiles\highres.dat") {

View file

@ -2,7 +2,6 @@
using LandblockExtraction.AtlasMaker;
using LandblockExtraction.DatEngine;
using LandblockExtraction.Tools;
using System.Dynamic;
using System.Numerics;
namespace LandblockExtraction.LandBlockExtractor {
@ -120,6 +119,7 @@ namespace LandblockExtraction.LandBlockExtractor {
//TEST
public void DoubleEdgeVertices(BlockStruct blockStruct) {
List<Vector3> newPositions = new List<Vector3>();
List<Vector3> newNormals = new List<Vector3>();
List<Vector4> newColors = new List<Vector4>();
List<Vector4> newFarColors = new List<Vector4>();
List<Vector2> newTexCoord = new List<Vector2>();
@ -131,7 +131,7 @@ namespace LandblockExtraction.LandBlockExtractor {
// Doubler les sommets sur le bord supérieur et inférieur
for(int i = 0; i < originalIndicesCount; i = i + 6) {
for (int i = 0; i < originalIndicesCount; i = i + 6) {
var one = blockStruct.indices[i + 0];
var two = blockStruct.indices[i + 1];
@ -179,11 +179,22 @@ namespace LandblockExtraction.LandBlockExtractor {
newTexCoord.Add(new(0, 1));
newTexCoord.Add(new(1, 0));
newTexCoord.Add(new(1, 1));
var normal = MathOperations.CalculateQuadNormal(blockStruct.verticesStruct.position[one],
blockStruct.verticesStruct.position[two],
blockStruct.verticesStruct.position[three],
blockStruct.verticesStruct.position[foor]);
newNormals.Add(normal);
newNormals.Add(normal);
newNormals.Add(normal);
newNormals.Add(normal);
}
// Ajouter les nouveaux sommets à la structure BlockStruct (étape 2)
// Vous devez également mettre à jour les indices dans blockStruct.indices
blockStruct.verticesStruct.position = newPositions.ToArray();
blockStruct.verticesStruct.normal = newNormals.ToArray();
blockStruct.verticesStruct.color = newColors.ToArray();
blockStruct.verticesStruct.farcolor = newFarColors.ToArray();
blockStruct.verticesStruct.texturecoord = newTexCoord.ToArray();
@ -195,7 +206,7 @@ namespace LandblockExtraction.LandBlockExtractor {
private int[] GenerateNewsIndices(int count) {
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 + 1);
indices.Add(i + 2);

View file

@ -1,8 +1,4 @@

using System;
using System.Numerics;
namespace LandblockExtraction.LandBlockExtractor {
namespace LandblockExtraction.LandBlockExtractor {
public class BlockStruct {
public readonly static int BlockSize = 17;

View file

@ -3,6 +3,7 @@
namespace LandblockExtraction.LandBlockExtractor {
public class VerticesStruct {
public Vector3[] position { get; set; }
public Vector3[] normal { get; set; }
public Vector4[] color { get; set; }
public Vector4[] farcolor { get; set; }
public Vector2[] texturecoord { get; set; }
@ -11,6 +12,7 @@ namespace LandblockExtraction.LandBlockExtractor {
public VerticesStruct(int blockSize) {
position = new Vector3[blockSize * blockSize];
normal = new Vector3[blockSize * blockSize];
color = new Vector4[blockSize * blockSize];
farcolor = new Vector4[blockSize * blockSize];
texturecoord = new Vector2[blockSize * blockSize];
@ -20,26 +22,29 @@ namespace LandblockExtraction.LandBlockExtractor {
public float[] Vertices() {
int length = position.Length;
float[] vertices = new float[length * 18]; // 3 pour position, 4 pour color, et 4 pour farcolor
for (int i = 0, vi = 0; i < length; i++, vi += 18) {
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) {
vertices[vi] = position[i].X;
vertices[vi + 1] = position[i].Y;
vertices[vi + 2] = position[i].Z;
vertices[vi + 3] = color[i].X;
vertices[vi + 4] = color[i].Y;
vertices[vi + 5] = color[i].Z;
vertices[vi + 6] = color[i].W;
vertices[vi + 7] = farcolor[i].X;
vertices[vi + 8] = farcolor[i].Y;
vertices[vi + 9] = farcolor[i].Z;
vertices[vi + 10] = farcolor[i].W;
vertices[vi + 11] = texturecoord[i].X;
vertices[vi + 12] = texturecoord[i].Y;
vertices[vi + 13] = terraintype[i].X;
vertices[vi + 14] = terraintype[i].Y;
vertices[vi + 15] = terraintype[i].Z;
vertices[vi + 16] = terraintype[i].W;
vertices[vi + 17] = realtype[i];
vertices[vi + 3] = normal[i].X;
vertices[vi + 4] = normal[i].Y;
vertices[vi + 5] = normal[i].Z;
vertices[vi + 6] = color[i].X;
vertices[vi + 7] = color[i].Y;
vertices[vi + 8] = color[i].Z;
vertices[vi + 9] = color[i].W;
vertices[vi + 10] = farcolor[i].X;
vertices[vi + 11] = farcolor[i].Y;
vertices[vi + 12] = farcolor[i].Z;
vertices[vi + 13] = farcolor[i].W;
vertices[vi + 14] = texturecoord[i].X;
vertices[vi + 15] = texturecoord[i].Y;
vertices[vi + 16] = terraintype[i].X;
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;
}

View file

@ -24,5 +24,22 @@ namespace LandblockExtraction.Tools {
public static Vector4 RGBAColorToVector4(RGBAColor color) {
return new Vector4(color.r, color.g, color.b, color.a);
}
// Calcule la normale d'un triangle à partir de trois points
public static Vector3 CalculateTriangleNormal(Vector3 A, Vector3 B, Vector3 C) {
Vector3 AB = B - A;
Vector3 AC = C - A;
Vector3 normal = Vector3.Cross(AB, AC);
normal = Vector3.Normalize(normal);
return normal;
}
// Calcule la normale moyenne d'un quadrilatère en utilisant deux de ses triangles
public static Vector3 CalculateQuadNormal(Vector3 A, Vector3 B, Vector3 C, Vector3 D) {
Vector3 normal1 = CalculateTriangleNormal(A, B, C);
Vector3 normal2 = CalculateTriangleNormal(C, B, D);
Vector3 averageNormal = (normal1 + normal2) / 2;
return Vector3.Normalize(averageNormal);
}
}
}

View file

@ -70,7 +70,7 @@ namespace Map3DRendering {
}
}
private void InitializeBlock(int x, int y, BlockStruct block, Shader _shader) {
int lenghPacket = 18;
int lenghPacket = 21;
// 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();
@ -93,25 +93,29 @@ namespace Map3DRendering {
GL.EnableVertexAttribArray(vertexLocation);
GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 0);
var vertexNormal = _shader.GetAttribLocation("aNormal");
GL.EnableVertexAttribArray(vertexNormal);
GL.VertexAttribPointer(vertexNormal, 3, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 3 * sizeof(float));
var colorLocation = _shader.GetAttribLocation("aColor");
GL.EnableVertexAttribArray(colorLocation);
GL.VertexAttribPointer(colorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 3 * sizeof(float));
GL.VertexAttribPointer(colorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 6 * sizeof(float));
var farcolorLocation = _shader.GetAttribLocation("aColorFar");
GL.EnableVertexAttribArray(farcolorLocation);
GL.VertexAttribPointer(farcolorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 7 * sizeof(float));
GL.VertexAttribPointer(farcolorLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 10 * sizeof(float));
var texturecoordLocation = _shader.GetAttribLocation("aTexCoord");
GL.EnableVertexAttribArray(texturecoordLocation);
GL.VertexAttribPointer(texturecoordLocation, 2, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 11 * sizeof(float));
GL.VertexAttribPointer(texturecoordLocation, 2, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 14 * sizeof(float));
var terraintypeLocation = _shader.GetAttribLocation("aTexType");
GL.EnableVertexAttribArray(terraintypeLocation);
GL.VertexAttribPointer(terraintypeLocation, 4, VertexAttribPointerType.Float, false, lenghPacket * sizeof(float), 13 * 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), 13 * sizeof(float));
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++) {

View file

@ -3,9 +3,12 @@
out vec4 outputColor;
uniform vec3 viewPos;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform sampler2D texture0;
in vec4 FarColor;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoord;
in vec4 TexType;
@ -41,8 +44,14 @@ void main()
vec4 mix2 = mix(blendedColor[1], blendedColor[3], weightX);
vec4 finalColor = mix(mix1, mix2, weightY);
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec4 litColor = vec4(diffuse, 1.0) * finalColor;
float distance = length(viewPos - FragPos);
float interpolationFactor = clamp(distance / 1000.0, 0.0, 1.0);
outputColor = mix(finalColor, FarColor, interpolationFactor);
outputColor = mix(litColor, FarColor, interpolationFactor);
}

View file

@ -1,16 +1,18 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec4 aColor;
layout (location = 2) in vec4 aColorFar;
layout (location = 3) in vec2 aTexCoord;
layout (location = 4) in vec4 aTexType;
layout (location = 5) in float aRealTexType;
layout (location = 1) in vec3 aNormal;
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;
uniform mat4 projection;
out vec3 FragPos;
out vec3 Normal;
out vec4 Color;
out vec4 FarColor;
out vec2 TexCoord;
@ -21,6 +23,7 @@ void main()
{
gl_Position = vec4(aPos, 1.0) * model * view * projection;
FragPos = vec3(vec4(aPos, 1.0) * model);
Normal = aNormal;
Color = aColor;
FarColor = aColorFar;
TexCoord = aTexCoord;

View file

@ -22,6 +22,7 @@ namespace Map3DRendering {
private bool _firstMove = true;
private Vector2 _lastPos;
private Vector3 _lightPosVec;
private double _time;
@ -44,6 +45,10 @@ namespace Map3DRendering {
GL.Enable(EnableCap.DepthTest);
int maxTextureUnits;
GL.GetInteger(GetPName.MaxTextureImageUnits, out maxTextureUnits);
Console.WriteLine($"Maximum number of texture units for fragment shaders: {maxTextureUnits}");
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_shader.Use();
@ -58,6 +63,7 @@ namespace Map3DRendering {
//CursorState = CursorState.Grabbed;
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
_lightPosVec = Vector3.UnitY * 1000;
}
protected override void OnRenderFrame(FrameEventArgs e) {
@ -73,10 +79,10 @@ namespace Map3DRendering {
_shader.SetMatrix4("view", _camera.GetViewMatrix());
_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("lightPos", _camera.Position);
_shader.SetVector3("viewPos", _camera.Position);*/
_shader.SetVector3("lightPos", _lightPosVec);
//_shader.SetVector3("viewPos", _camera.Position);
GL.LineWidth(5.0f);
@ -103,6 +109,23 @@ namespace Map3DRendering {
Close();
}
// Vitesse de déplacement de la lumière
float lightSpeed = 300.0f; // Ajustez cette valeur selon les besoins
// Mise à jour de la position de la lumière en fonction des entrées utilisateur
if (input.IsKeyDown(Keys.Down)) {
_lightPosVec += Vector3.UnitX * lightSpeed * (float)e.Time; // Déplace la lumière vers le bas
}
if (input.IsKeyDown(Keys.Up)) {
_lightPosVec -= Vector3.UnitX * lightSpeed * (float)e.Time; // Déplace la lumière vers le haut
}
if (input.IsKeyDown(Keys.Left)) {
_lightPosVec -= Vector3.UnitZ * lightSpeed * (float)e.Time; // Déplace la lumière vers la gauche
}
if (input.IsKeyDown(Keys.Right)) {
_lightPosVec += Vector3.UnitZ * lightSpeed * (float)e.Time; // Déplace la lumière vers la droite
}
const float sensitivity = 0.2f;
if (input.IsKeyDown(Keys.W)) {

View file

@ -0,0 +1,27 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LandblockExtraction.AtlasMaker;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LandblockExtraction.AtlasMaker.Tests;
[TestClass()]
public class TerrainTests {
[TestMethod()]
public void TerrainTest() {
Assert.Fail();
}
[TestMethod()]
public void AddSubTerrainTest() {
Assert.Fail();
}
[TestMethod()]
public void DetermineSubTerrainTest() {
Assert.Fail();
}
}