Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Settings texture wrapping to repeat using sf::Renderer  (Read 3163 times)

0 Members and 1 Guest are viewing this topic.

Rubenknex

  • Newbie
  • *
  • Posts: 21
    • View Profile
Settings texture wrapping to repeat using sf::Renderer
« on: June 08, 2011, 07:16:08 pm »
I was just trying out some ways to generate a racing track from a few points, generating the vertices worked out right but I'm having some problems with the texture coordinates. I currently have a distance variable which keeps track of the current track distance and a textureSize variable which determines how much pixels of the track the texture should cover.

The code which calculates the uv coordinates:
Code: [Select]

float textureSize = 64.0f //The texture is 64x64 so this should display it at the real scale.

// The two sides of the point in the track.
leftVertex.texCoords = sf::Vector2f(0.0f, distance / textureSize);
rightVertex.texCoords = sf::Vector2f(1.0f, distance / textureSize);


I render the vertices like this:
Code: [Select]

renderer.SaveGLStates();
   
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_WRAP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_WRAP);
   
renderer.SetTexture(&m_Texture);
renderer.Begin(sf::Renderer::TriangleStrip);
    for (int i = 0; i < m_sideVertices.size(); i++)
    {
        renderer.AddVertex(m_sideVertices[i].position.x,
                           m_sideVertices[i].position.y,
                           m_sideVertices[i].texCoords.x,
                           m_sideVertices[i].texCoords.y,
                           sf::Color::White);
    }
renderer.End();

renderer.RestoreGLStates();


But it looks like this (red lines show where the vertices are):


As you can see the beginning of the track (the left side) is textured correctly because the v coordinates are within the 0-1 range, after that the texture gets stretched out.

So is setting the texture wrapping mode to GL_REPEAT not working or am I using the wrong texture coordinates?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Settings texture wrapping to repeat using sf::Renderer
« Reply #1 on: June 08, 2011, 08:08:36 pm »
GL_TEXTURE_WRAP is a per-texture mode, not a global one. Therefore you must set it while the texture is active (and you can do it once at init).

You don't even need to save the GL states.

Code: [Select]
sf::Image m_Texture;
m_Texture.LoadFromFile(...);

m_Texture.Bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Laurent Gomila - SFML developer

Rubenknex

  • Newbie
  • *
  • Posts: 21
    • View Profile
Settings texture wrapping to repeat using sf::Renderer
« Reply #2 on: June 08, 2011, 08:16:21 pm »
Ah, I didn't know that, but it works perfectly now  :) .

 

anything