Hello Guest

Per mesh model matrices

  • 6 Replies
  • 4284 Views
Per mesh model matrices
« on: December 01, 2020, 10:20:43 »
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: https://streamable.com/1exa27

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

*

Offline KaiHH

  • ****
  • 334
Re: Per mesh model matrices
« Reply #1 on: December 01, 2020, 10:35:59 »
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).

Re: Per mesh model matrices
« Reply #2 on: December 01, 2020, 10:41:59 »
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?

*

Offline KaiHH

  • ****
  • 334
Re: Per mesh model matrices
« Reply #3 on: December 01, 2020, 10:43:56 »
A material does not have anything to do with object transformations/translations.

Re: Per mesh model matrices
« Reply #4 on: December 01, 2020, 12:32:55 »
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:
https://streamable.com/kyimhu
 The mesh2 is visible trough mesh1

*

Offline KaiHH

  • ****
  • 334
Re: Per mesh model matrices
« Reply #5 on: December 01, 2020, 12:46:45 »
The mesh2 is visible trough mesh1
Enable depth testing once somewhere in your initialization code:
Code: [Select]
glEnable(GL_DEPTH_TEST);

Re: Per mesh model matrices
« Reply #6 on: December 01, 2020, 12:59:58 »
Thank you.