PDA

View Full Version : 3D texture coordinates



Neon12
02-27-2011, 09:55 AM
Hi all,

I'm rendering a terrain heightfield with a 3D texture to blend various textures depending upon the height. The third texture coordinate is adjusted according to the height, resulting in snow, rock, grass and then dirt, from highest to lowest.

However, when I apply a third coordinate of close to 0.0 or 1.0, the texture gets slightly darker (see screenshots). I think I may be doing something slightly wrong, but I can't figure it out.

Here is my texture generation code:


GLuint num[1];
glEnable(GL_TEXTURE_3D);
glGenTextures(1, num);
glBindTexture(GL_TEXTURE_3D, num[0]);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexImage3D( GL_TEXTURE_3D, 0, bpp/8, w,h,d, 0, format, GL_UNSIGNED_BYTE, texture );
//bpp - bits per pixel
//w - width
//h - height
//d - depth (# of layers)
//format - either GL_BGR or GL_BGRA depending upon bpp
//textuer - pointer to pixel data

As you can see, I'm using GL_CLAMP for the third wrapping coordinate, because with GL_REPEAT the snow would wrap around to dirt, and vice versa. Interestingly, the dark area is in the place of the wrapped texture.
I would think coordinate 0.0 would map to the first layer, and 1.0 would map to the last layer. It seems that 1/layercount maps to layer 0, though. Is that the way it is supposed to be? I could just limit my coordinates to make the dark areas go away, but I was wondering if that was indeed the way it is designed to work.

Thanks,
Neon12

kyle_
02-27-2011, 10:15 AM
You probably want to use GL_CLAMP_TO_EDGE instead od GL_CLAMP.

When you are using GL_CLAMP with GL_LINEAR filter you blend first and last layer of texture with border color (which is all zeros by default). GL_CLAMP_TO_EDGE will effectively ignore border color (and this give you expected result).

Neon12
02-27-2011, 02:42 PM
Thanks for the fast reply.
GL_CLAMP_TO_EDGE works perfectly.

Neon12