• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
OmniRa

Rendering Boned Model Distoration

9 posts in this topic

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.
[attachment=21851:screenshot1.png]

However once i get the animator creating bone matrices for it to use things get weird (img 2).
[attachment=21852: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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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) ?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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 :)

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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. 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0