Planes, Terrains and texture layers. With HideEntity. Wierd, wierd problem.

Blitz3D Forums/Blitz3D Programming/Planes, Terrains and texture layers. With HideEntity. Wierd, wierd problem.

gburgess(Posted 2004) [#1]
Okay, another day, another question from me.

This one's a nit more nebulous, and I'm happy to just walk away from it if everyone draws a blank. But here's the deal:

I wa experimenting with ways to make shadows, and I decided to try a method where the a second camera hovered above the main camera, pointing down towards it. The idea being, this second camera would take a snapshot of everything around the main camera. While it did so, the terrain and the plane for the sea would be hidden, lights dropped to nothing and the background made plain white. This bird's-eye two-tone image of the area around the camera would then be copied onto a texture layer of the terrain. This texture layer would basically involved a small texture that moved along the terrain via UV co-ordinates calculated from the position of the main camera.

So far, so complicated.

Thing is, it almost worked. I expected to be able to figure out the oddness that left me with a shadow that was always too big and always ahead of the player's ship. No, that part didn't bother me. What did bother me was that when it came time to hide the terrain and the sea, the HideEntity command would not work. Or rather, it worked badly. I'll try and explain the effects of hiding these two entities:

1) Using HideEntity sea: Just to make sure the command was being read, I tried deliberately leaving it hidden. The result was that the sea was still there, but the two scrolling textures on it were stationary. When I used HideEntity on the terrain, it wasn't hidden.
2) Using Rotateentity sea,180,0,0: This is where it gets wierd. In a fit of desperation, I tried just turning the plane over 180 degrees. Since it isn't double sided, it should appear invisible from the top. Same effect as HideEntity. Then I tried Rotating the sea 90 degrees. And this was really wierd. The "frozen" sea plane was still visible, but there was a second sea plane that was turned 90 degrees. It had the animated scrolling texture layers on it too, but it was missing the base layer, which was blue. So it was basically a sea plane containing most of my sea effect, on it's side, and a frozen replica sitting on the original spot. Here's a screenie to illustrate.
[img]http://www.hailofbullets.co.uk/dirty/plane-problem.jpg[/img]

I tried the same thing without the layers of texture, just a simple blue one on the water, and the above screenshot was similar, except the sidewayes water was completely black. Those two planes are definitely the same plane somehow being drawn twice, since taking the layers off the original one affected both.

Now, I appreciate this is hard to follow, and I know you might well be thinking "his game's just buggy" and that's I've somehow got two planes for my sea and don't realise it. I'm convinced I'm doing things right, and that something else is afoot here. But the reason I've made this post is in case someone else recognises these odd symptoms. Maybe it's a well-known fact that Blitz has trouble hiding/rotating planes and terrains. Maybe I've forgotten an obvious command. Either way, if this is ringing bells with anyone, please do try and save my sanity and let me know.


Ross C(Posted 2004) [#2]
Do you have a source code i could see?

I've not had any troubles with rotating terrains and planes. There was an issue with terrains and ATI cards, but don't think that applied to planes, so scratch that one.


Ross C(Posted 2004) [#3]
Hmm, the fog doesn't look like an oddity :) Looks normal. Source code?


Ross C(Posted 2004) [#4]
Another thought. Watch for your globals. You might have two planes in exsistance. On created in the main program and another in a function. If you create a plane in a function using a local variable, then exit the function, i don't think there's anyway to access it again.


sswift(Posted 2004) [#5]
I'm going to make a guess that you set cameraclsmode for your camera to false, true to clear the z-buffer eahc frame but not the color buffer, and what you are seeing is not the actual object being unhidden, but an afterimage created by the "hall of mirrors effect" which you get when you don't clear part of a scene, and don't overdraw every part with geometry every frame.

You'll want to set cameraclsmode to true, true for your second camera.

Oh and just in case you don't realise it, all camera views get rendered when you do a renderworld, so you have to hide the main camera when rendering the shadow camera view and vice versa.


gburgess(Posted 2004) [#6]
Sure thing. I'll copy and paste from the relevant areas. Oh, this is a different shadow routine to what I mention in the other thread, in case that causes any confusion!

Okay, this is where the sea plane is created. This is called once:

watersurface%=CreatePlane()
watertex%=LoadTexture("bmp/water1.bmp",8) ;Basic blue sea texture
watertex2%=LoadTexture("bmp/water2.bmp",8) ;This is the first scrolly texture
watertex3%=LoadTexture("bmp/water3.bmp",8) ;This is the second

EntityTexture watersurface,watertex,0,0
EntityTexture watersurface,watertex2,0,1
EntityTexture watersurface,watertex3,0,2

TextureBlend watertex2,3
TextureBlend watertex3,3

ScaleTexture watertex,1500,1500
ScaleTexture watertex2,10000,10000
ScaleTexture watertex3,10000,10000


Then, here's the part where the shadows are made, called every frame:

.assignshadows

terrx#=40960:terry#=40960 ;These don't need to be set every frame, they're just here while I work on the routine!

;Deciding where to scroll the texture to.
acttx#=EntityX(maincam)
actty#=EntityZ(maincam)
placeshadx#=(1/terrx#)*acttx#
placeshady#=1-((1/terry#)*actty#)
placeshadx#=placeshadx#*4
placeshady#=placeshady#*4
placeshadx#=placeshadx#-0.5
placeshady#=placeshady#+0.5
PositionTexture shadowtex,placeshadx#,placeshady#

HideEntity maincam ;Hide the normal camera
ShowEntity shadowcam ;Show the camera for snapping the shadows

;Position the shadowcam above the main camera
PositionEntity shadowcam,EntityX(maincam),EntityY(maincam)+8000,EntityZ(maincam)
RotateEntity shadowcam,90,0,0

;Some of this is commented depending what method I use to get rid of the water. Right now, it's set up to do what's in the screenshot, rotating the water by 90 degrees. Incidentally, the alpha didn't work either.

;HideEntity watersurface
HideEntity groundmesh
RotateEntity watersurface,90,0,0
;RotateEntity groundmesh,90,0,0

;EntityAlpha watersurface,0
;EntityAlpha groundmesh,0

;No illumination for objects, and make background white...
AmbientLight 0,0,0
LightColor sun,0,0,0
CameraClsColor shadowcam,255,255,255
CameraZoom shadowcam,10

;Not really necessary, but I was desperate for anything to fix it...
Cls

;Snap the shadows
RenderWorld

;Copy from the backbuffer direct to the shadow texture
CopyRect 0,0,512,512,0,0,BackBuffer(),TextureBuffer(shadowtex)

;Reset values to what they were before being darkened
AmbientLight ambr%,ambg%,ambb%
LightColor sun,sunr%,sung%,sunb%

;Put the cameras back
HideEntity shadowcam
ShowEntity maincam

;Return visibility settings to normal

;ShowEntity watersurface
ShowEntity groundmesh
RotateEntity watersurface,0,0,0
;RotateEntity groundmesh,0,0,0

Return


Oh, and the code to scroll two layers of texture on the water. Probably not important, but you never know. This is also called every frame:
	If worldtype$="island" Or worldtype$="ocean"
		wx#=wx#+0.02*framedrawtime#
		PositionTexture watertex2,wx#,0
		PositionTexture watertex3,0-wx#,0.5	
	EndIf


I don't think I missed anything relevant, and I hope I've not made a really stupid obvious mistake. I can't see what possible error I could have made that results in a duplicate sea plane being made. But then I've had problems in Blitz before and figured there's a bug in Blitz, until I find the really stupid mistake I've made, so that could well be happening here.

I guess I should really write a seperate short program and see if this occurs there.

-Glenn


sswift(Posted 2004) [#7]
Other than that, I'd say it's a bug in your code.

Also, that fog bug I see int he pic which I just found is PROBABLY related to multitexturing.

Let's say you have fogging enabled.
And let's say that you have a plane with three textures on it.

Now let's say that your card only has TWO texture units, so Blitz cheats and renders one of the texture layers as an additional surface.

In this scenario, Blitz will render the scene incorrectly, because that second surface will ALSO receive fog, and so you will get 2x as much fog as normal.

The only way to prevent this, aside from petitioning Mark to fix the problem, ASSUMING IT'S POSSIBLE for him to fix it, is to always have HWMultiTexture False in your code, or make it an option for the user, OR ignore people with one texture unit, and presume that there will only ever be TWO texture units available, and manually create that third texture surface as another surface in your model so that Blitz won't try to do it improperly.

I could be wrong of course, Mark may have corrected the issue, but I had this problem a long time ago when doing terrains in Blitz, where I was getting a different fog color than expected.


Ross C(Posted 2004) [#8]
hm.. I take it you do do another renderworld?, in the main loop of your code?


gburgess(Posted 2004) [#9]
I agree about the fogging issue. Wellm partly. On my old laptop, it happened with fogging full stop. On my GeForce2MX it only happened when I added the layers. On the GeForce4 it doesn't happen at all, so I expect you're on the right track there.

I think I'll try and recreate this effect in a short, simple program. I bet it doesn't happen then. Grrrr. :D


sswift(Posted 2004) [#10]
From your code, it looks like what you are doing is:

Rotate water plane on it's side.
Render view.
Rotate water plane back.
Render view.


So why would you expect to see only the water plane rotated 90 degrees on it's side? You _should_ expect to see only the water plane laying flat on the ground.

As for why you are seeing both, there is one thing I can think of which would cause that.

And that is doing something a little different thaan the HOM effec tI described. Instead of clearing the zbuffer, and leaving the colro buffer intact, you're leaving both intact.

Thus, when you render the second camera view, everyhting in the scene is zbuffered with what was there before (the water plane rotate 90 degrees) and then you display that, and then you clear the buffer just before rendering the water at 90 degrees again for the shadow cam.


Ross C(Posted 2004) [#11]
Any chance you could e-mail the source and relevant mdeia, or is this a protected source game? If so, no problem. :) It's hard to know whats going on, in the whole picture :)


gburgess(Posted 2004) [#12]
Well, before we go any further, let's just marvel at how stupid I am:

I wrote a quick test to try and cause the fault, and I partly managed it. Now, the symptom seen in the screenshot isn't quite as it appears: black half-textured water is, in fact, the whole thing. It's just not catching the light, so it looks wrong. (Told you I wa stupid). That just leaves the mysterious second plane of water, which is bound to be my bad also. I really hate doing this: not being able to find a bug in my code, so having to blame Blitz, and then finding the fault lay with me after all. So far, Blitz has been 100% right in these matters. This is what I get for letting bugs keep my up late when I don't think straight.

I'd rather not share my source, partly because I'm hoping to sell this game eventually but mostly because my code is seriously untidy. I'd be embarrassed to let anyone else see it :D Its well annotated, but ugly. Very ugly.


Ross C(Posted 2004) [#13]
Debuglog is invaluable to me for finding bugs in my code :) So is a dictionary for my spelling :S Hope you get it sorted, and no problem about the source :)


gburgess(Posted 2004) [#14]
Yup, it was me. I'll get me coat. Sorry to waste your time, folks. I suddenly realised what the problem must be, and set my mind to hunting for a specific bug, and I soon found it: I was loading the entire world twice. Duh.

Still, I wouldn't have found it so quickly had it not been for this thread, so thanks, guys, for the help. :)


sswift(Posted 2004) [#15]
Let this be a lesson to you. DONT WRITE MESSY CODE!


gburgess(Posted 2004) [#16]
Sir, yes, sir!

It wasn't the messyness, really, it was me trying to be too efficient. I put the loading of level data and the world default features together, obivously intentionally at some point, but then I forgot about it later. But point taken.


sswift(Posted 2004) [#17]
Btw, you're the second person I know of to have loaded everyhting in his world twice.


gburgess(Posted 2004) [#18]
Really? Is the other person cool and handsome as well? :D


sswift(Posted 2004) [#19]
She sure is!




dangerdave(Posted 2004) [#20]
I thought you were going to stop showing pictures of your evil twin sister!

Wait a minute. Is that you? It's hard to tell sometimes.

;)


gburgess(Posted 2004) [#21]
The last time I saw that image, I hoped it would be the last. Now, I'm hoping that again.