简体   繁体   中英

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 ?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM