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