How to tile a texture?

Hi,

Can’t get more basic than that. I’m trying to tile a texture to cover the entire screen:

//in h file:
ofImage bg;

//in setup:
bg.load("bg.png");
bg.getTexture().setTextureWrap(GL_REPEAT, GL_REPEAT);

//in draw
bg.draw(ofGetWindowRect());

The texture is being stretched and not wrapped. What am I missing here?

Thanks in advance,

Tal

Hi,

bg.draw(ofGetWindowRect());

ask OF to draw the image streched to the entire screen.

Found in the forum:

The solution is here:

In your case:

ofImage bgImage;
ofPlanePrimitive bgPlane;

void ofApp::setup(){

    ofDisableArbTex();
    bgImage.loadImage("image.jpg");
    bgImage.getTextureReference().setTextureWrap( GL_REPEAT, GL_REPEAT );
    bgPlane.set( ofGetWidth(), ofGetHeight(), 2, 2 );
    bgPlane.setPosition( ofGetWidth() * 0.5f, ofGetHeight() * 0.5f, 0.f );
    bgPlane.mapTexCoords( 0, 0, ofGetWidth() / (float)bgImage.width, ofGetHeight() / (float)bgImage.height );

}

void ofApp::draw(){

    bgImage.bind();
    bgPlane.draw();
    bgImage.unbind();

}

src.zip (1.2 KB)

to tile a texture you need texture coordinates further than the maximum size of the orignal image (or 1 if using normalized tex coordinates) the default draw for a texture will always generate a rectangle with the maximum tex coordinates.

//.h
ofVboMesh quad;

//setup
quad.addVertex(0,0);
quad.addTexCoord(0,0);
quad.addVertex(ofGetWidth(),0);
quad.addTexCoord(ofGetWidth(),0);
quad.addVertex(ofGetWidth(),ofGetHeight());
quad.addTexCoord(ofGetWidth(),ofGetHeight());
quad.addVertex(0,ofGetHeight());
quad.addTexCoord(0,ofGetHeight());
quad.setMode(OF_PRIMITIVE_TRIANGLE_FAN);

//draw
texture.bind();
quad.draw();
texture.unbind();
2 Likes

Thanks!