6 #include "../gfxdevice.h"
7 #include "../gfxtools.h"
18 //#define ZSTEP (1/65536.0)
19 #define ZSTEP (1/32.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 printf("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 glDisable(GL_TEXTURE_2D);
268 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
271 z = (i->currentz*ZSTEP);
272 tesselatePolygon(i->tesselator, z, line);
274 //tesselatePolygon(i->tesselator_line, z, line);
277 typedef struct _gfxhash
282 char gfxhash_compare(gfxhash_t*h1, gfxhash_t*h2)
284 return !memcmp(h1->d, h2->d, 16);
287 typedef struct _imgopengl
292 struct _imgopengl*next;
295 imgopengl_t*img2texid = 0;
297 gfxhash_t gfximage_hash(gfximage_t*img)
300 int size = img->width*img->height*4;
301 U8*data = (U8*)img->data;
303 hash_md5(data, size, hash.d);
307 imgopengl_t*addTexture(gfximage_t*img)
309 gfxhash_t hash = gfximage_hash(img);
310 imgopengl_t*i = img2texid;
314 while(1<<width_bits < img->width)
316 while(1<<height_bits < img->height)
318 int newwidth = 1<<width_bits;
319 int newheight = 1<<height_bits;
322 if(gfxhash_compare(&hash, &i->hash) && newwidth==i->width && newheight==i->height) {
329 glGenTextures(1, texIDs);
331 i = malloc(sizeof(imgopengl_t));
333 i->texID = texIDs[0];
338 i->height = newheight;
340 unsigned char*data = malloc(newwidth*newheight*4);
342 for(y=0;y<img->height;y++) {
343 for(x=0;x<img->width;x++) {
344 data[(y*newwidth+x)*4+0] = img->data[y*img->width+x].r;
345 data[(y*newwidth+x)*4+1] = img->data[y*img->width+x].g;
346 data[(y*newwidth+x)*4+2] = img->data[y*img->width+x].b;
347 data[(y*newwidth+x)*4+3] = img->data[y*img->width+x].a;
349 int lastx = img->width - 1;
350 for(;x<newwidth;x++) {
351 data[(y*newwidth+x)*4+0] = img->data[y*img->width+lastx].r;
352 data[(y*newwidth+x)*4+1] = img->data[y*img->width+lastx].g;
353 data[(y*newwidth+x)*4+2] = img->data[y*img->width+lastx].b;
354 data[(y*newwidth+x)*4+3] = img->data[y*img->width+lastx].a;
357 int lasty = img->height - 1;
358 for(;y<newheight;y++) {
359 for(x=0;x<newwidth;x++) {
360 data[(y*newwidth+x)*4+0] = img->data[lasty*img->width+x].r;
361 data[(y*newwidth+x)*4+1] = img->data[lasty*img->width+x].g;
362 data[(y*newwidth+x)*4+2] = img->data[lasty*img->width+x].b;
363 data[(y*newwidth+x)*4+3] = img->data[lasty*img->width+x].a;
367 glEnable(GL_TEXTURE_2D);
368 glBindTexture(GL_TEXTURE_2D, i->texID);
369 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, i->width, i->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
374 void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
377 internal_t*i = (internal_t*)dev->internal;
382 glColor4f(1.0,0,0,1.0);
386 imgopengl_t* txt = addTexture(img);
389 gfxmatrix_invert(matrix, &m2);
390 m2.m00 /= txt->width;
391 m2.m10 /= txt->width;
393 m2.m01 /= txt->height;
394 m2.m11 /= txt->height;
395 m2.ty /= txt->height;
397 glEnable(GL_TEXTURE_2D);
398 glBindTexture(GL_TEXTURE_2D, txt->texID);
399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
402 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
403 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
405 gluTessBeginPolygon(i->tesselator_tex, NULL);
412 xyz = malloc(sizeof(double)*5*len);
416 if(l->type == gfx_moveTo) {
419 gluTessEndContour(i->tesselator_tex);
424 gluTessBeginContour(i->tesselator_tex);
429 xyz[len*5+2] = (i->currentz*ZSTEP);
432 gfxmatrix_transform(&m2, /*src*/&xyz[len*5+0], /*dest*/&xyz[len*5+3]);
434 gluTessVertex(i->tesselator_tex, &xyz[len*5], &xyz[len*5]);
442 gluTessEndContour(i->tesselator_tex);
444 gluTessEndPolygon(i->tesselator_tex);
447 glDisable(GL_TEXTURE_2D);
450 void opengl_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
455 void opengl_addfont(gfxdevice_t*dev, gfxfont_t*font)
457 internal_t*i = (internal_t*)dev->internal;
459 fontlist_t*last=0,*l = i->fontlist;
462 if(!strcmp((char*)l->font->id, font->id)) {
463 return; // we already know this font
467 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
477 void opengl_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
479 internal_t*i = (internal_t*)dev->internal;
483 if(i->font && i->font->id && !strcmp(font->id, i->font->id)) {
484 // current font is correct
486 fontlist_t*l = i->fontlist;
489 if(!strcmp((char*)l->font->id, font->id)) {
496 fprintf(stderr, "Unknown font id: %s", font->id);
501 gfxglyph_t*glyph = &i->font->glyphs[glyphnr];
503 gfxline_t*line2 = gfxline_clone(glyph->line);
504 gfxline_transform(line2, matrix);
505 opengl_fill(dev, line2, color);
515 void opengl_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
520 void opengl_endpage(struct _gfxdevice*dev)
525 int opengl_result_save(struct _gfxresult*gfx, char*filename)
530 void* opengl_result_get(struct _gfxresult*gfx, char*name)
535 void opengl_result_destroy(struct _gfxresult*gfx)
537 dbg("result:destroy");
541 gfxresult_t*opengl_finish(struct _gfxdevice*dev)
544 internal_t*i = (internal_t*)dev->internal;
545 gluDeleteTess(i->tesselator);i->tesselator=0;
546 gluDeleteTess(i->tesselator_tex);i->tesselator_tex=0;
547 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
548 memset(result, 0, sizeof(gfxresult_t));
549 result->save = opengl_result_save;
550 result->get = opengl_result_get;
551 result->destroy = opengl_result_destroy;
555 void gfxdevice_opengl_init(gfxdevice_t*dev)
558 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
559 memset(dev, 0, sizeof(gfxdevice_t));
561 dev->name = "opengl";
565 dev->setparameter = opengl_setparameter;
566 dev->startpage = opengl_startpage;
567 dev->startclip = opengl_startclip;
568 dev->endclip = opengl_endclip;
569 dev->stroke = opengl_stroke;
570 dev->fill = opengl_fill;
571 dev->fillbitmap = opengl_fillbitmap;
572 dev->fillgradient = opengl_fillgradient;
573 dev->addfont = opengl_addfont;
574 dev->drawchar = opengl_drawchar;
575 dev->drawlink = opengl_drawlink;
576 dev->endpage = opengl_endpage;
577 dev->finish = opengl_finish;
579 i->tesselator = gluNewTess();
580 gluTessCallback(i->tesselator, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
581 gluTessCallback(i->tesselator, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
582 gluTessCallback(i->tesselator, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
583 gluTessCallback(i->tesselator, GLU_TESS_END, (callbackfunction_t)endCallback);
584 gluTessCallback(i->tesselator, GLU_TESS_COMBINE, (callbackfunction_t)combineCallback);
586 i->tesselator_line = gluNewTess();
587 gluTessCallback(i->tesselator_line, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
588 gluTessCallback(i->tesselator_line, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
589 gluTessCallback(i->tesselator_line, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
590 gluTessCallback(i->tesselator_line, GLU_TESS_END, (callbackfunction_t)endCallback);
591 gluTessProperty(i->tesselator_line, GLU_TESS_BOUNDARY_ONLY, 1.0);
593 i->tesselator_tex = gluNewTess();
594 gluTessCallback(i->tesselator_tex, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
595 gluTessCallback(i->tesselator_tex, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallbackTex);
596 gluTessCallback(i->tesselator_tex, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
597 gluTessCallback(i->tesselator_tex, GLU_TESS_END, (callbackfunction_t)endCallback);
598 gluTessCallback(i->tesselator_tex, GLU_TESS_COMBINE, (callbackfunction_t)combineCallbackTex);
600 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);