From 47e1b12580b2b182e2e419ce2a7096950672cf1e Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Mon, 26 May 2003 20:13:42 +0000 Subject: [PATCH] add painting --- render2/.cvsignore | 2 + render2/Makefile.am | 3 +- render2/color.c | 2 + render2/instance.h | 4 + render2/paint.c | 338 ++++++++++++++++++++++++++++++++++++++++++++ render2/render.h | 27 +++- render2/surface.c | 18 ++- render2/surface.h | 21 ++- render2/texture.c | 4 +- 9 files changed, 409 insertions(+), 10 deletions(-) create mode 100644 render2/paint.c diff --git a/render2/.cvsignore b/render2/.cvsignore index 5a3f2d5a..da8adfca 100644 --- a/render2/.cvsignore +++ b/render2/.cvsignore @@ -11,3 +11,5 @@ debug.lo color.lo font.lo surface.lo +paint.lo +texture.lo diff --git a/render2/Makefile.am b/render2/Makefile.am index 8f9c4d21..3e3fe1fb 100644 --- a/render2/Makefile.am +++ b/render2/Makefile.am @@ -22,7 +22,8 @@ libobrender2_la_SOURCES=\ debug.c \ font.c \ surface.c \ - texture.h + texture.c \ + paint.c noinst_HEADERS=\ render.h \ diff --git a/render2/color.c b/render2/color.c index fe14e030..1458c0e5 100644 --- a/render2/color.c +++ b/render2/color.c @@ -13,10 +13,12 @@ int RrColorParse(struct RrInstance *inst, const char *colorname, ret->r = 0.0; ret->g = 0.0; ret->b = 0.0; + ret->a = 0.0; return 0; } ret->r = (xcol.red >> 8) / 255.0; ret->g = (xcol.green >> 8) / 255.0; ret->b = (xcol.blue >> 8) / 255.0; + ret->a = 0.0; return 1; } diff --git a/render2/instance.h b/render2/instance.h index e386cc70..da55fd81 100644 --- a/render2/instance.h +++ b/render2/instance.h @@ -21,6 +21,10 @@ void RrInstanceFree(struct RrInstance *inst); #define RrDisplay(i) ((i)->display) #define RrScreen(i) ((i)->screen) +#define RrScreenWidth(i) (WidthOfScreen(ScreenOfDisplay((i)->display, \ + (i)->screen))) +#define RrScreenHeight(i) (HeightOfScreen(ScreenOfDisplay((i)->display, \ + (i)->screen))) #define RrDepth(i) ((i)->visinfo.depth) #define RrVisual(i) ((i)->visinfo.visual) #define RrColormap(i) ((i)->cmap) diff --git a/render2/paint.c b/render2/paint.c new file mode 100644 index 00000000..2443c5c3 --- /dev/null +++ b/render2/paint.c @@ -0,0 +1,338 @@ +#include "render.h" +#include "surface.h" +#include "instance.h" +#include "debug.h" +#include +#include + +void planar_copy_parent(struct RrSurface *sur) +{ + int ncols; + int copy; + + switch (RrPlanarColorType(sur)) { + case RR_SURFACE_NONE: + return; + case RR_SURFACE_SOLID: + ncols = 1; + break; + case RR_SURFACE_HORIZONTAL: + ncols = 2; + break; + case RR_SURFACE_VERTICAL: + ncols = 2; + break; + case RR_SURFACE_DIAGONAL: + ncols = 2; + break; + case RR_SURFACE_CROSSDIAGONAL: + ncols = 2; + break; + case RR_SURFACE_PYRAMID: + ncols = 2; + break; + case RR_SURFACE_PIPECROSS: + ncols = 2; + break; + case RR_SURFACE_RECTANGLE: + ncols = 2; + break; + } + + copy = 0; + if (ncols >= 1 && RrColorHasAlpha(RrPlanarPrimaryColor(sur))) + copy = 1; + if (ncols >= 1 && RrColorHasAlpha(RrPlanarSecondaryColor(sur))) + copy = 1; + if (copy) { + /* + struct RrSurface *parent = RrSurfaceParent(sur); + + XXX COPY PARENT PLS + */ + } +} + +void paint_planar(struct RrSurface *sur, int x, int y, int w, int h) +{ + float pr,pg,pb; + float sr, sg, sb; + float ar, ag, ab; + + pr = RrColorRed(RrPlanarPrimaryColor(sur)); + pg = RrColorGreen(RrPlanarPrimaryColor(sur)); + pb = RrColorBlue(RrPlanarPrimaryColor(sur)); + sr = RrColorRed(RrPlanarSecondaryColor(sur)); + sg = RrColorGreen(RrPlanarSecondaryColor(sur)); + sb = RrColorBlue(RrPlanarSecondaryColor(sur)); + switch (RrPlanarColorType(sur)) { + case RR_SURFACE_NONE: + return; + case RR_SURFACE_SOLID: + glBegin(GL_TRIANGLES); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glVertex2i(x+w, y); + glVertex2i(x+w, y+h); + + glVertex2i(x+w, y+h); + glVertex2i(x, y+h); + glVertex2i(x, y); + glEnd(); + break; + case RR_SURFACE_HORIZONTAL: + glBegin(GL_TRIANGLES); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w, y); + glVertex2i(x+w, y+h); + + glVertex2i(x+w, y+h); + glColor3f(pr, pg, pb); + glVertex2i(x, y+h); + glVertex2i(x, y); + glEnd(); + break; + case RR_SURFACE_VERTICAL: + glBegin(GL_TRIANGLES); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glVertex2i(x+w, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w, y+h); + + glVertex2i(x+w, y+h); + glVertex2i(x, y+h); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glEnd(); + break; + case RR_SURFACE_DIAGONAL: + ar = (pr + sr) / 2.0; + ag = (pg + sg) / 2.0; + ab = (pb + sb) / 2.0; + glBegin(GL_TRIANGLES); + glColor3f(ar, ag, ab); + glVertex2i(x, y); + glColor3f(pr, pg, pb); + glVertex2i(x+w, y); + glColor3f(ar, ag, ab); + glVertex2i(x+w, y+h); + + glColor3f(ar, ag, ab); + glVertex2i(x+w, y+h); + glColor3f(sr, sg, sb); + glVertex2i(x, y+h); + glColor3f(ar, ag, ab); + glVertex2i(x, y); + glEnd(); + break; + case RR_SURFACE_CROSSDIAGONAL: + ar = (pr + sr) / 2.0; + ag = (pg + sg) / 2.0; + ab = (pb + sb) / 2.0; + glBegin(GL_TRIANGLES); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glColor3f(ar, ag, ab); + glVertex2i(x+w, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w, y+h); + + glColor3f(sr, sg, sb); + glVertex2i(x+w, y+h); + glColor3f(ar, ag, ab); + glVertex2i(x, y+h); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glEnd(); + break; + case RR_SURFACE_PYRAMID: + ar = (pr + sr) / 2.0; + ag = (pg + sg) / 2.0; + ab = (pb + sb) / 2.0; + glBegin(GL_TRIANGLES); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(ar, ag, ab); + glVertex2i(x, y+h/2); + + glVertex2i(x, y+h/2); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x, y+h); + + glVertex2i(x, y+h); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(ar, ag, ab); + glVertex2i(x+w/2, y+h); + + glVertex2i(x+w/2, y+h); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x+w, y+h); + + glVertex2i(x+w, y+h); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(ar, ag, ab); + glVertex2i(x+w, y+h/2); + + glVertex2i(x+w, y+h/2); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x+w, y); + + glVertex2i(x+w, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(ar, ag, ab); + glVertex2i(x+w/2, y); + + glVertex2i(x+w/2, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glEnd(); + break; + case RR_SURFACE_PIPECROSS: + glBegin(GL_TRIANGLES); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glVertex2i(x, y+h/2); + + glVertex2i(x, y+h/2); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x, y+h); + + glVertex2i(x, y+h); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glVertex2i(x+w/2, y+h); + + glVertex2i(x+w/2, y+h); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x+w, y+h); + + glVertex2i(x+w, y+h); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glVertex2i(x+w, y+h/2); + + glVertex2i(x+w, y+h/2); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x+w, y); + + glVertex2i(x+w, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glVertex2i(x+w/2, y); + + glVertex2i(x+w/2, y); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glEnd(); + break; + case RR_SURFACE_RECTANGLE: + glBegin(GL_TRIANGLES); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x, y+h); + + glVertex2i(x, y+h); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x+w, y+h); + + glVertex2i(x+w, y+h); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x+w, y); + + glVertex2i(x+w, y); + glColor3f(sr, sg, sb); + glVertex2i(x+w/2, y+h/2); + glColor3f(pr, pg, pb); + glVertex2i(x, y); + + glEnd(); + break; + } +} + +/*! Paints the surface, and all its children */ +void RrSurfacePaint(struct RrSurface *sur) +{ + RrSurfacePaintArea(sur, 0, 0, RrSurfaceWidth(sur), RrSurfaceHeight(sur)); +} + +/*! Paints the surface, and all its children, but only in the given area. */ +void RrSurfacePaintArea(struct RrSurface *sur, + int x, + int y, + int w, + int h) +{ + struct RrInstance *inst; + int ok; + + inst = RrSurfaceInstance(sur); + + /* can't paint a prototype! */ + assert(inst); + if (!inst) return; + + assert(x >= 0 && y >= 0); + if (!(x >= 0 && y >= 0)) return; + assert(x + w < RrSurfaceWidth(sur) && y + h < RrSurfaceHeight(sur)); + if (!(x + w < RrSurfaceWidth(sur) && y + h < RrSurfaceHeight(sur))) return; + + RrDebug("making %p, %p, %p current\n", + RrDisplay(inst), RrSurfaceWindow(sur), RrContext(inst)); + + ok = glXMakeCurrent(RrDisplay(inst), RrSurfaceWindow(sur),RrContext(inst)); + assert(ok); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho(0, RrScreenWidth(inst), RrScreenHeight(inst), 0, 0, 10); + glViewport(0, 0, RrScreenWidth(inst), RrScreenHeight(inst)); + glMatrixMode(GL_MODELVIEW); + glTranslatef(-RrSurfaceX(sur), + RrScreenHeight(inst)-RrSurfaceHeight(sur)-RrSurfaceY(sur), 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + switch (RrSurfaceType(sur)) { + case RR_SURFACE_PLANAR: + planar_copy_parent(sur); + paint_planar(sur, x, y, w, h); + break; + case RR_SURFACE_NONPLANAR: + assert(0); + break; + } + + glXSwapBuffers(RrDisplay(inst), RrSurfaceWindow(sur)); +} diff --git a/render2/render.h b/render2/render.h index 90a5b56e..4151b0b0 100644 --- a/render2/render.h +++ b/render2/render.h @@ -40,13 +40,15 @@ struct RrColor { }; /*! Returns the red component for an RrColor */ -#define RrColorRed(c) (c)->r +#define RrColorRed(c) (c).r /*! Returns the green component for an RrColor */ -#define RrColorGreen(c) (c)->g +#define RrColorGreen(c) (c).g /*! Returns the blue component for an RrColor */ -#define RrColorBlue(c) (c)->b +#define RrColorBlue(c) (c).b /*! Returns the alpha component for an RrColor */ -#define RrColorAlpha(c) (c)->a +#define RrColorAlpha(c) (c).a +/*! Returns if an RrColor is non-opaque */ +#define RrColorHasAlpha(c) ((c).a > 0.0000001) /*! Sets the values of all components for an RrColor */ #define RrColorSet(c, w, x, y, z) (c)->r = (w), (c)->g = (x), \ @@ -127,6 +129,12 @@ struct RrSurface *RrSurfaceCopyChild(struct RrSurface *sur, struct RrSurface *parent); void RrSurfaceFree(struct RrSurface *sur); +void RrSurfaceSetArea(struct RrSurface *sur, + int x, + int y, + int w, + int h); + Window RrSurfaceWindow(struct RrSurface *sur); /* textures */ @@ -162,4 +170,15 @@ void RrTextureSetText(struct RrSurface *sur, enum RrLayout layout, const char *text); +/* drawing */ + +/*! Paints the surface, and all its children */ +void RrSurfacePaint(struct RrSurface *sur); +/*! Paints the surface, and all its children, but only in the given area. */ +void RrSurfacePaintArea(struct RrSurface *sur, + int x, + int y, + int w, + int h); + #endif diff --git a/render2/surface.c b/render2/surface.c index ab265ef7..b436f6e1 100644 --- a/render2/surface.c +++ b/render2/surface.c @@ -18,6 +18,10 @@ static struct RrSurface *surface_new(enum RrSurfaceType type, memset(sur->texture, 0, sizeof(struct RrTexture) * numtex); } else sur->texture = NULL; + sur->x = 0; + sur->y = 0; + sur->w = 1; + sur->h = 1; return sur; } @@ -70,8 +74,6 @@ struct RrSurface *RrSurfaceNewChild(enum RrSurfaceType type, sur->inst = parent->inst; sur->win = create_window(sur->inst, parent->win); sur->parent = parent; - sur->parentx = 0; - sur->parenty = 0; return sur; } @@ -137,6 +139,18 @@ void RrSurfaceFree(struct RrSurface *sur) } } +void RrSurfaceSetArea(struct RrSurface *sur, + int x, + int y, + int w, + int h) +{ + sur->x = x; + sur->y = y; + sur->w = w; + sur->h = h; +} + Window RrSurfaceWindow(struct RrSurface *sur) { /* can't get a window for a prototype */ diff --git a/render2/surface.h b/render2/surface.h index 9218b67c..6391beb9 100644 --- a/render2/surface.h +++ b/render2/surface.h @@ -38,10 +38,27 @@ struct RrSurface { struct RrTexture *texture; struct RrSurface *parent; - int parentx; - int parenty; + + int x; + int y; + int w; + int h; }; struct RrTexture *RrSurfaceTexture(struct RrSurface *sur, int texnum); +#define RrSurfaceInstance(sur) ((sur)->inst) +#define RrSurfaceType(sur) ((sur)->type) + +#define RrSurfaceParent(sur) ((sur)->parent) + +#define RrSurfaceX(sur) ((sur)->x) +#define RrSurfaceY(sur) ((sur)->y) +#define RrSurfaceWidth(sur) ((sur)->w) +#define RrSurfaceHeight(sur) ((sur)->h) + +#define RrPlanarColorType(sur) ((sur)->data.planar.colortype) +#define RrPlanarPrimaryColor(sur) ((sur)->data.planar.primary) +#define RrPlanarSecondaryColor(sur) ((sur)->data.planar.primary) + #endif diff --git a/render2/texture.c b/render2/texture.c index d2b6da61..24726d0b 100644 --- a/render2/texture.c +++ b/render2/texture.c @@ -1,5 +1,7 @@ #include "texture.h" #include "surface.h" +#include +#include void texture_free(struct RrTexture *tex) { @@ -9,7 +11,7 @@ void texture_free(struct RrTexture *tex) case RR_TEXTURE_TEXT: free(tex->data.text.string); break; - case RR_TEXTUER_RGBA: + case RR_TEXTURE_RGBA: break; } tex->type = RR_TEXTURE_NONE; -- 2.34.1