Test with new technique texturing

This commit is contained in:
Troispoils 2024-02-29 16:51:41 +01:00
parent bb723036c0
commit 3a7d169b30
8 changed files with 125 additions and 61 deletions

View file

@ -1,7 +1,11 @@
using OpenTK.Graphics.OpenGL4;
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
namespace Map3DRendering.Common {
namespace Map3DRendering {
// A simple class meant to help create shaders.
public class Shader {
public readonly int Handle;
@ -163,18 +167,9 @@ namespace Map3DRendering.Common {
/// </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);
}
}
}
}

View file

@ -1,7 +1,11 @@
using OpenTK.Graphics.OpenGL4;
using System.Drawing;
using System.Drawing.Imaging;
using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
using StbImageSharp;
using System.IO;
namespace Map3DRendering.Common {
namespace Map3DRendering {
// A helper class, much like Shader, meant to simplify loading textures.
public class Texture {
public readonly int Handle;
@ -77,10 +81,5 @@ namespace Map3DRendering.Common {
GL.ActiveTexture(unit);
GL.BindTexture(TextureTarget.Texture2D, Handle);
}
public void Assign(int shader, int i) {
int location = GL.GetUniformLocation(shader, "textures[" + i.ToString() + "]");
GL.Uniform1(location, i);
}
}
}
}

View file

@ -6,7 +6,7 @@ namespace Map3DRendering {
public static class Program {
private static void Main() {
var nativeWindowSettings = new NativeWindowSettings() {
Size = new Vector2i(800, 600),
ClientSize = new Vector2i(800, 600),
Title = "LearnOpenTK - Map AC2",
// This is needed to run on macos
Flags = ContextFlags.ForwardCompatible,

View file

@ -2,11 +2,14 @@
out vec4 outputColor;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform vec3 viewPos;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform sampler2D texture0;
in vec4 Color;
in vec4 FarColor;
in vec3 Normal;
in vec3 FragPos;
@ -16,40 +19,20 @@ in float RealType;
void main()
{
float tileSize = 64.0 / 512.0;
float baseWeight = 0.2;
float dominantWeight = 0.4;
vec4 weights = vec4(baseWeight);
vec2 uvOffsets[4];
for (int i = 0; i < 4; i++) {
float type = TexType[i];
uvOffsets[i] = vec2(mod(type, 8.0), floor(type / 8.0)) * tileSize;
if (type == RealType) {
weights[i] = dominantWeight;
}
}
vec4 blendedColor[4];
for (int i = 0; i < 4; i++) {
vec2 uv = TexCoord * tileSize + uvOffsets[i];
blendedColor[i] = texture(texture0, uv);
}
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);
vec4 color0 = texture(texture0, TexCoord);
vec4 color1 = texture(texture1, TexCoord);
vec3 norm = normalize(Normal);
float inclineFactor = abs(norm.y);
vec4 finalColor = mix(color0, color1, inclineFactor);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec4 litColor = vec4(diffuse, 1.0) * finalColor;
vec4 litColor = vec4(diffuse, 1.0) * finalColor * Color;
float distance = length(viewPos - FragPos);
float interpolationFactor = clamp(distance / 1000.0, 0.0, 1.0);

View file

@ -16,6 +16,7 @@ namespace Map3DRendering {
private Shader _shader;
private Texture _texture;
private Texture _texture2;
private Camera _camera;
@ -53,16 +54,29 @@ namespace Map3DRendering {
_shader.Use();
mapRender.OnLoad(_shader);
_texture = Texture.LoadFromFile("atlas.jpg");
_texture = Texture.LoadFromFile("terrains/0.jpg");
// Texture units are explained in Texture.cs, at the Use function.
// First texture goes in texture unit 0.
_texture.Use(TextureUnit.Texture0);
// This is helpful because System.Drawing reads the pixels differently than OpenGL expects.
_texture2 = Texture.LoadFromFile("terrains/1.jpg");
// Then, the second goes in texture unit 1.
_texture2.Use(TextureUnit.Texture1);
// Next, we must setup the samplers in the shaders to use the right textures.
// The int we send to the uniform indicates which texture unit the sampler should use.
_shader.SetInt("texture0", 0);
_shader.SetInt("texture1", 1);
axesGizmo = new AxesGizmo();
_camera = new Camera(Vector3.UnitY * 300, Size.X / (float)Size.Y);
_camera.Fov = 60;
//CursorState = CursorState.Grabbed;
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
//GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
//GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
_lightPosVec = Vector3.UnitY * 1000;
}
@ -73,8 +87,9 @@ namespace Map3DRendering {
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
_shader.Use();
_texture.Use(TextureUnit.Texture0);
_texture2.Use(TextureUnit.Texture1);
_shader.Use();
_shader.SetMatrix4("view", _camera.GetViewMatrix());
_shader.SetMatrix4("projection", _camera.GetProjectionMatrix());
@ -82,6 +97,7 @@ namespace Map3DRendering {
//_shader.SetVector3("objectColor", new Vector3(0.5f, 0.5f, 0.5f));
_shader.SetVector3("lightColor", new Vector3(1.0f, 1.0f, 1.0f));
_shader.SetVector3("lightPos", _lightPosVec);
//_shader.SetVector3("viewPos", _camera.Position);
GL.LineWidth(5.0f);