# OpenGL How to add a single texture on a terrain in OpenGL?

This topic is 2566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

[font=Arial,]

## The below code would generate image tile , but I want to add single tile on entire terrain.[/font] glBindTexture(GL_TEXTURE_2D, land);for (int z = 0; z < MAP_Z-1; z++){ glBegin(GL_TRIANGLE_STRIP); for (int x = 0; x < MAP_X-1; x++) { glTexCoord2f(0.0f, 0.0f); glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]); glTexCoord2f(1.0f, 0.0f); glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]); glTexCoord2f(0.0f, 1.0f); glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]); glTexCoord2f(1.0f, 1.0f); glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]); } glEnd();}

##### Share on other sites
Well your code is assigning a tile to each quad, what do you expect?

//you can swap z and x here, doesn't matter that much.
glTexCoord2f(float(z)/float(MAP_Z), float(x)/float(MAP_X));

If z loops up to MAP_Z, then z/MAP_Z = 1
if z is halfway to MAP_Z, then z/MAP_Z = .5
if z = 0, then z/MAP_Z = 0

So they start at 0 for the first vertex, and then at the last one they are 1. Everything in between is then scaled accordingly.

##### Share on other sites

Well your code is assigning a tile to each quad, what do you expect?

//you can swap z and x here, doesn't matter that much.
glTexCoord2f(float(z)/float(MAP_Z), float(x)/float(MAP_X));

If z loops up to MAP_Z, then z/MAP_Z = 1
if z is halfway to MAP_Z, then z/MAP_Z = .5
if z = 0, then z/MAP_Z = 0

So they start at 0 for the first vertex, and then at the last one they are 1. Everything in between is then scaled accordingly.

Thank you it worked..

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633691
• Total Posts
3013341
×