|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Creating a GLSL Library |
|
![]() swiftcoder Member since: 7/3/2003 From: Boston, MA, United States |
||||
|
|
||||
| Very useful, thanks for sharing! |
||||
|
||||
![]() GamerSg Member since: 5/1/2003 From: Singapore |
||||
|
|
||||
| Im wondering if the code presented is efficient enough to be used in games? I believe SM2 cards have a huge slowdown with if statements which seem to be used all over the place in this article. |
||||
|
||||
![]() haegarr Member since: 10/11/2005 From: Selm, Germany |
||||
|
|
||||
| First, thanks for the article :) Second, I've to hint at this (AFAIK) error :( Quote: I'm pretty sure that it is the other way around: Point lights have w==1 in their GL_POSITION parameter, and directional lights have w==0 in their GL_POSITION parameter. That is the typical (normalized) distinction between points and directions. I assume it is not reverse in GLSL!? |
||||
|
||||
![]() donny dont Member since: 3/29/2007 |
||||
|
|
||||
Thanks for the feedback on the article.Quote: I honestly didn't run too many tests on various hardware to figure out the performance of the shader so your mileage may vary. Multiple lights seemed to choke the 6600 Go I was developing on, but were handled much better by a 7800 I had available to test on. The article was more about piecing together the lighting equations in a general purpose way, and to understand what they do, so I didn't take speed into account. For example the ShaderGen program will generate code specifically for the lights that are enabled and determine their type so there are no conditional statements. If you already know what type of lights will be used in your scene just move the calculateLighting function to the fragment code where main resides and do it in a similar fashion as ShaderGen. And with regard to the if statements in the functions technically the if statements should result in the GPU having fewer operations to do. The operations within the if statements can be moved outside of them and you'll get the same results. So you may want to make those optimizations for an older card. Quote: You're correct this is a typo that I didn't catch in the article. The code for the library is correct though and when w == 0 the directional light is used in PhongLighting.frag. I'll see if I can't get that fixed. Any more feedback is greatly appreciated. [Edited by - donny dont on October 31, 2007 4:45:38 PM] |
||||
|
||||
![]() pseudorandom Member since: 5/13/2008 From: Milano, Italy |
||||
|
|
||||
| Awesome article. One question: what could be the best way to unroll the loops found in calculateLighting() in order to make the library work with glsl 1.2? |
||||
|
||||
![]() donny dont Member since: 3/29/2007 |
||||
|
|
||||
Sorry for the late reply its been awhile since this article was published.void calculateLighting(in vec3 N, in vec3 V, in float shininess, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular) { // Check light 0 if (gl_LightSource[0].position.w == 0.0) directionalLight(0, N, shininess, ambient, diffuse, specular); else if (gl_LightSource[0].spotCutoff == 180.0) pointLight(0, N, V, shininess, ambient, diffuse, specular); else spotLight(0, N, V, shininess, ambient, diffuse, specular); // Copy the above and switch out 0 for the index for each light .... } Rather than using the loop you can just replicate the code over again. Since its a little repetitive it might be better to just create a macro. I haven't played around with macros in GLSL but assuming they behave the same as C/C++ the following should work. #define CALCULATE_INDIVIDUAL_LIGHT(i) if (gl_LightSource[i].position.w == 0.0) directionalLight(i, N, shininess, ambient, diffuse, specular); else if (gl_LightSource[i].spotCutoff == 180.0) pointLight(i, N, V, shininess, ambient, diffuse, specular); else spotLight(i, N, V, shininess, ambient, diffuse, specular); NOTE: I didn't include the \ to continue the macro since it made everything display on one line so don't forget to add them back. Hope this helps. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|