//------------------------------------ ofMesh ofTexture::getMeshForSubsection(float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh, bool vflipped, ofRectMode rectMode) const{ ofMesh quad; if(!texData.bAllocated){ return quad; } GLfloat px0 = x; // up to you to get the aspect ratio right GLfloat py0 = y; GLfloat px1 = w+x; GLfloat py1 = h+y; if (texData.bFlipTexture == vflipped){ swap(py0,py1); } // for rect mode center, let's do this: if (rectMode == OF_RECTMODE_CENTER){ px0 -= w/2; py0 -= h/2; px1 -= w/2; py1 -= h/2; } //we translate our drawing points by our anchor point. //we still respect ofRectMode so if you have rect mode set to //OF_RECTMODE_CENTER your anchor will be relative to that. GLfloat anchorX; GLfloat anchorY; if(bAnchorIsPct){ anchorX = anchor.x * w; anchorY = anchor.y * h; }else{ anchorX = anchor.x; anchorY = anchor.y; } px0 -= anchorX; py0 -= anchorY; px1 -= anchorX; py1 -= anchorY; // ------------------------------------------------- // complete hack to remove border artifacts. // slightly, slightly alters an image, scaling... // to remove the border. // we need a better solution for this, but // to constantly add a 2 pixel border on all uploaded images // is insane.. GLfloat offsetw = 0.0f; GLfloat offseth = 0.0f; if (!ofGLSupportsNPOTTextures() && bTexHackEnabled) { offsetw = 1.0f / (texData.tex_w); offseth = 1.0f / (texData.tex_h); } // ------------------------------------------------- ofPoint topLeft = getCoordFromPoint(sx, sy); ofPoint bottomRight = getCoordFromPoint(sx + sw, sy + sh); GLfloat tx0 = topLeft.x + offsetw; GLfloat ty0 = topLeft.y + offseth; GLfloat tx1 = bottomRight.x - offsetw; GLfloat ty1 = bottomRight.y - offseth; quad.setMode(OF_PRIMITIVE_TRIANGLE_FAN); quad.getVertices().resize(4); quad.getTexCoords().resize(4); quad.getVertices()[0].set(px0,py0,z); quad.getVertices()[1].set(px1,py0,z); quad.getVertices()[2].set(px1,py1,z); quad.getVertices()[3].set(px0,py1,z); quad.getTexCoords()[0].set(tx0,ty0); quad.getTexCoords()[1].set(tx1,ty0); quad.getTexCoords()[2].set(tx1,ty1); quad.getTexCoords()[3].set(tx0,ty1); return quad; }
//---------------------------------------------------------- void ofTexture::drawSubsection(float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) { if(!texData.bAllocated){ return; } GLfloat px0 = x; // up to you to get the aspect ratio right GLfloat py0 = y; GLfloat px1 = w+x; GLfloat py1 = h+y; if (texData.bFlipTexture == ofIsVFlipped()){ swap(py0,py1); } // for rect mode center, let's do this: if (ofGetRectMode() == OF_RECTMODE_CENTER){ px0 -= w/2; py0 -= h/2; px1 -= w/2; py1 -= h/2; } //we translate our drawing points by our anchor point. //we still respect ofRectMode so if you have rect mode set to //OF_RECTMODE_CENTER your anchor will be relative to that. GLfloat anchorX; GLfloat anchorY; if(bAnchorIsPct){ anchorX = anchor.x * w; anchorY = anchor.y * h; }else{ anchorX = anchor.x; anchorY = anchor.y; } px0 -= anchorX; py0 -= anchorY; px1 -= anchorX; py1 -= anchorY; // ------------------------------------------------- // complete hack to remove border artifacts. // slightly, slightly alters an image, scaling... // to remove the border. // we need a better solution for this, but // to constantly add a 2 pixel border on all uploaded images // is insane.. GLfloat offsetw = 0.0f; GLfloat offseth = 0.0f; if (!ofGLSupportsNPOTTextures() && bTexHackEnabled) { offsetw = 1.0f / (texData.tex_w); offseth = 1.0f / (texData.tex_h); } // ------------------------------------------------- ofPoint topLeft = getCoordFromPoint(sx, sy); ofPoint bottomRight = getCoordFromPoint(sx + sw, sy + sh); GLfloat tx0 = topLeft.x + offsetw; GLfloat ty0 = topLeft.y + offseth; GLfloat tx1 = bottomRight.x - offsetw; GLfloat ty1 = bottomRight.y - offseth; quad.getVertices()[0].set(px0,py0,z); quad.getVertices()[1].set(px1,py0,z); quad.getVertices()[2].set(px1,py1,z); quad.getVertices()[3].set(px0,py1,z); quad.getTexCoords()[0].set(tx0,ty0); quad.getTexCoords()[1].set(tx1,ty0); quad.getTexCoords()[2].set(tx1,ty1); quad.getTexCoords()[3].set(tx0,ty1); // make sure we are on unit 0 - we may change this when setting shader samplers // before glEnable or else the shader gets confused /// ps: maybe if bUsingArbTex is enabled we should use glActiveTextureARB? //glActiveTexture(GL_TEXTURE0); bool wasBound = texData.isBound; if(!wasBound) bind(0); quad.draw(); if(!wasBound) unbind(0); }