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 / Normal mapping light mapped objects?

Author
Message
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 16th Feb 2012 01:49
Hello!

I know there's a few threads out there on this but none of them have the solution I'm looking for (must be Murphy's law again...), so here it is:

I'm using a shader from GreenGandalf that I've tried to modify to take a lightmap as well. The problem is that when the shader is applied the object seems to loose it's lightmapped UV coords.

Here's the example (you'll need darklights to compile it):


I think the easiest solution would be to bake the light map into the diff texture, is there anyway that can be done with darklights?

But I'd rather do it 'properly' with a shader like this one!

Thanks,
BC

Attachments

Login to view attachments
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 16th Feb 2012 01:53 Edited at: 16th Feb 2012 01:57
Here's a screenshot of the problem and one of the lightmap texture for those of you without darklights so you can see the pattern.

[EDIT]
And I should probably say that I've changed the following bits in the effect file:


Thanks again!
BC

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Feb 2012 02:14
The shader needs to use the UVs associated with the lightmap - they are usually different from the base texture UVs.

I'll look at this on Friday unless someone else beats me to it. It's really quite an easy fix and I'm sure it's been done before on this board.
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 16th Feb 2012 13:04
Thanks, I've tried about a billion different methods, textures, shaders and tweaks and I still can't get it working!

BC
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Feb 2012 14:13
One thing to remember though - the normal mapping won't react correctly to the lightmap because there's no light direction information in the lightmap (unless you have a far more complicated beast such as a directional lightmap). You'll only get the normal mapping effect from the lights used in the shader.
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 16th Feb 2012 15:14
Yes, I was using Evolved's light+normal map shader before which handles both the lighting and the normal map quickly and looks really good too, but that didn't have the ability to have objects cast shadows on each other so I moved over to his cubic map shader but that ate too much (or many?) resources to be viable, so I decided on balance to use lightmapping to get the shadows and then a normal map for the depth, setting the normal map light to the pos/rot/range/col etc. of the nearest/brightest light (or maybe a weighted average of the lights in range?) used for lightmapping so that it looks more natural. I don't think that's anything revolutionary but I really am having trouble with it!

Thanks for the help,
BC
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Feb 2012 13:18
Thanks for clarifying that. I'll look into this later today.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Feb 2012 22:47
I get a file does not exist error when I run your code. This is the offending line:



I may have fixed the shader but need working code to test it.
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 18th Feb 2012 12:08
Yes, since there were ten objects the same I thought I'd let the code create them instead of adding the media! If you comment out the line "GOTO VIEW" and wait fo it to do it's thing then hit space it'll make and lightmap all the obejcts you need in this part of the code:



However, you've got to have DarkLights to compile it! I'm afraid I'm away from my main PC this weekend so the earliest I can re-jig the code to work without DL or upload some new media would be Monday.

Thanks again for the help!
BC
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Feb 2012 15:21 Edited at: 18th Feb 2012 16:12
Quote: "I thought I'd let the code create them instead of adding the media!"


Fine - but a prior warning would have been helpful.

Dark Lights is not a problem. I'll try your suggestion and have another go today.

Edit Try these (attached) in place of your fx and dba files.

The main things to note are:

1. You need to input TWO sets of UV coordinates to the shader.

2. Your object has stage 0 for the base texture and stage 1 for the lightmap so your FX code needed to declare the base texture first (that's a DBPro requirement).

3. You applied the lightmap in the wrong place - it should be added to the other lighting before being applied to the base texture.

4. You probably don't need ambient light - it should already be in the light map. Not necessarily though.

5. The extra light used by the shader probably needs to be turned down otherwise your scene gets "washed out" with too much light. I used a trial value of 0.5 in the dba code.

Those are just my suggestions though. Let me know if want to apply the lighting differently. Screenshot in next post.

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Feb 2012 16:10 Edited at: 18th Feb 2012 16:10
Screenshot for previous post:

Attachments

Login to view attachments
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 18th Feb 2012 19:46
Yeah, I probably should have explained myself better, sorry for the confusion!
I thought the lightmap was applied to stage 0? That's what the help said!? Thanks for sorting that out for me, I'll take a look on Monday and see what I can learn! That screenshot looks great though!

Thanks,
BC
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Feb 2012 21:22 Edited at: 18th Feb 2012 21:34
Quote: "I thought the lightmap was applied to stage 0? That's what the help said!?"


If that's true it would explain a lot. I'll see what my copy says and report back.

Edit Just checked the Dark Lights help file and could not find a reference to "stage" or "stages". Which Help file are you referring to?
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 19th Feb 2012 02:07
Nice model!

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Feb 2012 14:43 Edited at: 19th Feb 2012 20:31
Since the subject of self-shadowing has been raised, here's an updated version of one of my old demos which shows how a bump mapped object can be self-shadowed. It requires a special horizon map to be created from a suitable heightmap.

Just press "1" for standard bump mapping, or "2" for the combined self-shadowing effect.

The "standard" bump mapping part of the shader needs to be re-written since it contains an error which it inherited from an early shader of mine. I'll try to remember to correct and upload it. The error doesn't have much effect on this demo fortunately.

I see no reason why an occlusion map couldn't be added in the same way.

Edit Oh dear.... Posted this on the wrong thread.

Attachments

Login to view attachments
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 19th Feb 2012 17:03
@Pincho:
I'm afraid to say that's not my work but the free FPSC city model pack from TGC!

@GreenGandalf:
I got the idea of the texture stages from the description of the command 'LM Add Light Map Object' in the DarkLights.chm I got with the DL install which says "The light mapper will create a stage 1 (if it does not already exist), move the base texture into this stage 1 and then create the light map in stage 0." but clearly either I've got the wrong end of the stick or some out-dated help files!

Thanks for all the help, I'll probably thank you again after I've actually seen it in action!

BC
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 19th Feb 2012 17:05
Actually, the help file is called 'References.chm'.

BC
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Feb 2012 20:28 Edited at: 19th Feb 2012 20:29
Yes, you're right. I was looking at the other one called "Information.cfm" - didn't notice the second Help file . In fact the situation is rather puzzling in view of the next sentence:

Quote: "The light mapper will create a stage 1 (if it does not already exist), move the base texture into this stage 1 and then create the light map in stage 0. This is to allow the lightmap to be modified by the diffuse colour from dynamic lights before being multiplied by the base texture."


That explanation makes sense - but I don't think it fits the facts. I'll do a bit more checking and report back.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Feb 2012 19:08 Edited at: 21st Feb 2012 12:33
OK. I've done some checking as it seems that the Help file is confused or at best unclear.

I've tested the various options of the LM Add Light Map Object command and the only one that seems to have an effect is the dynamic lighting one - and that seems little more than using the set object light command with an appropriate flag.

To get dynamic lighting with your lightmap you need to add the two light sources together and then multiply the result by the base texture. So the Help file seems to be referring to texture blending stages not UV stages. Here's a typical snippet to get dynamic lighting plus lightmap applied to your object (without using a shader):



Notice that the first blending stage (texture blend stage 0) uses the stage 1 UV coords (1 = 11-10, the light map UVs, see Help file for set blend mapping on), and the second blending stage uses the stage 0 UV coords (0 = 10-10, the base texture UVs). [Edit Forgot to add that in my code the light map was image 2 and the base texture was image 3.]

The first blending stage uses ADD (mode 7), i.e. the lightmap value is added to the diffuse dynamic lighting, and the second uses MODULATE (mode 4), i.e. the result of the first stage is multiplied by the base texture.

Here's a typical screen shot where I've used a red light in place of the default white light:



So it seems quite clear to me that the Help file is misleading.

I don't know why mipmapping needs to be turned back on.

Attachments

Login to view attachments
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 19th Mar 2012 01:36
First - Thanks for your help last time GG!

Now, something else is wrong! At first GG's example worked fine and all was good, but now for some reason my lightmapper's outputting the lightmap texture in stage 0 and the diffuse texture in stage 1 (that's the reverse of before)! See screenshot.

So, I've tried to switch the texture stages and UV coords in the shader file but for some reason it's not working!

I need to switch the texture stages of the lightmap and the base textures in the shader posted by GG, can anyone help?

Thanks,
BC

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Mar 2012 02:25
Quote: "So, I've tried to switch the texture stages and UV coords in the shader file but for some reason it's not working!

I need to switch the texture stages of the lightmap and the base textures in the shader posted by GG, can anyone help?"


Can we see your shader code?
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 19th Mar 2012 08:29
Sure!

Here's the shader code:


That puts the base texture in the right place and
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 19th Mar 2012 08:42
Of course!

Here's the shader code:


This gives the correct texture and UV coords for the base and normal maps (the normal mapping works fine) but the wrong UV coords for the lightmap.

Media in the download, you'll need DarkLights to compile and to create the lightmapped objects you'll need to comment out the line "GOTO VIEW" in the project code, then uncomment to see the effect. Press F1 to see the texture paths of the textures on the object.

Any help would be appreciated!

Thanks,
BC

Attachments

Login to view attachments
Brendy boy
18
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 19th Mar 2012 09:07 Edited at: 19th Mar 2012 09:11
this
Quote: "
float2 UV0 : texcoord1; // swapped the texcoords of the UVs here
float2 UV1 : texcoord0;
"


and this
Quote: "
float2 UV0 : texcoord1; // ditto for output
float2 UV1 : texcoord0;
"

is wrong. It doesn't really matter what the values are.

You need to swap the values here
Quote: "
float4 baseColour = tex2D(baseSample, In.UV0); // UV swapped
float3 normal = 4 * tex2D(nMapSample, In.UV0) - 1.0; //UV swapped
"

If the lightmap uv's are in layer 0 then the first line is correct.
If the diffuse uv's are in layer 1 then second line needs to use UV1.
If it's the opposite then first line need to use UV1 and second UV0

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 19th Mar 2012 15:19
Quote: "If the lightmap uv's are in layer 0 then the first line is correct.
If the diffuse uv's are in layer 1 then second line needs to use UV1.
If it's the opposite then first line need to use UV1 and second UV0"


No. Those lines don't refer to the lightmap UV coords - just the base texture and normal map UV coords which should be the same in this case. This is the line which uses the lightmap UVs:




I can't see anything obviously wrong with the shader code (although I think it's confusing to pair UV0 with stage 1, etc). Perhaps we need to see the object and it's lightmaps? Is it possible the lightmap is on stage 2?
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 19th Mar 2012 17:39
I've tried again, this time just changing the line GG mentioned and still can't get it working!

The lightmap's definetly on stage 0, check the screenshot on my first new post, there's some media on my last post, but that'll make it on your PC, so I'll upload some more media later tonight to demonstrate.

Thanks,
BC
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 19th Mar 2012 22:43
OK, so I tried the new shader on objects made in a different program and it worked fine!

It appears that my problem is how the lightmapper in my editor is outputting the files/creating UV data for the object.

I can't see anything wrong with the LM code in my editor, but someone else can see the problem with my code, here's the bit responsible for the lightmapping:


Thanks for all the help,
BC
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 20th Mar 2012 15:11 Edited at: 20th Mar 2012 15:24
OK, I'm totally at a loss as to what the problem is, can anybody help shed some light on this?

I've got the light mapping from my editor coming out fine now, only the lightmap is on stage 0 and the base texture on stage 1. All is good.

Here's the lightmapping function from my editor:


I've also adjusted the shader to reflect the change in texture stages. There's an example attached to this post, there's also the original shader (the original is called GGbump06LM.fx) in there if anybody wants to see the changes.

I'm pretty sure that it's not a shader problem because when I use the lightmapping code attached to a previous post the shader works fine on those objects, but not at all on the objects outputted from my editor; does anyone know why?

Thanks,
BC

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Mar 2012 16:00
Do the objects look right without the shader applied? If so, it should be a simple matter to check which stage coordinates are being used for which texture. Also, what DBPro code are you using to apply the texture (or textures)?
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 20th Mar 2012 16:18
Yes they look fine without the shader applied, the lightmap and base texture line up nicely. As the base texture and lightmap texture are already applied, I've been using this to add the normal map:

load image "building14_N.dds",3
texture object obj,2,3

I've just been playing around with "convert object fvf obj, 274" and "set object normals obj" Which solves the normal mapping but screws up the uvs for the lightmap

Thanks,
BC
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 20th Mar 2012 21:38 Edited at: 20th Mar 2012 22:54
So I tried converting the object before lightmapping in my editor and it worked! Well, kind of. see screenshot.

However, on the backside of the object where the lights from the lightmapper don't reach there are some strange effects (see other screenshots)

Edit:
In fact it seems that the object has lost it's uv data, has that happened because of convert object fvf?
/Edit

Any ideas what might be up?

Thanks,
BC

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Mar 2012 01:26
Quote: "In fact it seems that the object has lost it's uv data, has that happened because of convert object fvf?"


Yes - because that is exactly what you've done.

A typical DBPro object with an additional UV stage for lightmapping will have object FVF = 530 = 2 + 16 + 2x256 = FVF_XYZ + FVF_NORMAL + FVF_TEX2. If you've set it back to the DBPro default 274 then you've removed the stage 1 UV data, i.e. your new FVF is

274 = 2 + 16 + 256 = FVF_XYZ + FVF_NORMAL + FVF_TEX1 (i.e. just one set of texture coordinates).

That would certainly explain some of your problems.

Why did you change the FVF?

As I said before, we need to see your DBPro code + your object + your lightmaps + your shader. I'm sure you've just got the various things a bit muddled and that you are almost there - just as you were before.
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 21st Mar 2012 19:36 Edited at: 22nd Mar 2012 19:15
Thanks for confirming that GG, I was playing around with various values, forcing 2 uv layers on the object but messing around like that just gave different problems!

I was trying to change it because I was having problems with some other TGC objects and last time that fixed it, but now that I know what the command actually does I see that wasn't going to help. At least I've now learnt about the "convert object fvf" command!

Ok, here's all the stuff uploaded in the other posts in one nice .rar file! I've also added all the code from my editor (I know it's a mess but it works and I didn't expect anyone else to see it!), which needs BBB GUI to compile, the function responsible for the lightmaping is the very last one, the export function is called "ExportMap".

You can easily adjust the quick model viewer's code to show just the object without any shader applied, see the comments in the file for more. The original shader file is "GGBump06LM.fx" and the one I've played with to account for the swap of the base and lightmap textures is "GGBump09LM.fx".

Ok, I don't think I've missed anything? I hope you can help me with this, I've been stuck on it for so long!

Thanks,
BC
[Edit: Download removed]
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Mar 2012 22:51
Just had a quick look. I suspect your object's FVF format is the problem. It has a DIFFUSE component which is known to be handled badly by DBPro.

I'll do a bit more testing to see if I can fix the problem and report back.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Mar 2012 23:48 Edited at: 21st Mar 2012 23:49
Is this anything like what you want?

Attachments

Login to view attachments
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 21st Mar 2012 23:55
GG you legend! That looks incredible! How did you do it?

Thanks,
BC
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Mar 2012 01:00 Edited at: 22nd Mar 2012 01:01
That object is confusing. The lightmap is on texture stage 0 and the base texture is on texture stage 1. But, on the other hand, the lightmap uses UV stage 1 and the base texture uses UV stage 0. [Corrected silly typo.]

The other thing I had to do (because of a long-standing bug in DBPro - Lee says it's a feature for reasons I don't understand ) was remove the DIFFUSE part of the object's FVF, i.e. I converted it to fvf 530.

I'm not surprised you had problems with it.

Here's the shader code:



and here's the dba code:

James H
17
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 22nd Mar 2012 01:04 Edited at: 22nd Mar 2012 01:06
hi, just been through your code - GG converted the fvf to 530(you might think it should be 786(ie 530+256), but the shader used steals uv data from stage 0, so 530 suffices and 768 produces same result), he also switched back to original 06 version of the fx file and ensured texture stages where textured that you remmed out;


The fvf info with a better explanation than help files provide is available on the forum, not sure where or by who, but I have a copy so here you go



EDIT sorry GG not tryin to steal your glory just thought I would help speed things up for him, only good intentions from me and it seems you posted just before me anyway
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Mar 2012 01:19
Quote: "he also switched back to original 06 version of the fx file and ensured texture stages where textured that you remmed out;"


That's not what I did - but works just as well.

And is more logical than what I did.

Time for me to retire perhaps.
James H
17
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 22nd Mar 2012 01:27
Quote: "Time for me to retire perhaps. "

NOOOOOOOO!!!! Who will we learn from????? How am I gonna work through Evolveds shaders without following your work too? This cannot happen, this must not happen, I will speak with the good lord and have him grant you an extended stay and campaign for a higher retirement age limit in the meantime XD

We will always need your guidance, even lurkers like me, so dont be going anywhere..
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Mar 2012 13:38
He has spoken and granted me an extension of one year renewable by negotiation.

@Broken_Code

Just to make it crystal clear: use James H's solution rather than mine (they do the same thing but his is more logical).
Broken_Code
13
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 22nd Mar 2012 19:12
Great! Thanks for all your help! I'm super stoked that I can get on with the rest of my game but, to be honest, a little annoyed that I spent so many hours swearing at my computer over something with such a simple solution! I do have a couple of questions though :
How did you know that this was the problem/how can I tell if another model has the same problem?
Should I just batch convert all my objects to the new fvf?

Thanks,
BC
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Mar 2012 23:06
Quote: "How did you know that this was the problem/how can I tell if another model has the same problem?"


Bitter experience?

The first thing I do in a situation like this is to check the object's FVF format, e.g.



Then change the FVF if necessary.

If that doesn't work check to see which UV stage is which. It's not difficult since there are rarely more than two. Similarly with the texture stages.

In your case, as soon as I saw the object had a DIFFUSE layer I knew that was likely to cause problems. I didn't expect the texture/UV stages to be reversed with respect to each other though.

Quote: "Should I just batch convert all my objects to the new fvf?
"


If you don't use it then yes that might be a good idea.

Login to post a reply

Server time is: 2024-05-04 21:20:09
Your offset time is: 2024-05-04 21:20:09