Per mesh model matrices

Started by Mihai_Ionut_Floares, December 01, 2020, 10:20:43

Previous topic - Next topic


I figured out the movementof the camera trough scene and the perspective/view/model matrices that I created from scratch sine I didn't want to use JOML. It is working:

But now I want to add another mesh, but the meshes share the same model matrix. The projection and view matrices should be the same, but the model should be different because when I translate one object, all meshes are translated. The matrices are sent to vertex shader, but I can't figure out how to make per mesh model matrices. I guess I need per mesh shaders???


1. bind shader
2. upload matrix for object 1
3. draw object 1
4. upload matrix for object 2
5. draw object 2

No need for a separate shader program. All you need is to update the model matrix (or model-view-projection combination matrix) for each model.

This is, however, the most unoptimal way to do it. You could also use a uniform buffer object to store all objects' model matrices and then lookup the respective matrix needed for a particular object via a vertex attribute that you advance per object.
This would allow you to draw all objects (capped by the maximum UBO size, of course) with a single draw call.

There are many more possibilities, such as using instancing and storing only the individual objects' translations as instanced vertex attributes (in case where the objects' transformations only differ in translation and not e.g. in rotation and scaling).


Thank you. My question is How game engines like unity that uses DirectX allow you to specify a HLSL program for each a material that you later assign that material to the object?


A material does not have anything to do with object transformations/translations.


I tried the same thing with fragment colors:
1) Upload uniform vec4 color of mesh1 to fragment shader
2) draw mesh1
3) Upload uniform vec4 color f mesh2 to fragment shader
4)draw mesh2
It works, the 2 meshes have different colors

But this happens:
The mesh2 is visible trough mesh1


Quote from: Mihai_Ionut_Floares on December 01, 2020, 12:32:55
The mesh2 is visible trough mesh1
Enable depth testing once somewhere in your initialization code: