Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "pins.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tmathutil.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
Campbell Barton 3b0737
#include <cmath></cmath>
Campbell Barton 3b0737
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
e280ae
#ifdef _MSC_VER
Toshihiro Shimizu 890ddd
#define ISNAN _isnan
Toshihiro Shimizu 890ddd
#else
MichaƂ Janiszewski b86749
#define ISNAN std::isnan
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool lineIntersection(const TPointD &P, const TPointD &R, const TPointD &Q,
Shinya Kitaoka 120a6e
                      const TPointD &S, TPointD &ret);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool lineIntersection(const TPointD &P, const TPointD &R, const TPointD &Q,
Shinya Kitaoka 120a6e
                      const TPointD &S, TPointD &ret) {
Shinya Kitaoka 120a6e
  TPointD u = R - P;
Shinya Kitaoka 120a6e
  TPointD v = S - Q;
Shinya Kitaoka 120a6e
  double r;
Shinya Kitaoka 120a6e
  if (u.y * v.x - u.x * v.y != 0) {
Shinya Kitaoka 120a6e
    r = (P.x * u.y - Q.x * u.y + u.x * (Q.y - P.y)) / (u.y * v.x - u.x * v.y);
Shinya Kitaoka 120a6e
    assert(!ISNAN(r));
Shinya Kitaoka 120a6e
    ret = Q + v * r;
Shinya Kitaoka 120a6e
    assert(!ISNAN(ret.x) && !ISNAN(ret.y));
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    ret = P;
Shinya Kitaoka 120a6e
    assert(!ISNAN(ret.x) && !ISNAN(ret.y));
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef checkErrorsByGL
Shinya Kitaoka 120a6e
#define checkErrorsByGL                                                        \
Shinya Kitaoka 120a6e
  {                                                                            \
Shinya Kitaoka 120a6e
    GLenum err = glGetError();                                                 \
Shinya Kitaoka 120a6e
    assert(err != GL_INVALID_ENUM);                                            \
Shinya Kitaoka 120a6e
    assert(err != GL_INVALID_VALUE);                                           \
Shinya Kitaoka 120a6e
    assert(err != GL_INVALID_OPERATION);                                       \
Shinya Kitaoka 120a6e
    assert(err != GL_STACK_OVERFLOW);                                          \
Shinya Kitaoka 120a6e
    assert(err != GL_STACK_UNDERFLOW);                                         \
Shinya Kitaoka 120a6e
    assert(err != GL_OUT_OF_MEMORY);                                           \
Shinya Kitaoka 120a6e
    assert(err == GL_NO_ERROR);                                                \
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// ---------------------------------------- --------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void subCompute(TRasterFxPort &m_input, TTile &tile, double frame,
Shinya Kitaoka 120a6e
                const TRenderSettings &ri, TPointD p00, TPointD p01,
Shinya Kitaoka 120a6e
                TPointD p11, TPointD p10, int details, bool wireframe,
Shinya Kitaoka 120a6e
                TDimension m_offScreenSize, bool isCast) {
Shinya Kitaoka 120a6e
  TPixel32 bgColor;
Shinya Kitaoka 120a6e
  TRectD outBBox, inBBox;
Shinya Kitaoka 120a6e
  outBBox = inBBox = TRectD(tile.m_pos, TDimensionD(tile.getRaster()->getLx(),
Shinya Kitaoka 120a6e
                                                    tile.getRaster()->getLy()));
Shinya Kitaoka 120a6e
  m_input->getBBox(frame, inBBox, ri);
Shinya Kitaoka 120a6e
  if (inBBox == TConsts::infiniteRectD)  // e' uno zerario
Shinya Kitaoka 120a6e
    inBBox = outBBox;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int inBBoxLx = (int)inBBox.getLx() / ri.m_shrinkX;
Shinya Kitaoka 120a6e
  int inBBoxLy = (int)inBBox.getLy() / ri.m_shrinkY;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (inBBox.isEmpty()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (p00 == p01 && p00 == p10 && p00 == p11 &&
Shinya Kitaoka 120a6e
      !isCast)  // significa che non c'e' deformazione
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    m_input->compute(tile, frame, ri);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRaster32P rasIn;
Shinya Kitaoka 120a6e
  TPointD rasInPos;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!wireframe) {
Shinya Kitaoka 120a6e
    if (ri.m_bpp == 64 || ri.m_bpp == 48) {
Shinya Kitaoka 120a6e
      TRaster64P aux = TRaster64P(inBBoxLx, inBBoxLy);
Shinya Kitaoka 120a6e
      rasInPos = TPointD(inBBox.x0 / ri.m_shrinkX, inBBox.y0 / ri.m_shrinkY);
Shinya Kitaoka 120a6e
      TTile tmp(aux, rasInPos);
Shinya Kitaoka 120a6e
      m_input->compute(tmp, frame, ri);
Shinya Kitaoka 120a6e
      rasIn = TRaster32P(inBBoxLx, inBBoxLy);
Shinya Kitaoka 120a6e
      TRop::convert(rasIn, aux);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      rasInPos = TPointD(inBBox.x0 / ri.m_shrinkX, inBBox.y0 / ri.m_shrinkY);
Shinya Kitaoka 120a6e
      TTile tmp(TRaster32P(inBBoxLx, inBBoxLy), rasInPos);
Shinya Kitaoka 120a6e
      m_input->allocateAndCompute(tmp, rasInPos, TDimension(inBBoxLx, inBBoxLy),
Shinya Kitaoka 120a6e
                                  TRaster32P(), frame, ri);
Shinya Kitaoka 120a6e
      rasIn = tmp.getRaster();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  unsigned int texWidth  = 2;
Shinya Kitaoka 120a6e
  unsigned int texHeight = 2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (texWidth < (unsigned int)inBBoxLx) texWidth = texWidth << 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (texHeight < (unsigned int)inBBoxLy) texHeight = texHeight << 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (texWidth > 1024 || texHeight > 1024)  // avevo usato la costante
Shinya Kitaoka 120a6e
                                               // GL_MAX_TEXTURE_SIZE invece di
Shinya Kitaoka 120a6e
                                               // 1024, ma non funzionava!
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    inBBoxLx  = inBBoxLx >> 1;
Shinya Kitaoka 120a6e
    inBBoxLy  = inBBoxLy >> 1;
Shinya Kitaoka 120a6e
    texWidth  = texWidth >> 1;
Shinya Kitaoka 120a6e
    texHeight = texHeight >> 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (rasIn->getLx() != inBBoxLx || rasIn->getLy() != inBBoxLy) {
Shinya Kitaoka 120a6e
    TRaster32P rasOut = TRaster32P(inBBoxLx, inBBoxLy);
Shinya Kitaoka 120a6e
    TRop::resample(rasOut, rasIn,
Shinya Kitaoka 120a6e
                   TScale((double)rasOut->getLx() / rasIn->getLx(),
Shinya Kitaoka 120a6e
                          (double)rasOut->getLy() / rasIn->getLy()));
Shinya Kitaoka 120a6e
    rasIn = rasOut;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int rasterWidth  = tile.getRaster()->getLx() + 2;
Shinya Kitaoka 120a6e
  int rasterHeight = tile.getRaster()->getLy() + 2;
Shinya Kitaoka 120a6e
  assert(rasterWidth > 0);
Shinya Kitaoka 120a6e
  assert(rasterHeight > 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRectD clippingRect =
Shinya Kitaoka 120a6e
      TRectD(tile.m_pos,
Shinya Kitaoka 120a6e
             TDimensionD(tile.getRaster()->getLx(), tile.getRaster()->getLy()));
Campbell Barton d869b5
#ifdef CREATE_GL_CONTEXT_ONE_TIME
Shinya Kitaoka 120a6e
  int ret = wglMakeCurrent(m_offScreenGL.m_offDC, m_offScreenGL.m_hglRC);
Shinya Kitaoka 120a6e
  assert(ret == TRUE);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  TOfflineGL offScreenRendering(TDimension(rasterWidth, rasterHeight));
Shinya Kitaoka 120a6e
  //#ifdef _WIN32
Shinya Kitaoka 120a6e
  offScreenRendering.makeCurrent();
Toshihiro Shimizu 890ddd
//#else
Toshihiro Shimizu 890ddd
//#if defined(LINUX) || defined(MACOSX)
Toshihiro Shimizu 890ddd
// offScreenRendering.m_offlineGL->makeCurrent();
Toshihiro Shimizu 890ddd
//#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  checkErrorsByGL
Shinya Kitaoka 120a6e
      // disabilito quello che non mi serve per le texture
Shinya Kitaoka 120a6e
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
Shinya Kitaoka 120a6e
  glDisable(GL_DITHER);
Shinya Kitaoka 120a6e
  glDisable(GL_DEPTH_TEST);
Shinya Kitaoka 120a6e
  glCullFace(GL_FRONT);
Shinya Kitaoka 120a6e
  glDisable(GL_STENCIL_TEST);
Shinya Kitaoka 120a6e
  glDisable(GL_LOGIC_OP);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // creo la texture in base all'immagine originale
Shinya Kitaoka 120a6e
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
Shinya Kitaoka 120a6e
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
Shinya Kitaoka 120a6e
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Shinya Kitaoka 120a6e
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Shinya Kitaoka 120a6e
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  checkErrorsByGL
Campbell Barton d869b5
#ifndef CREATE_GL_CONTEXT_ONE_TIME
Shinya Kitaoka 120a6e
      TRaster32P rasaux;
Shinya Kitaoka 120a6e
  if (!wireframe) {
Shinya Kitaoka 120a6e
    TRaster32P texture(texWidth, texHeight);
Shinya Kitaoka 120a6e
    texture->clear();
Shinya Kitaoka 120a6e
    rasaux = texture;
Shinya Kitaoka 120a6e
    rasaux->lock();
Shinya Kitaoka 120a6e
    texture->copy(rasIn);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
Shinya Kitaoka 120a6e
    glTexImage2D(GL_TEXTURE_2D, 0, 4, texWidth, texHeight, 0, GL_RGBA,
Shinya Kitaoka 120a6e
                 GL_UNSIGNED_BYTE, texture->getRawData());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      unsigned int texWidth = 1024;
Shinya Kitaoka 120a6e
  unsigned int texHeight    = 1024;
Shinya Kitaoka 120a6e
  rasaux                    = rasIn;
Shinya Kitaoka 120a6e
  rasaux->lock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasIn->getLx(), rasIn->getLy(),
Shinya Kitaoka 120a6e
                  GL_RGBA, GL_UNSIGNED_BYTE, rasIn->getRawData());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  checkErrorsByGL
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      glEnable(GL_TEXTURE_2D);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // cfr. help: OpenGL/Programming tip/OpenGL Correctness Tips
Shinya Kitaoka 120a6e
  glMatrixMode(GL_PROJECTION);
Shinya Kitaoka 120a6e
  glLoadIdentity();
Shinya Kitaoka 120a6e
  glOrtho(-rasterWidth * 0.5, rasterWidth * 0.5, -rasterHeight * 0.5,
Shinya Kitaoka 120a6e
          rasterHeight * 0.5, -1, 1);
Shinya Kitaoka 120a6e
  glViewport(0, 0, rasterWidth, rasterHeight);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  glMatrixMode(GL_MODELVIEW);
Shinya Kitaoka 120a6e
  glLoadIdentity();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Shinya Kitaoka 120a6e
  glClear(GL_COLOR_BUFFER_BIT);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // do OpenGL draw
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double lwTex = (double)(inBBoxLx - 1) / (double)(texWidth - 1);
Shinya Kitaoka 120a6e
  double lhTex = (double)(inBBoxLy - 1) / (double)(texHeight - 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPointD tex00 = TPointD(0.0, 0.0);
Shinya Kitaoka 120a6e
  TPointD tex10 = TPointD(lwTex, 0.0);
Shinya Kitaoka 120a6e
  TPointD tex11 = TPointD(lwTex, lhTex);
Shinya Kitaoka 120a6e
  TPointD tex01 = TPointD(0.0, lhTex);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  GLenum polygonStyle;
Shinya Kitaoka 120a6e
  if (wireframe) {
Shinya Kitaoka 120a6e
    polygonStyle = GL_LINE;
Shinya Kitaoka 120a6e
    glDisable(GL_TEXTURE_2D);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    polygonStyle = GL_FILL;
Shinya Kitaoka 120a6e
  checkErrorsByGL p00.x /= ri.m_shrinkX;
Shinya Kitaoka 120a6e
  p00.y /= ri.m_shrinkY;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  p10.x /= ri.m_shrinkX;
Shinya Kitaoka 120a6e
  p10.y /= ri.m_shrinkY;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  p11.x /= ri.m_shrinkX;
Shinya Kitaoka 120a6e
  p11.y /= ri.m_shrinkY;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  p01.x /= ri.m_shrinkX;
Shinya Kitaoka 120a6e
  p01.y /= ri.m_shrinkY;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPointD translate = TPointD(tile.m_pos.x + tile.getRaster()->getLx() * 0.5,
Shinya Kitaoka 120a6e
                              tile.m_pos.y + tile.getRaster()->getLy() * 0.5);
Shinya Kitaoka 120a6e
  glTranslated(-translate.x, -translate.y, 0.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // disegno il poligono
Shinya Kitaoka 120a6e
  double dist_p00_p01                 = tdistance2(p00, p01);
Shinya Kitaoka 120a6e
  double dist_p10_p11                 = tdistance2(p10, p11);
Shinya Kitaoka 120a6e
  double dist_p01_p11                 = tdistance2(p01, p11);
Shinya Kitaoka 120a6e
  double dist_p00_p10                 = tdistance2(p00, p10);
Shinya Kitaoka 120a6e
  bool vertical                       = (dist_p00_p01 == dist_p10_p11);
Shinya Kitaoka 120a6e
  bool horizontal                     = (dist_p00_p10 == dist_p01_p11);
Shinya Kitaoka 120a6e
  if (vertical && horizontal) details = 1;
Shinya Kitaoka 120a6e
  glPolygonMode(GL_FRONT_AND_BACK, polygonStyle);
Shinya Kitaoka 120a6e
  subdivision(p00, p10, p11, p01, tex00, tex10, tex11, tex01, clippingRect,
Shinya Kitaoka 120a6e
              details);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!wireframe) {
Shinya Kitaoka 120a6e
    // abilito l'antialiasing delle linee
Shinya Kitaoka 120a6e
    glEnable(GL_LINE_SMOOTH);
Shinya Kitaoka 120a6e
    glEnable(GL_BLEND);
Shinya Kitaoka 120a6e
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Shinya Kitaoka 120a6e
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // disegno il bordo del poligono
Shinya Kitaoka 120a6e
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Shinya Kitaoka 120a6e
    glBegin(GL_QUADS);
Shinya Kitaoka 120a6e
    glTexCoord2d(tex00.x, tex00.y);
Shinya Kitaoka 120a6e
    tglVertex(p00);
Shinya Kitaoka 120a6e
    glTexCoord2d(tex10.x, tex10.y);
Shinya Kitaoka 120a6e
    tglVertex(p10);
Shinya Kitaoka 120a6e
    glTexCoord2d(tex11.x, tex11.y);
Shinya Kitaoka 120a6e
    tglVertex(p11);
Shinya Kitaoka 120a6e
    glTexCoord2d(tex01.x, tex01.y);
Shinya Kitaoka 120a6e
    tglVertex(p01);
Shinya Kitaoka 120a6e
    glEnd();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // disabilito l'antialiasing per le linee
Shinya Kitaoka 120a6e
    glDisable(GL_LINE_SMOOTH);
Shinya Kitaoka 120a6e
    glDisable(GL_BLEND);
Shinya Kitaoka 120a6e
    glDisableClientState(GL_VERTEX_ARRAY);
Shinya Kitaoka 120a6e
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Shinya Kitaoka 120a6e
    glDisable(GL_TEXTURE_2D);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // force to finish
Shinya Kitaoka 120a6e
  glFlush();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // rimetto il disegno dei poligoni a GL_FILL
Shinya Kitaoka 120a6e
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // metto il frame buffer nel raster del tile
Shinya Kitaoka 120a6e
  glPixelStorei(GL_UNPACK_ROW_LENGTH, rasterWidth);
Shinya Kitaoka 120a6e
  glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRaster32P newRas(tile.getRaster()->getLx(), tile.getRaster()->getLy());
Shinya Kitaoka 120a6e
  newRas->lock();
Shinya Kitaoka 120a6e
  glReadPixels(1, 1, newRas->getLx(), newRas->getLy(), GL_RGBA,
Shinya Kitaoka 120a6e
               GL_UNSIGNED_BYTE, (void *)newRas->getRawData());
Shinya Kitaoka 120a6e
  newRas->unlock();
Shinya Kitaoka 120a6e
  checkErrorsByGL
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      rasaux->unlock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tile.getRaster()->copy(newRas);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// ------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void subdivision(const TPointD &p00, const TPointD &p10, const TPointD &p11,
Shinya Kitaoka 120a6e
                 const TPointD &p01, const TPointD &tex00, const TPointD &tex10,
Shinya Kitaoka 120a6e
                 const TPointD &tex11, const TPointD &tex01,
Shinya Kitaoka 120a6e
                 const TRectD &clippingRect, int details) {
Shinya Kitaoka 120a6e
  if (details == 1) {
Shinya Kitaoka 120a6e
    glBegin(GL_QUADS);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    glTexCoord2d(tex00.x, tex00.y);
Shinya Kitaoka 120a6e
    tglVertex(p00);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    glTexCoord2d(tex10.x, tex10.y);
Shinya Kitaoka 120a6e
    tglVertex(p10);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    glTexCoord2d(tex11.x, tex11.y);
Shinya Kitaoka 120a6e
    tglVertex(p11);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    glTexCoord2d(tex01.x, tex01.y);
Shinya Kitaoka 120a6e
    tglVertex(p01);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    glEnd();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    TPointD A = p00;
Shinya Kitaoka 120a6e
    TPointD B = p10;
Shinya Kitaoka 120a6e
    TPointD C = p11;
Shinya Kitaoka 120a6e
    TPointD D = p01;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*
Shinya Kitaoka 120a6e
*     D                L2               C
Shinya Kitaoka 120a6e
*     +----------------+----------------+
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*  H1 +----------------+----------------+ H2
Shinya Kitaoka 120a6e
*     |                | M              |
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*     |                |                |
Shinya Kitaoka 120a6e
*     +----------------+----------------+
Shinya Kitaoka 120a6e
*     A                L1               B
Shinya Kitaoka 120a6e
*
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TPointD M, L1, L2, H1, H2, P1, P2;
Shinya Kitaoka 120a6e
    bool intersection;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // M
Shinya Kitaoka 120a6e
    intersection = lineIntersection(A, C, B, D, M);
Shinya Kitaoka 120a6e
    assert(intersection);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // P1 (punto di fuga)
Shinya Kitaoka 120a6e
    intersection = lineIntersection(D, C, A, B, P1);
Shinya Kitaoka 120a6e
    if (!intersection) {
Shinya Kitaoka 120a6e
      P1.x = 0.5 * (A.x + D.x);
Shinya Kitaoka 120a6e
      P1.y = 0.5 * (A.y + D.y);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // H1
Shinya Kitaoka 120a6e
    intersection = lineIntersection(A, D, P1, M, H1);
Shinya Kitaoka 120a6e
    assert(intersection);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // H2
Shinya Kitaoka 120a6e
    intersection = lineIntersection(B, C, P1, M, H2);
Shinya Kitaoka 120a6e
    assert(intersection);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // P2 (punto di fuga)
Shinya Kitaoka 120a6e
    intersection = lineIntersection(A, D, B, C, P2);
Shinya Kitaoka 120a6e
    if (!intersection) {
Shinya Kitaoka 120a6e
      P2.x = 0.5 * (A.x + B.x);
Shinya Kitaoka 120a6e
      P2.y = 0.5 * (A.y + B.y);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // L1
Shinya Kitaoka 120a6e
    intersection = lineIntersection(A, B, P2, M, L1);
Shinya Kitaoka 120a6e
    assert(intersection);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // L2
Shinya Kitaoka 120a6e
    intersection = lineIntersection(D, C, P2, M, L2);
Shinya Kitaoka 120a6e
    assert(intersection);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TPointD texA = (tex00 + tex10) * 0.5;
Shinya Kitaoka 120a6e
    TPointD texB = (tex10 + tex11) * 0.5;
Shinya Kitaoka 120a6e
    TPointD texC = (tex11 + tex01) * 0.5;
Shinya Kitaoka 120a6e
    TPointD texD = (tex01 + tex00) * 0.5;
Shinya Kitaoka 120a6e
    TPointD texM = (texA + texC) * 0.5;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    details--;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRectD r1 = TRectD(
Shinya Kitaoka 120a6e
        std::min({A.x, L1.x, M.x, H1.x}), std::min({A.y, L1.y, M.y, H1.y}),
Shinya Kitaoka 120a6e
        std::max({A.x, L1.x, M.x, H1.x}), std::max({A.y, L1.y, M.y, H1.y}));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRectD r2 = TRectD(
Shinya Kitaoka 120a6e
        std::min({L1.x, B.x, H2.x, M.x}), std::min({L1.y, B.y, H2.y, M.y}),
Shinya Kitaoka 120a6e
        std::max({L1.x, B.x, H2.x, M.x}), std::max({L1.y, B.y, H2.y, M.y}));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRectD r3 = TRectD(
Shinya Kitaoka 120a6e
        std::min({M.x, H2.x, C.x, L2.x}), std::min({M.y, H2.y, C.y, L2.y}),
Shinya Kitaoka 120a6e
        std::max({M.x, H2.x, C.x, L2.x}), std::max({M.y, H2.y, C.y, L2.y}));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRectD r4 = TRectD(
Shinya Kitaoka 120a6e
        std::min({H1.x, M.x, L2.x, D.x}), std::min({H1.y, M.y, L2.y, D.y}),
Shinya Kitaoka 120a6e
        std::max({H1.x, M.x, L2.x, D.x}), std::max({H1.y, M.y, L2.y, D.y}));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (r1.overlaps(clippingRect))
Shinya Kitaoka 120a6e
      subdivision(A, L1, M, H1, tex00, texA, texM, texD, clippingRect, details);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (r2.overlaps(clippingRect))
Shinya Kitaoka 120a6e
      subdivision(L1, B, H2, M, texA, tex10, texB, texM, clippingRect, details);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (r3.overlaps(clippingRect))
Shinya Kitaoka 120a6e
      subdivision(M, H2, C, L2, texM, texB, tex11, texC, clippingRect, details);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (r4.overlaps(clippingRect))
Shinya Kitaoka 120a6e
      subdivision(H1, M, L2, D, texD, texM, texC, tex01, clippingRect, details);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// ------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// ------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// ------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
#define TINY 1.0e-20
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static int splitMatrix(double **a, int n, int *index) {
Shinya Kitaoka 120a6e
  int i, imax = 0, j, k;
Shinya Kitaoka 120a6e
  double big, dum, sum, temp;
Shinya Kitaoka 120a6e
  double *vv, d;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  vv = new double[n];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  d = 1.00;
Shinya Kitaoka 120a6e
  for (i = 0; i < n; i++) {
Shinya Kitaoka 120a6e
    big = 0.0;
Shinya Kitaoka 120a6e
    for (j = 0; j < n; j++)
Shinya Kitaoka 120a6e
      if ((temp = fabs(a[i][j])) > big) big = temp;
Shinya Kitaoka 120a6e
    if (big == 0.0) {
Shinya Kitaoka 120a6e
      /*printf("aho, sta matrice e 'vota!!\n");*/
Shinya Kitaoka 120a6e
      return 0;
Shinya Kitaoka 120a6e
      // exit(0);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    vv[i] = 1.0 / big;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (j = 0; j < n; j++) {
Shinya Kitaoka 120a6e
    for (i = 0; i < j; i++) {
Shinya Kitaoka 120a6e
      sum = a[i][j];
Shinya Kitaoka 120a6e
      for (k  = 0; k < i; k++) sum -= a[i][k] * a[k][j];
Shinya Kitaoka 120a6e
      a[i][j] = sum;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    big = 0.0;
Shinya Kitaoka 120a6e
    for (i = j; i < n; i++) {
Shinya Kitaoka 120a6e
      sum = a[i][j];
Shinya Kitaoka 120a6e
      for (k  = 0; k < j; k++) sum -= a[i][k] * a[k][j];
Shinya Kitaoka 120a6e
      a[i][j] = sum;
Shinya Kitaoka 120a6e
      if ((dum = vv[i] * fabs(sum)) >= big) {
Shinya Kitaoka 120a6e
        big  = dum;
Shinya Kitaoka 120a6e
        imax = i;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (j != imax) {
Shinya Kitaoka 120a6e
      for (k = 0; k < n; k++) {
Shinya Kitaoka 120a6e
        dum        = a[imax][k];
Shinya Kitaoka 120a6e
        a[imax][k] = a[j][k];
Shinya Kitaoka 120a6e
        a[j][k]    = dum;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      d        = -d;
Shinya Kitaoka 120a6e
      vv[imax] = vv[j];
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    index[j] = imax;
Shinya Kitaoka 120a6e
    if (fabsf(a[j][j]) <= TINY && (j != n - 1)) {
Shinya Kitaoka 120a6e
      /*printf("Cazzo, E' singolare %f!\n", a[j][j] );*/
Shinya Kitaoka 120a6e
      return imax + 1;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (j != n - 1) {
Shinya Kitaoka 120a6e
      dum = 1.0 / a[j][j];
Shinya Kitaoka 120a6e
      for (i = j + 1; i < n; i++) a[i][j] *= dum;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Campbell Barton 87094d
  delete[] vv;
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
shun-iwasawa 27b0cf
static void buildMatrixes(const FourPoints &ss, const FourPoints &dd,
shun-iwasawa 27b0cf
                          double **a, double *b) {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  TPointD s[4], d[4];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  s[0] = ss.m_p00, s[1] = ss.m_p01, s[2] = ss.m_p10, s[3] = ss.m_p11;
Shinya Kitaoka 120a6e
  d[0] = dd.m_p00, d[1] = dd.m_p01, d[2] = dd.m_p10, d[3] = dd.m_p11;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < 4; i++) {
Shinya Kitaoka 120a6e
    a[i][0] = a[i + 4][3] = s[i].x;
Shinya Kitaoka 120a6e
    a[i][1] = a[i + 4][4] = s[i].y;
Shinya Kitaoka 120a6e
    a[i][2] = a[i + 4][5] = 1;
Shinya Kitaoka 120a6e
    a[i][3] = a[i + 4][0] = 0;
Shinya Kitaoka 120a6e
    a[i][4] = a[i + 4][1] = 0;
Shinya Kitaoka 120a6e
    a[i][5] = a[i + 4][2] = 0;
Shinya Kitaoka 120a6e
    a[i][6]               = -s[i].x * d[i].x;
Shinya Kitaoka 120a6e
    a[i + 4][6]           = -s[i].x * d[i].y;
Shinya Kitaoka 120a6e
    a[i][7]               = -s[i].y * d[i].x;
Shinya Kitaoka 120a6e
    a[i + 4][7]           = -s[i].y * d[i].y;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    b[i]     = d[i].x;
Shinya Kitaoka 120a6e
    b[i + 4] = d[i].y;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void computeSolutions(double **a, int *index, double *b) {
Shinya Kitaoka 120a6e
  int i, ii = 0, ip, j;
Shinya Kitaoka 120a6e
  double sum;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < 8; i++) {
Shinya Kitaoka 120a6e
    ip    = index[i];
Shinya Kitaoka 120a6e
    sum   = b[ip];
Shinya Kitaoka 120a6e
    b[ip] = b[i];
Shinya Kitaoka 120a6e
    if (ii)
Shinya Kitaoka 120a6e
      for (j = ii - 1; j <= i - 1; j++) sum -= a[i][j] * b[j];
Shinya Kitaoka 120a6e
    else if (sum)
Shinya Kitaoka 120a6e
      ii = i + 1;
Shinya Kitaoka 120a6e
    b[i] = sum;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  for (i = 7; i >= 0; i--) {
Shinya Kitaoka 120a6e
    sum = b[i];
Shinya Kitaoka 120a6e
    for (j = i + 1; j < 8; j++) sum -= a[i][j] * b[j];
Shinya Kitaoka 120a6e
    b[i]   = sum / a[i][i];
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void solveSystems(double **a, double *bx) {
Shinya Kitaoka 120a6e
  int index[255], i, count = 0, bad_line;
Shinya Kitaoka 120a6e
  double **atmp;
Shinya Kitaoka 120a6e
  int n = 8;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  atmp = new double *[n];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (i = 0; i < n; i++) {
Shinya Kitaoka 120a6e
    atmp[i] = new double[n];
Shinya Kitaoka 120a6e
    memcpy(atmp[i], a[i], n * sizeof(double));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  while ((bad_line = splitMatrix(atmp, n, index)) != 0 && n > 0) {
Shinya Kitaoka 120a6e
    /*printf("la riga %d fa schifo!\n", bad_line);*/
Shinya Kitaoka 120a6e
    /*bad_lines[count] = bad_line;*/
Shinya Kitaoka 120a6e
    for (i = bad_line - 1; i < n - 1; i++)
Shinya Kitaoka 120a6e
      memcpy(atmp[i], a[i + 1], n * sizeof(a[i + 1]));
Shinya Kitaoka 120a6e
    n--;
Shinya Kitaoka 120a6e
    count++;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (count == 0) computeSolutions(atmp, index, bx);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (i = 0; i < n; i++) delete atmp[i];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  delete atmp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void computeTransformation(const FourPoints &s, const FourPoints &d,
Campbell Barton 8c6c57
                                  TAffine &aff, TPointD &perspectDen) {
Shinya Kitaoka 120a6e
  double **a, *b;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  a = new double *[8];
Shinya Kitaoka 120a6e
  for (i = 0; i < 8; i++) a[i] = new double[8];
Shinya Kitaoka 120a6e
  b                            = new double[8];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  buildMatrixes(s, d, a, b);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  solveSystems(a, b);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  aff.a11       = b[0];
Shinya Kitaoka 120a6e
  aff.a12       = b[1];
Shinya Kitaoka 120a6e
  aff.a13       = b[2];
Shinya Kitaoka 120a6e
  aff.a21       = b[3];
Shinya Kitaoka 120a6e
  aff.a22       = b[4];
Shinya Kitaoka 120a6e
  aff.a23       = b[5];
Shinya Kitaoka 120a6e
  perspectDen.x = b[6];
Shinya Kitaoka 120a6e
  perspectDen.y = b[7];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < 8; i++) delete a[i];
Shinya Kitaoka 120a6e
  delete b;
Shinya Kitaoka 120a6e
  delete a;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
FourPoints computeTransformed(const FourPoints &pointsFrom,
Shinya Kitaoka 120a6e
                              const FourPoints &pointsTo,
Shinya Kitaoka 120a6e
                              const FourPoints &from) {
Shinya Kitaoka 120a6e
  TAffine aff;
Shinya Kitaoka 120a6e
  TPointD perspectiveDen;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  computeTransformation(pointsFrom, pointsTo, aff, perspectiveDen);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double den;
Shinya Kitaoka 120a6e
  FourPoints fp;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  den = perspectiveDen.x * from.m_p00.x + perspectiveDen.y * from.m_p00.y + 1;
Shinya Kitaoka 120a6e
  assert(den != 0);
Shinya Kitaoka 120a6e
  fp.m_p00 = (1.0 / den) * (aff * from.m_p00);
Shinya Kitaoka 120a6e
  den = perspectiveDen.x * from.m_p01.x + perspectiveDen.y * from.m_p01.y + 1;
Shinya Kitaoka 120a6e
  assert(den != 0);
Shinya Kitaoka 120a6e
  fp.m_p01 = (1.0 / den) * (aff * from.m_p01);
Shinya Kitaoka 120a6e
  den = perspectiveDen.x * from.m_p10.x + perspectiveDen.y * from.m_p10.y + 1;
Shinya Kitaoka 120a6e
  assert(den != 0);
Shinya Kitaoka 120a6e
  fp.m_p10 = (1.0 / den) * (aff * from.m_p10);
Shinya Kitaoka 120a6e
  den = perspectiveDen.x * from.m_p11.x + perspectiveDen.y * from.m_p11.y + 1;
Shinya Kitaoka 120a6e
  assert(den != 0);
Shinya Kitaoka 120a6e
  fp.m_p11 = (1.0 / den) * (aff * from.m_p11);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}