Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / A normal mapping shader for many positional lights

Author
Message
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Feb 2019 00:50 Edited at: 1st Feb 2019 10:20
Hi everyone.

I've finally got around to setting up an AppGameKit shader demo which I hope you'll find interesting and useful.

The attached demo shows how to use a normal mapping shader for many lights on 3D objects. The shader provides both diffuse and specular lighting.

I have no idea what the limit is on number of lights. On my machine I've tested it with up to 144 lights which should be enough for most purposes (see attached screenshot) - and possibly far too many for most. If you have trouble running the code try reducing the values of maxLights, numLights and also change the hard-coded reference to 144 to match numLights.


[Edit: Forgot to add that the demo consists of two scenes: scene 0 using 6 lights with a few turned off, and scene 1 using the full 144 lights specified in the shader (as in the screenshot) .]
Green Gandalf of the TGC forums aka Scorpius of the Malevolence:Sword of Ahkranox forums.
Shader developer for Evochron Legacy produced by StarWraith 3D Games.

Attachments

Login to view attachments
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 1st Feb 2019 01:30
That's great GG! Dance party!
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Golelorn
8
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 1st Feb 2019 04:55
Nice. Thank you for posting.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 1st Feb 2019 05:35
Wow!!! That's fantastic!
chafari
Valued Member
18
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 1st Feb 2019 09:03
Nice work GG as always.
I'm not a grumpy grandpa
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Feb 2019 10:14
Thanks Chafari. How many lights were you able to use? Looking forward to meeting up in a week or so .

Thanks for the feedback everyone.

There is one issue that bothers me though. If you study the specular part of the lighting you might notice a rather abrupt cut-off at certain angles . Can anyone cast some light on this ? Is it a bug in my code - or a feature of specular lighting?

For those of us used to writing shader demos in DBPro I am very impressed with AGK's capabilities. I think it may have been several years before specular lighting was fully supported in DBPro - at least we can get it easily in a custom shader with AppGameKit although not natively yet (the built-in normal mapper works fine but is limited to 8 lights and doesn't include specular lighting ). It would also be useful to get some feedback on which devices support this shader and for how many lights?
Green Gandalf of the TGC forums aka Scorpius of the Malevolence:Sword of Ahkranox forums.
Shader developer for Evochron Legacy produced by StarWraith 3D Games.
chafari
Valued Member
18
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 1st Feb 2019 11:48
@GG whe I post about two hours, I was out and see your image and I was anxious to get home to test this . When I try to compile, I had an error ...getwindowwidth is not a recognized function or command , so I just changed with my real screen width /2 then I got a different error ...error c7528 penGL reserves names starting with ' gl_' ...I tried renaming those words adding a letter but didn't ge to work. Any idea ?
I'm not a grumpy grandpa
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 1st Feb 2019 12:16
Hey GG,
Very nice! I haven't seen much of you and your shaders lately.

What's this:
Quote: "Looking forward to meeting up in a week or so"

Is there another AppGameKit convention that I've overlooked?
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Feb 2019 17:50
Hi Scraggle!

Long time no speak . You’ re correct, I have been quiet recently but you can rest assured that now I have some shaders working nicely with AppGameKit that you’ll see more from me soon . By “soon” I mean once I get back from sunny Fuerteventura in March . Which brings me back to your other question: since we are staying in Chafari’s neck of the woods, we’re hoping to see him while we are there. It was an opportunity that was too good to miss .

Hi Chafari!

Sorry to hear you’re having problems getting my code to work . I have not seen either of those two problems: the first sounds like a version issue - do you have the latest version of the AppGameKit? Your second error is weird since that particular shader variable has to have that name - which makes me wonder whether your machine can run this particular shader. Are you able to run other shader demos - or even the built-in normal mapping system? Another possibility is that the messages are both caused by the same issue, such as the max number of lights? Will it run if you reduce the number to something like 8? You might need to change the maxLights value in both the pixel shader and AppGameKit code.
Green Gandalf of the TGC forums aka Scorpius of the Malevolence:Sword of Ahkranox forums.
Shader developer for Evochron Legacy produced by StarWraith 3D Games.
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 1st Feb 2019 18:43 Edited at: 1st Feb 2019 18:46
Quote: "Can anyone cast some light on this"

I got the pun

Had a quick look.
You asked for specular hightlights so if you replace your calculations with the lines below they look okay, but ...well the rest ...seems do be a bit off



Here you calculate the light position from the lights point of view, you want it to be from the fragments point of view so switch the variables .
Nitpicking but it makes things much easier for later, for example you don't need to negate the normalized lightPosition then, also I like you variable syntax

If you want to match your attenuation to AGK's... this is how it's calculated


And please turn down your specular values


Still something wrong but not much... I guess I leave the rest for you

Quote: "When I try to compile, I had an error ...getwindowwidth is not a recognized function or command"

