Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Tiling a heightmap


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 01 January 2012 - 11:38 AM

Hi!
I am currently writing a heightfield class for my engine. I want to use tiles for different heights.
How can i do this? Should i sort the indexbuffer by height of the triangles and call render once for every tile?
Or is there a better way to handle this with a vertex shader?

Thank you in advance!

Sponsor:

#2 Tournicoti   Prime Members   -  Reputation: 683

Like
0Likes
Like

Posted 01 January 2012 - 06:08 PM

If you use a depth buffer and opaque rendering, there's generally no need to sort your primitives by height/depth/etc... , AFAIK it's better to limitate communications between CPU and GPU because of the limited bandwidth between them. The computational power of nowadays GPUs is awesome, it's better to have a more complex pipeline rather than to communicate too frequently between CPU and GPU. A draw call for each tile must be avoided . You can group your primitives according to their vertex layouts and techniques and draw each group in a single draw call (at least far fewer draw calls). Nowadays,the less you have draw calls and shader constant updating, the more you can access to high performances. For example, that's why there is instancing (you give geometry and information about each instance to the GPU so that it can draw all geometry instances at once, so : limiting draw calls) and constant buffers (a single update for a whole set of parameters, so : limiting constant updates).

I hope that can help you
Nico Posted Image

#3 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 04:28 AM

Thank you. I really have to learn shader programming now
...

#4 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 04:32 AM

My idea was to sort the primitives at the start of the program and make a draw call for each texture. Would this be efficient, if i have about 4-5 textures?

#5 Tournicoti   Prime Members   -  Reputation: 683

Like
0Likes
Like

Posted 02 January 2012 - 05:13 AM

Hello IceBreaker !

My idea was to sort the primitives at the start of the program and make a draw call for each texture. Would this be efficient, if i have about 4-5 textures?


My question is : Why do you need to sort your primitives ?
If your tiles are fully opaque (even alpha testing or alpha to coverage) , and you use a depth buffer, you will get perfect overlapping results even if the tiles are not sorted.
(Sorting them would partially avoid to invoke the pixel shader for pixels that would be covered later in the rendering phase, but it would need extra CPU work.) Generally it's better to let the GPU eliminate the covered pixels thanks to the depth buffer rather than to sort your primitives. What's more, the depth buffer permits to display perfectly primitive intersections and "A over B, B over C, C over A" case, because it works at the pixel level.

Getting informations on the depth buffer could help I suppose ? There must be an good explanation of this in the DX SDK documentation

Nico

EDIT : I honestly don't get why sorting them at the start of the program only is enought, unless your point of view is static ?

#6 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 05:17 AM

Thank you, I´ll try it that way

#7 kauna   Crossbones+   -  Reputation: 2745

Like
1Likes
Like

Posted 02 January 2012 - 05:36 AM

You may also use texture arrays to store your landscape textures (up to 512 per texture array) or a 3d texture could work too.

These texture storage methods allow you to draw your landscape tiles in what ever order you want regardless of the number of materials. Of course, every tile needs an index to the desired texture in texture array or a coordinate to the 3d texture.

Otherwise, it is a good practice to arrange your objects by the resources they use such as material.

Cheers!

#8 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 06:10 AM

If in the 3d texture there is one 2d texture for y=0 and 1 for y=1 will directx interpolate between them?

#9 kauna   Crossbones+   -  Reputation: 2745

Like
1Likes
Like

Posted 02 January 2012 - 06:36 AM

If in the 3d texture there is one 2d texture for y=0 and 1 for y=1 will directx interpolate between them?


Direct3D will interpolate a 3d-texture according to the filtering mode. if you wish to interpolate between textures, the 3d texture has a limitation that the textures can only be blend with the slice above or below, which may turn out to be a huge limitation.
Otherwise you may use alpha maps (texture splatting) to blend between materials.

Cheers!

#10 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 06:51 AM

Thank you very much, I´ll definitly give it a try ;)

#11 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 09:02 AM

I´ve used a volume texture in .dds format now.
But directx isn´t interpolating between the different textures.
Which filter should i use for interpolating? I tried D3DX_FILTER_LINEAR or D3DX_FILTER_TRIANGLE.
On the attached screenshot you can see what i get.

Attached Thumbnails

  • terrain_not_interpolating.png


#12 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 09:21 AM

solved with
m_pd3dDevice->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
m_pd3dDevice->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);

I used mip maps:
m_pd3dDevice->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
terrain_mipmaps.png
But the problem is that there aren´t created mip maps for every texture.
The mipmaps are an interpolation of all 4 textures and that looks like the attached screen.

#13 kauna   Crossbones+   -  Reputation: 2745

Like
1Likes
Like

Posted 02 January 2012 - 09:46 AM

I think you should be able to create mip maps for a volume texture in the program you used to create the volume texture.

D3DTEXF_LINEAR Should be correct filtering mode.


It seems that there may be device based limitations on the volume textures. Some devices may not support volume texture mip maps and all blending modes may not be implemented.

It starts to feel like bad idea to use a volume texture.

Cheers!



#14 IceBreaker23   Members   -  Reputation: 614

Like
1Likes
Like

Posted 02 January 2012 - 09:49 AM

I used Directx Texturing Tool. I can create mip maps but the lower i get in the mip map level the less mip maps are there.
For example:
I use 4 textures and 9 mip map levels:
first level: 4 maps
second level: 2 maps
third level and below: only 1 map. And this is the bug, but how can i improve this?

#15 kauna   Crossbones+   -  Reputation: 2745

Like
0Likes
Like

Posted 02 January 2012 - 12:44 PM

And this is the bug, but how can i improve this?


Hi, actually I think that this is the expected behavior and one other point against using volume textures for terrain texturing.

I can see that you are using d3d9 level hardware and that you may not use texture arrays. The effect you show on your screen shots may be done easily with separated textures too and you may expand the technique to use texture splatting later for example.

Thank you for your efforts.

Best regards!

#16 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 01:06 PM

Can´t this be done with volume textures? I only want to reduce the flicker of terrain parts far away from the camera when moving. Isn´t there any possibility to remove this?
Should i use geometry shader for this?(I dunno if shader model 3 supports this)

It just had been so easily with a volume texture. :(

Thank you and i hope you can help me!

#17 froop   Members   -  Reputation: 636

Like
0Likes
Like

Posted 02 January 2012 - 01:26 PM

Should i use geometry shader for this?(I dunno if shader model 3 supports this)

No and no.

You can use a mip mapped texture atlas but it's not easy to get right and you'll end up wasting some texture space. Also if you going to use displacement mapping, texture atlas will become really expensive. A multipass technique might be the best choice.

#18 IceBreaker23   Members   -  Reputation: 614

Like
0Likes
Like

Posted 02 January 2012 - 01:44 PM

So should i use a vertexshader to determine which 2 textures i should use and set it?
You said a multipass technique should be the best choice. Should i use one for interpolation between dirt&grass, one for interpolation between grass&stone, etc.
Please don´t throw in some names of techniques cause I am not familiar with them, and wikipedia alone doesn´t seem to help me with this.
I know what a multipass technique is, but I don´t know what should this technique do?

Thank you ;)
I hope you´ll explain it to me

#19 froop   Members   -  Reputation: 636

Like
1Likes
Like

Posted 02 January 2012 - 02:15 PM

In a multi-pass technique you would render one texture per pass with additive blending and use an alpha-map or some other indication to define the alpha value of the pixel drawn. Research texture splatting (kauna already mentioned it), there are a lot of good resources about it.

Btw: You can do the same in a single pass by reading all terrain materials in the same pass. You'll potentially have more texture look-ups this way though.

#20 kauna   Crossbones+   -  Reputation: 2745

Like
1Likes
Like

Posted 03 January 2012 - 02:09 AM

Using vertex and pixel shaders will give you more freedom about how to manage the blending between textures. I strongly suggest to use them.

Depending on your graphics hardware, you may perform pretty many texture look ups per pixel without loss of performance.For example, at this moment I do something like 14 texture lookups (2 alpha, 4 base texture, 4 detail texture, 4 normal) per pixel in my terrain shader and it doesn't slow down that much.

Simple splatting pixel shader (D3D11) goes something like this (the code won't work directly)

float4 Alpha = Alphatexture.Sample(sampler,Tex.xy); //Read 4 alpha values from a rgba texture

float4 Color1 = Texture1.Sample(sampler,Tex.xy); //read diffuse texture 1
float4 Color2 = Texture2.Sample(sampler,Tex.xy); //read diffuse texture 2
float4 Color3 = Texture3.Sample(sampler,Tex.xy); //read diffuse texture 3
float4 Color4 = Texture4.Sample(sampler,Tex.xy); //read diffuse texture 4

float4 FinalColor = lerp(Color1,Color2,Alpha.r);
FinalColor = lerp(FinalColor,Color3,Alpha.g);
FinalColor = lerp(FinalColor,Color4,Alpha.b);


This code blends 4 textures based on the values stored in the alpha texture. 1 alpha texture is enough for blending 5 texture maps actually.
There is a way to blend 8 textures with 1 RGBA-map, but that's a different story.

Good luck!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS