Jump to content
  • Advertisement
Sign in to follow this  
miggs

OpenGL multiple shaders per model

This topic is 2457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi
(i'm using opengl 4.0 but this question is rather theroetical so thats not important)

currently i have a structure, like this (simplified)
model{ // object on screen with a world matrix
modelpart{ // a part of the model with a material, e.g. arm, leg, or pants, or window (if model is a house..)
mesh { vertBuffer, iBUffer... etc.. }
}
}


when i render, i activate the models shader, a normal shader for example ( sometimes in renderlists a shader is even activated along multiple models)

my question is, what if i would like to have different shaders on each modelpart, how do you guys go on about that?
if for example the naked arm has a different shader than some fancy blink metal armor on the chest, or a part has normalmapping, and a part is parallax?

because at the moment the Model->Render() method sets a ModelViewMatrix for the active shader, and now each ModelPart would need a reference to the parent Model's ModelViewMatrix, and activate it's own shader and set the modelviewmatrix


is this situation even realistic or does a shader per model suffice in most cases? Or is it that extreme that instead of rendering Models, you have renderlists of modelparts with the same shader?

would it help to chang my model structure somehow?


thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Different meshes can share the same vertex/index buffers -- the model can own these buffers, and then the meshes can contain offsets into the buffers.
Likewise, different meshes can share the same constant buffers (aka uniform buffers in GL) -- you can write the common parameters (e.g. modelViewMatrix) into one cbuffer, which is then used by every shader on that model.

Or is it that extreme that instead of rendering Models, you have renderlists of modelparts with the same shader?
Yes, kind of. When you "render" a model, I wouldn't actually have it render anything, I would have the model insert it's meshes into an array. After all models to be drawn have been inserted into the array, I would then sort it and then actually render the sorted meshes.

Share this post


Link to post
Share on other sites
ah very interesting. i had also thought about seperating meshes from models once but i took the convenient (and slow) route and set the buffer pointers on each mesh draw call. but with your way i can bind a buffer for a model of a kind once and then render it on different positions with different parameters.

this brings me to another question:

when you have your meshes sorted in a list, if we say for example 3 meshes share the same buffers and are drawn one after the other, but for some reason one of those wants to use a different shader. what would you prioritise?

- would you rather group meshes with the same buffer first, and then with those meshes sort those with the same shaders and change shaders more often
- or group shaders, and then within shaders group meshes which share buffers?



thanks is advance, your answer was very very helpful

Share this post


Link to post
Share on other sites

what would you prioritise?

Shader. However, nearly all my meshes come from one buffer (over 128MB buf in my current project) - so, there essentially are no buffer changes anyway. And there are only a few vertex formats (two, currently) sourcing from that buffer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!