Basic Texture Mapping

Member
Posts: 153
Joined: 2004.12
Post: #1
I am new to this so please bare with me Smile

Im generating a terrain using NeHe's tutorial ( http://nehe.gamedev.net/data/lessons/les...?lesson=34 ) on height maps. Everything works great but i want to texture the terrain with something a little nicer then the blue he sets you up with.

My problem is the texture remaps on every quad (like id expect it to). Obviously i want the texture to (encompass/cover/skin) the entire height map not just one individual quad. I'v looked at some sample code that does just that and it doesn't look any different then mine. If someone could give me a nudge in the right direction it would be greatly appreciated.

Link To Image Of Problem

Thanks in advanced,
Joe.



My Current Code (Excluding loading, and setting up the texture) is:
Code:
glBindTexture( GL_TEXTURE_2D, texture[ 0 ] );
   glBegin( GL_QUADS );   // Render Polygons
   for( X = 0; X < MAP_SIZE; X += STEP_SIZE )
      for( Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )
      {

         x = X;
         y = [ self heightFrom:theHeightMap x:X y:Y ];
         z = Y;

      glTexCoord2f(0.0f, 0.0f);
         glVertex3i( x, y, z );

         x = X;                                                  
         y = [ self heightFrom:theHeightMap x:X y:Y + STEP_SIZE ];
         z = Y + STEP_SIZE;
        
         glTexCoord2f(0.0f, 1.0f);
         glVertex3i( x, y, z );   // Send This Vertex To OpenGL To Be Rendered

         x = X + STEP_SIZE;
         y = [ self heightFrom:theHeightMap x:X + STEP_SIZE y:Y + STEP_SIZE ];
         z = Y + STEP_SIZE;
        
      glTexCoord2f(1.0f, 1.0f);
         glVertex3i( x, y, z );   // Send This Vertex To OpenGL To Be Rendered

         x = X + STEP_SIZE;
         y = [ self heightFrom:theHeightMap x:X + STEP_SIZE y:Y ];
         z = Y;
        
      glTexCoord2f(1.0f, 0.0f);
         glVertex3i( x, y, z );   // Send This Vertex To OpenGL To Be Rendered
      }

   glEnd();

EDIT: ----------

i tried using if statements to call the glTexCoord2f only when the corosponding corner of the entire mesh was being displayed. so for example

if(X == 0 && Y == 0)
glTexCoord2f(0,0);

But that didn't work i only got a uniform green color.

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
Yeah, you have to set the corresponding texture coordinate for every vertex. So, for the calls to glTexCoord2f, you want (i * (1.0f / MAP_SIZE), j * (1.0f / MAP_SIZE)) (Make sure that MAP_SIZE is a float, too: something like 10.0f is needed.

For instance, if MAP_SIZE is 10.0f, the texcoords will be 0, 0.1, 0.2, 0.3, 0.4 and so on, which is what you want.
Quote this message in a reply
Member
Posts: 153
Joined: 2004.12
Post: #3
Awesome, thanks a lot.

Thats what i thought it would be, but the sample code in "openGL game programing" for terrain maps was almost identical to what i did, they certainly didn't 'scale' it like that. Weird.

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #4
Also, you can make texture coordinate generation a little simpler if you use automatic texture coordinate generation.

You can see it in my post from a few days ago:
http://www.idevgames.com/forum/showthread.php?t=7923

The relevant code is here:
Code:
glActiveTextureARB( GL_TEXTURE0_ARB );
        glEnable( GL_TEXTURE_2D );
        glBindTexture( GL_TEXTURE_2D, _primaryTextureID );

        glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
        glTexGenf( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
        glTexGenf( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
        glEnable( GL_TEXTURE_GEN_S );
        glEnable( GL_TEXTURE_GEN_T );

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 0.0f);
        
        glMatrixMode( GL_TEXTURE );
        glLoadIdentity();
        glScalef( _primaryTextureScale, _primaryTextureScale, 1.0f );
        glTranslatef( -_xyScaleOverTwo, -_xyScaleOverTwo, 0.0f );
        
        glMatrixMode( GL_MODELVIEW );

What it's doing is this: first it turns on tex coord generation for S & T in "object linear" space, which basically means the coordinates are generated in the object's space as opposed to eye space or some other transformation.

The important part are the glScalef and glTranslatef while the matrix mode is GL_TEXTURE. You can pass whatever values you want here, but if you want repeating texture you might pass something like ( 1 / numRepeats ) or for one-to-one correspondence you'de pass 1.0. The translation just moves the center of the texture to the middle of the terrain.

This approach is convenient if you're using vertex buffers since you don't have to pass/upload a texture coordinate array. And in my experience the performance is the same or better than passing texture coordinates per-vertex -- presumably because there's less back-n-forth with the video card.
Quote this message in a reply
Member
Posts: 153
Joined: 2004.12
Post: #5
Thanks for the tip il give it a try when i have a chance.

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Member
Posts: 153
Joined: 2004.12
Post: #6
Both ways worked but for whatever reason automatic texture generation looked nicer. When i explicitly used glVertexCoord i could see the quad squares 'behind' the texture. I probably just didnt do it right.

Thanks a lot for the help Smile

EDIT----

Just to make sure im doing this right. the equations:

float i = X * (1.0f / MAP_SIZE_FLOAT);
float j = Y * (1.0f / MAP_SIZE_FLOAT);

get me the texCoords of my lower left vertex? so if i render my quads from the LL going clockwise, my points would be.

LL = (i,j)
UL = (i,j+1/MAP_SIZE_FLOAT)
UR = (i+1/MAP_SIZE_FLOAT, j+1/MAP_SIZE_FLOAT)
LR = (i+1/MAP_SIZE_FLOAT, j)
?

I hate to bother you guys again it just pisses me off that it doesnt look as good Mad

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Mapping 2D texture to multiple GL_TRIANGLES ardowz 2 1,095 Mar 12, 2014 08:56 AM
Last Post: ardowz
  Texture mapping single 3D object with one draw call jeonghyunhan 1 2,774 Jul 13, 2009 06:05 AM
Last Post: ThemsAllTook
  Texture Mapping: Loading a texture from a .bmp file? ishrock 5 6,212 Dec 13, 2008 09:27 AM
Last Post: ThemsAllTook
  OpenGL and cylindrical texture mapping spsweet 0 5,102 May 1, 2007 02:55 PM
Last Post: spsweet
  Texture Basic Question(s)... WhatMeWorry 2 3,323 Feb 27, 2007 10:06 AM
Last Post: arekkusu