Got the same today make sure you run the right version, this command was added lately.
For me it was just that I still had the .agk files linked to an older version of AGK.
chafari
Valued Member
18
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 1st Feb 2019 20:08
Quote: " the first sounds like a version issue - do you have the latest version of the AppGameKit?"


I have the 2018.11.08 version . I have not updated yet as I don't like too much new updates until it has really good things to test . I have still the old version of Windows Vista , the last my old desktop can support .

Quote: "make sure you run the right version, this command was added lately."


Definitely the error is something of my old version
I'm not a grumpy grandpa
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Feb 2019 21:07
janbo

Thanks for your comments . I take your point about my choice of name for the lightDirection vector - it is not ideal . I am not convinced that your name improves anything though and I don’t see a problem with the necessary sign change either. Anyway, I’ll think about it and see if I can find something better. The change would be cosmetic at best though. Unfortunately I am away from my machines till March so I can’t test or change anything till then . I agree with your other points though and had intended to square the denominator in the attenuation calculation so thanks for spotting that .
Green Gandalf of the TGC forums aka Scorpius of the Malevolence:Sword of Ahkranox forums.
Shader developer for Evochron Legacy produced by StarWraith 3D Games.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Feb 2019 21:57 Edited at: 2nd Feb 2019 00:04
Janbo

I have just had a closer look at your first snippet. You seem to be using the old “half vector “ approximation to the true specular reflection. I stopped using that as soon as I could. There is no need for that on good GPUs these days. However I realise the situation is probably different on mobile devices where such approximations may still be useful. But how many instructions do you save doing it that way?

That reminds me: I guess your replacement of clamp by max probably saves an instruction or two too, so thanks for suggesting that. I will look into all these suggestions on my return and will amend the download then .

Edit: here is a link to an excellent account of how specular highlights should be calculated

http://math.hws.edu/graphicsbook/c4/s1.html

See in particular the subsection on The OpenGL Lighting Equation. Unfortunately that reference doesn’t seem to mention the half vector approach which is sometimes referred to as “improved Phong lighting” or possibly Blinn-Phong Lighting. As far as I can tell the use of the word “improved” refers simply to the fact that it does not require the dot product to be calculated.

Actually that last statement is not correct . I have just found the following link which supports your suggestion so that is definitely something for me to ponder


https://paroj.github.io/gltut/Illumination/Tut11%20BlinnPhong%20Model.html

Edit2: I guess the moral of this is that it is never too late to learn . So a big thank you to janbo for sending me back to school
Green Gandalf of the TGC forums aka Scorpius of the Malevolence:Sword of Ahkranox forums.
Shader developer for Evochron Legacy produced by StarWraith 3D Games.
janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 2nd Feb 2019 22:46 Edited at: 2nd Feb 2019 22:48
The reflect command is (I - 2.0 * dot(N, I) * N) internally, so you need two dot calculations anyway, but you are right the reflect command is probably optimized while compiling I guess.

Quote: "The change would be cosmetic at best though"

Sure its cosmetic as it saves infinitesimal instructions, but if I have some patience left, I tend to spend it on "code cosmetics", which does save some time for the later me

Quote: "I guess the moral of this is that it is never too late to learn"

I learned from your posts back in the DBpro days so we are even ...There is still plenty to learn in the graphics field for me too.
The more I dive in, the bigger it seems to be
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Feb 2019 23:55
Quote: "I learned from your posts back in the DBpro days so we are even ...There is still plenty to learn in the graphics field for me too.
The more I dive in, the bigger it seems to be "


It certainly seems to be to me too .
Green Gandalf of the TGC forums aka Scorpius of the Malevolence:Sword of Ahkranox forums.
Shader developer for Evochron Legacy produced by StarWraith 3D Games.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Mar 2019 21:42 Edited at: 8th Mar 2019 21:44
I'm now back in the UK and have done some further tests on my laptop. Those tests confirm Chafari's second problem, i.e. "OpenGL reserves names starting with ' gl_'" etc. However, I only get that error when I use the Intel HD Graphics 3000 Integrated Graphics card instead of the High performance nVidia GeForce 630M card. The latter runs the demo without error - although rather slowly as you might expect .

According to the GFX card specs the nVidia card supports OpenGL4.0 whereas the Intel card supports only OpenGL2.1 so I guess the problem is related to that in some way (although the AppGameKit help files day that support for OpenGL2.0 should be sufficient). Perhaps that is out of date now and AppGameKit now requires a higher level of support for OpenGL? Does anyone know for certain? [I thought it now required OpenGL3.0 but I cannot find the relevant documentation now. ]
Green Gandalf of the TGC forums aka Scorpius of the Malevolence:Sword of Ahkranox forums.
Shader developer for Evochron Legacy produced by StarWraith 3D Games.
chafari
Valued Member
18
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 9th Mar 2019 13:59
Yeah it is a shame...so ...not sure but probably I'll upgrade when I come back from my bike trip.
I'm not a grumpy grandpa

Login to post a reply

Server time is: 2024-11-23 03:10:45
Your offset time is: 2024-11-23 03:10:45