X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fdevices%2Fopengl.c;h=a789f542b3d5392ad5528332ada5c1550c909e60;hb=66f6d2a40bed5716c469f0e2c8de1cc13e0022a2;hp=2a572165f94a6ef2982d18ba61c360767b15ae54;hpb=3d7281d36c24f2b20672639fe50d7e063412004f;p=swftools.git diff --git a/lib/devices/opengl.c b/lib/devices/opengl.c index 2a57216..a789f54 100644 --- a/lib/devices/opengl.c +++ b/lib/devices/opengl.c @@ -1,21 +1,23 @@ #include #include #include +#include #include "../gfxdevice.h" #include "../gfxtools.h" +#include #include #include #include #include -#define ZSTEP (1/65536.0) +//#define ZSTEP (1/65536.0) +#define ZSTEP (1/8.0) typedef struct _fontlist { gfxfont_t*font; - char*id; struct _fontlist*next; } fontlist_t; @@ -26,12 +28,14 @@ typedef struct _internal { int width, height; int currentz; + int config_polygonoutlines; + GLUtesselator *tesselator; + GLUtesselator *tesselator_line; GLUtesselator *tesselator_tex; } internal_t; -int verbose = 1; - +static int verbose = 0; static void dbg(char*format, ...) { char buf[1024]; @@ -77,6 +81,15 @@ void CALLBACK vertexCallback(GLvoid *vertex) double*xyz = (GLdouble*)vertex; glVertex3d(xyz[0],xyz[1],xyz[2]); } +void CALLBACK combineCallback(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out) +{ + GLdouble *vertex; + vertex = (GLdouble *) malloc(6 * sizeof(GLdouble)); + vertex[0] = coords[0]; + vertex[1] = coords[1]; + vertex[2] = coords[2]; + *out = vertex; +} void CALLBACK vertexCallbackTex(GLvoid *vertex) { double*v = (GLdouble*)vertex; @@ -98,7 +111,11 @@ void CALLBACK combineCallbackTex(GLdouble coords[3], GLdouble *data[4], GLfloat int opengl_setparameter(struct _gfxdevice*dev, const char*key, const char*value) { + internal_t*i = (internal_t*)dev->internal; dbg("setparameter %s=%s", key, value); + if(!strcmp(key, "polygonoutlines")) { + i->config_polygonoutlines = atoi(value); + } return 0; } @@ -123,12 +140,22 @@ void opengl_endclip(struct _gfxdevice*dev) 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) { + dbg("stroke"); internal_t*i = (internal_t*)dev->internal; + i->currentz++; char running = 0; gfxline_t*l=0; - dbg("stroke"); + glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0); + + //glLineWidth(width*64); + if(width <= 0) { + width = 1.0; + } glLineWidth(width); + double z = i->currentz*ZSTEP; + + glPolygonOffset(0.0, 500.0); l = line; while(l) { @@ -142,33 +169,38 @@ void opengl_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxc running = 1; glBegin(GL_LINE_STRIP); } - glVertex3d(l->x, l->y, (i->currentz*ZSTEP)); + glVertex3d(l->x, l->y, z); l=l->next; } if(running) { running = 0; glEnd(); } - i->currentz ++; + glLineWidth(1.0); } -void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color) +void tesselatePolygon(GLUtesselator*tesselator, double z, gfxline_t*line) { - internal_t*i = (internal_t*)dev->internal; - char running = 0; int len = 0; - double*xyz=0; gfxline_t*l=0; - dbg("fill"); - glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0); - - gluTessBeginPolygon(i->tesselator, NULL); + double lastx=0,lasty=0; + double*xyz=0; + char running = 0; + gluTessBeginPolygon(tesselator, NULL); l = line; len = 0; while(l) { - len++; + if(l->type == gfx_splineTo) { + double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))/2; + int steps = (int)c; + if(steps<1) steps = 1; + len += steps; + } else { + len++; + } l = l->next; } + //printf("full len:%d\n", len); xyz = malloc(sizeof(double)*3*len); l = line; len = 0; @@ -176,33 +208,70 @@ void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color) if(l->type == gfx_moveTo) { if(running) { running = 0; - gluTessEndContour(i->tesselator); + gluTessEndContour(tesselator); } } if(!running) { running = 1; - gluTessBeginContour(i->tesselator); + gluTessBeginContour(tesselator); } - xyz[len*3+0] = l->x; - xyz[len*3+1] = l->y; - xyz[len*3+2] = (i->currentz*ZSTEP); - gluTessVertex(i->tesselator, &xyz[len*3], &xyz[len*3]); - len++; + if(l->type == gfx_splineTo) { + int j; + double c = sqrt(abs(l->x-2*l->sx+lastx) + abs(l->y-2*l->sy+lasty))/2; + int steps = (int)c; + if(steps<1) steps = 1; + //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); + //printf("%f %f %f\n", lastx, l->sx, l->x); + //printf("%f %f %f\n", lasty, l->sy, l->y); + for(j=1;j<=steps;j++) { + //printf("%d\n", j); + double t = (double)j / (double)steps; + xyz[len*3+0] = lastx*(1-t)*(1-t) + 2*l->sx*(1-t)*t + l->x*t*t; + xyz[len*3+1] = lasty*(1-t)*(1-t) + 2*l->sy*(1-t)*t + l->y*t*t; + xyz[len*3+2] = z; + gluTessVertex(tesselator, &xyz[len*3], &xyz[len*3]); + len++; + } + //printf("%d\n", len); + } else { + xyz[len*3+0] = l->x; + xyz[len*3+1] = l->y; + xyz[len*3+2] = z; + gluTessVertex(tesselator, &xyz[len*3], &xyz[len*3]); + len++; + } + lastx = l->x; + lasty = l->y; l=l->next; } if(running) { running = 0; - gluTessEndContour(i->tesselator); + gluTessEndContour(tesselator); } - gluTessEndPolygon(i->tesselator); - i->currentz ++; + gluTessEndPolygon(tesselator); free(xyz); } +void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color) +{ + double z; + dbg("fill"); + internal_t*i = (internal_t*)dev->internal; + + glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0); + + i->currentz ++; + z = (i->currentz*ZSTEP); + tesselatePolygon(i->tesselator, z, line); + + //tesselatePolygon(i->tesselator_line, z, line); +} + void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform) { + dbg("fillbitmap"); internal_t*i = (internal_t*)dev->internal; char running = 0; int len = 0; @@ -210,6 +279,8 @@ void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gf gfxline_t*l=0; glColor4f(1.0,0,0,1.0); + i->currentz ++; + GLuint texIDs[1]; glGenTextures(1, texIDs); @@ -231,6 +302,22 @@ void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gf data[(y*newwidth+x)*4+2] = img->data[y*img->width+x].b; data[(y*newwidth+x)*4+3] = img->data[y*img->width+x].a; } + int lastx = img->width - 1; + for(;xdata[y*img->width+lastx].r; + data[(y*newwidth+x)*4+1] = img->data[y*img->width+lastx].g; + data[(y*newwidth+x)*4+2] = img->data[y*img->width+lastx].b; + data[(y*newwidth+x)*4+3] = img->data[y*img->width+lastx].a; + } + } + int lasty = img->height - 1; + for(;ydata[lasty*img->width+x].r; + data[(y*newwidth+x)*4+1] = img->data[lasty*img->width+x].g; + data[(y*newwidth+x)*4+2] = img->data[lasty*img->width+x].b; + data[(y*newwidth+x)*4+3] = img->data[lasty*img->width+x].a; + } } gfxmatrix_t m2; @@ -253,6 +340,7 @@ void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gf glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + gluTessBeginPolygon(i->tesselator_tex, NULL); l = line; len = 0; @@ -287,12 +375,12 @@ void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gf l=l->next; } + if(running) { running = 0; gluTessEndContour(i->tesselator_tex); } gluTessEndPolygon(i->tesselator_tex); - i->currentz ++; free(xyz); glDisable(GL_TEXTURE_2D); @@ -303,21 +391,20 @@ void opengl_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gr dbg("fillgradient"); } -void opengl_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font) +void opengl_addfont(gfxdevice_t*dev, gfxfont_t*font) { internal_t*i = (internal_t*)dev->internal; fontlist_t*last=0,*l = i->fontlist; while(l) { last = l; - if(!strcmp((char*)l->id, fontid)) { + if(!strcmp((char*)l->font->id, font->id)) { return; // we already know this font } l = l->next; } l = (fontlist_t*)rfx_calloc(sizeof(fontlist_t)); l->font = font; - l->id = strdup(fontid); l->next = 0; if(last) { last->next = l; @@ -326,26 +413,24 @@ void opengl_addfont(gfxdevice_t*dev, char*fontid, gfxfont_t*font) } } -void opengl_drawchar(gfxdevice_t*dev, char*fontid, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix) +void opengl_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix) { internal_t*i = (internal_t*)dev->internal; - if(i->font && i->fontid && !strcmp(fontid, i->fontid)) { + if(i->font && i->font->id && !strcmp(font->id, i->font->id)) { // current font is correct } else { fontlist_t*l = i->fontlist; i->font = 0; - i->fontid = 0; while(l) { - if(!strcmp((char*)l->id, fontid)) { + if(!strcmp((char*)l->font->id, font->id)) { i->font = l->font; - i->fontid = l->id; break; } l = l->next; } if(i->font == 0) { - fprintf(stderr, "Unknown font id: %s", fontid); + fprintf(stderr, "Unknown font id: %s", font->id); return; } } @@ -357,6 +442,8 @@ void opengl_drawchar(gfxdevice_t*dev, char*fontid, int glyphnr, gfxcolor_t*color opengl_fill(dev, line2, color); gfxline_free(line2); + i->currentz --; + return; } @@ -407,6 +494,8 @@ void gfxdevice_opengl_init(gfxdevice_t*dev) dbg("init"); internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t)); memset(dev, 0, sizeof(gfxdevice_t)); + + dev->name = "opengl"; dev->internal = i; @@ -429,6 +518,14 @@ void gfxdevice_opengl_init(gfxdevice_t*dev) gluTessCallback(i->tesselator, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback); gluTessCallback(i->tesselator, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback); gluTessCallback(i->tesselator, GLU_TESS_END, (callbackfunction_t)endCallback); + //gluTessCallback(i->tesselator, GLU_TESS_COMBINE, (callbackfunction_t)combineCallback); + + i->tesselator_line = gluNewTess(); + gluTessCallback(i->tesselator_line, GLU_TESS_ERROR, (callbackfunction_t)errorCallback); + gluTessCallback(i->tesselator_line, GLU_TESS_VERTEX, (callbackfunction_t)vertexCallback); + gluTessCallback(i->tesselator_line, GLU_TESS_BEGIN, (callbackfunction_t)beginCallback); + gluTessCallback(i->tesselator_line, GLU_TESS_END, (callbackfunction_t)endCallback); + gluTessProperty(i->tesselator_line, GLU_TESS_BOUNDARY_ONLY, 1.0); i->tesselator_tex = gluNewTess(); gluTessCallback(i->tesselator_tex, GLU_TESS_ERROR, (callbackfunction_t)errorCallback);