Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgl.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "tcurves.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef __sgi
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka d4642c
#include <cstdlib></cstdlib>
Toshihiro Shimizu 890ddd
#include <gl glut.h=""></gl>
Rozhuk Ivan ac51ab
#elif defined(LINUX) || defined(FREEBSD)
Campbell Barton 107701
#include <gl glut.h=""></gl>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#include <glut glut.h=""></glut>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Rozhuk Ivan ac51ab
#if defined(MACOSX) || defined(LINUX) || defined(FREEBSD)
Toshihiro Shimizu 890ddd
#include <qglcontext></qglcontext>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "tthread.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef SCALE_BY_GLU
Toshihiro Shimizu 890ddd
//#undef NEW_DRAW_TEXT
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// GLUquadric*  localDisk=0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
  Find the number of slices in function of radius size.
Shinya Kitaoka 120a6e
  \par radius of circle
Toshihiro Shimizu 890ddd
  \par size of pixel
Toshihiro Shimizu 890ddd
  \ret number of division to obtain a circle
Toshihiro Shimizu 890ddd
  */
Shinya Kitaoka 120a6e
int computeSlices(double radius, double pixelSize = 1.0) {
Shinya Kitaoka 120a6e
  if (radius < 0) return 2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double thetaStep;
Shinya Kitaoka 120a6e
  double temp = pixelSize * 0.5 / radius;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (fabs(1.0 - temp) <= 1)
Shinya Kitaoka 120a6e
    thetaStep = acos(1.0 - temp);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    thetaStep = M_PI_4;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(thetaStep != 0.0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int numberOfSlices = (int)(M_2PI / thetaStep);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return numberOfSlices != 0 ? numberOfSlices : 2;
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
}  // end of unnamed namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double tglGetPixelSize2() {
Shinya Kitaoka 120a6e
  double mat[16];
Shinya Kitaoka 120a6e
  glMatrixMode(GL_MODELVIEW);
Shinya Kitaoka 120a6e
  glGetDoublev(GL_MODELVIEW_MATRIX, mat);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double det                      = fabs(mat[0] * mat[5] - mat[1] * mat[4]);
Shinya Kitaoka 120a6e
  if (det < TConsts::epsilon) det = TConsts::epsilon;
Shinya Kitaoka 120a6e
  return 1.0 / det;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double tglGetTextWidth(const std::string &s, void *font) {
Shinya Kitaoka 120a6e
  double factor = 0.07;
Shinya Kitaoka 120a6e
  double w      = 0;
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)s.length(); i++) w += glutStrokeWidth(font, s[i]);
Shinya Kitaoka 120a6e
  return w * factor;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDrawText(const TPointD &p, const std::string &s, void *character) {
Toshihiro Shimizu 890ddd
#ifndef __sgi
Shinya Kitaoka 120a6e
  glPushMatrix();
Shinya Kitaoka 120a6e
  glTranslated(p.x, p.y, 0);
Shinya Kitaoka 120a6e
  double factor = 0.07;
Shinya Kitaoka 120a6e
  glScaled(factor, factor, factor);
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)s.size(); i++) glutStrokeCharacter(character, s[i]);
Shinya Kitaoka 120a6e
  glPopMatrix();
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  assert("Not Yet Implemented" && 0);
Shinya Kitaoka 120a6e
  std::cout << s << std::endl;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDrawText(const TPointD &p, const std::wstring &s, void *character) {
Toshihiro Shimizu 890ddd
#ifndef __sgi
Shinya Kitaoka 120a6e
  glPushMatrix();
Shinya Kitaoka 120a6e
  glTranslated(p.x, p.y, 0);
Shinya Kitaoka 120a6e
  double factor = 0.07;
Shinya Kitaoka 120a6e
  glScaled(factor, factor, factor);
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)s.size(); i++) glutStrokeCharacter(character, s[i]);
Shinya Kitaoka 120a6e
  glPopMatrix();
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  assert("Not Yet Implemented" && 0);
Shinya Kitaoka 120a6e
  std::cout << s << std::endl;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDrawSegment(const TPointD &p1, const TPointD &p2) {
Shinya Kitaoka 120a6e
  glBegin(GL_LINES);
Shinya Kitaoka 120a6e
  tglVertex(p1);
Shinya Kitaoka 120a6e
  tglVertex(p2);
Shinya Kitaoka 120a6e
  glEnd();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDrawCircle(const TPointD ¢er, double radius) {
Shinya Kitaoka 120a6e
  if (radius <= 0) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double pixelSize = 1;
Shinya Kitaoka 120a6e
  int slices       = 60;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (slices <= 0) slices = computeSlices(radius, pixelSize) >> 1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double step  = M_PI / slices;
Shinya Kitaoka 120a6e
  double step2 = 2.0 * step;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double cos_t, sin_t, cos_ts, sin_ts, t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glPushMatrix();
Shinya Kitaoka 120a6e
  glTranslated(center.x, center.y, 0.0);
Shinya Kitaoka 120a6e
  glBegin(GL_LINES);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  cos_t = radius /* *1.0*/;
Shinya Kitaoka 120a6e
  sin_t = 0.0;
Shinya Kitaoka 120a6e
  for (t = 0; t + step < M_PI_2; t += step2) {
Shinya Kitaoka 120a6e
    cos_ts = radius * cos(t + step);
Shinya Kitaoka 120a6e
    sin_ts = radius * sin(t + step);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    glVertex2f(cos_t, sin_t);
Shinya Kitaoka 120a6e
    glVertex2f(cos_ts, sin_ts);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    glVertex2f(-cos_t, sin_t);
Shinya Kitaoka 120a6e
    glVertex2f(-cos_ts, sin_ts);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    glVertex2f(-cos_t, -sin_t);
Shinya Kitaoka 120a6e
    glVertex2f(-cos_ts, -sin_ts);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    glVertex2f(cos_t, -sin_t);
Shinya Kitaoka 120a6e
    glVertex2f(cos_ts, -sin_ts);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    cos_t = cos_ts;
Shinya Kitaoka 120a6e
    sin_t = sin_ts;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  cos_ts = 0.0;
Shinya Kitaoka 120a6e
  sin_ts = radius /* *1.0*/;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glVertex2f(cos_t, sin_t);
Shinya Kitaoka 120a6e
  glVertex2f(cos_ts, sin_ts);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glVertex2f(-cos_t, sin_t);
Shinya Kitaoka 120a6e
  glVertex2f(-cos_ts, sin_ts);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glVertex2f(-cos_t, -sin_t);
Shinya Kitaoka 120a6e
  glVertex2f(-cos_ts, -sin_ts);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glVertex2f(cos_t, -sin_t);
Shinya Kitaoka 120a6e
  glVertex2f(cos_ts, -sin_ts);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glEnd();
Shinya Kitaoka 120a6e
  glPopMatrix();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDrawDisk(const TPointD &c, double r) {
Shinya Kitaoka 120a6e
  if (r <= 0) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double pixelSize = 1;
Shinya Kitaoka 120a6e
  int slices       = 60;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (slices <= 0) slices = computeSlices(r, pixelSize) >> 1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glPushMatrix();
Shinya Kitaoka 120a6e
  glTranslated(c.x, c.y, 0.0);
Shinya Kitaoka 120a6e
  GLUquadric *quadric = gluNewQuadric();
Shinya Kitaoka 120a6e
  gluDisk(quadric, 0, r, slices, 1);
Shinya Kitaoka 120a6e
  gluDeleteQuadric(quadric);
Shinya Kitaoka 120a6e
  glPopMatrix();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDrawRect(const TRectD &rect) {
Shinya Kitaoka 120a6e
  glBegin(GL_LINE_LOOP);
Shinya Kitaoka 120a6e
  tglVertex(rect.getP00());
Shinya Kitaoka 120a6e
  tglVertex(rect.getP10());
Shinya Kitaoka 120a6e
  tglVertex(rect.getP11());
Shinya Kitaoka 120a6e
  tglVertex(rect.getP01());
Shinya Kitaoka 120a6e
  glEnd();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglFillRect(const TRectD &rect) {
Shinya Kitaoka 120a6e
  glBegin(GL_POLYGON);
Shinya Kitaoka 120a6e
  tglVertex(rect.getP00());
Shinya Kitaoka 120a6e
  tglVertex(rect.getP10());
Shinya Kitaoka 120a6e
  tglVertex(rect.getP11());
Shinya Kitaoka 120a6e
  tglVertex(rect.getP01());
Shinya Kitaoka 120a6e
  glEnd();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglRgbOnlyColorMask() {
Shinya Kitaoka 120a6e
  tglMultColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
Shinya Kitaoka 120a6e
  tglEnableBlending(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglAlphaOnlyColorMask() {
Shinya Kitaoka 120a6e
  tglMultColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
Shinya Kitaoka 120a6e
  tglEnableBlending(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglEnableBlending(GLenum src, GLenum dst) {
Shinya Kitaoka 120a6e
  glEnable(GL_BLEND);
Shinya Kitaoka 120a6e
  glBlendFunc(src, dst);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglEnableLineSmooth(bool enable, double lineSize) {
Shinya Kitaoka 120a6e
  if (enable) {
Shinya Kitaoka 120a6e
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
Shinya Kitaoka 120a6e
    glEnable(GL_LINE_SMOOTH);
Shinya Kitaoka 120a6e
    glLineWidth(lineSize);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    glDisable(GL_LINE_SMOOTH);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglEnablePointSmooth(double pointSize) {
Shinya Kitaoka 120a6e
  glEnable(GL_BLEND);
Shinya Kitaoka 120a6e
  glPointSize(pointSize);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglGetColorMask(GLboolean &red, GLboolean &green, GLboolean &blue,
Shinya Kitaoka 120a6e
                     GLboolean &alpha) {
Shinya Kitaoka 120a6e
  GLboolean channels[4];
Shinya Kitaoka 120a6e
  glGetBooleanv(GL_COLOR_WRITEMASK, &channels[0]);
Shinya Kitaoka 120a6e
  red = channels[0], green = channels[1], blue = channels[2],
Shinya Kitaoka 120a6e
  alpha = channels[3];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglMultColorMask(GLboolean red, GLboolean green, GLboolean blue,
Shinya Kitaoka 120a6e
                      GLboolean alpha) {
Shinya Kitaoka 120a6e
  GLboolean channels[4];
Shinya Kitaoka 120a6e
  glGetBooleanv(GL_COLOR_WRITEMASK, &channels[0]);
Shinya Kitaoka 120a6e
  glColorMask(red && channels[0], green && channels[1], blue && channels[2],
Shinya Kitaoka 120a6e
              alpha && channels[3]);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
//============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class GlFontManager {
Shinya Kitaoka 120a6e
  GlFontManager();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ~GlFontManager();
Shinya Kitaoka 120a6e
  static GlFontManager *instance();
Shinya Kitaoka 120a6e
  bool setFont(void *font = GLUT_BITMAP_TIMES_ROMAN_10);
Shinya Kitaoka 120a6e
  void drawText(/*const TRectD bBox,*/
Shinya Kitaoka 120a6e
                std::wstring wtext /*,
Toshihiro Shimizu 890ddd
                TDimensionD scale = TDimensionD(1.0, 1.0)*/);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  static GlFontManager *m_instance;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //       font    font_height
Shinya Kitaoka 120a6e
  //         |       |
Shinya Kitaoka 120a6e
  std::map<void *,="" double=""> m_fonts;</void>
Shinya Kitaoka 120a6e
  std::vector<trectd> m_charsBBox;</trectd>
Shinya Kitaoka 120a6e
  void *m_currentFont;
Shinya Kitaoka 120a6e
  TRaster32P m_fontTexture;
Shinya Kitaoka 120a6e
  GLuint m_base;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
GlFontManager *GlFontManager::m_instance = 0L;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
GlFontManager::GlFontManager() : m_currentFont(0L), m_base(0) {
Shinya Kitaoka 120a6e
  m_fonts.insert(std::make_pair(GLUT_BITMAP_8_BY_13, 13.0));
Shinya Kitaoka 120a6e
  m_fonts.insert(std::make_pair(GLUT_BITMAP_9_BY_15, 15.0));
Shinya Kitaoka 120a6e
  m_fonts.insert(std::make_pair(GLUT_BITMAP_TIMES_ROMAN_10, 10.0));
Shinya Kitaoka 120a6e
  m_fonts.insert(std::make_pair(GLUT_BITMAP_TIMES_ROMAN_24, 24.0));
Shinya Kitaoka 120a6e
  m_fonts.insert(std::make_pair(GLUT_BITMAP_HELVETICA_10, 10.0));
Shinya Kitaoka 120a6e
  m_fonts.insert(std::make_pair(GLUT_BITMAP_HELVETICA_12, 12.0));
Shinya Kitaoka 120a6e
  m_fonts.insert(std::make_pair(GLUT_BITMAP_HELVETICA_18, 18.0));
Shinya Kitaoka 120a6e
  bool ok = setFont();
Shinya Kitaoka 120a6e
  assert(ok);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
GlFontManager::~GlFontManager() { m_instance = 0L; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
GlFontManager *GlFontManager::instance() {
Shinya Kitaoka 120a6e
  if (!m_instance) m_instance = new GlFontManager();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return m_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool GlFontManager::setFont(void *font) {
Shinya Kitaoka 120a6e
  // cerca il font scelto nella mappa dei fonts conosciuti
Shinya Kitaoka 120a6e
  std::map<void *,="" double="">::iterator it = m_fonts.find(font);</void>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // se e' stato trovato
Shinya Kitaoka 120a6e
  if (it != m_fonts.end()) {
Shinya Kitaoka 120a6e
    m_currentFont = font;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    glPushAttrib(GL_ALL_ATTRIB_BITS);
Shinya Kitaoka 120a6e
    glPushMatrix();
Shinya Kitaoka 120a6e
    m_base = glGenLists(256);
Shinya Kitaoka 120a6e
    glListBase(m_base);
Shinya Kitaoka 120a6e
    int i = 0;
Shinya Kitaoka 120a6e
    for (; i < 256; ++i) {
Shinya Kitaoka 120a6e
      glNewList(m_base + i, GL_COMPILE);
Shinya Kitaoka 120a6e
      glutStrokeCharacter(GLUT_STROKE_ROMAN, i);
Shinya Kitaoka 120a6e
      // glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, i);
Shinya Kitaoka 120a6e
      glEndList();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    glPopAttrib();
Shinya Kitaoka 120a6e
    glPopMatrix();
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void GlFontManager::drawText(/*const TRectD bBox,*/
Shinya Kitaoka 120a6e
                             std::wstring wtext /*,
Shinya Kitaoka 120a6e
                             TDimensionD scale*/) {
Shinya Kitaoka 120a6e
  if (!m_currentFont) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string text       = ::to_string(wtext);
Shinya Kitaoka 120a6e
  const char *textString = text.c_str();
Shinya Kitaoka 120a6e
  glListBase(m_base);
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
glPushMatrix();
Shinya Kitaoka 120a6e
glTranslated(bBox.x0, bBox.y0, 0.0);
Shinya Kitaoka 120a6e
glScaled(scale.lx*0.07, scale.ly*0.07, 1.0);
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  glCallLists((GLuint)strlen(textString), GL_BYTE, textString);
Shinya Kitaoka 120a6e
  /*glPopMatrix();*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // anonymous namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDraw(const TCubic &cubic, int precision, GLenum pointOrLine) {
Shinya Kitaoka 120a6e
  CHECK_ERRORS_BY_GL;
Shinya Kitaoka 120a6e
  assert(pointOrLine == GL_POINT || pointOrLine == GL_LINE);
Shinya Kitaoka 120a6e
  float ctrlPts[4][3];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ctrlPts[0][0] = cubic.getP0().x;
Shinya Kitaoka 120a6e
  ctrlPts[0][1] = cubic.getP0().y;
Shinya Kitaoka 120a6e
  ctrlPts[0][2] = 0.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ctrlPts[1][0] = cubic.getP1().x;
Shinya Kitaoka 120a6e
  ctrlPts[1][1] = cubic.getP1().y;
Shinya Kitaoka 120a6e
  ctrlPts[1][2] = 0.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ctrlPts[2][0] = cubic.getP2().x;
Shinya Kitaoka 120a6e
  ctrlPts[2][1] = cubic.getP2().y;
Shinya Kitaoka 120a6e
  ctrlPts[2][2] = 0.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ctrlPts[3][0] = cubic.getP3().x;
Shinya Kitaoka 120a6e
  ctrlPts[3][1] = cubic.getP3().y;
Shinya Kitaoka 120a6e
  ctrlPts[3][2] = 0.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  CHECK_ERRORS_BY_GL;
Shinya Kitaoka 120a6e
  glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlPts[0][0]);
Shinya Kitaoka 120a6e
  CHECK_ERRORS_BY_GL;
Shinya Kitaoka 120a6e
  glEnable(GL_MAP1_VERTEX_3);
Shinya Kitaoka 120a6e
  CHECK_ERRORS_BY_GL;
Shinya Kitaoka 120a6e
  glMapGrid1f(precision, 0.0, 1.0);
Shinya Kitaoka 120a6e
  CHECK_ERRORS_BY_GL;
Shinya Kitaoka 120a6e
  glEvalMesh1(pointOrLine, 0, precision);
Shinya Kitaoka 120a6e
  CHECK_ERRORS_BY_GL;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDraw(const TRectD &rect, const std::vector<traster32p> &textures,</traster32p>
Shinya Kitaoka 120a6e
             bool blending) {
Shinya Kitaoka 120a6e
  double pixelSize2 = tglGetPixelSize2();
Shinya Kitaoka 120a6e
  // level e' la minore potenza di 2 maggiore di sqrt(pixelSize2)
Shinya Kitaoka 120a6e
  unsigned int level = 1;
Shinya Kitaoka 120a6e
  while (pixelSize2 * level * level <= 1.0) level <<= 1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  unsigned int texturesCount       = (int)textures.size();
Shinya Kitaoka 120a6e
  if (level > texturesCount) level = texturesCount;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  level = texturesCount - level;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  tglDraw(rect, textures[level], blending);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDraw(const TRectD &rect, const TRaster32P &tex, bool blending) {
Shinya Kitaoka 120a6e
  CHECK_ERRORS_BY_GL;
Shinya Kitaoka 120a6e
  glPushAttrib(GL_ALL_ATTRIB_BITS);
Shinya Kitaoka 120a6e
  if (blending) {
Shinya Kitaoka 120a6e
    glEnable(GL_BLEND);
Shinya Kitaoka 120a6e
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  unsigned int texWidth  = 1;
Shinya Kitaoka 120a6e
  unsigned int texHeight = 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (texWidth < (unsigned int)tex->getLx()) texWidth = texWidth << 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (texHeight < (unsigned int)tex->getLy()) texHeight = texHeight << 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double lwTex = 1.0;
Shinya Kitaoka 120a6e
  double lhTex = 1.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRaster32P texture;
Shinya Kitaoka 120a6e
  unsigned int texLx = (unsigned int)tex->getLx();
Shinya Kitaoka 120a6e
  unsigned int texLy = (unsigned int)tex->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (texWidth != texLx || texHeight != texLy) {
Shinya Kitaoka 120a6e
    texture = TRaster32P(texWidth, texHeight);
Shinya Kitaoka 120a6e
    texture->fill(TPixel32(0, 0, 0, 0));
Shinya Kitaoka 120a6e
    texture->copy(tex);
Shinya Kitaoka 120a6e
    lwTex                  = (texLx) / (double)(texWidth);
Shinya Kitaoka 120a6e
    lhTex                  = (texLy) / (double)(texHeight);
Shinya Kitaoka 120a6e
    if (lwTex > 1.0) lwTex = 1.0;
Shinya Kitaoka 120a6e
    if (lhTex > 1.0) lhTex = 1.0;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    texture = tex;
Shinya Kitaoka 120a6e
  GLenum fmt =
Campbell Barton d869b5
#if defined(TNZ_MACHINE_CHANNEL_ORDER_BGRM)
Shinya Kitaoka 120a6e
      GL_BGRA_EXT;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_MBGR)
Shinya Kitaoka 120a6e
      GL_ABGR_EXT;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_RGBM)
Shinya Kitaoka 120a6e
      GL_RGBA;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_MRGB)
Shinya Kitaoka 120a6e
      GL_BGRA;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
//   Error  PLATFORM NOT SUPPORTED
Campbell Barton 42d601
#error "unknown channel order!"
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Generate a texture id and bind it.
Shinya Kitaoka 120a6e
  GLuint texId;
Shinya Kitaoka 120a6e
  glGenTextures(1, &texId);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glBindTexture(GL_TEXTURE_2D, texId);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->getWrap());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  texture->lock();
Shinya Kitaoka 120a6e
  glTexImage2D(GL_TEXTURE_2D, 0, 4, texWidth, texHeight, 0, fmt,
Toshihiro Shimizu 890ddd
#ifdef TNZ_MACHINE_CHANNEL_ORDER_MRGB
Shinya Kitaoka 120a6e
               GL_UNSIGNED_INT_8_8_8_8_REV,
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
               GL_UNSIGNED_BYTE,
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
               texture->getRawData());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
Shinya Kitaoka 120a6e
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Shinya Kitaoka 120a6e
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Shinya Kitaoka 120a6e
  glEnable(GL_TEXTURE_2D);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double rectLx = rect.getLx();
Shinya Kitaoka 120a6e
  double rectLy = rect.getLy();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  tglColor(TPixel32(0, 0, 0, 0));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glPushMatrix();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTranslated(rect.x0, rect.y0, 0.0);
Shinya Kitaoka 120a6e
  glBegin(GL_POLYGON);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTexCoord2d(0, 0);
Shinya Kitaoka 120a6e
  tglVertex(TPointD(0.0, 0.0));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTexCoord2d(lwTex, 0);
Shinya Kitaoka 120a6e
  tglVertex(TPointD(rectLx, 0.0));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTexCoord2d(lwTex, lhTex);
Shinya Kitaoka 120a6e
  tglVertex(TPointD(rectLx, rectLy));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glTexCoord2d(0, lhTex);
Shinya Kitaoka 120a6e
  tglVertex(TPointD(0.0, rectLy));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glEnd();
Shinya Kitaoka 120a6e
  glDisable(GL_TEXTURE_2D);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glPopMatrix();
Shinya Kitaoka 120a6e
  glPopAttrib();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Delete texture
Shinya Kitaoka 120a6e
  glDeleteTextures(1, &texId);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  texture->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void tglBuildMipmaps(std::vector<traster32p> &rasters,</traster32p>
Shinya Kitaoka 120a6e
                     const TFilePath &filepath) {
Shinya Kitaoka 120a6e
  assert(rasters.size() > 0);
Shinya Kitaoka 120a6e
  TRop::ResampleFilterType resampleFilter = TRop::ClosestPixel;
Shinya Kitaoka 120a6e
  TRasterP ras;
Shinya Kitaoka 120a6e
  TImageReader::load(filepath, ras);
Shinya Kitaoka 120a6e
  int rasLx = ras->getLx();
Shinya Kitaoka 120a6e
  int rasLy = ras->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int lx = 1;
Shinya Kitaoka 120a6e
  while (lx < rasLx) lx <<= 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int ly = 1;
Shinya Kitaoka 120a6e
  while (ly < rasLy) ly <<= 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRaster32P ras2(lx, ly);
Shinya Kitaoka 120a6e
  double sx = (double)lx / (double)ras->getLx();
Shinya Kitaoka 120a6e
  double sy = (double)ly / (double)ras->getLy();
Toshihiro Shimizu 890ddd
#ifndef SCALE_BY_GLU
Shinya Kitaoka 120a6e
  TRop::resample(ras2, ras, TScale(sx, sy), resampleFilter);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  ras->lock();
Shinya Kitaoka 120a6e
  gluScaleImage(GL_RGBA, ras->getLx(), ras->getLy(), GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
                ras->getRawData(), lx, ly, GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
                ras2->getRawData());
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  rasters[0] = ras2;
Shinya Kitaoka 120a6e
  int ras2Lx = ras2->getLx();
Shinya Kitaoka 120a6e
  int ras2Ly = ras2->getLy();
Shinya Kitaoka 120a6e
  for (int i = 1; i < (int)rasters.size(); ++i) {
Shinya Kitaoka 120a6e
    lx >>= 1;
Shinya Kitaoka 120a6e
    ly >>= 1;
Shinya Kitaoka 120a6e
    if (lx < 1) lx = 1;
Shinya Kitaoka 120a6e
    if (ly < 1) ly = 1;
Shinya Kitaoka 120a6e
    rasters[i]     = TRaster32P(lx, ly);
Shinya Kitaoka 120a6e
    sx             = (double)lx / (double)ras2Lx;
Shinya Kitaoka 120a6e
    sy             = (double)ly / (double)ras2Ly;
Shinya Kitaoka 120a6e
    rasters[i]     = TRaster32P(lx, ly);
Toshihiro Shimizu 890ddd
#ifndef SCALE_BY_GLU
Shinya Kitaoka 120a6e
    TRop::resample(rasters[i], ras2, TScale(sx, sy), resampleFilter);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
    ras2->lock();
Shinya Kitaoka 120a6e
    gluScaleImage(GL_RGBA, ras->getLx(), ras->getLy(), GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
                  ras2->getRawData(), lx, ly, GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
                  rasters[i]->getRawData());
Shinya Kitaoka 120a6e
    ras2->unlock();
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
// Forse si potrebbe togliere l'ifdef ed usare QT
Shinya Kitaoka 9f5a1b
#if defined(_WIN32)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TGlContext tglGetCurrentContext() {
Shinya Kitaoka 120a6e
  return std::make_pair(wglGetCurrentDC(), wglGetCurrentContext());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglMakeCurrent(TGlContext context) {
Shinya Kitaoka 120a6e
  wglMakeCurrent(context.first, context.second);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDoneCurrent(TGlContext) { wglMakeCurrent(NULL, NULL); }
Toshihiro Shimizu 890ddd
Rozhuk Ivan ac51ab
#elif defined(LINUX) || defined(FREEBSD) || defined(__sgi) || defined(MACOSX)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TGlContext tglGetCurrentContext() {
Shinya Kitaoka 120a6e
  return reinterpret_cast<tglcontext>(</tglcontext>
Shinya Kitaoka 120a6e
      const_cast<qglcontext *="">(QGLContext::currentContext()));</qglcontext>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // (Daniele) I'm not sure why QGLContext::currentContext() returns
Shinya Kitaoka 120a6e
  // const. I think it shouldn't, and guess (hope) this is safe...
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglMakeCurrent(TGlContext context) {
Shinya Kitaoka 120a6e
  if (context)
Shinya Kitaoka 120a6e
    reinterpret_cast<qglcontext *="">(context)->makeCurrent();</qglcontext>
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    tglDoneCurrent(tglGetCurrentContext());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tglDoneCurrent(TGlContext context) {
Shinya Kitaoka 120a6e
  if (context) reinterpret_cast<qglcontext *="">(context)->doneCurrent();</qglcontext>
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Campbell Barton 42d601
#else
Campbell Barton 42d601
#error "unknown platform!"
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------