Esempio n. 1
0
void tglDrawSegment(const TPointD &p1, const TPointD &p2)
{
	glBegin(GL_LINES);
	tglVertex(p1);
	tglVertex(p2);
	glEnd();
}
Esempio n. 2
0
void tglFillRect(const TRectD &rect)
{
	glBegin(GL_POLYGON);
	tglVertex(rect.getP00());
	tglVertex(rect.getP10());
	tglVertex(rect.getP11());
	tglVertex(rect.getP01());
	glEnd();
}
Esempio n. 3
0
void tglDrawRect(const TRectD &rect)
{
	glBegin(GL_LINE_LOOP);
	tglVertex(rect.getP00());
	tglVertex(rect.getP10());
	tglVertex(rect.getP11());
	tglVertex(rect.getP01());
	glEnd();
}
void ToonzExt::OverallDesigner::draw(ToonzExt::StraightCornerDeformation *sd)
{
#ifdef _DEBUG
	glPushMatrix();
	{
		this->setPosition();
		glColor3d(0.0, 1.0, 1.0);
		glBegin(GL_LINE_STRIP);
		tglVertex(TPointD(0.0, 0.0));
		tglVertex(TPointD(8.0, 12.0));
		tglVertex(TPointD(16.0, 0.0));
		glEnd();
	}
	glPopMatrix();
#endif
}
void ShiftTraceTool::drawCurve() {
  if (m_curveStatus == NoCurve) return;
  double r = 4 * sqrt(tglGetPixelSize2());
  double u = getPixelSize();
  if (m_curveStatus == TwoPointsCurve) {
    TPixel32 color =
        m_highlightedGadget == 1000 ? TPixel32(200, 100, 100) : TPixel32::White;
    drawDot(m_p0, r, color);
    glColor3d(0.2, 0.2, 0.2);
    tglDrawSegment(m_p0, m_p1);
    drawDot(m_p1, r, TPixel32::Red);
  } else if (m_curveStatus == ThreePointsCurve) {
    TPixel32 color =
        m_highlightedGadget == 1000 ? TPixel32(200, 100, 100) : TPixel32::White;
    drawDot(m_p0, r, color);
    color =
        m_highlightedGadget == 1001 ? TPixel32(200, 100, 100) : TPixel32::White;
    drawDot(m_p1, r, color);

    glColor3d(0.2, 0.2, 0.2);

    TPointD center;
    if (circumCenter(center, m_p0, m_p1, m_p2)) {
      double radius = norm(center - m_p1);
      glBegin(GL_LINE_STRIP);
      int n = 100;
      for (int i = 0; i < n; i++) {
        double t  = (double)i / n;
        TPointD p = (1 - t) * m_p0 + t * m_p2;
        p         = center + radius * normalize(p - center);
        tglVertex(p);
      }
      for (int i = 0; i < n; i++) {
        double t  = (double)i / n;
        TPointD p = (1 - t) * m_p2 + t * m_p1;
        p         = center + radius * normalize(p - center);
        tglVertex(p);
      }
      glEnd();
    } else {
      tglDrawSegment(m_p0, m_p1);
    }
    color =
        m_highlightedGadget == 1002 ? TPixel32(200, 100, 100) : TPixel32::White;
    drawDot(m_p2, r, color);
  }
}
Esempio n. 6
0
void subCompute(TRasterFxPort &m_input, TTile &tile, double frame,
                const TRenderSettings &ri, TPointD p00, TPointD p01,
                TPointD p11, TPointD p10, int details, bool wireframe,
                TDimension m_offScreenSize, bool isCast) {
  TPixel32 bgColor;
  TRectD outBBox, inBBox;
  outBBox = inBBox = TRectD(tile.m_pos, TDimensionD(tile.getRaster()->getLx(),
                                                    tile.getRaster()->getLy()));
  m_input->getBBox(frame, inBBox, ri);
  if (inBBox == TConsts::infiniteRectD)  // e' uno zerario
    inBBox = outBBox;

  int inBBoxLx = (int)inBBox.getLx() / ri.m_shrinkX;
  int inBBoxLy = (int)inBBox.getLy() / ri.m_shrinkY;

  if (inBBox.isEmpty()) return;

  if (p00 == p01 && p00 == p10 && p00 == p11 &&
      !isCast)  // significa che non c'e' deformazione
  {
    m_input->compute(tile, frame, ri);
    return;
  }

  TRaster32P rasIn;
  TPointD rasInPos;

  if (!wireframe) {
    if (ri.m_bpp == 64 || ri.m_bpp == 48) {
      TRaster64P aux = TRaster64P(inBBoxLx, inBBoxLy);
      rasInPos = TPointD(inBBox.x0 / ri.m_shrinkX, inBBox.y0 / ri.m_shrinkY);
      TTile tmp(aux, rasInPos);
      m_input->compute(tmp, frame, ri);
      rasIn = TRaster32P(inBBoxLx, inBBoxLy);
      TRop::convert(rasIn, aux);
    } else {
      rasInPos = TPointD(inBBox.x0 / ri.m_shrinkX, inBBox.y0 / ri.m_shrinkY);
      TTile tmp(TRaster32P(inBBoxLx, inBBoxLy), rasInPos);
      m_input->allocateAndCompute(tmp, rasInPos, TDimension(inBBoxLx, inBBoxLy),
                                  TRaster32P(), frame, ri);
      rasIn = tmp.getRaster();
    }
  }

  unsigned int texWidth  = 2;
  unsigned int texHeight = 2;

  while (texWidth < (unsigned int)inBBoxLx) texWidth = texWidth << 1;

  while (texHeight < (unsigned int)inBBoxLy) texHeight = texHeight << 1;

  while (texWidth > 1024 || texHeight > 1024)  // avevo usato la costante
                                               // GL_MAX_TEXTURE_SIZE invece di
                                               // 1024, ma non funzionava!
  {
    inBBoxLx  = inBBoxLx >> 1;
    inBBoxLy  = inBBoxLy >> 1;
    texWidth  = texWidth >> 1;
    texHeight = texHeight >> 1;
  }

  if (rasIn->getLx() != inBBoxLx || rasIn->getLy() != inBBoxLy) {
    TRaster32P rasOut = TRaster32P(inBBoxLx, inBBoxLy);
    TRop::resample(rasOut, rasIn,
                   TScale((double)rasOut->getLx() / rasIn->getLx(),
                          (double)rasOut->getLy() / rasIn->getLy()));
    rasIn = rasOut;
  }

  int rasterWidth  = tile.getRaster()->getLx() + 2;
  int rasterHeight = tile.getRaster()->getLy() + 2;
  assert(rasterWidth > 0);
  assert(rasterHeight > 0);

  TRectD clippingRect =
      TRectD(tile.m_pos,
             TDimensionD(tile.getRaster()->getLx(), tile.getRaster()->getLy()));
#if CREATE_GL_CONTEXT_ONE_TIME
  int ret = wglMakeCurrent(m_offScreenGL.m_offDC, m_offScreenGL.m_hglRC);
  assert(ret == TRUE);
#else
  TOfflineGL offScreenRendering(TDimension(rasterWidth, rasterHeight));
  //#ifdef _WIN32
  offScreenRendering.makeCurrent();
//#else
//#if defined(LINUX) || defined(MACOSX)
// offScreenRendering.m_offlineGL->makeCurrent();
//#endif
#endif

  checkErrorsByGL
      // disabilito quello che non mi serve per le texture
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  glDisable(GL_DITHER);
  glDisable(GL_DEPTH_TEST);
  glCullFace(GL_FRONT);
  glDisable(GL_STENCIL_TEST);
  glDisable(GL_LOGIC_OP);

  // creo la texture in base all'immagine originale
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

  checkErrorsByGL
#if !CREATE_GL_CONTEXT_ONE_TIME
      TRaster32P rasaux;
  if (!wireframe) {
    TRaster32P texture(texWidth, texHeight);
    texture->clear();
    rasaux = texture;
    rasaux->lock();
    texture->copy(rasIn);

    glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
    glTexImage2D(GL_TEXTURE_2D, 0, 4, texWidth, texHeight, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, texture->getRawData());
  }
#else

      unsigned int texWidth = 1024;
  unsigned int texHeight    = 1024;
  rasaux                    = rasIn;
  rasaux->lock();

  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasIn->getLx(), rasIn->getLy(),
                  GL_RGBA, GL_UNSIGNED_BYTE, rasIn->getRawData());

