• Advertisement

OpenGL Skeletal animation, ASSIMP, glm and me in between!

Recommended Posts

What better things to do on a rainy weekend than working on one of my beloved sideprojects, eh?

I am trying to bring skeletal animation in my engine with the kind help of ASSIMP and GLM.

I used http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html as my primary resource, along with: 

" title="External link">

My understanding may not be perfect, but I do hope that the fundamentals are clear to me (This may still not be the case though!).

First of: The model I use for testing purposes is the one from the video-tutorial above.

I exported it with Blender as a .dae/collada file, resulting in it rendering 90° rotated about the x-axis. Blender's collada exporter does not support


I tried to fix this manually by editing the .dae file and "counter-rotating" but to no avail. I did not want to work on this any longer before the animation would work.

This is how it looks like rendered without the bones:


And this is the abomination that has bones :(:


I am pretty sure I messed up the matrices at some point, given the fact that ASSIMP and glm are basically transposes of each other, but I am not entirely sure, hence I ask for your help!

Onto some code then, I say!


Creation of the model:

// Sample postprocessing

const aiScene *scene = importer.ReadFile(path,

aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_FlipUVs);

if(!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)

throw new std::runtime_error("Problem loading model: " + path);

this->inverseModelMatrix = glm::inverse(convertMatrix(scene->mRootNode->mTransformation));

The inverseModelMatrix is used later.

std::vector<VertexBoneData> bones = loadBones(mesh, baseVertex);
return Mesh(vertices, indices, textures, bones, baseVertex, baseIndex);
std::vector<VertexBoneData> Model::loadBones(aiMesh *mesh, uint baseVertex)


std::vector<VertexBoneData> bones;


for(uint a = 0; a < mesh->mNumBones; a++)


uint boneIndex = 0;

std::string boneName(mesh->mBones[a]->mName.data);

if (boneMapping.find(boneName) == boneMapping.end()) {

boneIndex = numBones;


BoneInfo bi;


boneMapping[boneName] = boneIndex;

boneInfo[boneIndex].boneOffset = convertMatrix(mesh->mBones[a]->mOffsetMatrix);


else {

boneIndex = boneMapping[boneName];


for (uint w = 0; w < mesh->mBones[a]->mNumWeights; w++)


uint vertexId = baseVertex + mesh->mBones[a]->mWeights[w].mVertexId;

float weight = mesh->mBones[a]->mWeights[w].mWeight;

bones[vertexId].AddBoneData(boneIndex, weight);



return bones;


The mesh looks like this:


const static int NUM_BONES_PER_VERTEX = 4;

struct VertexBoneData


uint ids[NUM_BONES_PER_VERTEX] = {0};

float weights[NUM_BONES_PER_VERTEX] = {0};

void AddBoneData(uint BoneID, float Weight);


struct Vertex


glm::vec4 position;

glm::vec4 normal;

glm::vec2 textureCoordinates;


struct BoneInfo


glm::mat4 boneOffset;

glm::mat4 finalTransformation;


class Mesh



Mesh(std::vector<Vertex> vertices, std::vector<GLuint> indices, std::vector<GLuint> textures,

std::vector<VertexBoneData> bones, uint baseVertex, uint baseIndex);

void render();


std::vector<Vertex> vertices;

std::vector<GLuint> indices;

std::vector<GLuint> textures;

GLuint vaoId, vboId, eboId;

uint baseVertex, baseIndex = 0;

std::vector<VertexBoneData> bones;

GLuint boneBufferId;

void setup();
// Implementation follows http://www.learnopengl.com/#!Model-Loading/Mesh

Mesh::Mesh(std::vector<Vertex> vertices, std::vector<GLuint> indices, std::vector<GLuint> textures,

std::vector<VertexBoneData> bones, uint baseVertex, uint baseIndex) {

this->vertices = vertices;

this->indices = indices;

this->textures = textures;

this->bones = bones;

this->baseVertex = baseVertex;

this->baseIndex = baseIndex;








void Mesh::setup() {


glGenVertexArrays(1, &this->vaoId);

glGenBuffers(1, &this->vboId);

glGenBuffers(1, &this->eboId);

glGenBuffers(1, &this->boneBufferId);

// Bind them all


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->eboId);

// Prepare vertex buffer

glBindBuffer(GL_ARRAY_BUFFER, this->vboId);

glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), &this->vertices[0], GL_STATIC_DRAW);

// Set vertex attribute pointers

// 0 = pos


glVertexAttribPointer(POSITION_LOCATION, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) 0);

// 1 = normals


glVertexAttribPointer(NORMAL_LOCATION, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof(Vertex, normal));

// 2 = texture coordinates


glVertexAttribPointer(TEXTURE_COORDINATES_LOCATION, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof(Vertex, textureCoordinates));

// Prepare bone buffer

glBindBuffer(GL_ARRAY_BUFFER, this->boneBufferId);

glBufferData(GL_ARRAY_BUFFER, this->bones.size() * sizeof(VertexBoneData), &this->bones[0], GL_STATIC_DRAW);

// Set bone attributes

// 3 = bones


glVertexAttribIPointer(BONES_LOCATION, 4, GL_INT, sizeof(VertexBoneData), (const GLvoid *) 0);

// 4 = bone weights


glVertexAttribPointer(BONE_WEIGHT_LOCATION, 4, GL_FLOAT, GL_FALSE, sizeof(VertexBoneData), (const GLvoid *) (NUM_BONES_PER_VERTEX * 4));

// indices

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->eboId);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_STATIC_DRAW);


