Пишу на C# с использованием OpenTK v.3.2.0
Шейдеры из уроков https://learnopengl.com
портированные для OpenTK https://github.com/opentk/LearnOpenTK/tree/master/Chapter2/4-LightingMaps
Маленький кубик - положение источника света
Из вершинного во фрагментный шейдер попадает нормаль нулевой длины.
После этой операции:
Normal = aNormal * mat3(transpose(inverse(model)));
Почему так происходит?
Вершинный шейдер:
#version 330 core
in vec3 aPosition;
in vec3 aNormal;
in vec2 aTexture;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec3 Normal;
out vec3 Position;
out vec2 Texture;
void main(void)
{
Position = vec3(vec4(aPosition, 1.0) * model);
Normal = aNormal * mat3(transpose(inverse(model)));
Texture = aTexture;
gl_Position = vec4(Position, 1.0) * view * projection;
}
Фрагментный шейдер:
#version 330
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
struct Light {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Light light;
uniform Material material;
uniform vec3 viewPosition;
out vec4 outputColor;
in vec3 Normal;
in vec3 Position;
in vec2 Texture;
uniform sampler2D texture0;
void main()
{
// ambient
vec3 ambient = light.ambient * vec3(texture(material.diffuse, Texture));
// Diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(light.position - Position);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * texture(material.diffuse, Texture).rgb;
// Specular
vec3 viewDir = normalize(viewPosition - Position);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * spec * texture(material.specular, Texture).rgb;
vec3 resultColor = ambient + diffuse + specular;
outputColor = vec4(resultColor, 1.0);
}
Ну и собственно класс объекта, на котором я это все тесктирую:
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using RUTClient.Core.Helpers;
using RUTClient.Core.Shaders;
using RUTClient.Core.Output;
using OpenTK.Input;
namespace RUTClient.Core.Primitive
{
class Primitive
{
protected float[] vertices;
protected int vertexBuffer;
protected int vertexArray;
protected int vertexLength;
protected Texture diffuse, specular;
protected Vector2 size;
protected Vector3 position;
protected float angle;
protected Vector3 scale;
protected Matrix4 rotationMatrix;
protected Matrix4 scaleMatrix;
protected Matrix4 translationMatrix;
protected Matrix4 modelMatrix;
protected bool Visible = true;
public virtual void Initialize(Vector2 size, Vector3 startPosition)
{
this.size = size;
angle = 0.0f;
scale = Vector3.One;
position = new Vector3(startPosition.X, -startPosition.Y, startPosition.Z);
vertices = new float[] {
// Positions Normals Texture coords
// front
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, //lb
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, //rb
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, //ru
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, //ru
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, //lu
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, //lb
// back
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, //ru
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, //rb
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, //lb
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, //lb
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, //lu
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, //ru
// right
+0.5f, -0.5f, +0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, //lb
+0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, //rb
+0.5f, +0.5f, -0.5f,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0f, //lb
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, //lu
+0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, //ru
};
vertexLength = 8;
rotationMatrix = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(angle));
scaleMatrix = Matrix4.CreateScale(scale);
translationMatrix = Matrix4.CreateTranslation(position);
//modelMatrix = Matrix4.Identity *
// translationMatrix *
// rotationMatrix *
// scaleMatrix;
modelMatrix = Matrix4.Identity;
vertexBuffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);
vertexArray = GL.GenVertexArray();
GL.BindVertexArray(vertexArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
int vertexLocation = ShaderManager.guiShader.GetAttribLocation("aPosition");
GL.EnableVertexAttribArray(vertexLocation);
GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, vertexLength * sizeof(float), 0);
int normalLocation = ShaderManager.guiShader.GetAttribLocation("aNormal");
GL.EnableVertexAttribArray(normalLocation);
GL.VertexAttribPointer(normalLocation, 3, VertexAttribPointerType.Float, false, vertexLength * sizeof(float), 3 * sizeof(float));
int texCoordLocation = ShaderManager.guiShader.GetAttribLocation("aTexCoord");
GL.EnableVertexAttribArray(texCoordLocation);
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, vertexLength * sizeof(float), 6 * sizeof(float));
}
public virtual void WindowResize(int width, int height)
{
Initialize(new Vector2(width, height), position);
}
public virtual void Update(double time)
{
angle -= 20.0f * (float)time;
rotationMatrix =
Matrix4.CreateRotationX(MathHelper.DegreesToRadians(angle * 1.0f)) *
Matrix4.CreateRotationY(MathHelper.DegreesToRadians(angle * 1.0f)) *
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(angle * 1.0f));
//modelMatrix = Matrix4.Identity * translationMatrix * rotationMatrix * scaleMatrix;
modelMatrix = Matrix4.Identity;
KeyboardState ks = Keyboard.GetState();
float lightSpeed = 1.0f * (float)time;
if (ks.IsKeyDown(Key.Left))
{
lightPos.X -= lightSpeed;
}
if (ks.IsKeyDown(Key.Right))
{
lightPos.X += lightSpeed;
}
if (ks.IsKeyDown(Key.Up))
{
lightPos.Y += lightSpeed;
}
if (ks.IsKeyDown(Key.Down))
{
lightPos.Y -= lightSpeed;
}
}
private Vector3 lightPos = new Vector3(0.0f, 0.0f, 4.0f);
public virtual void Draw(double time)
{
if (Visible)
{
GL.Enable(EnableCap.DepthTest);
GL.BindVertexArray(vertexArray);
diffuse.Use(TextureUnit.Texture0);
specular.Use(TextureUnit.Texture1);
ShaderManager.objectShader.Use();
ShaderManager.objectShader.SetMatrix4("model", modelMatrix);
ShaderManager.objectShader.SetMatrix4("view", CameraManager.Perspective.GetViewMatrix());
ShaderManager.objectShader.SetMatrix4("projection", CameraManager.Perspective.GetProjectionMatrix());
ShaderManager.objectShader.SetVector3("viewPosition", CameraManager.Perspective.Position);
// Here we specify to the shaders what textures they should refer to when we want to get the positions.
ShaderManager.objectShader.SetVector3("light.position", lightPos);
ShaderManager.objectShader.SetVector3("light.ambient", new Vector3(0.1f));
ShaderManager.objectShader.SetVector3("light.diffuse", new Vector3(10.5f));
ShaderManager.objectShader.SetVector3("light.specular", new Vector3(1.0f));
ShaderManager.objectShader.SetInt("material.diffuse", 0);
ShaderManager.objectShader.SetInt("material.specular", 1);
ShaderManager.objectShader.SetFloat("material.shininess", 32.0f);
GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length / vertexLength);
GL.BindVertexArray(vertexArray);
ShaderManager.objectShader.Use();
Matrix4 lampMatrix = Matrix4.Identity;
lampMatrix *= Matrix4.CreateScale(0.025f);
lampMatrix *= Matrix4.CreateTranslation(lightPos);
ShaderManager.objectShader.SetMatrix4("model", lampMatrix);
ShaderManager.objectShader.SetMatrix4("view", CameraManager.Perspective.GetViewMatrix());
ShaderManager.objectShader.SetMatrix4("projection", CameraManager.Perspective.GetProjectionMatrix());
ShaderManager.objectShader.SetVector3("light.ambient", new Vector3(1.0f));
GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length / vertexLength);
if (GL.GetError() != ErrorCode.NoError)
{
Console.WriteLine(GL.GetError());
}
}
}
public virtual void Dispose()
{
GL.DeleteBuffer(vertexBuffer);
GL.DeleteVertexArray(vertexArray);
}
public void Show()
{
Visible = true;
}
public void Hide()
{
Visible = false;
}
}
}
Из установленных флагов glEnable() имеется только CullFace и DepthTest, остальное по-умолчанию.