PDA

View Full Version : problem with glTexGen



FireCat12
06-23-2005, 07:51 AM
IM trying to use glTexGen to apply a 1D texture to a DEM based on the height of each pixel.

The function that creates the textures is


void DisplayWindow::create_land_texture(unsigned long res,
unsigned char start_red, unsigned char start_green, unsigned char start_blue,
unsigned char stop_red, unsigned char stop_green, unsigned char stop_blue)
{
// resize the buffer to make a 1d texure of the correct size
land_texture.resize(3*res);

unsigned long pos = 0;
double w1, w2;

typedef unsigned char byte;

for(unsigned long i = 0; i < res; ++i )
{
// get the weight factors
w2 = double(i) / (res-i);
w1 = 1.0 - w2;

land_texture[pos++] = byte(w1*start_red) + byte(w2*stop_red);
land_texture[pos++] = byte(w1*start_green) + byte(w2*stop_green);
land_texture[pos++] = byte(w1*start_blue) + byte(w2*stop_blue);

}

//check to see if there is allready a texture
if ( land_text == 0 )
{
glGenTextures(1,&amp;land_text);
}

glBindTexture(GL_TEXTURE_1D,land_text);


//create the new texture
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage1D(GL_TEXTURE_1D,0,GL_RGB,res,0,GL_RGB,G L_UNSIGNED_BYTE,&amp;land_texture[0]);
} The display code was like this

//draw other objects
if ( draw_surface &amp;&amp; flood_model.NumLandSurfaces())
{
if ( use_land_texture )
{
if ( make_land_texture )
{
this->create_land_texture(1024,
255,0,0,
255,255,0);
make_land_texture = false;
}

float length = flood_model.ZMax();
float scale = 1.0f / length;
float params[4] = {0,0,scale,0};

glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEA R);
glTexGenfv(GL_S,GL_OBJECT_PLANE,params);

//glMatrixMode(GL_TEXTURE);
//glLoadIdentity();
//glTranslatef(-flood_model.ZMin() / length, 0 , 0);
//glMatrixMode(GL_MODELVIEW);


}

if ( flood_model.LandNormals(0) ) glEnable(GL_LIGHTING);

if ( use_land_texture )
{
glBindTexture(GL_TEXTURE_1D,land_text);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_1D);
glColor3f(1.0f,1.0f,1.0f);
}
else
{
glColor3f(.7f,.7f,.7f);
}

for( unsigned long i = 0; i < flood_model.NumLandSurfaces(); ++i )
{
#ifdef _DEBUG_
std::cerr << "Drawing land surface\n";
#endif

flood_model.DrawLandSurface(i,max_display_size);
}

if ( use_land_texture )
{
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_1D);
}

if ( flood_model.LandNormals(0) ) glDisable(GL_LIGHTING);
}The resulting color mapping was inccorect so i tried to generate texture coordinates with the x coordinates. I changed scale to 1 / flood_model.XMax() and changed the params to {0,0,scale,0}. since all teh value of x in the model are between 0 and flood_model.XMax() the value of S should be between 0 and 1. However when i deisplayed the model i hade stips along the xvalues as if the texture had wrapped. In addition the strips were of varing width.

Anyone see what i missed.

T101
06-23-2005, 10:43 PM
OK, I admit it:
1. I haven't read all that code
2. I wonder why you would use X or Z as the source of the texture coordinate instead of Y (but then again, maybe that's because I didn't read all that code)

Usually (unless you rotate the camera), Y points upward, so that should be the source of the texgen.