// Unbind Element buffer AFTER VAO (else it's unbound when vao is activated!)



void Mesh::render() {






(void *) (sizeof(uint) * baseIndex),





void VertexBoneData::AddBoneData(uint BoneID, float Weight) {

for (uint i = 0; i < NUM_BONES_PER_VERTEX; i++) {

if (weights[i] == 0) {

ids[i] = BoneID;

weights[i] = Weight;





Here I want to point out that I did not miss the I in


for the boneIds!


That would be the construction of the mesh with the bones!

Later down the line I want to get the transformation within an animation. So now comes a lot of code, basically like the one from ogldev with a bit more return-typiness.


std::vector<glm::mat4> Model::boneTransform(const std::string &path, float timeInSeconds)
glm::mat4 identity;

Assimp::Importer importer;

// Sample postprocessing
const aiScene *scene = importer.ReadFile(path,
aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_FlipUVs);

if(!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
throw new std::runtime_error("Problem loading model: " + path);

aiAnimation *anim = scene->mAnimations[0];

std::vector<glm::mat4> transforms;
if(anim == nullptr) return transforms;

float ticksPerSecond = (float) scene->mAnimations[0]->mTicksPerSecond != 0 ? scene->mAnimations[0]->mTicksPerSecond : 25.0f;
float timeInTicks = timeInSeconds * ticksPerSecond;
float animationTime = fmod(timeInTicks, (float)scene->mAnimations[0]->mDuration);

readNodeHierarchy(animationTime, scene, scene->mRootNode, identity);

for(uint i = 0; i < this->numBones; i++) {
transforms[i] = boneInfo[i].finalTransformation;

return transforms;
aiQuaternion Model::calculateInterpolatedRotation(float AnimationTime, const aiNodeAnim *pNodeAnim)
// we need at least two values to interpolate...
if (pNodeAnim->mNumRotationKeys == 1) {
aiQuaternion quat = aiQuaternion(pNodeAnim->mRotationKeys[0].mValue);
return quat;

aiQuaternion quat;

uint RotationIndex = FindRotation(AnimationTime, pNodeAnim);
uint NextRotationIndex = (RotationIndex + 1);
assert(NextRotationIndex < pNodeAnim->mNumRotationKeys);
float DeltaTime = (float)(pNodeAnim->mRotationKeys[NextRotationIndex].mTime - pNodeAnim->mRotationKeys[RotationIndex].mTime);
float Factor = (AnimationTime - (float)pNodeAnim->mRotationKeys[RotationIndex].mTime) / DeltaTime;
assert(Factor >= 0.0f && Factor <= 1.0f);
const aiQuaternion& StartRotationQ = pNodeAnim->mRotationKeys[RotationIndex].mValue;
const aiQuaternion& EndRotationQ = pNodeAnim->mRotationKeys[NextRotationIndex].mValue;
aiQuaternion::Interpolate(quat, StartRotationQ, EndRotationQ, Factor);


return quat;

void Model::readNodeHierarchy(float animationTime, const aiScene *scene, const aiNode *node, const glm::mat4 &parentTransform)
std::string nodeName(node->mName.data);

const aiAnimation* pAnimation = scene->mAnimations[0];

glm::mat4 nodeTransform = convertMatrix(node->mTransformation);

const aiNodeAnim* pNodeAnim = findNodeAnim(pAnimation, nodeName);

if (pNodeAnim)
// Interpolate scaling and generate scaling transformation matrix
aiVector3D scaling = calculateInterpolatedScaling(animationTime, pNodeAnim);
glm::vec3 scale = glm::vec3(scaling.x, scaling.y, scaling.z);
glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), scale);

// Interpolate rotation and generate rotation transformation matrix
aiQuaternion RotationQ = calculateInterpolatedRotation(animationTime, pNodeAnim);
glm::quat rotation(RotationQ.x, RotationQ.y, RotationQ.z, RotationQ.w);
glm::mat4 rotationMatrix = glm::toMat4(rotation);

// Interpolate translation and generate translation transformation matrix
aiVector3D Translation = calculateInterpolatedPosition(animationTime, pNodeAnim);
glm::vec3 translation = glm::vec3(Translation.x, Translation.y, Translation.z);
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), translation);

// Combine the above transformations
nodeTransform = translationMatrix * rotationMatrix * scaleMatrix;
// TODO Check if inverting multiplication here is correct
//nodeTransform = scaleMatrix * rotationMatrix * translationMatrix;

glm::mat4 globalTransformation = parentTransform * nodeTransform;
// TODO Check if inverting multiplication here is correct
//glm::mat4 globalTransformation = nodeTransform * parentTransform;

if (boneMapping.find(nodeName) != boneMapping.end()) {
uint BoneIndex = boneMapping[nodeName];
boneInfo[BoneIndex].finalTransformation = inverseModelMatrix * globalTransformation * boneInfo[BoneIndex].boneOffset;
// TODO Check if inverting multiplication here is correct
// boneInfo[BoneIndex].finalTransformation = boneInfo[BoneIndex].boneOffset * globalTransformation * inverseModelMatrix;

for (uint i = 0 ; i < node->mNumChildren ; i++) {
readNodeHierarchy(animationTime, scene, node->mChildren[i], globalTransformation);

const aiNodeAnim* Model::findNodeAnim(const aiAnimation* pAnimation, const std::string nodeName)
for (uint c = 0 ; c < pAnimation->mNumChannels ; c++) {
const aiNodeAnim* pNodeAnim = pAnimation->mChannels[c];

if (std::string(pNodeAnim->mNodeName.data) == nodeName) {
return pNodeAnim;

return nullptr;

aiVector3D Model::calculateInterpolatedScaling(float animationTime, const aiNodeAnim *animatedNode)
if (animatedNode->mNumScalingKeys == 1) {
return animatedNode->mScalingKeys[0].mValue;

uint index = FindScaling(animationTime, animatedNode);
uint nextIndex = (index + 1);
assert(nextIndex < animatedNode->mNumScalingKeys);
float deltaTime = (float)(animatedNode->mScalingKeys[nextIndex].mTime - animatedNode->mScalingKeys[index].mTime);
float factor = (animationTime - (float)animatedNode->mScalingKeys[index].mTime) / deltaTime;
assert(factor >= 0.0f && factor <= 1.0f);
const aiVector3D& Start = animatedNode->mScalingKeys[index].mValue;
const aiVector3D& End = animatedNode->mScalingKeys[nextIndex].mValue;
aiVector3D Delta = End - Start;
aiVector3D end = Start + factor * Delta;

return end;

uint Model::FindScaling(float AnimationTime, const aiNodeAnim* pNodeAnim)
assert(pNodeAnim->mNumScalingKeys > 0);

for (uint i = 0 ; i < pNodeAnim->mNumScalingKeys - 1 ; i++) {
if (AnimationTime < (float)pNodeAnim->mScalingKeys[i + 1].mTime) {
return i;


return 0;

uint Model::FindRotation(float AnimationTime, const aiNodeAnim* pNodeAnim)
assert(pNodeAnim->mNumRotationKeys > 0);

for (uint i = 0 ; i < pNodeAnim->mNumRotationKeys - 1 ; i++) {
if (AnimationTime < (float)pNodeAnim->mRotationKeys[i + 1].mTime) {
return i;


return 0;

aiVector3D Model::calculateInterpolatedPosition(float animationTime, const aiNodeAnim *pNodeAnim)
if (pNodeAnim->mNumPositionKeys == 1) {
return pNodeAnim->mPositionKeys[0].mValue;

uint PositionIndex = FindPosition(animationTime, pNodeAnim);
uint NextPositionIndex = (PositionIndex + 1);
assert(NextPositionIndex < pNodeAnim->mNumPositionKeys);
float DeltaTime = (float)(pNodeAnim->mPositionKeys[NextPositionIndex].mTime - pNodeAnim->mPositionKeys[PositionIndex].mTime);
float Factor = (animationTime - (float)pNodeAnim->mPositionKeys[PositionIndex].mTime) / DeltaTime;
assert(Factor >= 0.0f && Factor <= 1.0f);
const aiVector3D& Start = pNodeAnim->mPositionKeys[PositionIndex].mValue;
const aiVector3D& End = pNodeAnim->mPositionKeys[NextPositionIndex].mValue;
aiVector3D Delta = End - Start;
return Start + Factor * Delta;

uint Model::FindPosition(float animationTime, const aiNodeAnim *pNodeAnim)
for (uint i = 0 ; i < pNodeAnim->mNumPositionKeys - 1 ; i++) {
if (animationTime < (float)pNodeAnim->mPositionKeys[i + 1].mTime) {
return i;


return 0;


In there I think I screwed up with the matrices. You can see the comments where I am unsure, but maybe I missed a line altogether.


Oh, convertMatrix is important to! It returns the matrix transposed!


* @param aiMat 
* @return transposed version of the aiMat to fit into glm's style of doing things 
glm::mat4 Model::convertMatrix(const aiMatrix4x4 &aiMat)
return {
aiMat.a1, aiMat.b1, aiMat.c1, aiMat.d1,
aiMat.a2, aiMat.b2, aiMat.c2, aiMat.d2,
aiMat.a3, aiMat.b3, aiMat.c3, aiMat.d3,
aiMat.a4, aiMat.b4, aiMat.c4, aiMat.d4

Maybe that one is wrong? At least in combination with the rest it's important.


Now onto the shader and the rendering:

#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 textureCoordinates;
layout (location = 3) in ivec4 boneIds;
layout (location = 4) in vec4 boneWeights;

out vec4 vertexPosition;
out vec4 vertexNormal;
out vec2 vertexTextureCoordinates;

uniform mat4 normalMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelMatrix;

const int MAX_BONES = 100;
uniform mat4 bones[MAX_BONES];

void main()
mat4 boneTransform = bones[boneIds[0]] * boneWeights[0];
boneTransform += bones[boneIds[1]] * boneWeights[1];
boneTransform += bones[boneIds[2]] * boneWeights[2];
boneTransform += bones[boneIds[3]] * boneWeights[3];

vec4 posL = boneTransform * vec4(position, 1);
gl_Position = modelViewProjectionMatrix * posL;

vec4 normalL = boneTransform * vec4(normal, 0);
vertexNormal = (modelMatrix * normalL);
vertexPosition = (modelMatrix * posL);


Pretty much like ogldev's version. I initialize the

uniform-array like this:

for(unsigned int a = 0; a < 100; a++) {
std::stringstream name;
name << "bones[" << a << "]";
attributes.boneLocations[a] = glGetUniformLocation(attributes.shaderId, name.str().c_str());


Rendering looks like this:


std::vector<glm::mat4> transforms = this->appearance.model->boneTransform("../res/models/demo_character_tm.dae", animationTime);

for(uint index = 0; index < transforms.size(); index++) {
//glm::mat4 mat = glm::transpose(transforms[index]);

glm::mat4 mat = transforms[index];


Again, not sure if it makes sense to set GL_TRUE for transposed (looks like hell then, even more!)


I know this is a lot of code... I tried to provide only the relevant parts, but the problem for me is, that the whole bone-rendering/animation stuff seems so overwhelming that there simply is a lot of code involved.

If you need any more code, please let me know.


Thanks to anyone having a look at this. I am really grateful for any hint you can give me!

Enjoy the rest of your weekends everybody!


Share this post

Link to post
Share on other sites

Sadly not. As I don't know which operating system you use I simply provide my cmake build for linux.

I only stripped some other models I used for testing. The one provided was released under public domain.

Once you're ready to go: if the application runs

1) Move around so you can face the (only) entity

2) just hit "TAB" twice (!) then you are in another camera mode

3) If you now roll your mousewheel UP it should start the animation (so animation is bound to mousewheel at the moment). It is hacky, so no modulo used, which means it crashes if you roll it downwards initially or up for too long.

But this should give an impression!

If you find the mistake you'll have my eternal gratitude and if you're interested I have quite a bag of unused game keys from humble bundles lying around where you could pick some (again, if you're interested, not trying to bribe anyone :))


edit: also I did not clean up the code much. I am totally aware it's a mess, partly. It's basically a playground for me. I am not a C++ developer by day and I think one sees that.

Edited by Salam

Share this post

Link to post
Share on other sites

CMake is totally fine, I use it on Linux too. And I don't mind messy code, but is this actually the version you described? I had to add a call to initWindow() and mouse wheel didn't seem to do much. Just tested it quickly though, I'll check this out better later today.

Share this post

Link to post
Share on other sites

Ok, I gave it a better try and seems like the main problem is in Model.cpp, line 263. glm::quat expects w,x,y,z for its constructor's parameters, and you've typed x,y,z,w. You should also probably enable depth testing somewhere with glEnable(GL_DEPTH_TEST). The model is still rotated 90 degrees by x-axis though, but that's some another problem.

I'd suggest making helper functions for converting all the required types to glm (just like you have the convertMatrix function), I did it like this the last time I used assimp:

static inline glm::vec3 vec3_cast(const aiVector3D &v) { return glm::vec3(v.x, v.y, v.z); } 
static inline glm::vec2 vec2_cast(const aiVector3D &v) { return glm::vec2(v.x, v.y); } // it's aiVector3D because assimp's texture coordinates use that
static inline glm::quat quat_cast(const aiQuaternion &q) { return glm::quat(q.w, q.x, q.y, q.z); } 
static inline glm::mat4 mat4_cast(const aiMatrix4x4 &m) { return glm::transpose(glm::make_mat4(&m.a1)); }

Also a tip for sending the bone transforms, instead of looping through the array, you can send everything at once, like this:

glUniformMatrix4fv(glGetUniformLocation(program, "BoneMatrices"), transforms.size(), GL_FALSE, glm::value_ptr(transforms[0]));

Hopefully this helped!

Edited by Sponji

Share this post

Link to post
Share on other sites

Hey, first of: Big thanks!

Secondly then I screwed up somewhere.

I got rid of the .git dir and some cmake cache etc. I tried out the program before, but I assume that afterwards I changed something and packaged then... I am sorry for the inconvenience, I can check in the evening.

Also the mousewheel code was gone because of these changes, probably. I did not try to hide anything, but also to provide a minimal example. So I got rid of the Raycaster and other implementations that (I thought) are not necessary for the example.

But in the Raycaster there would be the GL_DEPTH_TEST, afair.

Regarding the quat... yes, this is a relict which I found somewhere as a possible solution and I forgot about reverting this.

That optimization is super, thanks!


But as far as I understood you the model is still incorrect, right? I mean basically the feet are in the head or is that resolved with the quat? Because then I will bite myself.


The rotation... yes, I have no idea how to solve that with blender so far as the collada exporter does not allow for Y_UP and rotating around the x-axis does not change anything once exported :(

Share this post

Link to post
Share on other sites

The model and animation seemed to work nicely for me, and the only modifications I made to that zip were just 1) that one quaternion line 2) adding that missing call to initWindow before creating the GL context 3) and adding glEnable(GL_DEPTH_TEST). And I realized that the mouse wheel worked after all, I just didn't expect it to update time directly :P

Share this post

Link to post
Share on other sites

Cool, thanks a bunch. I will try in the evening and bite my ass if this was really it. (As I always assumed there is still something wrong with my understanding how to multiply assimp and glm matrices etc.)

I will create a helper for glm and assimp conversion.

Thanks again for your time and effort! If you're interested in some games I can go through my catalogue and see what keys are still unused :)

Share this post

Link to post
Share on other sites
I tried to fix this manually by editing the .dae file[/quote] Don't ever consider doing that. Not only are you likely to mess up, but what will you do when you have 25 or 30 models in your game? What if you have 200? Things must go somewhat automated, or you are doomed. No hand-editing files. Have you considered just rotating the model and the bones (your image looks like you only rotated the mesh, and left the skeleton) in Blender prior to exporting? I know it's annoying having to do that, but it takes a mere 6 keystrokes. I'm not sure why Blender only exports Collada as Z_UP, in my opinion that's the worst possible choice out of two possible options, but it is what it is, gotta live with it.

Share this post

Link to post
Share on other sites


I tried to fix this manually by editing the .dae file

Don't ever consider doing that. Not only are you likely to mess up, but what will you do when you have 25 or 30 models in your game? What if you have 200? Things must go somewhat automated, or you are doomed. No hand-editing files.

Have you considered just rotating the model and the bones (your image looks like you only rotated the mesh, and left the skeleton) in Blender prior to exporting? I know it's annoying having to do that, but it takes a mere 6 keystrokes.

I'm not sure why Blender only exports Collada as Z_UP, in my opinion that's the worst possible choice out of two possible options, but it is what it is, gotta live with it.


Once I figured out how to modify the .dae file I would actually have tried to automate that process and provide some kind of tool for myself and maybe even open source it so others can benefit. Also it helps me to understand the .dae format.

But I agree, of course it would be very tedious to do this by hand every time.

I am not a C++ pro and certainly no visual computing guru but I am a software engineer by heart and only do stuff by hand when I try to understand something or am 100% sure I will only do this once or twice :)

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

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
      Thank you
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement