6 #include "../gfxdevice.h"
7 #include "../gfxtools.h"
18 //#define ZSTEP (1/65536.0)
21 typedef struct _fontlist {
23 struct _fontlist*next;
26 typedef struct _internal {
33 int config_polygonoutlines;
35 GLUtesselator *tesselator;
36 GLUtesselator *tesselator_line;
37 GLUtesselator *tesselator_tex;
40 static int verbose = 0;
41 static void dbg(char*format, ...)
48 va_start(arglist, format);
49 vsprintf(buf, format, arglist);
52 while(l && buf[l-1]=='\n') {
56 printf("(device-opengl) %s\n", buf);
64 typedef void(*callbackfunction_t)();
66 void CALLBACK errorCallback(GLenum errorCode)
68 const GLubyte *estring;
69 estring = gluErrorString(errorCode);
70 dbg("Tessellation Error: %s\n", estring);
73 void CALLBACK beginCallback(GLenum which)
77 void CALLBACK endCallback(void)
81 void CALLBACK vertexCallback(GLvoid *vertex)
83 double*xyz = (GLdouble*)vertex;
84 glVertex3d(xyz[0],xyz[1],xyz[2]);
86 void CALLBACK combineCallback(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
89 vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));
90 vertex[0] = coords[0];
91 vertex[1] = coords[1];
92 vertex[2] = coords[2];
95 void CALLBACK vertexCallbackTex(GLvoid *vertex)
97 double*v = (GLdouble*)vertex;
98 glTexCoord2f(v[3],v[4]);
99 glVertex3d(v[0],v[1],v[2]);
101 void CALLBACK combineCallbackTex(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
103 GLdouble *vertex, *texCoord;
104 vertex = (GLdouble *) malloc(5 * sizeof(GLdouble));
105 vertex[0] = coords[0];
106 vertex[1] = coords[1];
107 vertex[2] = coords[2];
108 texCoord = &vertex[3];
109 vertex[3] = w[0]*data[0][3] + w[1]*data[1][3] + w[2]*data[2][3] + w[3]*data[3][3];
110 vertex[4] = w[0]*data[0][4] + w[1]*data[1][4] + w[2]*data[2][4] + w[3]*data[3][4];
114 int opengl_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
116 internal_t*i = (internal_t*)dev->internal;
117 dbg("setparameter %s=%s", key, value);
118 if(!strcmp(key, "polygonoutlines")) {
119 i->config_polygonoutlines = atoi(value);
124 void opengl_startpage(struct _gfxdevice*dev, int width, int height)
126 dbg("startpage %d %d", width, height);
127 internal_t*i = (internal_t*)dev->internal;
133 void opengl_startclip(struct _gfxdevice*dev, gfxline_t*line)
138 void opengl_endclip(struct _gfxdevice*dev)
143 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)
146 internal_t*i = (internal_t*)dev->internal;
151 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
153 //glLineWidth(width*64);
158 double z = i->currentz*ZSTEP;
160 glPolygonOffset(0.0, 500.0);
164 if(l->type == gfx_moveTo) {
172 glBegin(GL_LINE_STRIP);
174 glVertex3d(l->x, l->y, z);
184 #define SPLINE_SUBDIVISION 2
186 void tesselatePolygon(GLUtesselator*tesselator, double z, gfxline_t*line)
190 double lastx=0,lasty=0;
193 gluTessBeginPolygon(tesselator, NULL);
197 if(l->type == gfx_splineTo) {
198 double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))*SPLINE_SUBDIVISION;
200 if(steps<1) steps = 1;
207 //printf("full len:%d\n", len);
208 xyz = malloc(sizeof(double)*3*len);
212 if(l->type == gfx_moveTo) {
215 gluTessEndContour(tesselator);
220 gluTessBeginContour(tesselator);
223 if(l->type == gfx_splineTo) {
225 double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))*SPLINE_SUBDIVISION;
227 if(steps<1) steps = 1;
228 //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);
229 //printf("%f %f %f\n", lastx, l->sx, l->x);
230 //printf("%f %f %f\n", lasty, l->sy, l->y);
231 for(j=1;j<=steps;j++) {
233 double t = (double)j / (double)steps;
234 xyz[len*3+0] = lastx*(1-t)*(1-t) + 2*l->sx*(1-t)*t + l->x*t*t;
235 xyz[len*3+1] = lasty*(1-t)*(1-t) + 2*l->sy*(1-t)*t + l->y*t*t;
237 gluTessVertex(tesselator, &xyz[len*3], &xyz[len*3]);
240 //printf("%d\n", len);
245 gluTessVertex(tesselator, &xyz[len*3], &xyz[len*3]);
255 gluTessEndContour(tesselator);
257 gluTessEndPolygon(tesselator);
261 void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
265 internal_t*i = (internal_t*)dev->internal;
267 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
270 z = (i->currentz*ZSTEP);
271 tesselatePolygon(i->tesselator, z, line);
273 //tesselatePolygon(i->tesselator_line, z, line);
276 typedef struct _gfxhash
281 char gfxhash_compare(gfxhash_t*h1, gfxhash_t*h2)
283 return !memcmp(h1->d, h2->d, 16);
286 typedef struct _imgopengl
291 struct _imgopengl*next;
294 imgopengl_t*img2texid = 0;
296 gfxhash_t gfximage_hash(gfximage_t*img)
299 int size = img->width*img->height*4;
300 U8*data = (U8*)img->data;
302 hash_md5(data, size, hash.d);
306 imgopengl_t*addTexture(gfximage_t*img)
308 gfxhash_t hash = gfximage_hash(img);
309 imgopengl_t*i = img2texid;
313 while(1<<width_bits < img->width)
315 while(1<<height_bits < img->height)
317 int newwidth = 1<<width_bits;
318 int newheight = 1<<height_bits;
321 if(gfxhash_compare(&hash, &i->hash) && newwidth==i->width && newheight==i->height) {
328 glGenTextures(1, texIDs);
330 i = malloc(sizeof(imgopengl_t));
332 i->texID = texIDs[0];
337 i->height = newheight;
339 unsigned char*data = malloc(newwidth*newheight*4);
341 for(y=0;y<img->height;y++) {
342 for(x=0;x<img->width;x++) {
343 data[(y*newwidth+x)*4+0] = img->data[y*img->width+x].r;
344 data[(y*newwidth+x)*4+1] = img->data[y*img->width+x].g;
345 data[(y*newwidth+x)*4+2] = img->data[y*img->width+x].b;
346 data[(y*newwidth+x)*4+3] = img->data[y*img->width+x].a;
348 int lastx = img->width - 1;
349 for(;x<newwidth;x++) {
350 data[(y*newwidth+x)*4+0] = img->data[y*img->width+lastx].r;
351 data[(y*newwidth+x)*4+1] = img->data[y*img->width+lastx].g;
352 data[(y*newwidth+x)*4+2] = img->data[y*img->width+lastx].b;
353 data[(y*newwidth+x)*4+3] = img->data[y*img->width+lastx].a;
356 int lasty = img->height - 1;
357 for(;y<newheight;y++) {
358 for(x=0;x<newwidth;x++) {
359 data[(y*newwidth+x)*4+0] = img->data[lasty*img->width+x].r;
360 data[(y*newwidth+x)*4+1] = img->data[lasty*img->width+x].g;
361 data[(y*newwidth+x)*4+2] = img->data[lasty*img->width+x].b;
362 data[(y*newwidth+x)*4+3] = img->data[lasty*img->width+x].a;
366 glEnable(GL_TEXTURE_2D);
367 glBindTexture(GL_TEXTURE_2D, i->texID);
368 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, i->width, i->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
373 void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
376 internal_t*i = (internal_t*)dev->internal;
381 glColor4f(1.0,0,0,1.0);
385 imgopengl_t* txt = addTexture(img);
388 gfxmatrix_invert(matrix, &m2);
389 m2.m00 /= txt->width;
390 m2.m10 /= txt->width;
392 m2.m01 /= txt->height;
393 m2.m11 /= txt->height;
394 m2.ty /= txt->height;
396 glEnable(GL_TEXTURE_2D);
397 glBindTexture(GL_TEXTURE_2D, txt->texID);
398 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
402 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
404 gluTessBeginPolygon(i->tesselator_tex, NULL);
411 xyz = malloc(sizeof(double)*5*len);
415 if(l->type == gfx_moveTo) {
418 gluTessEndContour(i->tesselator_tex);
423 gluTessBeginContour(i->tesselator_tex);
428 xyz[len*5+2] = (i->currentz*ZSTEP);
431 gfxmatrix_transform(&m2, /*src*/&xyz[len*5+0], /*dest*/&xyz[len*5+3]);
433 gluTessVertex(i->tesselator_tex, &xyz[len*5], &xyz[len*5]);
441 gluTessEndContour(i->tesselator_tex);
443 gluTessEndPolygon(i->tesselator_tex);
446 glDisable(GL_TEXTURE_2D);
449 void opengl_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
454 void opengl_addfont(gfxdevice_t*dev, gfxfont_t*font)
456 internal_t*i = (internal_t*)dev->internal;
458 fontlist_t*last=0,*l = i->fontlist;
461 if(!strcmp((char*)l->font->id, font->id)) {
462 return; // we already know this font
466 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
476 void opengl_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
478 internal_t*i = (internal_t*)dev->internal;
482 if(i->font && i->font->id && !strcmp(font->id, i->font->id)) {
483 // current font is correct
485 fontlist_t*l = i->fontlist;
488 if(!strcmp((char*)l->font->id, font->id)) {
495 fprintf(stderr, "Unknown font id: %s", font->id);
500 gfxglyph_t*glyph = &i->font->glyphs[glyphnr];
502 gfxline_t*line2 = gfxline_clone(glyph->line);
503 gfxline_transform(line2, matrix);
504 opengl_fill(dev, line2, color);
514 void opengl_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
519 void opengl_endpage(struct _gfxdevice*dev)
524 int opengl_result_save(struct _gfxresult*gfx, char*filename)
529 void* opengl_result_get(struct _gfxresult*gfx, char*name)
534 void opengl_result_destroy(struct _gfxresult*gfx)
536 dbg("result:destroy");
540 gfxresult_t*opengl_finish(struct _gfxdevice*dev)
543 internal_t*i = (internal_t*)dev->internal;
544 gluDeleteTess(i->tesselator);i->tesselator=0;
545 gluDeleteTess(i->tesselator_tex);i->tesselator_tex=0;
546 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
547 memset(result, 0, sizeof(gfxresult_t));
548 result->save = opengl_result_save;
549 result->get = opengl_result_get;
550 result->destroy = opengl_result_destroy;
554 void gfxdevice_opengl_init(gfxdevice_t*dev)
557 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
558 memset(dev, 0, sizeof(gfxdevice_t));
560 dev->name = "opengl";
564 dev->setparameter = opengl_setparameter;
565 dev->startpage = opengl_startpage;
566 dev->startclip = opengl_startclip;
567 dev->endclip = opengl_endclip;
568 dev->stroke = opengl_stroke;
569 dev->fill = opengl_fill;
570 dev->fillbitmap = opengl_fillbitmap;
571 dev->fillgradient = opengl_fillgradient;
572 dev->addfont = opengl_addfont;
573 dev->drawchar = opengl_drawchar;
574 dev->drawlink = opengl_drawlink;
575 dev->endpage = opengl_endpage;
576 dev->finish = opengl_finish;
578 i->tesselator = gluNewTess();
579 gluTessCallback(i->tesselator, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
580 gluTessCallback(i->tesselator, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
581 gluTessCallback(i->tesselator, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
582 gluTessCallback(i->tesselator, GLU_TESS_END, (callbackfunction_t)endCallback);
583 //gluTessCallback(i->tesselator, GLU_TESS_COMBINE, (callbackfunction_t)combineCallback);
585 i->tesselator_line = gluNewTess();
586 gluTessCallback(i->tesselator_line, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
587 gluTessCallback(i->tesselator_line, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
588 gluTessCallback(i->tesselator_line, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
589 gluTessCallback(i->tesselator_line, GLU_TESS_END, (callbackfunction_t)endCallback);
590 gluTessProperty(i->tesselator_line, GLU_TESS_BOUNDARY_ONLY, 1.0);
592 i->tesselator_tex = gluNewTess();
593 gluTessCallback(i->tesselator_tex, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
594 gluTessCallback(i->tesselator_tex, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallbackTex);
595 gluTessCallback(i->tesselator_tex, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
596 gluTessCallback(i->tesselator_tex, GLU_TESS_END, (callbackfunction_t)endCallback);
597 gluTessCallback(i->tesselator_tex, GLU_TESS_COMBINE, (callbackfunction_t)combineCallbackTex);
599 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);