c# - 使用 Assimp 在 OpenGL (GLSL) 中进行骨骼动画

标签 c# opengl animation glsl skeletal-animation

我正在尝试在一个程序中实现 GLSL 支持的骨骼动画……我创建了一个怪物而不是漂亮的 3D 动画:https://i.imgur.com/dmpKg6n.gif

在尝试了几种不同的技术之后,我创造了一个不那么可怕但仍然很可怕的东西:

Monster

模型是一个穿着西装的基本人,动画应该是腿摆动而其他一切保持静止(一个非常简单的测试动画)。 所有骨骼在其原始位置的 t=0 处都有静态关键帧,以尽量减少它们每隔几秒就跳出位置。

问题...应该是相当明显的。此外,我相当确定该模型比预期的要高。

无论如何,这是我原来的顶点着色器:

#version 430 core
layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texcoords;
layout (location = 3) in vec4 color;
layout (location = 4) in vec4 Weights;
layout (location = 5) in vec4 BoneID;
layout (location = 0) out vec4 f_position;
layout (location = 1) out vec3 f_normal;
layout (location = 2) out vec2 f_texcoord;
const int MAX_BONES = 50;
layout (location = 1) uniform mat4 proj_matrix;
layout (location = 2) uniform mat4 mv_matrix;
layout (location = 6) uniform mat4 boneTrans[MAX_BONES];
void main(void)
{
mat4 boneTransform = (boneTrans[int(BoneID[0])] * Weights[0]) +
(boneTrans[int(BoneID[1])] * Weights[1]) +
(boneTrans[int(BoneID[2])] * Weights[2]) +
(boneTrans[int(BoneID[3])] * Weights[3]);
float rem = 1 - (Weights[0] + Weights[1] + Weights[2] + Weights[3]);
boneTransform += mat4(1.0) * rem;
f_texcoord = texcoords;
f_position = mv_matrix * (boneTransform * position);
mat4 mv_mat_simple = mv_matrix;
mv_mat_simple[3][0] = 0.0;
mv_mat_simple[3][1] = 0.0;
mv_mat_simple[3][2] = 0.0;
vec4 norm1 = boneTransform * vec4(normal, 1.0);
vec4 nnormal = mv_mat_simple * vec4(norm1.xyz, 1.0);
f_normal = nnormal.xyz / nnormal.w; // TODO: Normalize?
vec4 pos1 = boneTransform * position;
gl_Position = proj_matrix * mv_matrix * vec4(pos1.xyz, 1.0);
}

这是我的新的:

#version 430 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texcoords;
layout (location = 3) in vec4 color;
layout (location = 4) in vec4 Weights;
layout (location = 5) in vec4 BoneID;

layout (location = 0) out vec4 f_position;
layout (location = 1) out vec3 f_normal;
layout (location = 2) out vec2 f_texcoord;

const int MAX_BONES = 50;

layout (location = 1) uniform mat4 proj_matrix;
layout (location = 2) uniform mat4 mv_matrix;
layout (location = 6) uniform mat4 boneTrans[MAX_BONES];

void main(void)
{
    vec4 pos1 = vec4(position, 1.0);
    pos1 += (boneTrans[int(BoneID[0])] * vec4(position, 0.0)) * Weights[0];
    pos1 += (boneTrans[int(BoneID[1])] * vec4(position, 0.0)) * Weights[1];
    pos1 += (boneTrans[int(BoneID[2])] * vec4(position, 0.0)) * Weights[2];
    pos1 += (boneTrans[int(BoneID[3])] * vec4(position, 0.0)) * Weights[3];
    vec4 norm1 = vec4(normal, 1.0);
    norm1 += (boneTrans[int(BoneID[0])] * vec4(normal, 0.0)) * Weights[0];
    norm1 += (boneTrans[int(BoneID[1])] * vec4(normal, 0.0)) * Weights[1];
    norm1 += (boneTrans[int(BoneID[2])] * vec4(normal, 0.0)) * Weights[2];
    norm1 += (boneTrans[int(BoneID[3])] * vec4(normal, 0.0)) * Weights[3];
    f_texcoord = texcoords;
    f_position = mv_matrix * vec4(pos1.xyz, 1.0);
    mat4 mv_mat_simple = mv_matrix;
    mv_mat_simple[3][0] = 0.0;
    mv_mat_simple[3][1] = 0.0;
    mv_mat_simple[3][2] = 0.0;
    //vec4 norm1 = boneTransform * vec4(normal, 1.0);
    vec4 nnormal = mv_mat_simple * vec4(norm1.xyz, 1.0);
    f_normal = nnormal.xyz / nnormal.w; // TODO: Normalize?
    gl_Position = proj_matrix * mv_matrix * vec4(pos1.xyz, 1.0);
}

骨骼处理代码既不漂亮也不短:http://pastebin.com/A8x1GdUw

该代码和原始着色器派生自 http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html

新的着色器来自于随机谷歌搜索。

编辑:我现在明白了:

Leg twisty twisty

使用此代码:http://pastebin.com/PvCWUdJn

现在...我做错了什么?!处理这个问题的“正确”方法是什么?我应该使用任何更高质量的教程来代替这个教程吗?

更新:测试应用程序的完整源代码:https://github.com/mcmonkey4eva/skeletalanimationtest

最佳答案

所以答案很简单 - 不要在着色器中乘以矩阵 x 向量,乘以向量 x 矩阵。

关于c# - 使用 Assimp 在 OpenGL (GLSL) 中进行骨骼动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29931749/

相关文章:

c - 在 SDL 中设置 OGL 主要版本会导致错误

c++ - 使用 Stencil-buffer 绘制 3D 模型的轮廓

c++ - 圆柱纹理映射opengl

鼠标悬停时的 CSS3 动画

c# - 如何在 WinForms 中实现这种窗口样式?

c# - 网络核心 : Type or namespace name 'RoleProvider' could not be found

c# - 从 C# 项目导航到 VB.NET 项目中的类或方法时,有没有办法阻止显示 C# 元数据

android - 布局动画 Android[Facebook]

animation - 如何为 svg 的 animateTransform 设置原点

c# - 如何将数据读取器转换为动态查询结果