Elfayer
Elfayer

Reputation: 4561

OpenGl - How to repeat a texture?

Here is what I have so far :

 Rectangle::Rectangle(int x, int y)
  {
    sizeX_ = x * CASE;
    sizeY_ = y * CASE;
    texture_ = gdl::Image::load("./ressources/floor.jpg");
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  }

  void  Rectangle::draw()
{                                                                          
    texture_.bind();
    glBegin(GL_QUADS);

    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 0.0f);

    glTexCoord2f(sizeX_ / CASE, 0.0f);
    glVertex3f(sizeX_, 0.0f, 0.0f);

    glTexCoord2f(0.0f, sizeY_ / CASE);
    glVertex3f(sizeX_, 0.0f, sizeY_);

    glTexCoord2f(sizeX_ / CASE, sizeY_ / CASE);
    glVertex3f(0.0f, 0.0f, sizeY_);

    glEnd();
  }

The constructor takes the size of the map in cases. For example : (10, 10). and the real size will be (10 * CASE), where CASE = 400.

But this is not repeating correctly the texture. The texture seems to be reduced (good point) and packed from the up left point to the bottom right point.

Am I doing something wrong ?

Upvotes: 0

Views: 2887

Answers (1)

Shahbaz
Shahbaz

Reputation: 47523

glTexParameter works on the currently bound texture. It doesn't globally set parameters. This means that you need to bind a texture before calling this function:

glBindTexture(GL_TEXTURE_2D, ...);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

Upvotes: 5

Related Questions