GLBasic forum

Main forum => GLBasic - en => Topic started by: kaotiklabs on 2012-May-12

Title: how to dynamic texturize a terrain contour
Post by: kaotiklabs on 2012-May-12
Hi,

for my LD entry I posted a 2D horizontal scroller with a guy walking through a terrain.

I would like to know if its possible to apply a terrain texture the ground sprite in order to paint it on-the-fly following the contour.

Maybe by using polyvectors and some kind of dynamic adapted texture??

In my sample, the terrain is a sprite but if I what I want can be achieved with another kind of object like a poligon mesh is not a problem, Im just looking for the result, texturizing dynamic terrain :)

I attach you here the samples for better understanding of what I want to achieve.


[attachment deleted by admin]
Title: Re: how to dynamic texturize a terrain contour
Post by: MrTAToad on 2012-May-12
I can't think of a way of doing it dynamically, and certainly not fast enough for a game unfortunately...  It would generally be individual blocks of graphics, created in some art package.
Title: Re: how to dynamic texturize a terrain contour
Post by: Schranz0r on 2012-May-12
Worms did it that way!
Title: Re: how to dynamic texturize a terrain contour
Post by: erico on 2012-May-12
Lets suppose your terrain is mostly horizontal.

You could create it first as a black and white thing, then go scanning pixels vertically while plotting your pixels, grab the final result and use it. Altough it seems it is what you are currently doing right?
Title: Re: how to dynamic texturize a terrain contour
Post by: kaotiklabs on 2012-May-12
Erico, my sample result is now beeing drawn by hand.  :(

As erico suggested maybe I could upload the sprite to mem and paint the pixels before but I was looking for a better looking method, not raw pixeling.
I want to have several types of texture terrains, like forest, snow, desert, etc that I could apply to the same shape template (black&white).

Maybe is possible to generate a kind of uv map and use polys for the shape?
Title: Re: how to dynamic texturize a terrain contour
Post by: erico on 2012-May-12
Polyvectors may help as they are able to ´liquify´ around the shape, but you may get distortions.

If you want to use a texture, let´s say a 8x8 blocks, you could use similar method.
Read the pixels to know when to change textures based on distance of the ground´s ´end´.
You could then use a B&W mask or blocks to trim the top part of it if needed or construct a set of pre made top bloks to use.

That is, if you want an horizontal terrain.

This way, your textures are unaltered, but you could also have a routine to ´share vertically´ the pixels of the up most blocks.

I´m not sure if this all make sense, just theories... :)
Title: Re: how to dynamic texturize a terrain contour
Post by: erico on 2012-May-12
What I meant, as an example, is similar to this screenie, but you crop tops so it dosen´t look blocky.
You could also draw your full things as blocks or a map and them just crop the borders(ops redundancy), or add blocks that have different curve levels to suite your needs. If this is overdone with options, then no one will notice it is a tile map, I guess some 6 different levels could pull the trick!

hope it inspires! :good:

http://playstarbound.com/wp-content/uploads/2012/05/31.png
Title: Re: how to dynamic texturize a terrain contour
Post by: erico on 2012-May-12
Also, if you need to have the outline, be it done by you or the user (like I once half did for a "draw your course" race game), you could run a pass over the outline to make it blocky in the first place, and work from there on...
Title: Re: how to dynamic texturize a terrain contour
Post by: mentalthink on 2012-May-13
I don´t understand very well the question?¿, but putting 2 textures and making something like a And or Xor...
Title: Re: how to dynamic texturize a terrain contour
Post by: Slydog on 2012-May-14
If you are wanting to wrap a texture on top of a dynamically generated 'rolling' hill, I don't think this is too difficult, if you use POLYVECTORs.

Take the below screen shot as an example.

The bottom set of red circles would represent your rolling terrain points.
The bottom set of blue circles would be calculated based on the red points.
It should be an easy algorithm to determine these blue dots, somebody on here may provide the technical term for that trig function, I have no idea.
Then repeat this (and reuse the same texture) for all of the terrain points.

Then design your texture like the top texture in the screenshot (but with a even horizontal base, unlike the cut/paste I did from yours).
Using polyvectors, map the red dots from the bottom to the red dots on the texture.
Then do the same for the blue dots.
This will result in the texture 'wrapping' around the terrain.
It will distort a slight bit of course, but experiment and you may be happy.

You could have a longer texture to provide less repeating, each time just pick a point to start mapping from.
Of course you may have to predetermine certain valid starting points so the results flow together with the neighboring terrain nicely.  (same concept as a tileable texture)

[Edit]  The more reds dots you use along the terrain, the better it may look.

[attachment deleted by admin]
Title: Re: how to dynamic texturize a terrain contour
Post by: Kitty Hello on 2012-May-15
Slydog's method is the best, IMO.
One thing: When you go "downhill", the math for the tiles might get complicated once they are to small that they could possibly overlap:

(http://www.glbasic.com/pix/tnterrain_problems.png) (http://www.glbasic.com/pix/terrain_problems.png)
See the image: The grey polyvectors are just perpendicular to the terrain edge you want to render. The green ones are to fill the holes (if you want that). But if you go uphill, the green ones will be the red ones, that overlap with other graphics - which is bad. So in this case you need very sophisticated math, or just make the terrain texture like grass, that looks good even when it overlaps.
Title: Re: how to dynamic texturize a terrain contour
Post by: Slydog on 2012-May-16
Later today I'm going to post the code I was working on yesterday that results in the screenshot below.
It endlessly scrolls and creates randomly generated rolling hills, and textures the hill portion using the method I suggested above.

As you can see there are artifacts when the hills peak and valley.
That is because I think the uv coordinates are too squished to look good.
I have an idea to fix that, fingers crossed.



[attachment deleted by admin]
Title: Re: how to dynamic texturize a terrain contour
Post by: Schranz0r on 2012-May-16
looking good Slydog!
Title: Re: how to dynamic texturize a terrain contour
Post by: kaotiklabs on 2012-May-16
Yes!

thats really cool!!  :nw:

Im still trying but havent go so far.
well done!!
Title: Re: how to dynamic texturize a terrain contour
Post by: Wampus on 2012-May-16
+1 interest

Cool stuff
Title: Re: how to dynamic texturize a terrain contour
Post by: erico on 2012-May-16
Looks great Slydog!
Title: Re: how to dynamic texturize a terrain contour
Post by: Slydog on 2012-May-16
Bug Fixed!!!

I added a new topic to the 'Code Snippets' section here:
http://www.glbasic.com/forum/index.php?topic=8118.0 (http://www.glbasic.com/forum/index.php?topic=8118.0)

It includes the source code plus a little explanation.
Title: Re: how to dynamic texturize a terrain contour
Post by: MrTAToad on 2012-May-16
It'll be interesting to see this running as a real time program
Title: Re: how to dynamic texturize a terrain contour
Post by: erico on 2012-May-31
This article has a part a bit about this(I mean the terrain thing):
http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/