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.

DarkBASIC Professional Discussion / The Ultimate Shader Thread 2.0

Author
Message
Vector ScOpE
18
Years of Service
User Offline
Joined: 4th Nov 2005
Location: middle world UK
Posted: 18th Jan 2006 14:11
@Catalyst i did try his shader but it is not the same when you turn the object it looks flat along the surface, no longer bumped


Nothing is foolproof to a sufficiently talented fool
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 18th Jan 2006 17:27
That's what normal map shaders do, they're not physically offsetting anything. But they look good from most angles.
FoxBlitzz
20
Years of Service
User Offline
Joined: 19th Nov 2003
Location: United States
Posted: 18th Jan 2006 21:21 Edited at: 18th Jan 2006 21:25
Bump-mapping changes the normal map of the object, which is the image that tells the computer how to render the light. That is why it looks bad at narrow angles. There are more realistic methods: parallax mapping offsets the texture co-ordinates as well, and relief actually manipulates polygons to make the details "pop out". Go to page 5 for a better explanation of this. Also, ATI's Toyshop tech demo shows parallax mapping as well as many other kinds of texture mapping techniques. You might want to check out its video.

And now for a request:


http://www.ati.com/developer/samples/dx9/ShadowMap.html

That's a shadow shader that I want to be able to use in DBPro. It's darker, softer, faster, more accurate, and generally better than the one that comes built-in. Is it possible to tweak it, export it, or somehow code a similar shader to this that can run in DBPro?

AMD Athlon 64 X2 4200+
1GB DDR-SDRAM (May increase to 2GB one day)
512MB ATI Radeon X1800 Series - Finally! A card that does pixel shaders correctly!
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 19th Jan 2006 02:26 Edited at: 19th Jan 2006 02:29
Well I think I figured out how blend two layers together. It seems (although I havent had time to test) I simply need to use the alpha channel of the first layer as a 'window' that determines how much of the second layer is visible (could it be any more simple?). Ill let you know how it goes

On a side note, I got a normal map shader working (written in HLSL). I followed the article here...

http://dotnet.org.za/pieterg/archive/2005/07/29/40407.aspx

Note that I swiped some of Ninja Matts textures to produce the effect. Below is a screen (is there no way to DISPLAY a uploaded image in the thread body?) and the project itself for you to play around with.

The whole point of writting this shader was to convert Ninjas shader to HLSL. Now I want to add a parallax effect, can anyone assist? Heres the HLSL...



All you need is zeal

Attachments

Login to view attachments
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 19th Jan 2006 02:27
Heres the project itself

All you need is zeal

Attachments

Login to view attachments
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 19th Jan 2006 04:53
Well I may have found a bug (that or there is something wrong with my normal map shader). The effect seems to work fine in the above download (try it and see), however I tryed to apply the effect to my custom terrain chunks (created via memblocks) and the thing freaked out. The texture did not display at all, the lighting was very off, ect...

Ive narrowed the problem down to the vertex Input TANGENT value. While it works fine with most other objects (cubes, spheres, ect) it produces strange results with a custom made quad (a simple two poly mesh created via memblocks). I included the code I use to make the memblock mesh so you can look at it, but I dont think there is anything wrong with it.

Heres the dbpro source (commented where the problem is)...



And the HLSL source (also commented)...



I also put the entire (now buggy) project back together so you can download it and play with it.

Help!

All you need is zeal

Attachments

Login to view attachments
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 20th Jan 2006 05:23
So I'm getting to the point that I can at least see what a shader is doing a lot of the time and make some very very very very basic adjustments. The thing that is the most confusing to me right now is defining things and setting up passes. For instance, here is a cel shader taken from a book I'm studying...which I think they took it from a DirectX SDK.



That's it. The whole thing. Where are any of those constants defined? What about any texture handling, shouldn't it be telling it whether to wrap the UV coordinates or not? The rendermonkey shader I'm trying to figure out does the same thing, nothing is defined in the output shader. In Rendermonkey I can open up the vertex shader and see that it knows what to fill in those constants, but in that output I don't see those being defined. Am I missing something?
Pulsar Coder
20
Years of Service
User Offline
Joined: 3rd Jan 2004
Location:
Posted: 20th Jan 2006 14:52 Edited at: 20th Jan 2006 14:54
Well, that is a compiled shadder (Assembly-type). What you are thinking of is the script-like shadders (.fx) in HLSL like the ones Zealous has been posting ...