#endif
  checkErrorsByGL

      glEnable(GL_TEXTURE_2D);

  // cfr. help: OpenGL/Programming tip/OpenGL Correctness Tips
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-rasterWidth * 0.5, rasterWidth * 0.5, -rasterHeight * 0.5,
          rasterHeight * 0.5, -1, 1);
  glViewport(0, 0, rasterWidth, rasterHeight);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT);

  // do OpenGL draw

  double lwTex = (double)(inBBoxLx - 1) / (double)(texWidth - 1);
  double lhTex = (double)(inBBoxLy - 1) / (double)(texHeight - 1);

  TPointD tex00 = TPointD(0.0, 0.0);
  TPointD tex10 = TPointD(lwTex, 0.0);
  TPointD tex11 = TPointD(lwTex, lhTex);
  TPointD tex01 = TPointD(0.0, lhTex);

  GLenum polygonStyle;
  if (wireframe) {
    polygonStyle = GL_LINE;
    glDisable(GL_TEXTURE_2D);
  } else
    polygonStyle = GL_FILL;
  checkErrorsByGL p00.x /= ri.m_shrinkX;
  p00.y /= ri.m_shrinkY;

  p10.x /= ri.m_shrinkX;
  p10.y /= ri.m_shrinkY;

  p11.x /= ri.m_shrinkX;
  p11.y /= ri.m_shrinkY;

  p01.x /= ri.m_shrinkX;
  p01.y /= ri.m_shrinkY;

  TPointD translate = TPointD(tile.m_pos.x + tile.getRaster()->getLx() * 0.5,
                              tile.m_pos.y + tile.getRaster()->getLy() * 0.5);
  glTranslated(-translate.x, -translate.y, 0.0);

  // disegno il poligono
  double dist_p00_p01                 = tdistance2(p00, p01);
  double dist_p10_p11                 = tdistance2(p10, p11);
  double dist_p01_p11                 = tdistance2(p01, p11);
  double dist_p00_p10                 = tdistance2(p00, p10);
  bool vertical                       = (dist_p00_p01 == dist_p10_p11);
  bool horizontal                     = (dist_p00_p10 == dist_p01_p11);
  if (vertical && horizontal) details = 1;
  glPolygonMode(GL_FRONT_AND_BACK, polygonStyle);
  subdivision(p00, p10, p11, p01, tex00, tex10, tex11, tex01, clippingRect,
              details);

  if (!wireframe) {
    // abilito l'antialiasing delle linee
    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

    // disegno il bordo del poligono
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glBegin(GL_QUADS);
    glTexCoord2d(tex00.x, tex00.y);
    tglVertex(p00);
    glTexCoord2d(tex10.x, tex10.y);
    tglVertex(p10);
    glTexCoord2d(tex11.x, tex11.y);
    tglVertex(p11);
    glTexCoord2d(tex01.x, tex01.y);
    tglVertex(p01);
    glEnd();

    // disabilito l'antialiasing per le linee
    glDisable(GL_LINE_SMOOTH);
    glDisable(GL_BLEND);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_TEXTURE_2D);
  }

  // force to finish
  glFlush();

  // rimetto il disegno dei poligoni a GL_FILL
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

  // metto il frame buffer nel raster del tile
  glPixelStorei(GL_UNPACK_ROW_LENGTH, rasterWidth);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

  TRaster32P newRas(tile.getRaster()->getLx(), tile.getRaster()->getLy());
  newRas->lock();
  glReadPixels(1, 1, newRas->getLx(), newRas->getLy(), GL_RGBA,
               GL_UNSIGNED_BYTE, (void *)newRas->getRawData());
  newRas->unlock();
  checkErrorsByGL

      rasaux->unlock();

  tile.getRaster()->copy(newRas);
}
Esempio n. 7
0
void subdivision(const TPointD &p00, const TPointD &p10, const TPointD &p11,
                 const TPointD &p01, const TPointD &tex00, const TPointD &tex10,
                 const TPointD &tex11, const TPointD &tex01,
                 const TRectD &clippingRect, int details) {
  if (details == 1) {
    glBegin(GL_QUADS);

    glTexCoord2d(tex00.x, tex00.y);
    tglVertex(p00);

    glTexCoord2d(tex10.x, tex10.y);
    tglVertex(p10);

    glTexCoord2d(tex11.x, tex11.y);
    tglVertex(p11);

    glTexCoord2d(tex01.x, tex01.y);
    tglVertex(p01);

    glEnd();
  } else {
    TPointD A = p00;
    TPointD B = p10;
    TPointD C = p11;
    TPointD D = p01;

    /*
*     D                L2               C
*     +----------------+----------------+
*     |                |                |
*     |                |                |
*     |                |                |
*     |                |                |
*     |                |                |
*  H1 +----------------+----------------+ H2
*     |                | M              |
*     |                |                |
*     |                |                |
*     |                |                |
*     |                |                |
*     +----------------+----------------+
*     A                L1               B
*
*/

    TPointD M, L1, L2, H1, H2, P1, P2;
    bool intersection;

    // M
    intersection = lineIntersection(A, C, B, D, M);
    assert(intersection);

    // P1 (punto di fuga)
    intersection = lineIntersection(D, C, A, B, P1);
    if (!intersection) {
      P1.x = 0.5 * (A.x + D.x);
      P1.y = 0.5 * (A.y + D.y);
    }
    // H1
    intersection = lineIntersection(A, D, P1, M, H1);
    assert(intersection);

    // H2
    intersection = lineIntersection(B, C, P1, M, H2);
    assert(intersection);

    // P2 (punto di fuga)
    intersection = lineIntersection(A, D, B, C, P2);
    if (!intersection) {
      P2.x = 0.5 * (A.x + B.x);
      P2.y = 0.5 * (A.y + B.y);
    }
    // L1
    intersection = lineIntersection(A, B, P2, M, L1);
    assert(intersection);

    // L2
    intersection = lineIntersection(D, C, P2, M, L2);
    assert(intersection);

    TPointD texA = (tex00 + tex10) * 0.5;
    TPointD texB = (tex10 + tex11) * 0.5;
    TPointD texC = (tex11 + tex01) * 0.5;
    TPointD texD = (tex01 + tex00) * 0.5;
    TPointD texM = (texA + texC) * 0.5;

    details--;

    TRectD r1 = TRectD(
        std::min({A.x, L1.x, M.x, H1.x}), std::min({A.y, L1.y, M.y, H1.y}),
        std::max({A.x, L1.x, M.x, H1.x}), std::max({A.y, L1.y, M.y, H1.y}));

    TRectD r2 = TRectD(
        std::min({L1.x, B.x, H2.x, M.x}), std::min({L1.y, B.y, H2.y, M.y}),
        std::max({L1.x, B.x, H2.x, M.x}), std::max({L1.y, B.y, H2.y, M.y}));

    TRectD r3 = TRectD(
        std::min({M.x, H2.x, C.x, L2.x}), std::min({M.y, H2.y, C.y, L2.y}),
        std::max({M.x, H2.x, C.x, L2.x}), std::max({M.y, H2.y, C.y, L2.y}));

    TRectD r4 = TRectD(
        std::min({H1.x, M.x, L2.x, D.x}), std::min({H1.y, M.y, L2.y, D.y}),
        std::max({H1.x, M.x, L2.x, D.x}), std::max({H1.y, M.y, L2.y, D.y}));

    if (r1.overlaps(clippingRect))
      subdivision(A, L1, M, H1, tex00, texA, texM, texD, clippingRect, details);

    if (r2.overlaps(clippingRect))
      subdivision(L1, B, H2, M, texA, tex10, texB, texM, clippingRect, details);

    if (r3.overlaps(clippingRect))
      subdivision(M, H2, C, L2, texM, texB, tex11, texC, clippingRect, details);

    if (r4.overlaps(clippingRect))
      subdivision(H1, M, L2, D, texD, texM, texC, tex01, clippingRect, details);
  }
}
Esempio n. 8
0
void tglDraw(const TRectD &rect, const TRaster32P &tex, bool blending)
{
	CHECK_ERRORS_BY_GL;
	glPushAttrib(GL_ALL_ATTRIB_BITS);
	if (blending) {
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	}

	unsigned int texWidth = 1;
	unsigned int texHeight = 1;

	while (texWidth < (unsigned int)tex->getLx())
		texWidth = texWidth << 1;

	while (texHeight < (unsigned int)tex->getLy())
		texHeight = texHeight << 1;

	double lwTex = 1.0;
	double lhTex = 1.0;

	TRaster32P texture;
	unsigned int texLx = (unsigned int)tex->getLx();
	unsigned int texLy = (unsigned int)tex->getLy();

	if (texWidth != texLx ||
		texHeight != texLy) {
		texture = TRaster32P(texWidth, texHeight);
		texture->fill(TPixel32(0, 0, 0, 0));
		texture->copy(tex);
		lwTex = (texLx) / (double)(texWidth);
		lhTex = (texLy) / (double)(texHeight);
		if (lwTex > 1.0)
			lwTex = 1.0;
		if (lhTex > 1.0)
			lhTex = 1.0;
	} else
		texture = tex;
	GLenum fmt =
#ifdef TNZ_MACHINE_CHANNEL_ORDER_BGRM
		GL_BGRA_EXT;
#elif TNZ_MACHINE_CHANNEL_ORDER_MBGR
		GL_ABGR_EXT;
#elif TNZ_MACHINE_CHANNEL_ORDER_RGBM
		GL_RGBA;
#elif TNZ_MACHINE_CHANNEL_ORDER_MRGB
		GL_BGRA;
#else
//   Error  PLATFORM NOT SUPPORTED
#error "unknown channel order!"
#endif

	// Generate a texture id and bind it.
	GLuint texId;
	glGenTextures(1, &texId);

	glBindTexture(GL_TEXTURE_2D, texId);

	glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->getWrap());

	texture->lock();
	glTexImage2D(GL_TEXTURE_2D,
				 0,
				 4,
				 texWidth,
				 texHeight,
				 0,
				 fmt,
#ifdef TNZ_MACHINE_CHANNEL_ORDER_MRGB
				 GL_UNSIGNED_INT_8_8_8_8_REV,
#else
				 GL_UNSIGNED_BYTE,
#endif
				 texture->getRawData());

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glEnable(GL_TEXTURE_2D);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	double rectLx = rect.getLx();
	double rectLy = rect.getLy();

	tglColor(TPixel32(0, 0, 0, 0));

	glPushMatrix();

	glTranslated(rect.x0, rect.y0, 0.0);
	glBegin(GL_POLYGON);

	glTexCoord2d(0, 0);
	tglVertex(TPointD(0.0, 0.0));

	glTexCoord2d(lwTex, 0);
	tglVertex(TPointD(rectLx, 0.0));

	glTexCoord2d(lwTex, lhTex);
	tglVertex(TPointD(rectLx, rectLy));

	glTexCoord2d(0, lhTex);
	tglVertex(TPointD(0.0, rectLy));

	glEnd();
	glDisable(GL_TEXTURE_2D);

	glPopMatrix();
	glPopAttrib();

	// Delete texture
	glDeleteTextures(1, &texId);

	texture->unlock();
}