Click here to Skip to main content
13,044,467 members (58,917 online)
Rate this:
Please Sign up or sign in to 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?

Posted 26-Jan-12 20:22pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 27 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100