Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Rendering Boned Model Distoration


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 OmniRa   Members   -  Reputation: 132

Like
0Likes
Like

Posted 27 May 2014 - 11:29 AM

Hi, i've been using Assimp to extract model information into a custom format and use in my program. However I havnt been able to render it correctly.
 
I have managed to get the vertices rendered without using the bones. This creates the basic pose of the guy (img 1) just holding his hands out.
screenshot1.png

However once i get the animator creating bone matrices for it to use things get weird (img 2).
screenshot2.png

I decided to use the assimp source code to check if some of my calculations were correct. First i checked the final transformations of each bone matched assimps at frametime 0. They are now matched, so I have the same final formations for each bone (except mine are column-major, and assimp is row-major).
 
Since my final formations seemed ok I decided to keep looking for my distortion problem. I checked my vertices contained the correct boneIDs and weights.
 
After checking those were correct I double checked my shader

#version 330

const int MAX_BONES_PER_MESH = 50;

uniform mat4  mvp_matrix;
uniform float alpha;
uniform mat4  bone_matrix[MAX_BONES_PER_MESH];

in vec3  in_vertex;
in vec3  in_colour;
in vec2  in_texCoord0;
in vec3  in_normal;
in ivec4 in_boneID;
in vec4  in_boneWeight;

out vec4 colour;
out vec2 texCoord0;
out vec3 normal;

void main(void) 
{
	mat4 boneMatrix = bone_matrix[in_boneID[0]] * in_boneWeight[0];

	boneMatrix +=     bone_matrix[in_boneID[1]] * in_boneWeight[1];
	boneMatrix +=     bone_matrix[in_boneID[2]] * in_boneWeight[2];
	boneMatrix +=     bone_matrix[in_boneID[3]] * in_boneWeight[3];

	vec4 position = boneMatrix * vec4(in_vertex, 1.0);

	gl_Position = mvp_matrix * position;

	colour = vec4(in_colour, alpha);
	texCoord0 = in_texCoord0;
	normal = in_normal;	
}

which I don't think contains any error.

 

So i was wondering is anyone has experienced distortion like these images and had the problem lay elsewhere. Is there something I should check next. I will double check my data in the meanwhile.

I can give more information if u need, just ask!

 

Ty for any help smile.png



Sponsor:

#2 beans222   Members   -  Reputation: 1220

Like
0Likes
Like

Posted 27 May 2014 - 12:55 PM

Can you post the code that builds the bone array?


New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#3 OmniRa   Members   -  Reputation: 132

Like
0Likes
Like

Posted 27 May 2014 - 02:47 PM

Can you post the code that builds the bone array?

 

Not sure which part you mean, the creation of the final transforms (that will be bone_matrix in the shader) or bone array (containing boneID+vertex).

 

During the converter, for each mesh the code

// bones list
if (pMesh->HasBones())
{
    if (m_type == TYPE_ANIMATED)
        m_type = TYPE_BONED;

    mesh.boneList.resize(pMesh->mNumVertices);

    for (unsigned int bone = 0; bone < pMesh->mNumBones; ++bone)
    {
        CBoneData boneData;

        int boneID = 0;
        std::string boneName = pMesh->mBones[bone]->mName.data;
        
        if (m_boneMapping.find(boneName) != m_boneMapping.end())
        {
            boneID = m_boneMapping[boneName];
        }
        else
        {
            boneID = m_boneInfo.size();
            m_boneMapping[boneName] = boneID;
            
            BONED_MODEL_SKELETON_BONE boneInfo;

            boneInfo.m_id = boneID;
            strcpy(boneInfo.m_name, boneName.c_str());
            CMatrix(pMesh->mBones[bone]->mOffsetMatrix).FillAsColMajor(boneInfo.m_boneOffset);

            m_boneInfo.push_back(boneInfo);
        }

        for (unsigned int w = 0; w < pMesh->mBones[bone]->mNumWeights; ++w)
            _AddBone(&mesh, pMesh->mBones[bone]->mWeights[w].mVertexId, boneID, pMesh->mBones[bone]->mWeights[w].mWeight);
    }
}

// validate the boneslist (weights should total to 1.0)
for (unsigned int i = 0; i < mesh.boneList.size(); ++i)
{
    float total = 0.0f;

    for (int w = 0; w < MAX_BONES_PER_VERTEX; ++w)
        total += mesh.boneList[i].weight[w];

    if (total > 0.0f && total < 1.0f)
        throw std::string("Total weights are not equal to 1.0");
}
void CScene::_AddBone(CModelMesh *pMesh, unsigned int vertexID, int boneID, float weight)
{
	for (int i = 0; i < MAX_BONES_PER_VERTEX; ++i)
		if (pMesh->boneList[vertexID].weight[i] == 0.0f)
		{
			pMesh->boneList[vertexID].id[i] = boneID;
			pMesh->boneList[vertexID].weight[i] = weight;
			return;
		}

	throw std::string("Cannot add more weights to this vertex");
}

Above is the first part of collecting all the bone information. Here also the boneList is populated, through _AddBone. The boneList is defined as

const int MAX_BONES_PER_VERTEX = 4;

struct CBoneData
{
    CBoneData(void); // init all entries to 0

    int id[MAX_BONES_PER_VERTEX];
    float weight[MAX_BONES_PER_VERTEX];
};

In my actual program where the data of the custom format is being read. The bone list is created by reading the data

if (m_header.m_numBones > 0)
    pFileManager->ReadBytes(pFile, m_header.m_numBones * sizeof(CBone), &m_bones[0]);

A buffer is generated and setup

if (m_bones.size() > 0)
{
    glGenBuffers(1, &m_boneBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_boneBuffer);
    glBufferData(GL_ARRAY_BUFFER, m_bones.size() * sizeof(CBone), &m_bones[0], GL_STATIC_DRAW);
}

Once a shader has been set for the mesh the following code is used

if (m_bones.size() > 0)
{
    glBindBuffer(GL_ARRAY_BUFFER, m_boneBuffer);
    glVertexAttribIPointer(m_pShader->GetBoneInID(), CBone::MAX_BONES_PER_VERTEX, GL_INT, sizeof(CBone), (const GLvoid*)CBone::OFFSET_TO_ID);
    glVertexAttribPointer(m_pShader->GetBoneWeightInID(), CBone::MAX_BONES_PER_VERTEX, GL_FLOAT, GL_FALSE, sizeof(CBone), (const GLvoid*)CBone::OFFSET_TO_WEIGHT);
}

The bone structure used in my main program (CBone) is defined as

class CBone
{
public:
    static const int MAX_BONES_PER_VERTEX = 4;

    // based on order, since boneID then weight
    static const int OFFSET_TO_ID = 0;
    static const int OFFSET_TO_WEIGHT = sizeof(int) * MAX_BONES_PER_VERTEX;

    explicit CBone(void)
    {
        for (int i = 0; i < MAX_BONES_PER_VERTEX; ++i)
        {
            boneID[i] = 0;
            weight[i] = 0;
        }
    }

    int boneID[MAX_BONES_PER_VERTEX];
    float weight[MAX_BONES_PER_VERTEX];
};

// ===========================================================================================================

This is how the skeleton is created by the converter, which is eventually read by the main program. Just listed for reference.

void CScene::BuildSkeleton(const aiNode *pNode)
{
	int boneID = 0;
	std::string name = pNode->mName.C_Str();

	// check if its a bone (will have its name in our already built bone list)
	// if its not there it will need creating, even if a camera etc, as it may contain children bones
	if (m_boneMapping.find(name) == m_boneMapping.end())
	{
		boneID = m_boneInfo.size();
		m_boneMapping[name] = boneID;
			
		BONED_MODEL_SKELETON_BONE boneInfo;

		boneInfo.m_id = boneID;
		strcpy(boneInfo.m_name, name.c_str());
		CMatrix().LoadIdentity().FillAsColMajor(boneInfo.m_boneOffset);

		m_boneInfo.push_back(boneInfo);
	}
	else
		boneID = m_boneMapping[name];

	// add to skeleton
	CMatrix(pNode->mTransformation).FillAsColMajor(m_boneInfo[boneID].m_transform);
	m_boneInfo[boneID].m_numChildren = pNode->mNumChildren;

	m_skeleton.push_back(m_boneInfo[boneID]);

	// handle all its children
	for (unsigned int i = 0; i < pNode->mNumChildren; ++i)
		BuildSkeleton(pNode->mChildren[i]);
}

Sorry for so much spam biggrin.png However f you meant how the bone tranforms are made, its

void CModelBone::_ProcessTransforms(const CModelAnimator & animator, CModelBone *pBone, const aat::matrix4f & parentTransform, const aat::matrix4f & globalInverse)
{
	aat::matrix4f transformation = pBone->m_transform;

	if (animator.HasAnimations() && animator.BoneHasKeyframes(pBone))
	{
		aat::matrix4f translation = animator.GetInterpolatedTranslation(pBone);
		aat::matrix4f rotation = animator.GetInterpolatedRotation(pBone);
		aat::matrix4f scale = animator.GetInterpolatedScale(pBone);

		transformation = translation * rotation * scale;
	}

	transformation = parentTransform * transformation;

	pBone->m_finalTrans = globalInverse * transformation * pBone->m_offset;

	foreach (pBone->m_subBones, bone)
		_ProcessTransforms(animator, (*bone), transformation, globalInverse);
}

Called by

void CModelBone::ProcessTransforms(const CModelAnimator & animator, const aat::matrix4f & globalInverse)
{
    _ProcessTransforms(animator, this, aat::matrix4f().Identity(), globalInverse);
}

Which is called in the model update from the rootbone

m_pRootBone->ProcessTransforms(m_animator, m_bonesGlobalInverse);

Edited by OmniRa, 27 May 2014 - 02:50 PM.


#4 beans222   Members   -  Reputation: 1220

Like
0Likes
Like

Posted 27 May 2014 - 06:00 PM

The offset matrix should only be multiplied with the final transform for the bone's node (after all parent nodes have been multiplied in).


New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#5 OmniRa   Members   -  Reputation: 132

Like
0Likes
Like

Posted 27 May 2014 - 06:07 PM

I set it to an identity matrix for non bonenodes so it wouldnt do anything anyway



#6 beans222   Members   -  Reputation: 1220

Like
0Likes
Like

Posted 27 May 2014 - 11:07 PM

Everything looks in order there. Before I didn't understand your method of going rootnode down but still giving out final transforms on the way; wouldn't have thought of that.

 

How about how the final array is generated for the shader (bone_matrix) ?


New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#7 JordanBonser   Members   -  Reputation: 569

Like
0Likes
Like

Posted 28 May 2014 - 02:50 AM

I believe I had a similar issue to you when I was doing skeletal animation, and in fact I was using the same model as you smile.png

 

Anyway if you go to the animation category of my blog: 
http://jordanbonser.weebly.com/blog/category/animation

 

and look at the "Skeletal Animation Completed!" post it explains some of the issues I had.

 

To sum it up I had to ensure that an assimp flag "aiProcess_FindInvalidData" was disabled.  I also had the Rotation * Scale * Translation order the wrong way round for my application.

 

Hope this helps smile.png

 

As an added point, I've got a link to a simple collada model in one of those blog posts. It really helped as I could really simplify the problem, maybe it might be worthwhile you doing this if the issues persists. 


Edited by JordanBonser, 28 May 2014 - 04:10 AM.


#8 OmniRa   Members   -  Reputation: 132

Like
0Likes
Like

Posted 28 May 2014 - 04:41 PM

Thanks I'll check that link out tommoz.

 

The locations are found when the shader is created

// keep adding bones till it fails
int i = 0;
int location;

while (_GetUniformInfo(FormattedString("bone_matrix[%d]", i), &location))
{
    m_boneLocation.push_back(location);
    ++i;
}

LOG->Send("Shader supports %d bones", m_boneLocation.size());
bool CShader::_GetUniformInfo(const std::string & name, int *pLocation)
{
    ASSERT(pLocation != nullptr, "pLocation is NULL");

    *pLocation = glGetUniformLocation(m_programID, name.c_str());
		
    if (*pLocation == -1)
        return false;
		
    return true;
}

My bones are sent from the Mesh

void CBonedMesh::Render(float alpha, const std::vector<CModelBone> *pBoneTrans)
{
    CVideo *pVideo = GAME->GetVideoManager()->GetVideo();

    m_pShader->UseShader();

    m_pShader->SendMVPMatrix(pVideo->PassModelViewProjectionMatrix());
    m_pShader->SendAlpha(alpha);
    m_pShader->SendTextureID(0);
    m_pShader->SendBoneTransforms(pBoneTrans);

    if (m_texSlot != NO_TEXTURE)
        GAME->GetVideoManager()->GetTextureController()->BindTexture(m_texSlot);

    glBindVertexArray(m_object);
    glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

to the shader, which sends each one

void CShader::SendBoneTransforms(const std::vector<CModelBone> *pBoneTrans, bool transpose)
{
    ASSERT(pBoneTrans->size() < m_boneLocation.size(), "This shader doesn't support this many bone transforms");
	
    foreach ((*pBoneTrans), bone)
        _SendUniform(m_boneLocation[bone->GetID()], bone->GetFinalTrans().GetData(), transpose);
}

Sending of the uniform, transpose is defaulted to false

void CShader::_SendUniform(int location, const float *pMatrix, bool transpose)
{
    glUniformMatrix4fv(location, 1, transpose, pMatrix);
}

Ty again for link, hopefully I can find some info to use :)



#9 OmniRa   Members   -  Reputation: 132

Like
0Likes
Like

Posted 29 May 2014 - 08:52 PM

I figured it out, it was such a silly mistake :(

When i first made my static models i flipped the vertex z and y's because the blender file i used had them flipped. I totally forgot I did this!!

Ofc the vectors and quaternions i left the same for animation. Which meant all my transforms looked like the other program I was trying to match and it was my vertices were wrong!!!!!!!!

So sorry :|



#10 JordanBonser   Members   -  Reputation: 569

Like
0Likes
Like

Posted 30 May 2014 - 02:40 AM

At least you got it figured out :)

 

Don't worry It's pretty difficult to see what's wrong with animation by just looking at it. 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS