Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: DOH! Need help stapling texture to terrain map properly.

  1. #1
    Intern Contributor
    Join Date
    Mar 2001
    Posts
    56

    DOH! Need help stapling texture to terrain map properly.

    Ok, I have found no really good explanation of how to properly calculate texture coordinates for this problem ... mebbe someone can help me out.

    I have a variable size terrain whose dimensions as passed to glVertex3f are :

    X Axis : -xMinT to xMaxT
    Y Axis : -yMinT to yMaxT

    (Note Z is up in this system)

    So I want to staple the texture onto the terrain map with a 1:1 correspondence and rely on scaling the texture matrix to distort it at run time.

    My last effort went like this ...

    Compute the scaling functions ...
    Code :
    	// Compute function to map range [xMinT,xMaxT] onto [0,1]
    	m_TexCoordSlope[0] = 1.0f/float(xMaxT-xMinT);
    	m_TexCoordIntercept[0] = -float(xOffset)*m_TexCoordSlope[0];
     
    	// Compute function to map range [yMinT,yMaxT] onto [0,1]
    	m_TexCoordSlope[1] = 1.0f/float(yMaxT-yMinT);
    	m_TexCoordIntercept[1] = -float(yOffset)*m_TexCoordSlope[1];
    and my function to place a vertex and staple its texture coordinate ...
    Code :
    void TerrainRenderer: : placeVertex(float x, float y, float z)
    {
    	double z2 = z*m_ScaledElevationSlope+m_ScaledElevationIntercept;
    	setHeightColor(z);
    	glVertex3f(x,y,z2);
     
    	float xt = m_TexCoordSlope[0]*x + m_TexCoordIntercept[0];
    	float yt = m_TexCoordSlope[1]*y + m_TexCoordIntercept[1];
     
    	glTexCoord2f(xt,yt);
    }
    The code that places the vertex is ...
    Code :
    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    glScalef(m_TexStretch[0],m_TexStretch[1],1);
    glMatrixMode(GL_MODELVIEW);
     
    // ...
     
    placeVertex(terrainX, terrainY, terrainZ);
     
    // ...
     
    glMatrixMode(GL_TEXTURE);
    glPopmatrix();
    glMatrixMode(GL_MODEL_VIEW);
     
    // ...
    Well, I end up with a really nicely textured terrain but that texture is repeated over the terrain roughly 100 times in each direction when m_TexStretch[i] = 1. I want that case to result in the texture being stapled to the corners with a ONE TO ONE match of the texture to the terrain map.

    Does anyone see what I am doing wrong? Or, are the better ways to do this?

    Thanks in advance ...

    Ice out


    [This message has been edited by Iceman (edited 09-21-2002).]

  2. #2
    Intern Contributor
    Join Date
    Mar 2001
    Posts
    56

    Re: DOH! Need help stapling texture to terrain map properly.

    oops

    [This message has been edited by Iceman (edited 09-21-2002).]

  3. #3
    Intern Contributor
    Join Date
    Mar 2001
    Posts
    56

    Re: DOH! Need help stapling texture to terrain map properly.

    oops

    [This message has been edited by Iceman (edited 09-21-2002).]

  4. #4
    Intern Contributor
    Join Date
    Mar 2001
    Posts
    56

    Re: DOH! Need help stapling texture to terrain map properly.

    oops

  5. #5
    Super Moderator OpenGL Guru dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    4,388

    Re: DOH! Need help stapling texture to terrain map properly.

    How about for each vertex:

    s_tex = x_coord/terrain_x_size;
    t_tex = 1.0-z_coord/terrain_z_size;

    This assumes that the y axis points up.

    There's no need for all the texture matrix stuff.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •