Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

liquidAir

Texture mapping problem

This topic is 5756 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

Well, I have a file which contains a list of triangle vertices (for a simple terrain) which I load and read into an array of a simple vertex structure. The way I render the triangles to the scene goes like so... glBegin(GL_TRIANGLES); for(i=0; i.x, g_pVertices[i].y, g_pVertices[i].z); glVertex3f(g_pVertices[i+1].x, g_pVertices[i+1].y, g_pVertices[i+1].z); glVertex3f(g_pVertices[i+2].x, g_pVertices[i+2].y, g_pVertices[i+2].z); } glEnd(); Now, I have a single texture that I want to map onto the whole terrain. How do I do this?

Share this post


Link to post
Share on other sites
Advertisement
im not an advanced user, but i think you should call the
"glTextCoord" four times with the edge vertex points as the parameters.

note: dont call it after each triangle, call it after each vertex point that represents the end of your plane, like so

gltextcoord . . . . . . . . gltextcoord
__________. . . . . . . .____________
__________. . . . . . . .____________
__________. . . . . . . .____________
__________. . . . . . . .____________
gltextcoord . . . . . . . . gltextcoord

i hope you understand, and this will solve your problem, but still im not advanced so this might not work...

[edited by - BigFatBaby on February 9, 2003 3:41:01 PM]

[edited by - BigFatBaby on February 9, 2003 3:41:42 PM]

Share this post


Link to post
Share on other sites
One solution is to use glTexGen to generate the texture coordinates like so:


            
// Enable texgen

glEnable( GL_TEXTURE_GEN_S );
glEnable( GL_TEXTURE_GEN_T );

// Set texgen mode

glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );

// Define planes for linear texgen in object space

float planeS[] = { 1.0f, 0.0f, 0.0f, 0.0f }; // Plane facing down x axis

float planeT[] = { 0.0f, 0.0f, 1.0f, 0.0f }; // Plane facing down z axis


// Set texgen planes

glTexGenfv( GL_S, GL_OBJECT_PLANE, planeS );
glTexGenfv( GL_T, GL_OBJECT_PLANE, planeT );

// Modify texture matrix so that texgen produces tex coordinates in

// the range 0-1 whilst taking terrain offset from origin into account

glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glScalef( 1.0f/yourTerrainWidth, 1.0f/yourTerrainLength, 1.0f );
glTranslatef( yourTerrainXOffset, yourTerrainZOffset, 0.0f );

glMatrixMode( GL_MODELVIEW );

glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, yourTexture );

// Your rendering code here - no need to specify texture coordinates!


// Disable tex coord generation

glDisable( GL_TEXTURE_GEN_S );
glDisable( GL_TEXTURE_GEN_T );

// Reset texture matrix

glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );




Alternatively you could simply generate the required texture coordinates after you have loaded in the terrain. This just requires a bit of linear scaling and is a trivial task if you understand how texture coordinates work - think about it .

[edited by - Blivvy on February 11, 2003 11:33:41 PM]

Share this post


Link to post
Share on other sites
Hey, thnx! It worked! But your code raised a set of problems for me. I dunno how to calculate the terrain''s x- and y- offsets and the width and length of the terrain. The terrain is stored as a list of vertices in a file. How can I do this?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!