OpenGL动画似乎偶尔缺少骨骼数据

时间:2013-09-14 01:12:36

标签: c++ opengl

好吧,所以我在OpenGL引擎中加载和渲染基本模型。我有动画为模特工作。但是,当我尝试将多个带动画的模型添加到场景中时,我得到了一些奇怪的行为 - 最后一个模型动画不正确。

在尝试隔离问题时,我相信我发生了一些可能相关的事情 - 在渲染模型时,如果我在OpenGL中“清零”骨骼数据(即,发送一堆身份矩阵) ),然后发送实际的骨骼数据,我在模型动画中得到奇怪的“口吃”。看起来动画中存在间隙,模型突然回到中立位置,然后快速回到下一帧的动画。

我正在使用安装了专有NVidia图形驱动程序的Debian 7 64bit(带有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();
    }

着色器绑定代码:

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 Wrapper代码:

...
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));
}

如果我没有提供足够的信息,我道歉 - 我提出了我认为有用的内容。如果您希望/需要查看更多内容,可以在master_animation_work分支下的https://github.com/jarrettchisholm/glr处获取所有代码。

1 个答案:

答案 0 :(得分:1)

这不是特定的opengl。

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

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

  1. 您的导出器导出此“皮肤游行”作为第一帧(并且动画循环在其上)
  2. 或者您的动画框架无法正常循环, - 当它位于最后一个动画关键字时无法找到下一帧,并使用“皮肤阅兵”作为默认关键。
  3. 问题可能在于计算动画变换的例程。

    以下是调试方法。

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

    enter image description here

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

    目测检查骨骼层次结构是否正确移动。

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

    如果它没有出现(即骨骼正确移动但是几何体位于皮肤游行姿势),那就是着色器问题。

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