6 #include "../gfxdevice.h"
7 #include "../gfxtools.h"
18 //#define ZSTEP (1/65536.0)
19 #define ZSTEP (1/32.0)
20 //#define ZSTEP (1/4.0)
22 typedef struct _fontlist {
24 struct _fontlist*next;
27 typedef struct _internal {
34 int config_polygonoutlines;
36 GLUtesselator *tesselator;
37 GLUtesselator *tesselator_line;
38 GLUtesselator *tesselator_tex;
41 static int verbose = 0;
42 static void dbg(char*format, ...)
49 va_start(arglist, format);
50 vsprintf(buf, format, arglist);
53 while(l && buf[l-1]=='\n') {
57 printf("(device-opengl) %s\n", buf);
65 typedef void(*callbackfunction_t)();
67 void CALLBACK errorCallback(GLenum errorCode)
69 const GLubyte *estring;
70 estring = gluErrorString(errorCode);
71 printf("Tessellation Error: %s\n", estring);
74 void CALLBACK beginCallback(GLenum which)
78 void CALLBACK endCallback(void)
82 void CALLBACK vertexCallback(GLvoid *vertex)
84 double*xyz = (GLdouble*)vertex;
85 glVertex3d(xyz[0],xyz[1],xyz[2]);
87 void CALLBACK combineCallback(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
90 vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));
91 vertex[0] = coords[0];
92 vertex[1] = coords[1];
93 vertex[2] = coords[2];
96 void CALLBACK vertexCallbackTex(GLvoid *vertex)
98 double*v = (GLdouble*)vertex;
99 glTexCoord2f(v[3],v[4]);
100 glVertex3d(v[0],v[1],v[2]);
102 void CALLBACK combineCallbackTex(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
105 vertex = (GLdouble *) malloc(5 * sizeof(GLdouble));
106 vertex[0] = coords[0];
107 vertex[1] = coords[1];
108 vertex[2] = coords[2];
109 if(data[2] && data[3]) {
110 vertex[3] = w[0]*data[0][3] + w[1]*data[1][3] + w[2]*data[2][3] + w[3]*data[3][3];
111 vertex[4] = w[0]*data[0][4] + w[1]*data[1][4] + w[2]*data[2][4] + w[3]*data[3][4];
113 vertex[3] = w[0]*data[0][3] + w[1]*data[1][3];
114 vertex[4] = w[0]*data[0][4] + w[1]*data[1][4];
119 int opengl_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
121 internal_t*i = (internal_t*)dev->internal;
122 dbg("setparameter %s=%s", key, value);
123 if(!strcmp(key, "polygonoutlines")) {
124 i->config_polygonoutlines = atoi(value);
129 void opengl_startpage(struct _gfxdevice*dev, int width, int height)
131 dbg("startpage %d %d", width, height);
132 internal_t*i = (internal_t*)dev->internal;
138 void opengl_startclip(struct _gfxdevice*dev, gfxline_t*line)
143 void opengl_endclip(struct _gfxdevice*dev)
148 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)
151 internal_t*i = (internal_t*)dev->internal;
156 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
158 //glLineWidth(width*64);
163 double z = i->currentz*ZSTEP;
165 glPolygonOffset(0.0, 500.0);
169 if(l->type == gfx_moveTo) {
177 glBegin(GL_LINE_STRIP);
179 glVertex3d(l->x, l->y, z);
189 #define SPLINE_SUBDIVISION 2
191 void tesselatePolygon(GLUtesselator*tesselator, double z, gfxline_t*line)
195 double lastx=0,lasty=0;
198 gluTessBeginPolygon(tesselator, NULL);
202 if(l->type == gfx_splineTo) {
203 double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))*SPLINE_SUBDIVISION;
205 if(steps<1) steps = 1;
212 //printf("full len:%d\n", len);
213 xyz = malloc(sizeof(double)*3*len);
217 if(l->type == gfx_moveTo) {
220 gluTessEndContour(tesselator);
225 gluTessBeginContour(tesselator);
228 if(l->type == gfx_splineTo) {
230 double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))*SPLINE_SUBDIVISION;
232 if(steps<1) steps = 1;
233 //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);
234 //printf("%f %f %f\n", lastx, l->sx, l->x);
235 //printf("%f %f %f\n", lasty, l->sy, l->y);
236 for(j=1;j<=steps;j++) {
238 double t = (double)j / (double)steps;
239 xyz[len*3+0] = lastx*(1-t)*(1-t) + 2*l->sx*(1-t)*t + l->x*t*t;
240 xyz[len*3+1] = lasty*(1-t)*(1-t) + 2*l->sy*(1-t)*t + l->y*t*t;
242 gluTessVertex(tesselator, &xyz[len*3], &xyz[len*3]);
245 //printf("%d\n", len);
250 gluTessVertex(tesselator, &xyz[len*3], &xyz[len*3]);
260 gluTessEndContour(tesselator);
262 gluTessEndPolygon(tesselator);
266 void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
269 dbg("fill %02x%02x%02x%02x", color->a, color->r, color->g, color->b);
270 internal_t*i = (internal_t*)dev->internal;
272 glDisable(GL_TEXTURE_2D);
273 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
276 z = (i->currentz*ZSTEP);
277 tesselatePolygon(i->tesselator, z, line);
279 //tesselatePolygon(i->tesselator_line, z, line);
282 typedef struct _gfxhash
287 char gfxhash_compare(gfxhash_t*h1, gfxhash_t*h2)
289 return !memcmp(h1->d, h2->d, 16);
292 typedef struct _imgopengl
297 struct _imgopengl*next;
300 imgopengl_t*img2texid = 0;
302 gfxhash_t gfximage_hash(gfximage_t*img)
305 int size = img->width*img->height*4;
306 U8*data = (U8*)img->data;
308 hash_md5(data, size, hash.d);
312 imgopengl_t*addTexture(gfximage_t*img)
314 gfxhash_t hash = gfximage_hash(img);
315 imgopengl_t*i = img2texid;
319 while(1<<width_bits < img->width)
321 while(1<<height_bits < img->height)
323 int newwidth = 1<<width_bits;
324 int newheight = 1<<height_bits;
327 if(gfxhash_compare(&hash, &i->hash) && newwidth==i->width && newheight==i->height) {
334 glGenTextures(1, texIDs);
336 i = malloc(sizeof(imgopengl_t));
338 i->texID = texIDs[0];
343 i->height = newheight;
345 unsigned char*data = malloc(newwidth*newheight*4);
347 for(y=0;y<img->height;y++) {
348 for(x=0;x<img->width;x++) {
349 data[(y*newwidth+x)*4+0] = img->data[y*img->width+x].r;
350 data[(y*newwidth+x)*4+1] = img->data[y*img->width+x].g;
351 data[(y*newwidth+x)*4+2] = img->data[y*img->width+x].b;
352 data[(y*newwidth+x)*4+3] = img->data[y*img->width+x].a;
354 int lastx = img->width - 1;
355 for(;x<newwidth;x++) {
356 data[(y*newwidth+x)*4+0] = img->data[y*img->width+lastx].r;
357 data[(y*newwidth+x)*4+1] = img->data[y*img->width+lastx].g;
358 data[(y*newwidth+x)*4+2] = img->data[y*img->width+lastx].b;
359 data[(y*newwidth+x)*4+3] = img->data[y*img->width+lastx].a;
362 int lasty = img->height - 1;
363 for(;y<newheight;y++) {
364 for(x=0;x<newwidth;x++) {
365 data[(y*newwidth+x)*4+0] = img->data[lasty*img->width+x].r;
366 data[(y*newwidth+x)*4+1] = img->data[lasty*img->width+x].g;
367 data[(y*newwidth+x)*4+2] = img->data[lasty*img->width+x].b;
368 data[(y*newwidth+x)*4+3] = img->data[lasty*img->width+x].a;
372 glEnable(GL_TEXTURE_2D);
373 glBindTexture(GL_TEXTURE_2D, i->texID);
374 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, i->width, i->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
379 void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
382 internal_t*i = (internal_t*)dev->internal;
387 glColor4f(1.0,0,0.7,1.0);
391 imgopengl_t* txt = addTexture(img);
394 gfxmatrix_invert(matrix, &m2);
395 m2.m00 /= txt->width;
396 m2.m10 /= txt->width;
398 m2.m01 /= txt->height;
399 m2.m11 /= txt->height;
400 m2.ty /= txt->height;
402 glEnable(GL_TEXTURE_2D);
403 glBindTexture(GL_TEXTURE_2D, txt->texID);
404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
405 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
408 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
410 gluTessBeginPolygon(i->tesselator_tex, NULL);
417 xyz = malloc(sizeof(double)*5*len);
421 if(l->type == gfx_moveTo) {
424 gluTessEndContour(i->tesselator_tex);
429 gluTessBeginContour(i->tesselator_tex);
434 xyz[len*5+2] = (i->currentz*ZSTEP);
437 gfxmatrix_transform(&m2, /*src*/&xyz[len*5+0], /*dest*/&xyz[len*5+3]);
439 gluTessVertex(i->tesselator_tex, &xyz[len*5], &xyz[len*5]);
447 gluTessEndContour(i->tesselator_tex);
449 gluTessEndPolygon(i->tesselator_tex);
452 glDisable(GL_TEXTURE_2D);
455 void opengl_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
460 void opengl_addfont(gfxdevice_t*dev, gfxfont_t*font)
462 internal_t*i = (internal_t*)dev->internal;
464 fontlist_t*last=0,*l = i->fontlist;
467 if(!strcmp((char*)l->font->id, font->id)) {
468 return; // we already know this font
472 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
482 void opengl_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
484 internal_t*i = (internal_t*)dev->internal;
488 if(i->font && i->font->id && !strcmp(font->id, i->font->id)) {
489 // current font is correct
491 fontlist_t*l = i->fontlist;
494 if(!strcmp((char*)l->font->id, font->id)) {
501 opengl_addfont(dev, font);
503 //fprintf(stderr, "Unknown font id: %s", font->id);
508 gfxglyph_t*glyph = &i->font->glyphs[glyphnr];
510 gfxline_t*line2 = gfxline_clone(glyph->line);
511 gfxline_transform(line2, matrix);
512 opengl_fill(dev, line2, color);
522 void opengl_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
527 void opengl_endpage(struct _gfxdevice*dev)
532 int opengl_result_save(struct _gfxresult*gfx, const char*filename)
537 void* opengl_result_get(struct _gfxresult*gfx, const char*name)
542 void opengl_result_destroy(struct _gfxresult*gfx)
544 dbg("result:destroy");
548 gfxresult_t*opengl_finish(struct _gfxdevice*dev)
551 internal_t*i = (internal_t*)dev->internal;
552 gluDeleteTess(i->tesselator);i->tesselator=0;
553 gluDeleteTess(i->tesselator_tex);i->tesselator_tex=0;
554 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
555 memset(result, 0, sizeof(gfxresult_t));
556 result->save = opengl_result_save;
557 result->get = opengl_result_get;
558 result->destroy = opengl_result_destroy;
562 void gfxdevice_opengl_init(gfxdevice_t*dev)
565 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
566 memset(dev, 0, sizeof(gfxdevice_t));
568 dev->name = "opengl";
572 dev->setparameter = opengl_setparameter;
573 dev->startpage = opengl_startpage;
574 dev->startclip = opengl_startclip;
575 dev->endclip = opengl_endclip;
576 dev->stroke = opengl_stroke;
577 dev->fill = opengl_fill;
578 dev->fillbitmap = opengl_fillbitmap;
579 dev->fillgradient = opengl_fillgradient;
580 dev->addfont = opengl_addfont;
581 dev->drawchar = opengl_drawchar;
582 dev->drawlink = opengl_drawlink;
583 dev->endpage = opengl_endpage;
584 dev->finish = opengl_finish;
586 i->tesselator = gluNewTess();
587 gluTessCallback(i->tesselator, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
588 gluTessCallback(i->tesselator, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
589 gluTessCallback(i->tesselator, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
590 gluTessCallback(i->tesselator, GLU_TESS_END, (callbackfunction_t)endCallback);
591 gluTessCallback(i->tesselator, GLU_TESS_COMBINE, (callbackfunction_t)combineCallback);
593 i->tesselator_line = gluNewTess();
594 gluTessCallback(i->tesselator_line, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
595 gluTessCallback(i->tesselator_line, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
596 gluTessCallback(i->tesselator_line, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
597 gluTessCallback(i->tesselator_line, GLU_TESS_END, (callbackfunction_t)endCallback);
598 gluTessProperty(i->tesselator_line, GLU_TESS_BOUNDARY_ONLY, 1.0);
600 i->tesselator_tex = gluNewTess();
601 gluTessCallback(i->tesselator_tex, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
602 gluTessCallback(i->tesselator_tex, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallbackTex);
603 gluTessCallback(i->tesselator_tex, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
604 gluTessCallback(i->tesselator_tex, GLU_TESS_END, (callbackfunction_t)endCallback);
605 gluTessCallback(i->tesselator_tex, GLU_TESS_COMBINE, (callbackfunction_t)combineCallbackTex);
607 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);