Jump to content

  • Log In with Google      Sign In   
  • Create Account

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.

  • You cannot reply to this topic
2 replies to this topic

#1 vikku_   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();}


Sponsor:

#2 dpadam450   Members   -  Reputation: 552

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.

#3 vikku_   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