PDA

View Full Version : Textured Terrain Rendering



Avantarius
07-23-2009, 05:14 AM
As a beginner in OpenGL i was wondering what could be the best way to render a textured terrain.

The data is stored in a grid-like structure and for every point the height and the texture (which could be i.e. grass, mud, stone ...) are saved.

At the moment i render the grid multiple times, one time for every possible texture, specifying glColor3f(1.0,1.0,1.0) if the texture is set on a given point and glColor4f(1.0,1.0,1.0, 0.0) if the texture is not set, creating a smooth fade.

To me this doesn't seem to be an efficient way so i'm asking which other ways could be possible to do this. Any hint would be apprechiated.

scratt
07-23-2009, 08:52 AM
Absolutely the best place to start with this is The Virtual Terrain Project.

http://www.vterrain.org/

It covers just about everything you'll ever need to know. :)

I am not deliberately ignoring your question, but when you look at that site you'll see there is just such depth to this subject that you really need to absorb some more info before asking questions about technique.. It will save you time in the long run. :)

Avantarius
07-23-2009, 09:29 AM
I already thought there would be no simple answer to my question :D

I took a quick look on that site, and some techniques described there go pretty much in the direction of my idea (even if much more advanced!), gonna take a closer look at the links provided there.

Thank you!

scratt
07-23-2009, 09:38 AM
np. Have fun. I have spent many hours reading through stuff on that site (and the ones it links to) for the last few years!! ;)

Avantarius
07-23-2009, 10:35 AM
I found a technique called "splatting" which suits my needs. It uses multitexturing and the setup for the texture stages, unfortunably, is written for D3D.

Can anyone "translate" me this to OpenGl code, please?


// stage 0 coloring : get color from texture0*diffuse
lpDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
lpDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
lpDev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);

// stage 0 alpha : nada
lpDev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
lpDev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_CURRENT);

// stage 1 coloring : nada
lpDev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
lpDev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_DIFFUSE);

// stage 1 alpha : get alpha from texture1
lpDev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
lpDev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

Avantarius
07-24-2009, 03:37 AM
The code should setup the texture stages in order to take the RGB values from texture stage 0 and the alpha value from texture stage 1.

After a bit of searching i've managed to put together this code which does the job, but it kills also the lighting:


glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glActiveTexture(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, detalpha);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);

What do i have to add for lighting?
And i would like to understand how this things work together but couldn't find any explanation of glTexEnv which covers also the GL_COMBINE_x and GL_SOURCEx_x parameters. Is there any good reference or tutorial?