Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to write Flat, Gouraud and Phong shadings in the same application.
I think it's a problem writing them in the same shader with "if" statements, because some of the variables are supposed to be uniform in flat while supposed to be varying in Phong.

If I write 3 different shaders, does it mean that I will have to bind them or remove them separately, whenever I want to switch the shading?

Can I write functions that all 3 shaders can use in GLSL? Where should they be written? in which files?

Thanks!
Posted

1 solution

Starting with those three classic shaders is a good idea. I would suggest that you use three separate shaders and do not tryto integrate them into one. Shaders are like short functions which are going to be loaded into the GPU for execution. The GPU has only limited memory to store the shaders. It has grown larger over time, but still it is better to keep them as short as possible. Also, the shader's code is executed for every pixel and any additional overhead to distinguish between the three cases will slow the GPU down. Perhaps not very much, but it still is a waste.

It has been a long time I had a look at OpenGL, now I mostly use DirectX and XNA. Therefore I can't tell you what must be done to switch between different shaders, but it sounds logical to set up your model for rendering when switching shaders, just as you do when you first load the model.

As to your third question: Due to the memory limitation and the plugin-like character of the shaders it would be unusual to have the option to write function libraries for the GPU. The idea is not bad and may be possible in the future, but up to now shaders were seen as simple stand alone functions.

Edit: I just looked up GLSL and found out that user defined functions generally are possible. It would be best to consult the GLSL manual or reference to see what they can be used for or what kinds of limitations exist.

Edit^2: There also is another reason for keeping shaders as simple as possible: If you want to support older graphics hardware, then you must stay within the specifications of earlier versions. If you use the latest features, only users with the newest hardware can run your program. It's your decision how far you go back in order to make your program more cmpatible and where you draw the line. Many programs have several shaders for the same purpose, each one designed for a different version. This usually is part of the graphics detail and quality settings.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900