Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef USE_MESA
Toshihiro Shimizu 890ddd
#include <gl osmesa.h=""></gl>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "tstrokeutil.h"
Toshihiro Shimizu 890ddd
#include "tmathutil.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgl.h"
Toshihiro Shimizu 890ddd
#include "tcurves.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef __sgi
Toshihiro Shimizu 890ddd
#include <cstdio></cstdio>
Toshihiro Shimizu 890ddd
#include <cstdlib></cstdlib>
Toshihiro Shimizu 890ddd
#include <cstring></cstring>
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(LINUX) || defined(__sgi)
Toshihiro Shimizu 890ddd
#include <gl glx.h=""></gl>
Toshihiro Shimizu 890ddd
#include <x11 xlib.h=""></x11>
Toshihiro Shimizu 890ddd
#include <x11 xutil.h=""></x11>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef USE_MESA
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
extern "C" int GLAPIENTRY wglChoosePixelFormat(HDC,
Shinya Kitaoka 120a6e
                                               const PIXELFORMATDESCRIPTOR *);
Shinya Kitaoka 120a6e
extern "C" int GLAPIENTRY wglDescribePixelFormat(HDC, int, unsigned int,
Shinya Kitaoka 120a6e
                                                 LPPIXELFORMATDESCRIPTOR);
Shinya Kitaoka 120a6e
extern "C" int GLAPIENTRY wglSetPixelFormat(HDC, int,
Shinya Kitaoka 120a6e
                                            const PIXELFORMATDESCRIPTOR *);
Toshihiro Shimizu 890ddd
extern "C" int GLAPIENTRY wglSwapBuffers(HDC);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define ChoosePixelFormat wglChoosePixelFormat
Toshihiro Shimizu 890ddd
#define SetPixelFormat wglSetPixelFormat
Toshihiro Shimizu 890ddd
#define SwapBuffers wglSwapBuffers
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// init a BITMAPINFO structure
Shinya Kitaoka 120a6e
void initBITMAPINFO(BITMAPINFO &info, const TRasterP img) {
Shinya Kitaoka 120a6e
  memset(&info, 0, sizeof(BITMAPINFOHEADER));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  info.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
Shinya Kitaoka 120a6e
  info.bmiHeader.biWidth         = img->getLx();
Shinya Kitaoka 120a6e
  info.bmiHeader.biHeight        = img->getLy();
Shinya Kitaoka 120a6e
  info.bmiHeader.biPlanes        = 1;
Shinya Kitaoka 120a6e
  info.bmiHeader.biBitCount      = 32;
Shinya Kitaoka 120a6e
  info.bmiHeader.biCompression   = BI_RGB;
Shinya Kitaoka 120a6e
  info.bmiHeader.biSizeImage     = 0;
Shinya Kitaoka 120a6e
  info.bmiHeader.biXPelsPerMeter = 1000;
Shinya Kitaoka 120a6e
  info.bmiHeader.biYPelsPerMeter = 1000;
Shinya Kitaoka 120a6e
  info.bmiHeader.biClrUsed       = 0;
Shinya Kitaoka 120a6e
  info.bmiHeader.biClrImportant  = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
#ifdef BUBU
Shinya Kitaoka 120a6e
void hardRenderVectorImage_MESA(const TVectorRenderData &rd, TRaster32P &ras,
Shinya Kitaoka 120a6e
                                const TVectorImageP &vimg) {
Shinya Kitaoka 120a6e
  int rasterWidth  = ras->getLx();
Shinya Kitaoka 120a6e
  int rasterHeight = ras->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--- begin mesa stuff
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Create an RGBA-mode context */
Toshihiro Shimizu 890ddd
#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
Shinya Kitaoka 120a6e
  /* specify Z, stencil, accum sizes */
Shinya Kitaoka 120a6e
  OSMesaContext ctx = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, NULL);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  OSMesaContext ctx = OSMesaCreateContext(OSMESA_RGBA, NULL);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  if (!ctx) {
Shinya Kitaoka 120a6e
    throw TException("OSMesaCreateContext failed!\n");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ras->lock();
Shinya Kitaoka 120a6e
  /* Bind the buffer to the context and make it current */
Shinya Kitaoka 120a6e
  if (!OSMesaMakeCurrent(ctx, ras->getRawData(), GL_UNSIGNED_BYTE, ras->getLx(),
Shinya Kitaoka 120a6e
                         ras->getLy())) {
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      ras->unlock();
Shinya Kitaoka 120a6e
      throw TException("OSMesaMakeCurrent failed!\n");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //---end mesa stuff
Shinya Kitaoka 120a6e
  // cfr. help: OpenGL/Programming tip/OpenGL Correctness Tips
Shinya Kitaoka 120a6e
  glViewport(0, 0, rasterWidth, rasterHeight);
Shinya Kitaoka 120a6e
  glMatrixMode(GL_PROJECTION);
Shinya Kitaoka 120a6e
  glLoadIdentity();
Shinya Kitaoka 120a6e
  gluOrtho2D(0, rasterWidth, 0, rasterHeight);
Shinya Kitaoka 120a6e
  glMatrixMode(GL_MODELVIEW);
Shinya Kitaoka 120a6e
  glLoadIdentity();
Shinya Kitaoka 120a6e
  glTranslatef(0.375, 0.375, 0.0);
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
// draw background
Shinya Kitaoka 120a6e
glRasterPos2d(0, 0);
Shinya Kitaoka 120a6e
glDrawPixels( ras->getLx(),ras->getLy(), GL_BGRA_EXT, GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
ras->getRawData());
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  // do OpenGL draw
Shinya Kitaoka 120a6e
  assert(vimg);
Shinya Kitaoka 120a6e
  tglDraw(rd, vimg.getPointer());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // force to finish
Shinya Kitaoka 120a6e
  glFlush();
Shinya Kitaoka 120a6e
  glFinish();
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
// set info in out raster
Shinya Kitaoka 120a6e
TDimension size = ras->getSize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
if( ras->getWrap() == rasterWidth )
Shinya Kitaoka 120a6e
memcpy( ras->getRawData(), offData, ras->getPixelSize()*size.lx*size.ly );
Shinya Kitaoka 120a6e
else
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
TRaster32P  temp( ras->getLx(), ras->getLy());
Shinya Kitaoka 120a6e
memcpy( temp->getRawData(), offData, ras->getPixelSize()*size.lx*size.ly );
Shinya Kitaoka 120a6e
ras->copy(temp);
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  OSMesaDestroyContext(ctx);
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
#endif  // BUBU
Shinya Kitaoka 120a6e
void hardRenderVectorImage(const TVectorRenderData &rd, TRaster32P &ras,
Shinya Kitaoka 120a6e
                           const TVectorImageP &vimg) {
Shinya Kitaoka 120a6e
  int rasterWidth  = ras->getLx();
Shinya Kitaoka 120a6e
  int rasterHeight = ras->getLy();
Shinya Kitaoka 120a6e
  ras->lock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef USE_MESA
Toshihiro Shimizu 890ddd
/* Create an RGBA-mode context */
Toshihiro Shimizu 890ddd
#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
Shinya Kitaoka 120a6e
  /* specify Z, stencil, accum sizes */
Shinya Kitaoka 120a6e
  OSMesaContext ctx = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, NULL);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  OSMesaContext ctx = OSMesaCreateContext(OSMESA_RGBA, NULL);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  if (!ctx) {
Shinya Kitaoka 120a6e
    ras->unlock();
Shinya Kitaoka 120a6e
    throw TException("OSMesaCreateContext failed!\n");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* Bind the buffer to the context and make it current */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!OSMesaMakeCurrent(ctx, ras->getRawData(), GL_UNSIGNED_BYTE, ras->getLx(),
Shinya Kitaoka 120a6e
                         ras->getLy())) {
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      ras->unlock();
Shinya Kitaoka 120a6e
      throw TException("OSMesaMakeCurrent failed!\n");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  BITMAPINFO info;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  initBITMAPINFO(info, ras);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void *offData = 0;  // a pointer to buffer
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // open an offscreen device
Shinya Kitaoka 120a6e
  HDC offDC = CreateCompatibleDC(NULL);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  HBITMAP offDIB =  // and a bitmap image
Shinya Kitaoka 120a6e
      CreateDIBSection(offDC, &info, DIB_RGB_COLORS, &offData, NULL, 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(offDIB);
Shinya Kitaoka 120a6e
  assert(offData);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int dataSize =  // number of byte of raster
Shinya Kitaoka 120a6e
      rasterWidth * rasterHeight * 4;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  memset(offData, 0, dataSize);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  HGDIOBJ oldobj =  // select BIB to write
Shinya Kitaoka 120a6e
      SelectObject(offDC, offDIB);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  static PIXELFORMATDESCRIPTOR pfd = {
Shinya Kitaoka 120a6e
      sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
Shinya Kitaoka 120a6e
      1,                              // version number
Shinya Kitaoka 120a6e
      0 | (false ? (PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER)
Shinya Kitaoka 120a6e
                 : (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI)) |
Shinya Kitaoka 120a6e
          PFD_SUPPORT_OPENGL,  // support OpenGL
Shinya Kitaoka 120a6e
      PFD_TYPE_RGBA,           // RGBA type
Shinya Kitaoka 120a6e
      32,                      // 32-bit color depth
Shinya Kitaoka 120a6e
      0,
Shinya Kitaoka 120a6e
      0, 0, 0, 0, 0,   // color bits ignored
Shinya Kitaoka 120a6e
      0,               // no alpha buffer
Shinya Kitaoka 120a6e
      0,               // shift bit ignored
Shinya Kitaoka 120a6e
      0,               // no accumulation buffer
Shinya Kitaoka 120a6e
      0, 0, 0, 0,      // accum bits ignored
Shinya Kitaoka 120a6e
      32,              // 32-bit z-buffer
Shinya Kitaoka 120a6e
      0,               // no stencil buffer
Shinya Kitaoka 120a6e
      0,               // no auxiliary buffer
Shinya Kitaoka 120a6e
      PFD_MAIN_PLANE,  // main layer
Shinya Kitaoka 120a6e
      0,               // reserved
Shinya Kitaoka 120a6e
      0, 0, 0          // layer masks ignored
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // get the best available match of pixel format for the device context
Shinya Kitaoka 120a6e
  int iPixelFormat = ChoosePixelFormat(offDC, &pfd);
Shinya Kitaoka 120a6e
  assert(iPixelFormat != 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // make that the pixel format of the device context
Shinya Kitaoka 120a6e
  int ret = SetPixelFormat(offDC, iPixelFormat, &pfd);
Shinya Kitaoka 120a6e
  assert(ret == TRUE);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // make a valid context for OpenGL rendering
Shinya Kitaoka 120a6e
  HGLRC hglRC = wglCreateContext(offDC);
Shinya Kitaoka 120a6e
  assert(hglRC);
Shinya Kitaoka 120a6e
  ret = wglMakeCurrent(offDC, hglRC);
Shinya Kitaoka 120a6e
  assert(ret == TRUE);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  // cfr. help: OpenGL/Programming tip/OpenGL Correctness Tips
Shinya Kitaoka 120a6e
  glViewport(0, 0, rasterWidth, rasterHeight);
Shinya Kitaoka 120a6e
  glMatrixMode(GL_PROJECTION);
Shinya Kitaoka 120a6e
  glLoadIdentity();
Shinya Kitaoka 120a6e
  gluOrtho2D(0, rasterWidth, 0, rasterHeight);
Shinya Kitaoka 120a6e
  glMatrixMode(GL_MODELVIEW);
Shinya Kitaoka 120a6e
  glLoadIdentity();
Shinya Kitaoka 120a6e
  glTranslatef(0.375, 0.375, 0.0);
Toshihiro Shimizu 890ddd
#ifndef USE_MESA
Shinya Kitaoka 120a6e
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Shinya Kitaoka 120a6e
  glClear(GL_COLOR_BUFFER_BIT);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // draw background
Shinya Kitaoka 120a6e
  glRasterPos2d(0, 0);
Shinya Kitaoka 120a6e
  glDrawPixels(ras->getLx(), ras->getLy(), GL_BGRA_EXT, GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
               ras->getRawData());
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  // do OpenGL draw
Shinya Kitaoka 120a6e
  assert(vimg);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  tglDraw(rd, vimg.getPointer());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // force to finish
Shinya Kitaoka 120a6e
  glFlush();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef USE_MESA
Shinya Kitaoka 120a6e
  OSMesaDestroyContext(ctx);
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  // set info in out raster
Shinya Kitaoka 120a6e
  TDimension size = ras->getSize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (ras->getWrap() == rasterWidth)
Shinya Kitaoka 120a6e
    memcpy(ras->getRawData(), offData, ras->getPixelSize() * size.lx * size.ly);
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TRaster32P temp(ras->getLx(), ras->getLy());
Shinya Kitaoka 120a6e
    memcpy(temp->getRawData(), offData,
Shinya Kitaoka 120a6e
           ras->getPixelSize() * size.lx * size.ly);
Shinya Kitaoka 120a6e
    ras->copy(temp);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ret = wglMakeCurrent(offDC, NULL);
Shinya Kitaoka 120a6e
  assert(ret == TRUE);
Shinya Kitaoka 120a6e
  wglDeleteContext(hglRC);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // release object
Shinya Kitaoka 120a6e
  SelectObject(offDC, oldobj);
Shinya Kitaoka 120a6e
  DeleteObject(offDIB);
Shinya Kitaoka 120a6e
  DeleteObject(offDC);
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// end of WIN32
Toshihiro Shimizu 890ddd
#elif defined(__sgi) || defined(LINUX)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
GLXContext ctx;
Toshihiro Shimizu 890ddd
XVisualInfo *visinfo;
Toshihiro Shimizu 890ddd
GC gc;
Shinya Kitaoka 120a6e
Window make_rgb_window(Display *dpy, unsigned int width, unsigned int height) {
Shinya Kitaoka 120a6e
  const int sbAttrib[] = {GLX_RGBA, GLX_RED_SIZE,  1, GLX_GREEN_SIZE,
Shinya Kitaoka 120a6e
                          1,        GLX_BLUE_SIZE, 1, None};
Shinya Kitaoka 120a6e
  const int dbAttrib[] = {GLX_RGBA, GLX_RED_SIZE,  1, GLX_GREEN_SIZE,
Shinya Kitaoka 120a6e
                          1,        GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER,
Shinya Kitaoka 120a6e
                          None};
Shinya Kitaoka 120a6e
  int scrnum;
Shinya Kitaoka 120a6e
  XSetWindowAttributes attr;
Shinya Kitaoka 120a6e
  TUINT32 mask;
Shinya Kitaoka 120a6e
  Window root;
Shinya Kitaoka 120a6e
  Window win;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  scrnum = DefaultScreen(dpy);
Shinya Kitaoka 120a6e
  root   = RootWindow(dpy, scrnum);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  visinfo = glXChooseVisual(dpy, scrnum, (int *)sbAttrib);
Shinya Kitaoka 120a6e
  if (!visinfo) {
Shinya Kitaoka 120a6e
    visinfo = glXChooseVisual(dpy, scrnum, (int *)dbAttrib);
Shinya Kitaoka 120a6e
    if (!visinfo) {
Shinya Kitaoka 120a6e
      printf("Error: couldn't get an RGB visual\n");
Shinya Kitaoka 120a6e
      exit(1);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* window attributes */
Shinya Kitaoka 120a6e
  attr.background_pixel = 0;
Shinya Kitaoka 120a6e
  attr.border_pixel     = 0;
Shinya Kitaoka 120a6e
  /* TODO: share root colormap if possible */
Shinya Kitaoka 120a6e
  attr.colormap   = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
Shinya Kitaoka 120a6e
  attr.event_mask = StructureNotifyMask | ExposureMask;
Shinya Kitaoka 120a6e
  mask            = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  win = XCreateWindow(dpy, root, 0, 0, width, height, 0, visinfo->depth,
Shinya Kitaoka 120a6e
                      InputOutput, visinfo->visual, mask, &attr);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* make an X GC so we can do XCopyArea later */
Shinya Kitaoka 120a6e
  gc = XCreateGC(dpy, win, 0, NULL);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* need indirect context */
Shinya Kitaoka 120a6e
  ctx = glXCreateContext(dpy, visinfo, NULL, False);
Shinya Kitaoka 120a6e
  if (!ctx) {
Shinya Kitaoka 120a6e
    printf("Error: glXCreateContext failed\n");
Shinya Kitaoka 120a6e
    exit(-1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  printf("Direct rendering: %s\n", glXIsDirect(dpy, ctx) ? "Yes" : "No");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return win;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
GLXPixmap make_pixmap(Display *dpy, Window win, unsigned int width,
Shinya Kitaoka 120a6e
                      unsigned int height, Pixmap *pixmap) {
Shinya Kitaoka 120a6e
  Pixmap pm;
Shinya Kitaoka 120a6e
  GLXPixmap glxpm;
Shinya Kitaoka 120a6e
  XWindowAttributes attr;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  pm = XCreatePixmap(dpy, win, width, height, visinfo->depth);
Shinya Kitaoka 120a6e
  if (!pm) {
Shinya Kitaoka 120a6e
    printf("Error: XCreatePixmap failed\n");
Shinya Kitaoka 120a6e
    exit(-1);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  XGetWindowAttributes(dpy, win, &attr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
    * IMPORTANT:
Toshihiro Shimizu 890ddd
    *   Use the glXCreateGLXPixmapMESA funtion when using Mesa because
Toshihiro Shimizu 890ddd
    *   Mesa needs to know the colormap associated with a pixmap in order
Toshihiro Shimizu 890ddd
    *   to render correctly.  This is because Mesa allows RGB rendering
Toshihiro Shimizu 890ddd
    *   into any kind of visual, not just TrueColor or DirectColor.
Toshihiro Shimizu 890ddd
    */
Shinya Kitaoka 120a6e
#ifdef PROBLEMI_CON_IL_DRIVER_NVIDIA  // GLX_MESA_pixmap_colormap //
Shinya Kitaoka 120a6e
  if (strstr(glXQueryExtensionsString(dpy, 0), "GLX_MESA_pixmap_colormap")) {
Shinya Kitaoka 120a6e
    /* stand-alone Mesa, specify the colormap */
Shinya Kitaoka 120a6e
    glxpm = glXCreateGLXPixmapMESA(dpy, visinfo, pm, attr.colormap);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    glxpm = glXCreateGLXPixmap(dpy, visinfo, pm);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  /* This will work with Mesa too if the visual is TrueColor or DirectColor */
Shinya Kitaoka 120a6e
  glxpm = glXCreateGLXPixmap(dpy, visinfo, pm);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!glxpm) {
Shinya Kitaoka 120a6e
    printf("Error: GLXCreateGLXPixmap failed\n");
Shinya Kitaoka 120a6e
    exit(-1);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  *pixmap = pm;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return glxpm;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// void  offscreenRender(TRaster32P& ras, const TVectorImageP& vimg, const
Shinya Kitaoka 120a6e
// TAffine& aff)
Shinya Kitaoka 120a6e
void hardRenderVectorImage(const TVectorRenderData &rd, TRaster32P &ras,
Shinya Kitaoka 120a6e
                           const TVectorImageP &vimg) {
Shinya Kitaoka 120a6e
  Display *dpy;
Shinya Kitaoka 120a6e
  Window win;
Shinya Kitaoka 120a6e
  Pixmap pm;
Shinya Kitaoka 120a6e
  GLXPixmap glxpm;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ras->lock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  dpy = XOpenDisplay(NULL);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  win   = make_rgb_window(dpy, ras->getLx(), ras->getLy());
Shinya Kitaoka 120a6e
  glxpm = make_pixmap(dpy, win, ras->getLx(), ras->getLy(), &pm);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  GLXContext oldctx  = glXGetCurrentContext();
Shinya Kitaoka 120a6e
  GLXDrawable olddrw = glXGetCurrentDrawable();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glXMakeCurrent(dpy, glxpm, ctx);
Shinya Kitaoka 120a6e
  // printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // cfr. help: OpenGL/Programming tip/OpenGL Correctness Tips
Shinya Kitaoka 120a6e
  glViewport(0, 0, ras->getLx(), ras->getLy());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glMatrixMode(GL_PROJECTION);
Shinya Kitaoka 120a6e
  glLoadIdentity();
Shinya Kitaoka 120a6e
  gluOrtho2D(0, ras->getLx(), 0, ras->getLy());
Shinya Kitaoka 120a6e
  glMatrixMode(GL_MODELVIEW);
Shinya Kitaoka 120a6e
  glLoadIdentity();
Shinya Kitaoka 120a6e
  glTranslatef(0.375, 0.375, 0.0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Shinya Kitaoka 120a6e
  glClear(GL_COLOR_BUFFER_BIT);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // draw background
Shinya Kitaoka 120a6e
  glRasterPos2d(0, 0);
Shinya Kitaoka 120a6e
  glDrawPixels(ras->getLx(), ras->getLy(), GL_RGBA, GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
               ras->getRawData());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // do OpenGL draw
Shinya Kitaoka 120a6e
  assert(vimg);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  tglDraw(rd, vimg.getPointer());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glFlush();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(__sgi)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glReadPixels(0, 0, ras->getLx(), ras->getLy(), GL_ABGR_EXT, GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
               ras->getRawData());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#elif defined(LINUX)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  glReadPixels(0, 0, ras->getLx(), ras->getLy(), GL_RGBA, GL_UNSIGNED_BYTE,
Shinya Kitaoka 120a6e
               ras->getRawData());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Bool ret = glXMakeCurrent(dpy, olddrw, oldctx);
Toshihiro Shimizu 890ddd
#ifdef DEBUG
Shinya Kitaoka 120a6e
  if (!ret) {
Shinya Kitaoka 120a6e
    std::cerr << __FUNCTION__ << " error in glXMakeCurrent olddrw=" << olddrw
Shinya Kitaoka 120a6e
              << " oldctx=" << oldctx << std::endl;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif