C++ OpenGL动画似乎偶尔会丢失骨骼数据
好的,我已经在OpenGL引擎中加载和渲染了基本模型。我为一个模特制作了动画。然而,当我尝试在一个场景中添加多个带有动画的模型时,我得到了一堆奇怪的行为——上一个模型的动画设置不正确 在试图隔离这个问题时,我相信我遇到了一些可能相关的事情——在渲染模型时,如果我在OpenGL中“归零”骨骼数据(即,发送一组标识矩阵),然后发送实际的骨骼数据,我会在模型动画中感到奇怪的“口吃”。看起来动画中有一个间隙,模型突然返回到其中性位置,然后在下一帧快速返回到动画 我正在使用Debian 7 64位,并安装了专有的NVidia图形驱动程序(GeForce GTX 560M和3GB VRAM) 我这里有一段视频: 在视频中有点难看(我想它并没有捕捉到所有的帧)。当狼站在它一边时,你可以看得更清楚。这在整个动画中都会发生 我的模型渲染代码:C++ OpenGL动画似乎偶尔会丢失骨骼数据,c++,opengl,C++,Opengl,好的,我已经在OpenGL引擎中加载和渲染了基本模型。我为一个模特制作了动画。然而,当我尝试在一个场景中添加多个带有动画的模型时,我得到了一堆奇怪的行为——上一个模型的动画设置不正确 在试图隔离这个问题时,我相信我遇到了一些可能相关的事情——在渲染模型时,如果我在OpenGL中“归零”骨骼数据(即,发送一组标识矩阵),然后发送实际的骨骼数据,我会在模型动画中感到奇怪的“口吃”。看起来动画中有一个间隙,模型突然返回到其中性位置,然后在下一帧快速返回到动画 我正在使用Debian 7 64位,并安装
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();
}
动画代码:
...
// This gets called when we create an Animation object
void Animation::setupAnimationUbo()
{
bufferId_ = openGlDevice_->createBufferObject(GL_UNIFORM_BUFFER, 100 * sizeof(glm::mat4), ¤tTransforms_[0]);
}
void Animation::loadIntoVideoMemory()
{
glBindBuffer(GL_UNIFORM_BUFFER, bufferId_);
glBufferSubData(GL_UNIFORM_BUFFER, 0, currentTransforms_.size() * sizeof(glm::mat4), ¤tTransforms_[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));
}
如果我没有提供足够的信息,我深表歉意——我把我认为有用的东西放进去了。如果您想/需要查看更多内容,可以在
master\u animation\u work
分支下获取所有代码 它不是真正特定于opengl的
导出器导出模型时,其中一些导出“皮肤游行”姿势。即,最初应用“骨骼修改器”的姿势
在你的情况下,它可能是其中之一
调试动画骨架应该在没有任何骨骼权重的情况下渲染-只需计算骨骼的世界空间位置并用简单的线连接它们。使用尽可能最愚蠢的着色器或固定函数。感谢回复@SigTerm-结果是,我的
bindPoints\uuucode>向量中有一个太多的绑定点,因此当动画试图绑定到无效的绑定点时,opengl给出了一个错误,只使用了先前绑定的动画值。我不知道关于“皮肤游行”的事——很高兴知道!
#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));
}