PDA

View Full Version : map a repeating texture to glucylinder(...)



m_code
06-04-2010, 11:59 AM
I want to map a texture to a cylinder created by glucylinder(...) api and I want my texture to be repeated through the cylinder height, and I use the following texture parameters to do this but again the texture get stretched and doesn't repeat in the height of cylinder what's my problem.
GLuint TextureID;
glGenTextures(1, &TextureID);

glBindTexture(GL_TEXTURE_2D, TextureID);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
nTxWidth,
nTxHeight,
GL_RGB,
GL_UNSIGNED_BYTE,
pColorBytes);

strattonbrazil
06-04-2010, 09:14 PM
I believe gluCylinder provides basic texture coordinates you can use, so you just need to make sure the texture is bound. After that, you can use glScale() while the matrix mode is set to glTexture. Scaling this matrix will affect the provided texture coordinates. You can also look at automatic texture coordinate generation if gluCylinder isn't cutting it for you.

m_code
06-04-2010, 10:10 PM
Thank you I think this would be my answer but would you please give a brief sample.

strattonbrazil
06-05-2010, 06:03 AM
Sure. First, just bind the texture like you normally would and get it appearing on the cylinder. I assume you've gotten that far. Regardless of how high the cylinder is, it probably is mapped to just fit the texture onto it 1x1. As you have it setup to repeat in T, you just need to scale the texture coordinates. The default matrix is the identity, which means vertices get what the unchanged UV coordinate.

glMatrixMode(GL_TEXTURE) // transformations like glTranslate, glScale, etc. now affect the texture matrix
glLoadIdentity() // just to clear out whatever is there
glScale(1,a,1) // scale the matrix by a units to stretch texture along cylinder

I believe that should be what you need.

m_code
06-09-2010, 12:38 AM
Thanks very usefull It solved my problem.