5 #include "../gfxdevice.h"
6 #include "../gfxtools.h"
14 #define ZSTEP (1/65536.0)
16 typedef struct _fontlist {
18 struct _fontlist*next;
21 typedef struct _internal {
28 GLUtesselator *tesselator;
29 GLUtesselator *tesselator_tex;
32 static int verbose = 1;
33 static void dbg(char*format, ...)
40 va_start(arglist, format);
41 vsprintf(buf, format, arglist);
44 while(l && buf[l-1]=='\n') {
48 printf("(device-opengl) %s\n", buf);
56 typedef void(*callbackfunction_t)();
58 void CALLBACK errorCallback(GLenum errorCode)
60 const GLubyte *estring;
61 estring = gluErrorString(errorCode);
62 dbg("Tessellation Error: %s\n", estring);
65 void CALLBACK beginCallback(GLenum which)
69 void CALLBACK endCallback(void)
73 void CALLBACK vertexCallback(GLvoid *vertex)
75 double*xyz = (GLdouble*)vertex;
76 glVertex3d(xyz[0],xyz[1],xyz[2]);
78 void CALLBACK combineCallback(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
81 vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));
82 vertex[0] = coords[0];
83 vertex[1] = coords[1];
84 vertex[2] = coords[2];
87 void CALLBACK vertexCallbackTex(GLvoid *vertex)
89 double*v = (GLdouble*)vertex;
90 glTexCoord2f(v[3],v[4]);
91 glVertex3d(v[0],v[1],v[2]);
93 void CALLBACK combineCallbackTex(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
95 GLdouble *vertex, *texCoord;
96 vertex = (GLdouble *) malloc(5 * sizeof(GLdouble));
97 vertex[0] = coords[0];
98 vertex[1] = coords[1];
99 vertex[2] = coords[2];
100 texCoord = &vertex[3];
101 vertex[3] = w[0]*data[0][3] + w[1]*data[1][3] + w[2]*data[2][3] + w[3]*data[3][3];
102 vertex[4] = w[0]*data[0][4] + w[1]*data[1][4] + w[2]*data[2][4] + w[3]*data[3][4];
106 int opengl_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
108 dbg("setparameter %s=%s", key, value);
112 void opengl_startpage(struct _gfxdevice*dev, int width, int height)
114 dbg("startpage %d %d", width, height);
115 internal_t*i = (internal_t*)dev->internal;
121 void opengl_startclip(struct _gfxdevice*dev, gfxline_t*line)
126 void opengl_endclip(struct _gfxdevice*dev)
131 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)
134 internal_t*i = (internal_t*)dev->internal;
138 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
143 if(l->type == gfx_moveTo) {
151 glBegin(GL_LINE_STRIP);
153 glVertex3d(l->x, l->y, (i->currentz*ZSTEP));
163 void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
166 internal_t*i = (internal_t*)dev->internal;
170 double lastx=0,lasty=0;
173 glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
175 gluTessBeginPolygon(i->tesselator, NULL);
179 if(l->type == gfx_splineTo) {
180 double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))/2;
182 if(steps<1) steps = 1;
189 //printf("full len:%d\n", len);
190 double z = (i->currentz*ZSTEP);
191 xyz = malloc(sizeof(double)*3*len);
195 if(l->type == gfx_moveTo) {
198 gluTessEndContour(i->tesselator);
203 gluTessBeginContour(i->tesselator);
206 if(l->type == gfx_splineTo) {
208 double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))/2;
210 if(steps<1) steps = 1;
211 //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);
212 //printf("%f %f %f\n", lastx, l->sx, l->x);
213 //printf("%f %f %f\n", lasty, l->sy, l->y);
214 for(j=1;j<=steps;j++) {
216 double t = (double)j / (double)steps;
217 xyz[len*3+0] = lastx*(1-t)*(1-t) + 2*l->sx*(1-t)*t + l->x*t*t;
218 xyz[len*3+1] = lasty*(1-t)*(1-t) + 2*l->sy*(1-t)*t + l->y*t*t;
220 gluTessVertex(i->tesselator, &xyz[len*3], &xyz[len*3]);
223 //printf("%d\n", len);
228 gluTessVertex(i->tesselator, &xyz[len*3], &xyz[len*3]);
238 gluTessEndContour(i->tesselator);
240 gluTessEndPolygon(i->tesselator);
245 void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
248 internal_t*i = (internal_t*)dev->internal;
253 glColor4f(1.0,0,0,1.0);
256 glGenTextures(1, texIDs);
260 while(1<<width_bits < img->width)
262 while(1<<height_bits < img->height)
264 int newwidth = 1<<width_bits;
265 int newheight = 1<<height_bits;
267 unsigned char*data = malloc(newwidth*newheight*4);
269 for(y=0;y<img->height;y++) {
270 for(x=0;x<img->width;x++) {
271 data[(y*newwidth+x)*4+0] = img->data[y*img->width+x].r;
272 data[(y*newwidth+x)*4+1] = img->data[y*img->width+x].g;
273 data[(y*newwidth+x)*4+2] = img->data[y*img->width+x].b;
274 data[(y*newwidth+x)*4+3] = img->data[y*img->width+x].a;
279 gfxmatrix_invert(matrix, &m2);
287 glEnable(GL_TEXTURE_2D);
288 glBindTexture(GL_TEXTURE_2D, texIDs[0]);
289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, newwidth, newheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
291 glEnable(GL_TEXTURE_2D);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
296 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
299 gluTessBeginPolygon(i->tesselator_tex, NULL);
306 xyz = malloc(sizeof(double)*5*len);
310 if(l->type == gfx_moveTo) {
313 gluTessEndContour(i->tesselator_tex);
318 gluTessBeginContour(i->tesselator_tex);
323 xyz[len*5+2] = (i->currentz*ZSTEP);
326 gfxmatrix_transform(&m2, /*src*/&xyz[len*5+0], /*dest*/&xyz[len*5+3]);
328 gluTessVertex(i->tesselator_tex, &xyz[len*5], &xyz[len*5]);
336 gluTessEndContour(i->tesselator_tex);
338 gluTessEndPolygon(i->tesselator_tex);
342 glDisable(GL_TEXTURE_2D);
345 void opengl_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
350 void opengl_addfont(gfxdevice_t*dev, gfxfont_t*font)
352 internal_t*i = (internal_t*)dev->internal;
354 fontlist_t*last=0,*l = i->fontlist;
357 if(!strcmp((char*)l->font->id, font->id)) {
358 return; // we already know this font
362 l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t));
372 void opengl_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
374 internal_t*i = (internal_t*)dev->internal;
376 if(i->font && i->font->id && !strcmp(font->id, i->font->id)) {
377 // current font is correct
379 fontlist_t*l = i->fontlist;
382 if(!strcmp((char*)l->font->id, font->id)) {
389 fprintf(stderr, "Unknown font id: %s", font->id);
394 gfxglyph_t*glyph = &i->font->glyphs[glyphnr];
396 gfxline_t*line2 = gfxline_clone(glyph->line);
397 gfxline_transform(line2, matrix);
398 opengl_fill(dev, line2, color);
406 void opengl_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
411 void opengl_endpage(struct _gfxdevice*dev)
416 int opengl_result_save(struct _gfxresult*gfx, char*filename)
421 void* opengl_result_get(struct _gfxresult*gfx, char*name)
426 void opengl_result_destroy(struct _gfxresult*gfx)
428 dbg("result:destroy");
432 gfxresult_t*opengl_finish(struct _gfxdevice*dev)
435 internal_t*i = (internal_t*)dev->internal;
436 gluDeleteTess(i->tesselator);i->tesselator=0;
437 gluDeleteTess(i->tesselator_tex);i->tesselator_tex=0;
438 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
439 memset(result, 0, sizeof(gfxresult_t));
440 result->save = opengl_result_save;
441 result->get = opengl_result_get;
442 result->destroy = opengl_result_destroy;
446 void gfxdevice_opengl_init(gfxdevice_t*dev)
449 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
450 memset(dev, 0, sizeof(gfxdevice_t));
454 dev->setparameter = opengl_setparameter;
455 dev->startpage = opengl_startpage;
456 dev->startclip = opengl_startclip;
457 dev->endclip = opengl_endclip;
458 dev->stroke = opengl_stroke;
459 dev->fill = opengl_fill;
460 dev->fillbitmap = opengl_fillbitmap;
461 dev->fillgradient = opengl_fillgradient;
462 dev->addfont = opengl_addfont;
463 dev->drawchar = opengl_drawchar;
464 dev->drawlink = opengl_drawlink;
465 dev->endpage = opengl_endpage;
466 dev->finish = opengl_finish;
468 i->tesselator = gluNewTess();
469 gluTessCallback(i->tesselator, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
470 gluTessCallback(i->tesselator, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback);
471 gluTessCallback(i->tesselator, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
472 gluTessCallback(i->tesselator, GLU_TESS_END, (callbackfunction_t)endCallback);
473 //gluTessCallback(i->tesselator, GLU_TESS_END, (callbackfunction_t)combineCallback);
475 i->tesselator_tex = gluNewTess();
476 gluTessCallback(i->tesselator_tex, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);
477 gluTessCallback(i->tesselator_tex, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallbackTex);
478 gluTessCallback(i->tesselator_tex, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback);
479 gluTessCallback(i->tesselator_tex, GLU_TESS_END, (callbackfunction_t)endCallback);
480 gluTessCallback(i->tesselator_tex, GLU_TESS_COMBINE, (callbackfunction_t)combineCallbackTex);
482 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);