-
Hello, I'm encountering an issue while working with Silk.NET and C# for rendering. I'm developing an application that involves loading models, which is why I have various classes calling rendering methods. The problem I'm facing is related to shader usage, as it seems the shader is always utilizing the texture from the texture slot 0. Let me provide you with the relevant code sections to illustrate the issue: Mesh class [NotThreadSafe]
public unsafe void Render()
{
// Bind the geometry and shader.
Engine.Instance.GL.BindVertexArray(vertexArray);
// Bind the material
material.Render();
// Draw the geometry.
Engine.Instance.GL.DrawElements(PrimitiveType.Triangles, (uint)indices.Length, DrawElementsType.UnsignedInt, null);
} Material Class: [NotThreadSafe]
public void Render()
{
// Use the shader program
Shader.Render();
// Set the view and projection uniform
var view = Matrix4x4.CreateLookAt(Camera.ActiveCamera.Transform.Position, Camera.ActiveCamera.Transform.Position + Camera.ActiveCamera.Front, Camera.ActiveCamera.Up);
var projection = Matrix4x4.CreatePerspectiveFieldOfView(Helpers.Math.DegreesToRadians(Camera.ActiveCamera.Zoom), 800 / 600, 0.1f, 100.0f);
Shader.SetUniform("uView", view);
Shader.SetUniform("uProjection", projection);
int i = 0;
foreach (var keyValue in _textures)
{
Console.WriteLine($"Setting {i}: {keyValue.Key} - {keyValue.Value.combinedFilePath}.");
keyValue.Value.Render(Silk.NET.OpenGL.TextureUnit.Texture0.Next(i));
Shader.SetUniform(keyValue.Key, i);
i++;
}
// Set the transform view matrix of the owner
Shader.SetUniform("uModel", Owner.Transform.ViewMatrix);
} ( Shader class [NotThreadSafe]
public void SetUniform(string name, float value)
{
int location = Engine.Instance.GL.GetUniformLocation(shaderLocation, name);
if (location == -1)
{
Logger.Log($"{name} uniform not found on shader {this.name}.", Logger.LogType.error);
return;
}
Console.WriteLine($"Setting {name} to {value}.");
Engine.Instance.GL.Uniform1(location, value);
} Texture class [NotThreadSafe]
public void Render(TextureUnit textureSlot = TextureUnit.Texture0)
{
Engine.Instance.GL.ActiveTexture(textureSlot);
Engine.Instance.GL.BindTexture(TextureTarget.Texture2D, _handle);
} Shader The shader has a simple structure. It uses various Sampler2D variables, then performs some calculations and displays the texture on the mesh. I have checked the shader in another project – it works. I have already spent over four hours debugging the code and unfortunately found no error. Furthermore, I would guess it's the order, but even after a few attempted changes it didn't fix anything. Even though I attempt to set the texture unit to |
Beta Was this translation helpful? Give feedback.
Please overload a method with an int type parameter.