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

@ -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)) {