Hey everyone,
is it possible to draw just a subsection of an image, instead of the whole image? The code I have so far was created using the information in several posts on these forums. This is my approach:
- do not use ofImage’s draw function
- instead, get the texture reference of it
- bind the texture reference and perform the draw using texture coords other than (0,0)-(1,1)
- unbind the texture again
My problem is that the texture is not drawn. Instead, I see a solid grey rectangle of the correct dimensions and at the correct location. Can anyone tell me what is wrong with the code, or if there are alternative (easier) solutions?
void testApp::draw(){
backgroundImage.draw(0,0);
// make sure the center of the panel can not go
// too far to the left or right
float x = ofClamp(mouseX, 0 + 0.5 * panelWidth, ofGetWidth() - 0.5 * panelWidth);
// determine the boundaries of the rectangle
float left = x - 0.5 * panelWidth;
float right = x + 0.5 * panelWidth;
// determine the texture coordinates of the rectangle
float tx_left = left / foregroundImage.width;
float tx_top = 0.0;
float tx_right = right / foregroundImage.width;
float tx_bottom = 1.0;
// get the texture data from the foregroundImage
ofTexture tex = foregroundImage.getTextureReference();
// now perform the draw yourself,
// using not the full texture, but part of it
tex.bind();
glPushMatrix();
glTranslatef(left, 0, 0);
glScalef(panelWidth, foregroundImage.height, 1);
glBegin(GL_QUADS);
glTexCoord2f(tx_left, tx_top);
glVertex3f(0, 0, 0);
glTexCoord2f(tx_right, tx_top);
glVertex3f(1, 0, 0);
glTexCoord2f(tx_right, tx_bottom);
glVertex3f(1, 1, 0);
glTexCoord2f(tx_left, tx_bottom);
glVertex3f(0, 1, 0);
glEnd();
glPopMatrix();
tex.unbind();
}