• Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

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

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1vikku_  Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 November 2011 - 09:35 PM

Am new to OpenGL. Is it possible to wrap a single image on a terrain?

The below code would generate image tile , but I want to add single tile on entire terrain.

`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();}`

### #2dpadam450  Members   -  Reputation: 537

Like
1Likes
Like

Posted 14 November 2011 - 12:19 AM

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.

### #3vikku_  Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 December 2011 - 06:37 PM

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..

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS