Jump to content
  • Advertisement
Sign in to follow this  
SrivigneshwarRPrasad

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[font=Arial,]

Am new to OpenGL. Is it possible to wrap a single image on a terrain?[/font]

[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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!