Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model transformations

Tags:

c++

opengl

I have a model successfully loaded, but now I want to transform it. Suffice it to say that I know how transformations work and the various transformations I can do through vectors and matrices and simply applying them to the model matrix which is then passed to the vertex shader through a uniform variable. But this is my issue:

My model is a class, and I also have a scene class which has the models loaded in it and is where all the graphical stuff comes together. If I have a model matrix as a private variable in Model, so:

class Model
{
public:
    Model();
    ~Model();

    // other functions here

    setPosition(vec3 pos);
    setRotation(vec3 axis, float angle);
    setScale(vec3 factor);

    // getters

    render(GLuint shaderProgram);
    update(GLuint shaderProgram);

private:
    mat4 pModelMatrix;
    vector<Mesh> pMeshes;
};

Is it wise for a model matrix to be inside the model class; or should there just be ONE model matrix that's in the scene class which is THEN passed by uniform into the vertex shader. My only concern with the latter is that if I do that then all models will have the same transformations. So, if I just want to move one model (which a player would have [yes, it's that has-a or is-a debate!]) then it would make sense to only apply the transformations to that one model and therefore to the model matrix it concerns. But then, what would the vertex shader say then? I'm still fairly new to OpenGL and it's a bit complex to understand the whole business with how OpenGL deals with transformations with multiple rendered models; I know they serve a purpose such as what has been described earlier, but whether there can be many model matrices of many models being passed to just one single uniform matrix is hard to realise.

What the update() function does is take pModelMatrix and uses glUniform() stuff to pass the matrix into the vertex shader which is then multiplied with all the other matrices and so on. The reason I put the glUniform() stuff into the update() function is because while my program runs I want the position my model to be recorded through setPosition(..) then finally update the model as required so long as the program is running.

The vector pMeshes, long story short, simply holds all the meshes of the model together which another class loads mesh by mesh.

like image 302
Poriferous Avatar asked Jun 19 '26 09:06

Poriferous


1 Answers

Model/world transform is specific to each mesh/model in your scene. Like you mentioned, to transform each model independently, having a matrix for each is apt.

As for the vertex shader, you could have the projection-view matrix in a uniform and the model matrix in a uniform and transform the mesh's vertices to the clip space. Then for the next model, just change the model matrix uniform with that mesh's matrix and repeat.

like image 80
legends2k Avatar answered Jun 21 '26 21:06

legends2k