michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set ts=8 sts=4 et sw=4 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "DecomposeIntoNoRepeatTriangles.h" michael@0: #include "gfxMatrix.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gl { michael@0: michael@0: void michael@0: RectTriangles::AppendRectToCoordArray(InfallibleTArray& array, michael@0: GLfloat x0, GLfloat y0, michael@0: GLfloat x1, GLfloat y1) michael@0: { michael@0: coord* v = array.AppendElements(6); michael@0: michael@0: v[0].x = x0; v[0].y = y0; michael@0: v[1].x = x1; v[1].y = y0; michael@0: v[2].x = x0; v[2].y = y1; michael@0: v[3].x = x0; v[3].y = y1; michael@0: v[4].x = x1; v[4].y = y0; michael@0: v[5].x = x1; v[5].y = y1; michael@0: } michael@0: michael@0: void michael@0: RectTriangles::addRect(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1, michael@0: GLfloat tx0, GLfloat ty0, GLfloat tx1, GLfloat ty1, michael@0: bool flip_y /* = false */) michael@0: { michael@0: if (flip_y) { michael@0: std::swap(ty0, ty1); michael@0: } michael@0: AppendRectToCoordArray(mVertexCoords, x0, y0, x1, y1); michael@0: AppendRectToCoordArray(mTexCoords, tx0, ty0, tx1, ty1); michael@0: } michael@0: michael@0: bool michael@0: RectTriangles::isSimpleQuad(gfx3DMatrix& aOutTextureTransform) const michael@0: { michael@0: if (mVertexCoords.Length() == 6 && michael@0: mVertexCoords[0].x == 0.0f && michael@0: mVertexCoords[0].y == 0.0f && michael@0: mVertexCoords[5].x == 1.0f && michael@0: mVertexCoords[5].y == 1.0f) michael@0: { michael@0: GLfloat tx0 = mTexCoords[0].x; michael@0: GLfloat ty0 = mTexCoords[0].y; michael@0: GLfloat tx1 = mTexCoords[5].x; michael@0: GLfloat ty1 = mTexCoords[5].y; michael@0: aOutTextureTransform = gfx3DMatrix::From2D(gfxMatrix(tx1 - tx0, 0, 0, ty1 - ty0, tx0, ty0)); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static GLfloat michael@0: WrapTexCoord(GLfloat v) michael@0: { michael@0: // fmodf gives negative results for negative numbers; michael@0: // that is, fmodf(0.75, 1.0) == 0.75, but michael@0: // fmodf(-0.75, 1.0) == -0.75. For the negative case, michael@0: // the result we need is 0.25, so we add 1.0f. michael@0: if (v < 0.0f) { michael@0: return 1.0f + fmodf(v, 1.0f); michael@0: } michael@0: michael@0: return fmodf(v, 1.0f); michael@0: } michael@0: michael@0: void michael@0: DecomposeIntoNoRepeatTriangles(const nsIntRect& aTexCoordRect, michael@0: const nsIntSize& aTexSize, michael@0: RectTriangles& aRects, michael@0: bool aFlipY /* = false */) michael@0: { michael@0: // normalize this michael@0: nsIntRect tcr(aTexCoordRect); michael@0: while (tcr.x >= aTexSize.width) michael@0: tcr.x -= aTexSize.width; michael@0: while (tcr.y >= aTexSize.height) michael@0: tcr.y -= aTexSize.height; michael@0: michael@0: // Compute top left and bottom right tex coordinates michael@0: GLfloat tl[2] = michael@0: { GLfloat(tcr.x) / GLfloat(aTexSize.width), michael@0: GLfloat(tcr.y) / GLfloat(aTexSize.height) }; michael@0: GLfloat br[2] = michael@0: { GLfloat(tcr.XMost()) / GLfloat(aTexSize.width), michael@0: GLfloat(tcr.YMost()) / GLfloat(aTexSize.height) }; michael@0: michael@0: // then check if we wrap in either the x or y axis; if we do, michael@0: // then also use fmod to figure out the "true" non-wrapping michael@0: // texture coordinates. michael@0: michael@0: bool xwrap = false, ywrap = false; michael@0: if (tcr.x < 0 || tcr.x > aTexSize.width || michael@0: tcr.XMost() < 0 || tcr.XMost() > aTexSize.width) michael@0: { michael@0: xwrap = true; michael@0: tl[0] = WrapTexCoord(tl[0]); michael@0: br[0] = WrapTexCoord(br[0]); michael@0: } michael@0: michael@0: if (tcr.y < 0 || tcr.y > aTexSize.height || michael@0: tcr.YMost() < 0 || tcr.YMost() > aTexSize.height) michael@0: { michael@0: ywrap = true; michael@0: tl[1] = WrapTexCoord(tl[1]); michael@0: br[1] = WrapTexCoord(br[1]); michael@0: } michael@0: michael@0: NS_ASSERTION(tl[0] >= 0.0f && tl[0] <= 1.0f && michael@0: tl[1] >= 0.0f && tl[1] <= 1.0f && michael@0: br[0] >= 0.0f && br[0] <= 1.0f && michael@0: br[1] >= 0.0f && br[1] <= 1.0f, michael@0: "Somehow generated invalid texture coordinates"); michael@0: michael@0: // If xwrap is false, the texture will be sampled from tl[0] michael@0: // .. br[0]. If xwrap is true, then it will be split into tl[0] michael@0: // .. 1.0, and 0.0 .. br[0]. Same for the Y axis. The michael@0: // destination rectangle is also split appropriately, according michael@0: // to the calculated xmid/ymid values. michael@0: michael@0: // There isn't a 1:1 mapping between tex coords and destination coords; michael@0: // when computing midpoints, we have to take that into account. We michael@0: // need to map the texture coords, which are (in the wrap case): michael@0: // |tl->1| and |0->br| to the |0->1| range of the vertex coords. So michael@0: // we have the length (1-tl)+(br) that needs to map into 0->1. michael@0: // These are only valid if there is wrap involved, they won't be used michael@0: // otherwise. michael@0: GLfloat xlen = (1.0f - tl[0]) + br[0]; michael@0: GLfloat ylen = (1.0f - tl[1]) + br[1]; michael@0: michael@0: NS_ASSERTION(!xwrap || xlen > 0.0f, "xlen isn't > 0, what's going on?"); michael@0: NS_ASSERTION(!ywrap || ylen > 0.0f, "ylen isn't > 0, what's going on?"); michael@0: NS_ASSERTION(aTexCoordRect.width <= aTexSize.width && michael@0: aTexCoordRect.height <= aTexSize.height, "tex coord rect would cause tiling!"); michael@0: michael@0: if (!xwrap && !ywrap) { michael@0: aRects.addRect(0.0f, 0.0f, michael@0: 1.0f, 1.0f, michael@0: tl[0], tl[1], michael@0: br[0], br[1], michael@0: aFlipY); michael@0: } else if (!xwrap && ywrap) { michael@0: GLfloat ymid = (1.0f - tl[1]) / ylen; michael@0: aRects.addRect(0.0f, 0.0f, michael@0: 1.0f, ymid, michael@0: tl[0], tl[1], michael@0: br[0], 1.0f, michael@0: aFlipY); michael@0: aRects.addRect(0.0f, ymid, michael@0: 1.0f, 1.0f, michael@0: tl[0], 0.0f, michael@0: br[0], br[1], michael@0: aFlipY); michael@0: } else if (xwrap && !ywrap) { michael@0: GLfloat xmid = (1.0f - tl[0]) / xlen; michael@0: aRects.addRect(0.0f, 0.0f, michael@0: xmid, 1.0f, michael@0: tl[0], tl[1], michael@0: 1.0f, br[1], michael@0: aFlipY); michael@0: aRects.addRect(xmid, 0.0f, michael@0: 1.0f, 1.0f, michael@0: 0.0f, tl[1], michael@0: br[0], br[1], michael@0: aFlipY); michael@0: } else { michael@0: GLfloat xmid = (1.0f - tl[0]) / xlen; michael@0: GLfloat ymid = (1.0f - tl[1]) / ylen; michael@0: aRects.addRect(0.0f, 0.0f, michael@0: xmid, ymid, michael@0: tl[0], tl[1], michael@0: 1.0f, 1.0f, michael@0: aFlipY); michael@0: aRects.addRect(xmid, 0.0f, michael@0: 1.0f, ymid, michael@0: 0.0f, tl[1], michael@0: br[0], 1.0f, michael@0: aFlipY); michael@0: aRects.addRect(0.0f, ymid, michael@0: xmid, 1.0f, michael@0: tl[0], 0.0f, michael@0: 1.0f, br[1], michael@0: aFlipY); michael@0: aRects.addRect(xmid, ymid, michael@0: 1.0f, 1.0f, michael@0: 0.0f, 0.0f, michael@0: br[0], br[1], michael@0: aFlipY); michael@0: } michael@0: } michael@0: michael@0: } michael@0: }