[gl2ps] [patch] Enable use without an OpenGL context
David Lonie
david.lonie at kitware.com
Tue Jan 12 22:08:36 CET 2016
Hi all,
I maintain the GL2PS export implementation in VTK, and we recently
revamped our rendering engine to use newer OpenGL APIs, and the
versions we're targeting have deprecated the OpenGL feedback buffer.
Not wanting to implement my own vector graphics export library, I've
made a few modifications to GL2PS so that we may continue to use it,
and would like to see about getting these merged upstream rather than
make a fork of GL2PS. I've attached a diff made against the current
(as of today) SVN repo.
For the curious, newer OpenGL adds Transform Feedback buffers that can
be used to capture varying GLSL variables from vertex/geometry
shaders. These buffers can provide transformed coordinates, normals,
colors etc that the old feedback buffer used to catch. The buffers are
parsed manually and the vertex attributes are added manually into
gl2ps using gl2psAddPolyPrimitive.
Summary of changes:
- Expose gl2psAddPolyPrimitive and required datatypes/defined values
(the GL2PS_[...] primitive defines, GL2PSxyz, GL2PSrgba, and
GL2PSvertex).
- Allow gl2psBeginPage to accept a buffersize of 0 -- this indicates
that the feedback buffer should not be parsed and that geometry will
be supplied manually. This is a change in behavior -- the old
implementation would allocate a default 4MB feedback buffer.
- Add new gl2psForceRasterPos method, which allows the raster position
to be set for text and image export without using OpenGL functions.
Feedback, questions, suggestions are welcome.
Cheers,
Dave
-------------- next part --------------
Index: gl2ps.c
===================================================================
--- gl2ps.c (revision 615)
+++ gl2ps.c (working copy)
@@ -65,20 +65,6 @@
#define GL2PS_ZOFFSET_LARGE 20.0F
#define GL2PS_ZERO(arg) (fabs(arg) < 1.e-20)
-/* Primitive types */
-
-#define GL2PS_NO_TYPE -1
-#define GL2PS_TEXT 1
-#define GL2PS_POINT 2
-#define GL2PS_LINE 3
-#define GL2PS_QUADRANGLE 4
-#define GL2PS_TRIANGLE 5
-#define GL2PS_PIXMAP 6
-#define GL2PS_IMAGEMAP 7
-#define GL2PS_IMAGEMAP_WRITTEN 8
-#define GL2PS_IMAGEMAP_VISIBLE 9
-#define GL2PS_SPECIAL 10
-
/* BSP tree primitive comparison */
#define GL2PS_COINCIDENT 1
@@ -119,7 +105,6 @@
T_VAR_ALPHA = 1<<4
} GL2PS_TRIANGLE_PROPERTY;
-typedef GLfloat GL2PSxyz[3];
typedef GLfloat GL2PSplane[4];
typedef struct _GL2PSbsptree2d GL2PSbsptree2d;
@@ -143,11 +128,6 @@
};
typedef struct {
- GL2PSxyz xyz;
- GL2PSrgba rgba;
-} GL2PSvertex;
-
-typedef struct {
GL2PSvertex vertex[3];
int prop;
} GL2PStriangle;
@@ -220,6 +200,8 @@
FILE *stream;
GL2PScompress *compress;
GLboolean header;
+ GL2PSvertex rasterpos;
+ GLboolean forcerasterpos;
/* BSP-specific */
GLint maxbestroot;
@@ -878,11 +860,18 @@
if(gl2ps->options & GL2PS_NO_TEXT) return GL2PS_SUCCESS;
- glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid);
- if(GL_FALSE == valid) return GL2PS_SUCCESS; /* the primitive is culled */
+ if (gl2ps->forcerasterpos) {
+ pos[0] = gl2ps->rasterpos.xyz[0];
+ pos[1] = gl2ps->rasterpos.xyz[1];
+ pos[2] = gl2ps->rasterpos.xyz[2];
+ pos[3] = 1.f;
+ }
+ else {
+ glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid);
+ if(GL_FALSE == valid) return GL2PS_SUCCESS; /* the primitive is culled */
+ glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
+ }
- glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
-
prim = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive));
prim->type = type;
prim->boundary = 0;
@@ -898,10 +887,20 @@
prim->pattern = 0;
prim->factor = 0;
prim->width = 1;
- if (color)
+ if (color) {
memcpy(prim->verts[0].rgba, color, 4 * sizeof(float));
- else
- glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba);
+ }
+ else {
+ if (gl2ps->forcerasterpos) {
+ prim->verts[0].rgba[0] = gl2ps->rasterpos.rgba[0];
+ prim->verts[0].rgba[1] = gl2ps->rasterpos.rgba[1];
+ prim->verts[0].rgba[2] = gl2ps->rasterpos.rgba[2];
+ prim->verts[0].rgba[3] = gl2ps->rasterpos.rgba[3];
+ }
+ else {
+ glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba);
+ }
+ }
prim->data.text = (GL2PSstring*)gl2psMalloc(sizeof(GL2PSstring));
prim->data.text->str = (char*)gl2psMalloc((strlen(str)+1)*sizeof(char));
strcpy(prim->data.text->str, str);
@@ -911,9 +910,17 @@
prim->data.text->alignment = alignment;
prim->data.text->angle = angle;
- gl2psListAdd(gl2ps->auxprimitives, &prim);
- glPassThrough(GL2PS_TEXT_TOKEN);
+ gl2ps->forcerasterpos = GL_FALSE;
+ /* If no buffer, just add directly to primitives */
+ if (gl2ps->buffersize > 0) {
+ gl2psListAdd(gl2ps->auxprimitives, &prim);
+ glPassThrough(GL2PS_TEXT_TOKEN);
+ }
+ else {
+ gl2psListAdd(gl2ps->primitives, &prim);
+ }
+
return GL2PS_SUCCESS;
}
@@ -2185,11 +2192,11 @@
*
*********************************************************************/
-static void gl2psAddPolyPrimitive(GLshort type, GLshort numverts,
- GL2PSvertex *verts, GLint offset,
- GLfloat ofactor, GLfloat ounits,
- GLushort pattern, GLint factor,
- GLfloat width, char boundary)
+GL2PSDLL_API void gl2psAddPolyPrimitive(GLshort type, GLshort numverts,
+ GL2PSvertex *verts, GLint offset,
+ GLfloat ofactor, GLfloat ounits,
+ GLushort pattern, GLint factor,
+ GLfloat width, char boundary)
{
GL2PSprimitive *prim;
@@ -3913,7 +3920,6 @@
}
/* Images & Mask Shader XObject names */
-
static int gl2psPDFgroupListWriteXObjectResources(void)
{
int i;
@@ -5611,9 +5617,11 @@
{
GL2PSbsptree *root;
GL2PSxyz eye = {0.0F, 0.0F, 100.0F * GL2PS_ZSCALE};
- GLint used;
+ GLint used = 0;
- used = glRenderMode(GL_RENDER);
+ if (gl2ps->buffersize > 0) {
+ used = glRenderMode(GL_RENDER);
+ }
if(used < 0){
gl2psMsg(GL2PS_INFO, "OpenGL feedback buffer overflow");
@@ -5738,6 +5746,7 @@
}
gl2ps->header = GL_TRUE;
+ gl2ps->forcerasterpos = GL_FALSE;
gl2ps->maxbestroot = 10;
gl2ps->options = options;
gl2ps->compress = NULL;
@@ -5766,7 +5775,7 @@
gl2ps->threshold[1] = ng ? 1.0F / (GLfloat)ng : 0.034F;
gl2ps->threshold[2] = nb ? 1.0F / (GLfloat)nb : 0.100F;
gl2ps->colormode = colormode;
- gl2ps->buffersize = buffersize > 0 ? buffersize : 2048 * 2048;
+ gl2ps->buffersize = buffersize;
for(i = 0; i < 3; i++){
gl2ps->lastvertex.xyz[i] = -1.0F;
}
@@ -5786,9 +5795,15 @@
/* get default blending mode from current OpenGL state (enabled by
default for SVG) */
- gl2ps->blending = (gl2ps->format == GL2PS_SVG) ? GL_TRUE : glIsEnabled(GL_BLEND);
- glGetIntegerv(GL_BLEND_SRC, &gl2ps->blendfunc[0]);
- glGetIntegerv(GL_BLEND_DST, &gl2ps->blendfunc[1]);
+ if (gl2ps->buffersize > 0) {
+ gl2ps->blending = (gl2ps->format == GL2PS_SVG) ? GL_TRUE
+ : glIsEnabled(GL_BLEND);
+ glGetIntegerv(GL_BLEND_SRC, &gl2ps->blendfunc[0]);
+ glGetIntegerv(GL_BLEND_DST, &gl2ps->blendfunc[1]);
+ }
+ else {
+ gl2ps->blending = GL_FALSE;
+ }
if(gl2ps->colormode == GL_RGBA){
gl2ps->colorsize = 0;
@@ -5848,9 +5863,12 @@
gl2ps->primitives = gl2psListCreate(500, 500, sizeof(GL2PSprimitive*));
gl2ps->auxprimitives = gl2psListCreate(100, 100, sizeof(GL2PSprimitive*));
gl2ps->feedback = (GLfloat*)gl2psMalloc(gl2ps->buffersize * sizeof(GLfloat));
- glFeedbackBuffer(gl2ps->buffersize, GL_3D_COLOR, gl2ps->feedback);
- glRenderMode(GL_FEEDBACK);
+ if (gl2ps->buffersize > 0) {
+ glFeedbackBuffer(gl2ps->buffersize, GL_3D_COLOR, gl2ps->feedback);
+ glRenderMode(GL_FEEDBACK);
+ }
+
return GL2PS_SUCCESS;
}
@@ -5952,13 +5970,23 @@
return GL2PS_ERROR;
}
- glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid);
- if(GL_FALSE == valid) return GL2PS_SUCCESS; /* the primitive is culled */
+ if (gl2ps->forcerasterpos) {
+ pos[0] = gl2ps->rasterpos.xyz[0];
+ pos[1] = gl2ps->rasterpos.xyz[1];
+ pos[2] = gl2ps->rasterpos.xyz[2];
+ pos[3] = 1.f;
+ /* Hardcode zoom factors (for now?) */
+ zoom_x = 1.f;
+ zoom_y = 1.f;
+ }
+ else {
+ glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &valid);
+ if(GL_FALSE == valid) return GL2PS_SUCCESS; /* the primitive is culled */
+ glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
+ glGetFloatv(GL_ZOOM_X, &zoom_x);
+ glGetFloatv(GL_ZOOM_Y, &zoom_y);
+ }
- glGetFloatv(GL_CURRENT_RASTER_POSITION, pos);
- glGetFloatv(GL_ZOOM_X, &zoom_x);
- glGetFloatv(GL_ZOOM_Y, &zoom_y);
-
prim = (GL2PSprimitive*)gl2psMalloc(sizeof(GL2PSprimitive));
prim->type = GL2PS_PIXMAP;
prim->boundary = 0;
@@ -5974,7 +6002,15 @@
prim->pattern = 0;
prim->factor = 0;
prim->width = 1;
- glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba);
+ if (gl2ps->forcerasterpos) {
+ prim->verts[0].rgba[0] = gl2ps->rasterpos.rgba[0];
+ prim->verts[0].rgba[1] = gl2ps->rasterpos.rgba[1];
+ prim->verts[0].rgba[2] = gl2ps->rasterpos.rgba[2];
+ prim->verts[0].rgba[3] = gl2ps->rasterpos.rgba[3];
+ }
+ else {
+ glGetFloatv(GL_CURRENT_RASTER_COLOR, prim->verts[0].rgba);
+ }
prim->data.image = (GL2PSimage*)gl2psMalloc(sizeof(GL2PSimage));
prim->data.image->width = width;
prim->data.image->height = height;
@@ -5983,6 +6019,8 @@
prim->data.image->format = format;
prim->data.image->type = type;
+ gl2ps->forcerasterpos = GL_FALSE;
+
switch(format){
case GL_RGBA:
if(gl2ps->options & GL2PS_NO_BLENDING || !gl2ps->blending){
@@ -6011,8 +6049,14 @@
break;
}
- gl2psListAdd(gl2ps->auxprimitives, &prim);
- glPassThrough(GL2PS_DRAW_PIXELS_TOKEN);
+ /* If no buffer, just add directly to primitives */
+ if (gl2ps->buffersize > 0) {
+ gl2psListAdd(gl2ps->auxprimitives, &prim);
+ glPassThrough(GL2PS_DRAW_PIXELS_TOKEN);
+ }
+ else {
+ gl2psListAdd(gl2ps->primitives, &prim);
+ }
return GL2PS_SUCCESS;
}
@@ -6179,3 +6223,22 @@
{
return gl2ps->format;
}
+
+GL2PSDLL_API GLint gl2psForceRasterPos(GL2PSvertex *vert)
+{
+
+ if(!gl2ps) {
+ return GL2PS_UNINITIALIZED;
+ }
+
+ gl2ps->forcerasterpos = GL_TRUE;
+ gl2ps->rasterpos.xyz[0] = vert->xyz[0];
+ gl2ps->rasterpos.xyz[1] = vert->xyz[1];
+ gl2ps->rasterpos.xyz[2] = vert->xyz[2];
+ gl2ps->rasterpos.rgba[0] = vert->rgba[0];
+ gl2ps->rasterpos.rgba[1] = vert->rgba[1];
+ gl2ps->rasterpos.rgba[2] = vert->rgba[2];
+ gl2ps->rasterpos.rgba[3] = vert->rgba[3];
+
+ return GL2PS_SUCCESS;
+}
Index: gl2ps.h
===================================================================
--- gl2ps.h (revision 615)
+++ gl2ps.h (working copy)
@@ -159,7 +159,26 @@
#define GL2PS_TEXT_TR 9
typedef GLfloat GL2PSrgba[4];
+typedef GLfloat GL2PSxyz[3];
+typedef struct {
+ GL2PSxyz xyz;
+ GL2PSrgba rgba;
+} GL2PSvertex;
+
+/* Primitive types */
+#define GL2PS_NO_TYPE -1
+#define GL2PS_TEXT 1
+#define GL2PS_POINT 2
+#define GL2PS_LINE 3
+#define GL2PS_QUADRANGLE 4
+#define GL2PS_TRIANGLE 5
+#define GL2PS_PIXMAP 6
+#define GL2PS_IMAGEMAP 7
+#define GL2PS_IMAGEMAP_WRITTEN 8
+#define GL2PS_IMAGEMAP_VISIBLE 9
+#define GL2PS_SPECIAL 10
+
#if defined(__cplusplus)
extern "C" {
#endif
@@ -175,6 +194,7 @@
GL2PSDLL_API GLint gl2psGetOptions(GLint *options);
GL2PSDLL_API GLint gl2psBeginViewport(GLint viewport[4]);
GL2PSDLL_API GLint gl2psEndViewport(void);
+GL2PSDLL_API GLint gl2psForceRasterPos(GL2PSvertex *vert);
GL2PSDLL_API GLint gl2psText(const char *str, const char *fontname,
GLshort fontsize);
GL2PSDLL_API GLint gl2psTextOpt(const char *str, const char *fontname,
@@ -199,6 +219,11 @@
GL2PSDLL_API const char *gl2psGetFileExtension(GLint format);
GL2PSDLL_API const char *gl2psGetFormatDescription(GLint format);
GL2PSDLL_API GLint gl2psGetFileFormat();
+GL2PSDLL_API void gl2psAddPolyPrimitive(GLshort type, GLshort numverts,
+ GL2PSvertex *verts, GLint offset,
+ GLfloat ofactor, GLfloat ounits,
+ GLushort pattern, GLint factor,
+ GLfloat width, char boundary);
#if defined(__cplusplus)
}