c++ - OpenGL 动画似乎偶尔会丢失骨骼数据

标签 c++ opengl

好的,我已经在 OpenGL 引擎中加载和渲染了基本模型。我有为模型工作的动画。然而,当我尝试向一个场景中添加多个带有动画的模型时,我遇到了一堆奇怪的行为——最后一个模型动画不正确。

在尝试隔离问题时,我相信我遇到了一些可能相关的事情 - 在渲染模型时,如果我将 OpenGL 中的骨骼数据“归零”(即,发送一堆单位矩阵),然后发送实际的骨骼数据,我在模型动画中出现奇怪的“断断续续”。看起来动画中有一个间隙,模型突然回到中间位置,然后快速回到下一帧的动画。

我正在使用安装了专有 NVidia 图形驱动程序的 Debian 7 64 位(带有 3GB VRAM 的 GeForce GTX 560M)。

我在这里有一段视频:http://jarrettchisholm.com/static/videos/wolf_model_animation_problem_1.ogv

在视频中有点难看(我猜它没有捕捉到所有帧)。当狼在它身边时,你可以更清楚地看到它。这贯穿整个动画。

我的模型渲染代码:

for ( glm::detail::uint32 i = 0; i < meshes_.size(); i++ )
    {
        if ( textures_[i] != nullptr )
        {
            // TODO: bind to an actual texture position (for multiple textures per mesh, which we currently don't support...maybe at some point we will???  Why would we need multiple textures?)
            textures_[i]->bind();
            //shader->bindVariable( "Texture", textures_[i]->getBindPoint() );
        }

        if ( materials_[i] != nullptr )
        {
            materials_[i]->bind();
            shader->bindVariable( "Material", materials_[i]->getBindPoint() );
        }       

        if (currentAnimation_ != nullptr)
        {
            // This is when I send the Identity matrices to the shader
            emptyAnimation_->bind();
            shader->bindVariable( "Bones", emptyAnimation_->getBindPoint() );

            glw::Animation* a = currentAnimation_->getAnimation();
            a->setAnimationTime( currentAnimation_->getAnimationTime() );
            // This generates the new bone matrices
            a->generateBoneTransforms(globalInverseTransformation_, rootBoneNode_, meshes_[i]->getBoneData());
            // This sends the new bone matrices to the shader,
            // and also binds the buffer
            a->bind();
            // This sets the bind point to the Bone uniform matrix in the shader
            shader->bindVariable( "Bones", a->getBindPoint() );
        }
        else
        {
            // Zero out the animation data
            // TODO: Do we need to do this?
            // TODO: find a better way to load 'empty' bone data in the shader
            emptyAnimation_->bind();
            shader->bindVariable( "Bones", emptyAnimation_->getBindPoint() );
        }

        meshes_[i]->render();
    }

着色器绑定(bind)代码:

void GlslShaderProgram::bindVariable(std::string varName, GLuint bindPoint)
{
    GLuint uniformBlockIndex = glGetUniformBlockIndex(programId_, varName.c_str());
    glUniformBlockBinding(programId_, uniformBlockIndex, bindPoint);
}

动画代码:

...
// This gets called when we create an Animation object
void Animation::setupAnimationUbo()
{
    bufferId_ = openGlDevice_->createBufferObject(GL_UNIFORM_BUFFER, 100 * sizeof(glm::mat4), &currentTransforms_[0]);
}

void Animation::loadIntoVideoMemory()
{
    glBindBuffer(GL_UNIFORM_BUFFER, bufferId_);
    glBufferSubData(GL_UNIFORM_BUFFER, 0, currentTransforms_.size() * sizeof(glm::mat4), &currentTransforms_[0]);
}

/**
 * Will stream the latest transformation matrices into opengl memory, and will then bind the data to a bind point.
 */
void Animation::bind()
{
    loadIntoVideoMemory();

    bindPoint_ = openGlDevice_->bindBuffer( bufferId_ );
}
...

我的 OpenGL 包装器代码:

...
GLuint OpenGlDevice::createBufferObject(GLenum target, glmd::uint32 totalSize, const void* dataPointer)
{
    GLuint bufferId = 0;
    glGenBuffers(1, &bufferId);
    glBindBuffer(target, bufferId);

    glBufferData(target, totalSize, dataPointer, GL_DYNAMIC_DRAW);
    glBindBuffer(target, 0);

    bufferIds_.push_back(bufferId);

    return bufferId;
}
...
GLuint OpenGlDevice::bindBuffer(GLuint bufferId)
{

    // TODO: Do I need a better algorithm here?
    GLuint bindPoint = bindPoints_[currentBindPoint_];
    currentBindPoint_++;

    if ( currentBindPoint_ > bindPoints_.size() )
        currentBindPoint_ = 1;

    glBindBufferBase(GL_UNIFORM_BUFFER, bindPoint, bufferId);

    return bindPoint;
    }
...

我的顶点着色器:

#version 150 core

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform mat4 pvmMatrix;
uniform mat3 normalMatrix;

in vec3 in_Position;
in vec2 in_Texture;
in vec3 in_Normal;
in ivec4 in_BoneIds;
in vec4 in_BoneWeights;

out vec2 textureCoord;
out vec3 normalDirection;
out vec3 lightDirection;

struct Light {
    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
    vec4 position;
    vec4 direction;
};


layout(std140) uniform Lights 
{
    Light lights[ 2 ];
};


layout(std140) uniform Bones 
{
    mat4 bones[ 100 ];
};

void main() {
    // Calculate the transformation on the vertex position based on the bone weightings
    mat4 boneTransform = bones[ in_BoneIds[0] ] * in_BoneWeights[0];
    boneTransform     += bones[ in_BoneIds[1] ] * in_BoneWeights[1];
    boneTransform     += bones[ in_BoneIds[2] ] * in_BoneWeights[2];
    boneTransform     += bones[ in_BoneIds[3] ] * in_BoneWeights[3];


    vec4 tempPosition = boneTransform * vec4(in_Position, 1.0);

    gl_Position = pvmMatrix * tempPosition;


    vec4 lightDirTemp = viewMatrix * lights[0].direction;

    textureCoord = in_Texture;

    normalDirection = normalize(normalMatrix * in_Normal);

    lightDirection = normalize(vec3(lightDirTemp));
}

如果我没有包含足够的信息,我深表歉意 - 我输入了我认为有用的信息。如果您想/需要查看更多信息,可以在 https://github.com/jarrettchisholm/glr 获取所有代码。在 master_animation_work 分支下。

最佳答案

它并不是特定于 opengl 的。

当导出器导出模型时,其中一些导出“皮肤游行”姿势。 IE。最初应用“骨骼修改器”的姿势。

在你的情况下,它可能是其中之一

  1. 您的导出器将此“皮肤游行”导出为第一帧(动画在其上循环)
  2. 或者您的动画框架无法正确循环,- 在最后一个动画键上找不到下一帧,并使用“skin parade”作为默认键。

问题可能出在计算动画变换的例程中。

这是调试它的方法。

渲染调试骨骼层次结构(可能使用最笨的着色器,甚至使用固定功能的 opengl)。调试骨骼层次结构可能如下所示:

enter image description here

在图片中 - 橙色线表示动画骨骼的当前位置。飞行坐标系(未连接的坐标系)显示默认位置。三角形和正方形是用于其他目的的调试几何体,与动画系统无关。

目视检查骨骼层级是否正确移动。

如果这个“默认帧”出现在调试层次结构中(即骨骼本身偶尔采取“皮肤游行”姿势),它要么是一个动画框架问题,纯粹是数学问题,它与 opengl 没有任何关系本身,或者是导出商问题(额外的框架)

如果它没有出现在那里(即骨骼正确移动但几何体以皮肤游行姿势站立),这是着色器问题。

调试动画骨架应该在没有任何骨骼权重的情况下渲染 - 只需计算骨骼的世界空间位置并用简单的线条连接它们。尽可能使用最笨的着色器或固定功能。

关于c++ - OpenGL 动画似乎偶尔会丢失骨骼数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18797328/

相关文章:

c++ - 使用 Visual C++ 在 Opengl 中创建 3D 球体

c++ - Linux VMware 11 guest 台面驱动程序安装以更新 OpenGL

c++ - 为什么不采用非类型偏特化元组?

嵌套在模板类中的 C++ 类

c++ - 后缀表达式求值

c++ - 如何从 OpenGL 加载图像 BACK?

macos - NSView 初始化/同步 OpenGL 的正确方法

c++ - 在 OSX Big Sur 上使用 OpenGL 4.x 进行开发

c++ - 2路队列中的访问冲突写入

c++ - 如果不更改全局信号处理程序,是否可以强制 OpenSSL 不生成 SIGPIPE?