EDIT: do not mess with a shadder in assembly unless you want to optimize it -meaning, to reduce the number of commands used to get to a result.
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 20th Jan 2006 16:44
How do you mean? Assembly still has to define things to be used, and you can have assembly .FX files. Actually, I've been messing around with one of the shaders in assembly to make it load, and have successfully loaded the CloudsEffect_ASM effect from RenderMonkey into DBPro. Need to be leaving right now, but when I get home from work I'll post a working example for anyone interested, it's a really cool sky effect.
Pulsar Coder
20
Years of Service
User Offline
Joined: 3rd Jan 2004
Location:
Posted: 20th Jan 2006 17:16
Correct me if I wrong, but some constants and varying registers are already defined in the GPU.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 21st Jan 2006 00:42
Did anyone get a chance to test the last project I posted with the 'buggy' quad? Why wont the shader play with my quad !!?

All you need is zeal
Olby
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location:
Posted: 21st Jan 2006 01:03
Anyone looking at the soft shadow shader that was posted by FoxBlitzz?

"Gangsters die, they don't go to heaven where angels fly!"
AMD Sempron 3.1+ Ghz, 512MB Ram, ATI R9550 256MB Ram, Sound Blaster Live!, WinXP SP2, DirectX 9.0c
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 23rd Jan 2006 17:10
@Pulsar Coder
I think the input streams are technically standardized and on some versions don't need to be declared, but some versions require you to specifically declare them. The ones I'm referring to are not automatically set, they are things that need to be set up. After looking around a bit, I saw other shaders from RenderMonkey were setting them up, just not the one I wanted.

Anyway, here it is, the sky shader and a DBPro project to run it.

Attachments

Login to view attachments
Ninja Matt
19
Years of Service
User Offline
Joined: 5th Jun 2004
Location: Lincolnshire, UK
Posted: 23rd Jan 2006 20:23 Edited at: 23rd Jan 2006 20:24
Sorry I went quiet again - I've just built a new PC and haven't had the time to browse any forums!

@FoxBlitzz:
Not a lot of chance of seeing that shader in DB, I'm afraid. It uses a few techniques quite close to the heart of DirectX, which DB simply won't let us touch! Someone (Evolved, I think) came very close to getting a similar effect earlier in the thread, but found it too slow to be any good.

@Zealous:
Did you get your alpha system working properly in the end? You're quite right that you use the alpha channel to weight the two inputs, and the exact formula goes something like this:

Output.rgb = (Lower.rgb * 1-Upper.a) + (Upper.rgb * Upper.a)

Where upper is the texture to be drawn over the top of lower.

In assembly, at least, the code should look something like this, although it can probably be optimised in some (most?) situations:



Tangent problems? Hmm, I'm not sure, but it could be that your mesh doesn't have a vertex tangent vector nor, in that case, a binormal vector. Usually, I think they need to be defined in the FVF declaration and stored in the vertex data, since I'm pretty sure they won't be calculated on the fly. I'd guess DB calculates and stores them itself upon loading an object, which is why it behaves itself most of the time.

@Catalyst:
In pure assembly shader like that, the constants are normally defined in the C++ code and associated with it upon compilation. I can't quite remember the syntax, but if you've got the source code, look for something like this:

D3D9Output -> SetVertexStreamConstant(0, D3D_VIEW_TRANSFORM_MATRIX);

You'll see a lot of them together usually, which can give them away! Once you've found them, it's just a case of decoding which input maps to which constant.

@Everyone:
Be wary of some of my old shaders! The normal mapping shader I know for certain is buggy, and some of the other probably won't always work as you'd expect. Still, I posted them mainly as a learning and reference tools, so I'm sure they can remain useful.

Once I've got settled down with my new hardware and get round to installing FXComposer again, I'm be sure to post some new material!
Olby
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location:
Posted: 23rd Jan 2006 21:40 Edited at: 24th Jan 2006 18:26
@ Catalyst:
Your sky shader is awesome and it even does not eat very much of my fps.

Anyway can I use it in my projects? Course I will give you a credit, you deserve it!
Drop me a mail with your full name so I can write it in credits screen of my upcoming game.

"Gangsters die, they don't go to heaven where angels fly!"
AMD Sempron 3.1+ Ghz, 512MB Ram, ATI R9550 256MB Ram, Sound Blaster Live!, WinXP SP2, DirectX 9.0c
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 24th Jan 2006 04:09 Edited at: 24th Jan 2006 04:23
@Olby

If only I deserved credit for that...not that clever yet. That was a RenderMonkey shader, I was just trying to figure out how to make it work in DBPro. I'm really new to shaders, basically just started studying them a week or so ago, I was using this as a learning experiment just trying to modify shaders to make them work. It originally was just a blank screen. As for using Rendermonkey shaders, I don't know what the legal usage of that would be. I think they're supposed to be free to use, and that version there has been modified from the original. I've been looking at HLSL, looks pretty easy so maybe I'll redo this in HLSL and that would be considered mine enough to use freely.

@Ninja Matt

Interesting to note...I was seeing them be set up in the C++ code in a book I was reading, thought it was pretty strange to do that when it could just be defined in the shader itself. Are there any advantages/differences at all to doing it that way? I'd prefer to just set it up so everything is contained within the shader as much as possible, makes it more portable. It was also strange, since it looked like most of the RenderMonkey shaders were being set up in the shader itself, this one was the only exception out of the ones I was looking at.


EDIT: Just occured to me I forgot to ask the questions I was going to ask. First off, the sky shader. I had set the textures to wrap and all was well and good, but on a buddy of mine's system it seemed like they were set to linear. They'd leave the scene and be done, so the effect would last a minute or so, then be just the background. Any ideas? His card is a GeForce 5950 if I remember correctly, other than that I've tested it on a Radeon 9800 Pro and a Quadro FX 1400, worked fine on both of those.

The other question I have is what are some possible reasons that a particular card could just completely suck at doing a certain shader? I'm using the cel shader written by PrestonC, here's the shader:


Nice shader that seems to be overlooked by people wanting a good cel shader. This one here I added variable "S" to it so you can easily control the stroke thickness from within your code. Now, on my game I was getting about 140 FPS. I used this shader on my two characters and got 145 FPS. Nice I thought, good when it runs faster and looks better. Then I give it to my buddy to test out (Same guy who's having the issue with the sky shader, also had other issues with DirectX stuff...I really don't trust his video card). Anyway, on his computer before he was getting about 150 FPS I think...use this shader, he gets 90. Any ideas for things to check? He uses the regular nVIDIA drivers, not doing any sort of Tom Foolery with mods or anything. Thoughts?
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 24th Jan 2006 04:27 Edited at: 24th Jan 2006 09:11
@ninja

Yes my alpha works perfect now.

For the FVF format, im using the default 338, how can I change that to include a binormal and tangent? You would think DBPro would auto generate this data, since its a part of all other objects.

Anyone have FVF info that includes binormal and tangent data?

*edit hmm.. since im going to be applying this to my terrain shader (which updates at runtime), if I use a new fvf format, it will slow my update routine down a bit (depending on how hard it is to calculate these things)... Is there anyway to calculate this stuff INSIDE the shader simply given the position and normal? or would that be too slow?

All you need is zeal
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 24th Jan 2006 23:45
Okay, that cel shader I had just posted above...is there any reason for it to run really well on ATI, but really bad on nVIDIA? I just tested the cel shader and the sky shader on a Quadro FX 4500...the sky shader runs at over 800 FPS as compared to about 240 or so that I get on a Radeon 9800 Pro. Then I run my game that uses the cel shader and I get 127 FPS on the Quadro, as compared to 145 FPS on the Radeon. I doubt that it's a processor issue, since the Radeon is on an Athlon XP 2600 and the Quadro is on Dual processor dual core Opteron 280. It's obviously running the other shader way better, just not the cel shader. Maybe it got offended that I wanted it to do non-photorealistic rendering?
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 26th Jan 2006 11:52
Well this is just great, im looking at the new 5.8 vertex manipulation commands, and theres no way to set a binormal or tangent. So even if I could create a mesh with the proper FVF format, theres no way to set the damn values!

