简体   繁体   中英

When to use GLSL shaders in OpenGL in a game?

I am learning GLSL and how to do transformations and lighting etc however I just have 1 shader program that is being applied to everything. I'm not sure if I'm supposed to have a different program for each object in game (ie a box or the player models) or if I am supposed to just have 1 shader for everything.

How am I supposed to use shaders? How are they normally used?

Thanks.

Shaders are a bit like functions that run on the GPU instead of the CPU. Having one shader that does everything is like having one function that does everything; ie usually a huge, unmaintainable mess with branches everywhere. That said, having one shader for each object is usually overkill, as many objects can be rendered using the same shader, just with different models or textures.

For example, you can render in-world objects using one shader that does lighting, normal mapping, etc. However, HUD elements don't need any of those effects; they're usually drawn as unlit, diffuse textures, so you wouldn't use your shader that does lighting on them.

I'd say a good rule is that if you're writing a lot of branches ( if , switch , etc) in your shader, especially if the conditions are based on uniform s, then it's probably time to split it up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM