0

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

在试图隔离这个问题时,我相信我遇到了一些可能相关的事情——在渲染模型时,如果我在 OpenGL 中将骨骼数据“归零”(也就是说,发送一堆身份矩阵),并且然后发送实际的骨骼数据,我在模型动画中得到奇怪的“口吃”。看起来动画中有一个间隙,模型突然回到它的中立位置,然后在下一帧迅速回到动画。

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

我在这里有一段视频: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 包装器代码:

...
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

4

1 回答 1

1

它不是真正的opengl特定的。

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

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

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

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

这是调试它的方法。

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

在此处输入图像描述

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

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

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

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

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

于 2013-09-14T02:45:43.137 回答