Can anyone think of a workaround? There has to be a way to create and manipulate a custom memblock mesh, AND have it still work with a normal map shader... Could you perhaps perform the tangent calculations in the shader itself? I know that given the tangent and normal you can calculate the binormal (or is it the other way around).

Help!

All you need is zeal
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 27th Jan 2006 08:49
In my continued attempt to make sense of why the cel shader runs very badly on any nVIDIA card, I started digging through and removing a lot of things. For instance, there's specularity and ambient light being calculated...which I don't find to be particularly useful in a cel shader. Also there's specularity being calculated on the stroke...again, makes no sense as to why. So I removed all that stuff, cutting out a lot of the operations in the shader. Now, does this make it faster on any hardware? No. Why not? I would think if you have something, then make it have less to do it should go faster, ja? Another thing I tried doing was removing ambient and specular lighting all together. As in, remove the lines that tell it to expect a specular value to be returned, remove lines that would return that value, remove lines that would lead up to anything related to specular lighting, etc. Now does it run faster? No. In fact, it runs slower! Any reasoning on that?
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 27th Jan 2006 09:58 Edited at: 27th Jan 2006 10:00
@ Catalyst the performance difference youre reporting with the sky shader seems to indicate theres something wrong. However the cell shader performance difference doesnt seem too huge. Arent these things supposed to run at slightly different speeds on different cards? Even when they have comparable 'specs'?

PS - How come everyone seems to be using ASM these days? Is there something wrong with hlsl? I found it pretty easy to pickup

All you need is zeal
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 28th Jan 2006 01:28
@Zealous
A card with a $3,000 list price on a far better machine performing better than a card from 3 years ago seems strange, but that same card performing worse than the outdated one sounds about right? Hhhmm.......

As for ASM, I think ASM is easier to troubleshoot than HLSL. Really I just started getting into shaders this month so I'm not too good at either. But yes, HLSL looks pretty easy...though I like seeing the registers each step.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 28th Jan 2006 04:17
@Catalyst yes the 18 fps difference doesnt seem like that big of a deal, however the 560 fps difference seems a tad stranger. But regardless, what explanation could there really be? Assuming youre using the same code on both, different hardware just works differently. I imagine you could rewrite parts of the code to get one or the other working better, what parts those would be I have no idea though.

All you need is zeal
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 28th Jan 2006 05:56
The card that ran the sky shader at over 800 FPS did so because it is a very good and recent video card...if I'm not mistaken, it's the highest level of Quadro cards availble. List price of $3,000, so I'd expect it to run things well. Like the cel shader. It should run that one a lot better. But no, it doesn't. It actually runs it at the same FPS as a Quadro FX 1400 and a GeForce FX 5950. All nVIDIA cards run it around the same FPS. They are all very different cards, only similar in being nVIDIA. The ATI card, which is the oldest of the bunch, runs it faster. I wish I had access to one of the more recent ATI cards to try it on, but I don't. If these were similar cards, I'd not really think anything of it. But they're very different. I'd expect the Radeon 9800 and the GeForce FX 5950 to be pretty similar, but they weren't. The GeForce 5950 got 90 FPS initially, after some modification to the shader I brought that up to 135...with no change at all on the Radeon.

I'm still just confused as to why any slight change has dramatic difference on any of the nVIDIA cards, but the ATI card runs it pretty much the same each time....in any modification the lowest it's gone is 135 and the highest is 147. The nVIDIA cards have gone from 90 to 154....given the 154 FPS was on a $30,000 machine, I'm not impressed.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 28th Jan 2006 07:16
I guess the moral of the story is ATI FTW! Although I dont really like ATI since the fan on my radeon 9800 freaked out (I guess its not the cards fault, but still I mean come on, I never overclocked the thing). For awhile I was able to "jump start" it by flicking it with my finger on boot, but eventually it just gave out. Now I have a case fan duct taped underneath it (ghetto style).

Ehe but seriously I have no idea about your problem. Maybe you can help me though. Do you know of anyway to implement normal mapping (and eventually parallax mapping) without a vertex tangent or binormal? Unless of course you know of a way to set the tangent and binormal inside DBPro (no vertex manipulation commands that I can see will allow you to do it).

