6 #include "../gfxdevice.h"
7 #include "../gfxtools.h"
16 //#define ZSTEP (1/65536.0)
19 typedef struct _fontlist {
21 struct _fontlist*next;
24 typedef struct _internal {
31 int config_polygonoutlines;
33 GLUtesselator *tesselator;
34 GLUtesselator *tesselator_line;
35 GLUtesselator *tesselator_tex;
38 static int verbose = 0;
39 static void dbg(char*format, ...)
46 va_start(arglist, format);
47 vsprintf(buf, format, arglist);
50 while(l && buf[l-1]=='\n') {
54 printf("(device-opengl) %s\n", buf);
62 typedef void(*callbackfunction_t)();
64 void CALLBACK errorCallback(GLenum errorCode)
66 const GLubyte *estring;
67 estring = gluErrorString(errorCode);
68 dbg("Tessellation Error: %s\n", estring);
71 void CALLBACK beginCallback(GLenum which)
75 void CALLBACK endCallback(void)
79 void CALLBACK vertexCallback(GLvoid *vertex)
81 double*xyz = (GLdouble*)vertex;
82 glVertex3d(xyz[0],xyz[1],xyz[2]);
84 void CALLBACK combineCallback(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
87 vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));
88 vertex[0] = coords[0];
89 vertex[1] = coords[1];
90 vertex[2] = coords[2];
93 void CALLBACK vertexCallbackTex(GLvoid *vertex)
95 double*v = (GLdouble*)vertex;
96 glTexCoord2f(v[3],v[4]);
97 glVertex3d(v[0],v[1],v[2]);
99 void CALLBACK combineCallbackTex(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
101 GLdouble *vertex, *texCoord;
102 vertex = (GLdouble *) malloc(5 * sizeof(GLdouble));
103 vertex[0] = coords[0];
104 vertex[1] = coords[1];
105 vertex[2] = coords[2];
106 texCoord = &vertex[3];
107 vertex[3] = w[0]*data[0][3] + w[1]*data[1][3] + w[2]*data[2][3] + w[3]*data[3][3];
108 vertex[4] = w[0]*data[0][4] + w[1]*data[1][4] + w[2]*data[2][4] + w[3]*data[3][4];
112 int opengl_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
114 internal_t*i = (internal_t*)dev->internal;
115 dbg("setparameter %s=%s", key, value);
116 if(!strcmp(key, "polygonoutlines")) {
117 i->config_polygonoutlines = atoi(value);
122 void opengl_startpage(struct _gfxdevice*dev, int width, int height)
124 dbg("startpage %d %d", width, height);
125 internal_t*i = (internal_t*)dev->internal;
131 void opengl_startclip(struct _gfxdevice*dev, gfxline_t*line)
136 void opengl_endclip(struct _gfxdevice*dev)
141 void opengl_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
144 internal_t*i = (internal_t*)dev->internal;
149 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
151 //glLineWidth(width*64);
156 double z = i->currentz*ZSTEP;
158 glPolygonOffset(0.0, 500.0);
162 if(l->type == gfx_moveTo) {
170 glBegin(GL_LINE_STRIP);
172 glVertex3d(l->x, l->y, z);
182 void tesselatePolygon(GLUtesselator*tesselator, double z, gfxline_t*line)
186 double lastx=0,lasty=0;
189 gluTessBeginPolygon(tesselator, NULL);
193 if(l->type == gfx_splineTo) {
194 double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))/2;
196 if(steps<1) steps = 1;
203 //printf("full len:%d\n", len);
204 xyz = malloc(sizeof(double)*3*len);
208 if(l->type == gfx_moveTo) {
211 gluTessEndContour(tesselator);
216 gluTessBeginContour(tesselator);
219 if(l->type == gfx_splineTo) {
221 double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))/2;
223 if(steps<1) steps = 1;
224 //printf("c=%f d1=%f (%f/%f) d2=%f (%f/%f)\n", c,d1,l->x-l->sx,l->y-l->sy,d2,lastx-l->sx,lasty-l->sy);
225 //printf("%f %f %f\n", lastx, l->sx, l->x);
226 //printf("%f %f %f\n", lasty, l->sy, l->y);
227 for(j=1;j<=steps;j++) {
229 double t = (double)j / (double)steps;
230 xyz[len*3+0] = lastx*(1-t)*(1-t) + 2*l->sx*(1-t)*t + l->x*t*t;
231 xyz[len*3+1] = lasty*(1-t)*(1-t) + 2*l->sy*(1-t)*t + l->y*t*t;
233 gluTessVertex(tesselator, &xyz[len*3], &xyz[len*3]);
236 //printf("%d\n", len);
241 gluTessVertex(tesselator, &xyz[len*3], &xyz[len*3]);
251 gluTessEndContour(tesselator);
253 gluTessEndPolygon(tesselator);
257 void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
261 internal_t*i = (internal_t*)dev->internal;
263 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
266 z = (i->currentz*ZSTEP);
267 tesselatePolygon(i->tesselator, z, line);
269 //tesselatePolygon(i->tesselator_line, z, line);
272 void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
275 internal_t*i = (internal_t*)dev->internal;
280 glColor4f(1.0,0,0,1.0);
285 glGenTextures(1, texIDs);
289 while(1<<width_bits < img->width)
291 while(1<<height_bits < img->height)
293 int newwidth = 1<<width_bits;
294 int newheight = 1<<height_bits;
296 unsigned char*data = malloc(newwidth*newheight*4);
298 for(y=0;y<img->height;y++) {
299 for(x=0;x<img->width;x++) {
300 data[(y*newwidth+x)*4+0] = img->data[y*img->width+x].r;
301 data[(y*newwidth+x)*4+1] = img->data[y*img->width+x].g;
302 data[(y*newwidth+x)*4+2] = img->data[y*img->width+x].b;
303 data[(y*newwidth+x)*4+3] = img->data[y*img->width+x].a;
305 int lastx = img->width - 1;
306 for(;x<newwidth;x++) {
307 data[(y*newwidth+x)*4+0] = img->data[y*img->width+lastx].r;
308 data[(y*newwidth+x)*4+1] = img->data[y*img->width+lastx].g;
309 data[(y*newwidth+x)*4+2] = img->data[y*img->width+lastx].b;
310 data[(y*newwidth+x)*4+3] = img->data[y*img->width+lastx].a;
313 int lasty = img->height - 1;
314 for(;y<newheight;y++) {
315 for(x=0;x<newwidth;x++) {
316 data[(y*newwidth+x)*4+0] = img->data[lasty*img->width+x].r;
317 data[(y*newwidth+x)*4+1] = img->data[lasty*img->width+x].g;
318 data[(y*newwidth+x)*4+2] = img->data[lasty*img->width+x].b;
319 data[(y*newwidth+x)*4+3] = img->data[lasty*img->width+x].a;
324 gfxmatrix_invert(matrix, &m2);
332 glEnable(GL_TEXTURE_2D);
333 glBindTexture(GL_TEXTURE_2D, texIDs[0]);
334 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, newwidth, newheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
336 glEnable(GL_TEXTURE_2D);
337 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
338 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
339 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
340 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
341 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
344 gluTessBeginPolygon(i->tesselator_tex, NULL);
351 xyz = malloc(sizeof(double)*5*len);
355 if(l->type == gfx_moveTo) {
358 gluTessEndContour(i->tesselator_tex);
363 gluTessBeginContour(i->tesselator_tex);
368 xyz[len*5+2] = (i->currentz*ZSTEP);
371 gfxmatrix_transform(&m2, /*src*/&xyz[len*5+0], /*dest*/&xyz[len*5+3]);
373 gluTessVertex(i->tesselator_tex, &xyz[len*5], &xyz[len*5]);
381 gluTessEndContour(i->tesselator_tex);
383 gluTessEndPolygon(i->tesselator_tex);
386 glDisable(GL_TEXTURE_2D);
389 void opengl_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
394 void opengl_addfont(gfxdevice_t*dev, gfxfont_t*font)
396 internal_t*i = (internal_t*)dev->internal;
398 fontlist_t*last=0,*l = i->fontlist;
401 if(!strcmp((char*)l->font->id, font->id)) {
402 return; // we already know this font
406 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
416 void opengl_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
418 internal_t*i = (internal_t*)dev->internal;
420 if(i->font && i->font->id && !strcmp(font->id, i->font->id)) {
421 // current font is correct
423 fontlist_t*l = i->fontlist;
426 if(!strcmp((char*)l->font->id, font->id)) {
433 fprintf(stderr, "Unknown font id: %s", font->id);
438 gfxglyph_t*glyph = &i->font->glyphs[glyphnr];
440 gfxline_t*line2 = gfxline_clone(glyph->line);
441 gfxline_transform(line2, matrix);
442 opengl_fill(dev, line2, color);
452 void opengl_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
457 void opengl_endpage(struct _gfxdevice*dev)
462 int opengl_result_save(struct _gfxresult*gfx, char*filename)
467 void* opengl_result_get(struct _gfxresult*gfx, char*name)
472 void opengl_result_destroy(struct _gfxresult*gfx)
474 dbg("result:destroy");
478 gfxresult_t*opengl_finish(struct _gfxdevice*dev)
481 internal_t*i = (internal_t*)dev->internal;
482 gluDeleteTess(i->tesselator);i->tesselator=0;
483 gluDeleteTess(i->tesselator_tex);i->tesselator_tex=0;
484 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
485 memset(result, 0, sizeof(gfxresult_t));
486 result->save = opengl_result_save;
487 result->get = opengl_result_get;
488 result->destroy = opengl_result_destroy;
492 void gfxdevice_opengl_init(gfxdevice_t*dev)
495 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
496 memset(dev, 0, sizeof(gfxdevice_t));
498 dev->name = "opengl";
502 dev->setparameter = opengl_setparameter;
503 dev->startpage = opengl_startpage;
504 dev->startclip = opengl_startclip;
505 dev->endclip = opengl_endclip;
506 dev->stroke = opengl_stroke;
507 dev->fill = opengl_fill;
508 dev->fillbitmap = opengl_fillbitmap;
509 dev->fillgradient = opengl_fillgradient;
510 dev->addfont = opengl_addfont;
511 dev->drawchar = opengl_drawchar;
512 dev->drawlink = opengl_drawlink;
513 dev->endpage = opengl_endpage;
514 dev->finish = opengl_finish;
516 i->tesselator = gluNewTess();
517 gluTessCallback(i->tesselator, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
518 gluTessCallback(i->tesselator, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
519 gluTessCallback(i->tesselator, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
520 gluTessCallback(i->tesselator, GLU_TESS_END, (callbackfunction_t)endCallback);
521 //gluTessCallback(i->tesselator, GLU_TESS_COMBINE, (callbackfunction_t)combineCallback);
523 i->tesselator_line = gluNewTess();
524 gluTessCallback(i->tesselator_line, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
525 gluTessCallback(i->tesselator_line, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
526 gluTessCallback(i->tesselator_line, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
527 gluTessCallback(i->tesselator_line, GLU_TESS_END, (callbackfunction_t)endCallback);
528 gluTessProperty(i->tesselator_line, GLU_TESS_BOUNDARY_ONLY, 1.0);
530 i->tesselator_tex = gluNewTess();
531 gluTessCallback(i->tesselator_tex, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
532 gluTessCallback(i->tesselator_tex, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallbackTex);
533 gluTessCallback(i->tesselator_tex, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
534 gluTessCallback(i->tesselator_tex, GLU_TESS_END, (callbackfunction_t)endCallback);
535 gluTessCallback(i->tesselator_tex, GLU_TESS_COMBINE, (callbackfunction_t)combineCallbackTex);
537 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);