views:

45

answers:

1

I have a texture with a 3x9 repeating section. I don't want to store the tesselated 1920x1080 image that I have for the texture, I'd much rather generate it in code so that it can be applied correctly at other resolutions. Any ideas on how I can do this? The original texture is here: http://img684.imageshack.us/img684/6282/matte1.png

I know that the texture is a non-power-of-2, so I have to do the repeating within a shader, which I do:

    uniform sampler2D tex;
    varying vec2 texCoord;

    void main() {
      gl_FragColor = texture2D(tex, mod(texCoord, vec2(3.0, 9.0)) * vec2(0.75, 0.5625));
    }

This is how I'm drawing the quad:

      glBegin(GL_QUADS)

      glColor4f(1.0, 1.0, 1.0, 1.0)
      glMultiTexCoord2f(GL_TEXTURE1, self.widgetPhysicalRect.topLeft().x(), self.widgetPhysicalRect.topLeft().y())
      glVertex2f(-1.0, 1.0)
      glMultiTexCoord2f(GL_TEXTURE1, self.widgetPhysicalRect.topRight().x(), self.widgetPhysicalRect.topRight().y())
      glVertex2f(1.0, 1.0)
      glMultiTexCoord2f(GL_TEXTURE1, self.widgetPhysicalRect.bottomRight().x(), self.widgetPhysicalRect.bottomRight().y())
      glVertex2f(1.0, -1.0)
      glMultiTexCoord2f(GL_TEXTURE1, self.widgetPhysicalRect.bottomLeft().x(), self.widgetPhysicalRect.bottomLeft().y())
      glVertex2f(-1.0, -1.0)

      glEnd()

Any ideas would be greatly appreciated.

Thanks!

+2  A: 

Almost like you'd do it with fixed pipe. Set your bound texture's wrap mode to repeat before setting sampler uniform and texture coordinates outside 0-1 range will repeat the texture.

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

In your case, texture coordinates will be s = 0-640 and t = 0-120. Fragment shader doesn't need anything special, just normal texture2D(tex, texCoord) will do.

Ivan Baldin