All you need is zeal
Olby
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location:
Posted: 28th Jan 2006 10:49
Recently I used FX5700 and the card burnt because the fan on it broke.
Then my PC store changed it to ATI R9550. My version is even without a cooler on it but it works 2x faster than the GeForce with the same amount of RAM and from the same price category. And ATI has a system that makes image on your screen waaaay more sharpen than on GeForces. I'm very satisfied with ATI now, although few months ago I said like "WTF! I will never but that crap from ATI", but now I take my words back. It's like AMD & Pentium - something better, something worser ya' never know what cathces you!

"Gangsters die, they don't go to heaven where angels fly!"
AMD Sempron 3.1+ Ghz, 512MB Ram, ATI R9550 256MB Ram, Sound Blaster Live!, WinXP SP2, DirectX 9.0c
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 28th Jan 2006 11:37
@Zealous
You know, I don't think I can help you much there...I'm new to shaders and really only at the point of being able to modify some shaders that completely did not work in DBPro so that they can work. I also never took any math when I was in college (damn art schools!), so any math concepts are really just my guessing of how they work.

One thing I wonder...though I doubt it would be accurate...is if you make a matrix that will do a 90 degree Y rotation, apply that to your normal to make a new vector for the tangent, then cross product that vector and the normal to make the binormal. That could easily be done in the shader.

The other thing that comes to mind is that isn't this just going to give you a normalized vertex to light vector? Couldn't you just subtract the normal and light vectors, then normalize?

Sorry if I'm completely missing some concepts here, I just woke up and also had no idea what either vertex tangents or binormals were 5 minutes ago.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 28th Jan 2006 12:59
@Catalyst yeah im thinking something like that too. I know the binormal can be calculated given the normal and tangent, so the question is how do we calculate the tangent INSIDE the shader? Some scary matrices are in order I fear...

All you need is zeal
Darkbasic MADPSP
18
Years of Service
User Offline
Joined: 15th Jun 2005
Location: Uk
Posted: 28th Jan 2006 13:00
There's to many long posts here to read it all but where is the ati shader designer located at?

where i went on holiday to
www.portaventura.es and also http://themepark.nl/ubbthreads/showflat.php?Cat=0&Board=UBB7&Number=661483&page=0&fpart=all
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 28th Jan 2006 20:29
@Zealous
I'm going to throw some random things into the old version of Ninja Matt's normal map shader, the part where the light is put into texture space is really the same as it is on any normal map shader I've seen. I'll see if I can come up with something, but again, don't be too hopeful. I'm suprised that you were thinking something along those same lines, guess I might actually be getting a little understanding of these things!

@DarkBasic MAD
http://www.ati.com/developer/rendermonkey/index.html
Don't expect that to make this whole thing suddenly be easy though, but it does have some nice effects already in it. Some of those can be modified to work in DBPro, like the atmospheric sky shader I posted above.
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 28th Jan 2006 21:45
@Zealous
Okay, I think I got it! This is in ASM, so you'll have to convert it to HLSL, but it shouldn't be too hard.

First, define a Y rotate matrix





Then, we make a tangent, and from that, a binormal. At this point we have our rotation matrix stored in c15, and our vertex normal is v1:







We now have a tangent stored in r2 and a binormal stored in r4. So to move the light into tangent space, we replace the code





With the new code



I did this on Ninja Matt's NormalMap10.fx and used the same sphere/texture/DBPro code he provided with it. Didn't notice any difference between the two ways of doing it in the end. Hope this works for yours!

I've included that original zip file Ninja Matt had with the texture, shader, and code. There's two versions of the normal map shader in there. one have old in the name and one has new in the name. Just take old or new out of the name and run the code to see them in action or compare what has been changed.

Attachments

Login to view attachments
Darkbasic MADPSP
18
Years of Service
User Offline
Joined: 15th Jun 2005
Location: Uk
Posted: 28th Jan 2006 21:50
@Catalyst
Thanks

where i went on holiday to
www.portaventura.es and also http://themepark.nl/ubbthreads/showflat.php?Cat=0&Board=UBB7&Number=661483&page=0&fpart=all
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 29th Jan 2006 00:12
@Catalyst Thanks bud! Will look at this later tonight. Gotta learn ASM sooner or later, here goes... gulp

All you need is zeal
Darkbasic MADPSP
18
Years of Service
User Offline
Joined: 15th Jun 2005
Location: Uk
Posted: 29th Jan 2006 13:26
*starts to learn how to make shaders*

This is gonna be hard but fun

where i went on holiday to
www.portaventura.es and also http://themepark.nl/ubbthreads/showflat.php?Cat=0&Board=UBB7&Number=661483&page=0&fpart=all
Chris Franklin
18
Years of Service
User Offline
Joined: 2nd Aug 2005
Location: UK
Posted: 29th Jan 2006 21:21
Nice shader effects I just made a thread with some links to tutorials for making shaders

Fps world of mayhem 10%
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 29th Jan 2006 23:54
ASM 1, Zealous 0.

I just dont see how a "y" rotation matrix produces a tangent (not that I have a really solid foundation on what a tangent IS anyway). Combine that with the fact this is in ASM = brain no likey.

If I understand what youre doing, youre just 'generating' a tangent via a matrix. If I could just see how THATS done in HLSL, I could do the rest (calc the binormal, do the actuall normal mapping, ect...).

All you need is zeal
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 30th Jan 2006 03:13
Are you familiar at all with matrix maths? It was a little strange for me having taken nothing higher than a mid-level math class, but once I saw how matrices work it all made sense.

Let's start with a tangent and binormal, it's far easier than it seems. Think of the XYZ axes, where you just have a line going to Y, line to X, and a line to Z. Let's say that Y axis is the normal, i.e. just a vector facing directly up. If that was the case, then the tangent would be X, a vector facing perpendicular to Y. Then the binormal would be Z, a vector perpendicular to both X and Y. So you basically just created a set of axes with the origin being the vertex.

Make sense? Now, with matrix math, what it's doing is systematically multiplying all the numbers in a vector by all the numbers in the matrix. You can dig around for some examples of how exactly they're multiplied...which would show you how I came up with those numbers to make a 90 degree rotation matrix.

Now, I don't know HLSL, but I think that same matrix from ASM would be written in HLSL like this:

float4x4 fMatrix = { 0.0f, 0.0f, -1.0f, 0.0f, // row 1
0.0f, 1.0f, 0.0f, 0.0f, // row 2
1.0f, 0.0f, 0.0f, 0.0f, // row 3
0.0f, 0.0f, 0.0f, 1.0f // row 4
};

That should be declaring it properly...I think. So basically what that matrix will do is adjust the values in a vector to be rotated 90 degrees. So a value pointing up (the normal) will be rotate around the Y axis to become the tangent. Why is it the Y axis instead of the Z axis? Well, this is texture space, so X and Y are what lay flat, so in this particular case Z would be pointing outwards.

This is all works if I understand what I'm doing correctly. Since most of this was determined after reading some books while waiting at airports one week, I could very well be wrong. But, using the normal map by Ninja Matt, this code and concept worked.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 30th Jan 2006 06:47 Edited at: 31st Jan 2006 11:35
That was a very clear explanation, I think youre right. Ill let you know shortly...

Thanks!!

*Edit Wohoo it works! Well kinda. The custom memblock mesh 'works', however something is wrong with the normal lighting. When the camera moves around the object, the light on the bumps does not update (it looks like the thing is lit at a constant angle). HOWEVER the thing does get brighter/darker based on camera distance, so lighting isnt totally broken... Heres the modified source, can you see anything wrong?



...im guessing im not multiplying the normal correctly? or is my matrix wrong

*Edit -

To clarify, should I be multiplying the Y matrix (which im pretty sure I copied exactly) DIRECTLY with the Normal?

float3 tangent = normalize( mul(YMatrix, IN.normal) );

or is that wrong?

Then the binormal should be a simple

float3 binormal = normalize( cross(tangent, IN.normal) );

assuming the tangent is correct, right?

All you need is zeal
Chris Franklin
18
Years of Service
User Offline
Joined: 2nd Aug 2005
Location: UK
Posted: 30th Jan 2006 17:47 Edited at: 30th Jan 2006 17:49
Edit:

sorry posted in the wrong thread

Fps world of mayhem 10%
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 1st Feb 2006 08:38 Edited at: 1st Feb 2006 08:39
Blast it all! No matter how much I play with the matrix I cant get the lighting to look right. I have to be generating a 'bad' tangent. I recompiled the whole project so you can see exactly what its doing.

Also (since im sure the error is in the HLSL), heres the shader code again...



Its so close to working its driven me nuts! Thanks in advance for any help!

All you need is zeal

Attachments

Login to view attachments
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 1st Feb 2006 09:25
I tried moving the light vector around and it looks like it's being used, but the effect is just fairly small. It's definately calculating things and getting a bump, I multiplied the diffuse by 3 in the last step and got a more noticable effect. But I'm not too sure what's going on with the light...as in, why you think there's a problem. I moved it off to the side and it looked like it was more off to the side. It does seem to be a little...omnipotent. But I don't quite know if that's just something with the setup. I'll look into it a bit more later on, since it's HLSL it will take me a bit of time to figure out.

I'm not too good with HLSL input and syntax, but for this test you are using a regular cube so you should be able to just use the tangent and binormal provided by that geometry. Can you make that change to the shader and post that? I'd like to see it working using the real tangent and binormal, so we can make sure we know what it is supposed to look like.

At least it shows up and has bumps now! I see what you mean by so close to working. Probably just some random value that needs to be negated or something.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 1st Feb 2006 09:29 Edited at: 1st Feb 2006 09:48
Notice how the bumps change from bumps, to dents, depending on the angle. Move to all the corners of the box and look inward, and youll see what I mean. The corner closest to where the camera starts LOOKS like everything is normal (oh how excited I was when I first saw it), BUT if you move to the OPPOSITE corner, all the bumps change to dents...

But you think the tangent is ok? Im just doing a straight mul(normal, Ymatrix). Thats all it should take right?

Something has to be wrong...

*Edit here is the original normal map HLSL (which passes a tangent and binormal direct from darkbasic). It wont work with the custom memblock mesh, but looks GREAT on a cube primitive. Compare the two and youll see what im talking about.

All you need is zeal

Attachments

Login to view attachments
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 1st Feb 2006 18:51
I'll check this out when I get home from work, but one thing I'm thinking. My test with generating the tangent and binormal this way was done using the original code provided with the normal map shader that made a static sphere and moved a light vector around it. I'm wondering if maybe generating the tangent this way makes it relative to the camera, so it works when the camera is in one place but not when it's moving around. Since the normal is still being transformed when it's in object space I wouldn't think that would make a difference, but I have no way of viewing any of there while I'm at work.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 2nd Feb 2006 01:10 Edited at: 2nd Feb 2006 01:11
That might make sense, as my HLSL version DOES look right from the default camera position. So perhaps some kind of transformation is in order?

The only parts of the HLSL you need to worry about are at the top, with the matrix declaration...




OR the part where the tangent is generated...



The binormal (calculated via "float3 binormal = normalize( cross(tangent, normal) );" ) should be accurate assuming were working with a good tangent.

Im so close to shooting my video (been playing around with windows movie maker), this is the last thing my terrain needs before its ready to be shown. I know you can figure it out Catalyst!

All you need is zeal
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 2nd Feb 2006 05:30
Okay, so I went back to just the ASM version (easier for me to read) and found a few things. I think the rotation might be for the wrong coordinate system....that rotation matrix will change depending on it being left handed or right handed. I don't have much time right now, but I played with a few numbers and managed two things.

Actually, first off, let me mention that it is definately a bit off. Even in my test it was inverted. I made some code so I could swap which normal map was in use on the fly and compared mine to the original and saw it was strange. I made a few changes and got it to match perfectly on the sides, but flipped on top. Fun. So a few changes, I have it so that the whole thing is flipped...now I just need to flip it all around. Shouldn't be too hard...but won't happen right now, as it's dinner time.

So I'll leave it at yes there is a problem, but it does look like it is very fixable and I'll try to do so a bit later on tonight.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 2nd Feb 2006 06:30 Edited at: 2nd Feb 2006 06:36
Yeah I was just about to post something to that effect. I inverted the tangent and binormal and things SEEMED to be almost perfect. However there is STILL a slight problem at the top/bottom of the cube (sounds like you noticed that too).

Check out this new project I put together (with * -1 added to the tangent and binormal). Looks almost perfect, but sometimes the top/bottom of the cube will pop in and out.

SO CLOSE!

*Edit holy crap there is a spicy mc chicken sandwich down here... thats new...

*Edit2 wow so I thought I would be nice and click the mc chicken banner (give TGC 5c or whatever they get), and the damn thing CRASHES IE. thats one spicy sandwich

All you need is zeal

Attachments

Login to view attachments
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 2nd Feb 2006 07:02
Okay, just to give you an update, Firefox is able to handle spice much better than IE. It can take that spicy sandwich and not even crash! I love how if you click through there and see the advert for the sandwich, they have to specify it's "real chicken". Look people, we know we've been doing the whole fake chicken thing for a while. Here's some real chicken, just to keep it interesting.

Now, for an update on the shader. It looks like I did have it in the wrong coordinate system, I got the sin and -sin flipped. So, I flipped them in that matrix, and it worked. In the ASM version at least...the HLSL version, still giving me trouble. I need to go back to the original versions of them to make sure I'm not trying to change something that already has other things changed, but in the ASM version it really was just switching the rotate matrix. Though that didn't completely fix it...it made them all into proper bumps, but the light vector was going the wrong way. That's easy enough to fix though, just as soon as I get the HLSL version working like the ASM one is now.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 2nd Feb 2006 07:33 Edited at: 2nd Feb 2006 07:40
Well since it sounds like we (and by we I mean you ) are on the verge of cracking this case, I thought I would post a screen of my upcoming video.

This one shows the perlin clouds shader ive been working on (although youll see it looks much nicer when animated). The water is a slightly modified version of EVOLVED's water shader. The terrain itself uses normal splatting (yes a per pixel splatted with detail and normal maps). I hope to eventually add a height map to the normal alpha channel and add parallax.

Once I get these damn tangents working ill shoot the video so you can see everything in action



All you need is zeal
Catalyst
20
Years of Service
User Offline
Joined: 6th Sep 2003
Location:
Posted: 2nd Feb 2006 08:21
That screenshot reminds me of Giants:Citizen Kabuto. If you want way too much detail in your sky, check out that sky shader from RenderMonkey that I got to work in DBPro.

Per pixel splatting....you know, I have no clue what that is right now. That's okay, last Saturday I didn't know what tangents or binormals were either.

Which brings me to my current issue. I'm not seeing it invert anymore! I don't know what happened, I thought I had it working, then I tried comparing to the older version of faked tangent and didn't see the denting! I just deleted everything and brought the originals back, but still don't see any dents. The light vector is definately wrong, but the bumps themselves are, well, bumps. I think I need to start fresh on this, so I'll call it quits for tonight and deleta any instance I have of this shader on my computer. Tomorrow I'll download it again and start over...maybe for now I just look into if there's a more specific way of calculating tangents than just rotating a normal.
Zealous
19
Years of Service
User Offline
Joined: 13th Sep 2004
Location: Colorado Springs
Posted: 2nd Feb 2006 10:39
Hmm well im beginning to fear we CANT calculate a tangent with ONLY a normal. From what ive been reading/hearing we need information about the entire triangle to calculate a true tangent. I hope you can prove that wrong.

I came across this article which explains (or trys to explain) how to calculate a tangent. If it was as simple as a single matrix multiplication, I doubt this guy would have written all this...

http://www.terathon.com/code/tangent.html

HOWEVER, if its true, and you cant calculate a tangent based soley on the normal, heres what im thinking - Since I want to apply this shader to my terrain (of which all polys are perfectly alligned with the world axis), perhaps we can use that to our advantage? I mean if you know a normal, and you know the polygon for that normal extends to the 'right' perfectly along the x axis, and 'up' perfectly along the z axis, would that give us enough 'triangle' information?

All you need is zeal

Login to post a reply

Server time is: 2024-05-08 16:35:32
Your offset time is: 2024-05-08 16:35:32