Terrain Painting

Blitz3D Forums/Blitz3D Beginners Area/Terrain Painting

Zeotrope(Posted 2008) [#1]
I saw this old thread...err 5 years old.

http://www.blitzbasic.com/Community/posts.php?topic=23628

Was a discussion about painting a terrain. Then it went all 'french' on me and could'nt follow.

So my query is pretty simple.

Using a B3d terrain, I place trees based on altitude. During the placement, would it be possible to call up a 'painting' routine and draw tree shadows directy onto the terrain texture? My texture is 1024 X 1024 and spans the entire terrain without repeating.

Possible?


Matty(Posted 2008) [#2]
Simple answer Yes.
Complicated Answer - there are many ways to do this, I don't know all of them but I can give a simple method which assumes you have one 'sun' ie a single lightsource...

Create a second texture, setting textureblend mode to multiply.
Use the command EntityTexture terrain,secondtexture,0,1.

What this does is creates a second texture which will blend with the terrain texture you are currently using. If you leave it at this stage though you will only see a 'black' terrain as the second texture will multiply blend its '0' color value with the terrain.

What you then want to do is, each frame, using a second orthogonal camera (which was created as follows create camera ---> cameraprojmode secondcamera,2) which is positioned above the terrain and looking down directly at it, fitting the entire terrain width/depth into view.

Hide the terrain, set the cameraclscolor of the second camera to white (255,255,255 rgb), set the color of all your trees and / or other entities to black, render the image to a small viewport - eg 256x256 (can be larger though, but not larger than the screen res) then apply this render to the secondary texture using the copyrect command.

Make everything visible again, turn the second camera off (cameraprojmode secondcamera,0) and when you get to rendering your main image it should appear with shadows on the terrain..

That hopefully gives you a rough idea of how to do it this way.


Zeotrope(Posted 2008) [#3]
This method is for real-time processing? Or for 1 pass @ load time?

I was looking for a load time solution and while interesting, I was hoping for a texture based solution rather than a cam / screen capture approach.

Currenty, I have a colour map that stretches across my terrain.

the 2nd Layer is a tiled, rough approximation of sand

3rd layer, a drifting cloud map shadow.

Trees are places at load time from an array.

I wanted to use a shadow sprite like a decal - and map them at the base of each tree, directly onto the colour-map so that they at least darken the area at the trunk base. I also thought this would be a good way to graphically anchor the vegetation so it doesnt look like it is floating.

I also understand that the decal shadow's quality would be a little blocky, but could live with that.


Still possible?


Naughty Alien(Posted 2008) [#4]
..you want flex shadows, static one..then yes, it can be done..


Zeotrope(Posted 2008) [#5]
Good to know it can be done....

To get me pointed in the right direction, how would I map a sprite to a texture? Do I use the clamp function?

If I know a tree is at loaction 100,10,100...and my Colour Map is 1024 X 1024, where do I go from there? Will the terrain size be a factor in the calculations?

Any simple examples?


Warner(Posted 2008) [#6]
Hopefully this helps:



Zeotrope(Posted 2008) [#7]
Thanks Warner. Nice idea!

I studied that code...was pretty cool and close to what I wanted.

Here is what I found and the associated drawbacks.

Copyrect is ok…but beyond when I copy a ratio beyond my Screen width / height, It copies black (blank)

My Heightmap image is 1024 * 1024. My Colormap for the terrain is 4 layers;

Island map 0,0 : 1st layer – stretched across the entity terrain (512 * 512)

Grass map 0,1 : 2nd layer – adds some detail to the terrain

Dirt map 0,2 : 3rd layer – adds some detail to the terrain

Cloud map 0,3 : 4th layer – adds some drifting cloud shadows to the terrain


Terrain = Loadterrain(“heightmap.bmp”)
Scaleentity 4,52,4

That’s where my calculations don’t make sense…..I can add shadows to objects (it seems) as long as I don’t scale the terrain up to 4 times. It works on scaleentity 2,26,2 if I adjust the createtexture tex(512) to (1024)…..???

Anyway, when it did work at a lower scale, I found that I could easily add a ‘drawimage’ command to the loop and create the shadow silhouette I require, however, it’s so ‘low res’ it looks ugly.

Should I now increase my terrain heightmap to….4096 * 4096? Would this be the same as a 1024 * 1024 on a terrain with a scale of 4?

Not sure where to go from here…..

I need to load a heightmap, scale it, add trees, cast lightmap type shadows to the trees, and finally map the texture to the terrain.

My other choice is: Photoshop in all the shadows when I want trees, then locate the X/Y/Z for the shaows and create a data loader (lots of data considering I have 1000 trees)


Warner(Posted 2008) [#8]
Maybe you can CopyRect smaller portions at once ? A lightmap of 1024x1024 can be divided into 4 or 8 parts each sized 256x256 or 512x512. That way, it will fit into the screen.
You can also directly draw onto a texture by using SetBuffer TextureBuffer(texture), but I've heard it isn't reliable to work on all systems.

To convert from world coordinates to the lightmap texture (UV) coordinates, you should think in percentages.
The upperleft corner of the terrain is at 0%, 0%. The lowerright corner of the terrain is 100%,100%. First, convert from the mesh position (X,Z) to a relative position on the terrain (in percent), then convert it to a position on the texture (x% * texturewidth, y% * textureheight).

I've adjusted the example to a terrain size of 1024x1024, the texture is created in 4 chunks. Instead of 100 cubes, it now generates a 1000:



Zeotrope(Posted 2008) [#9]
Cool stuff. I also managed to cast lightmapped shadows across a multi textured terrain.

I think, for my current project, I am stuck with my blitz terrain. My engine is 'dug in' that way with no way out other than completion! :)

If I could start over (And I probably should) I would use a modular mesh system (lots of small meshes) to simulate my Island(s). My project is a combat simulation (began 2004) and while it's really starting to look nice, it really needs some shadows. Perhaps I will post a demo to show what is lacking...(showcase)

Thanks for your help, Warner. Will try your code and see how I go.


stayne(Posted 2008) [#10]
ThermO have you taken a look at L3DT? It can generate some very terrains and exports all the textures at the same time. Here's a shot of an exported terrain in my editor. Of course you will still need to create your object shadows but I think you may be pleased with what L3DT can do. You'll need to add a detail texture of course but that's no big deal.

www.headpile.com/stuff/ed1.jpg


Zeotrope(Posted 2008) [#11]
Grabing the standard version now (L3DT)...looks iteresting.

On another note: Ran a test yesterday in B3D. Used 3dsMax to make an Island mesh. Added a light source, Took a top down view and rendered the resulting shadow map.

This rendering was then mapped back to the island terrain and resulted in some nice, soft-edged shadows.

I feel I really need to dump the B3D terrains and get jiggy with multiple 3DS Meshed terrains.

Anyway, thanks for the L3DT suggestion. Will check it out.

PPS: Nice water in that shot....yours?


stayne(Posted 2008) [#12]
No it's from the Fastext lib. No need to reinvent the wheel I reckon :).


Zeotrope(Posted 2008) [#13]
L3DT (standard) is pretty cool...got some nice results and the output is good quality.

I need to check out Fastext.lib for reflections. Easy to use?