summaryrefslogtreecommitdiff
path: root/meme
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2014-09-13 01:43:06 -0300
committerWerner Almesberger <werner@almesberger.net>2014-09-13 01:43:06 -0300
commitcd04bf72f39f290297305021a4788f830f897747 (patch)
treef294ff25affb79930aa0061ee011935b79e23f17 /meme
parent3bba45ef5ecdc435b46b0ab99aeab728c36b8b9e (diff)
downloadmisc-cd04bf72f39f290297305021a4788f830f897747.tar.gz
misc-cd04bf72f39f290297305021a4788f830f897747.tar.bz2
misc-cd04bf72f39f290297305021a4788f830f897747.zip
meme/: add YZ plane cut
All this doesn't look very good. Should sort facets for drawing from differnent angles then do depth grading.
Diffstat (limited to 'meme')
-rw-r--r--meme/gui.c55
-rw-r--r--meme/plane.c59
-rw-r--r--meme/plane.h1
3 files changed, 115 insertions, 0 deletions
diff --git a/meme/gui.c b/meme/gui.c
index 711e5eb..715bac9 100644
--- a/meme/gui.c
+++ b/meme/gui.c
@@ -114,6 +114,30 @@ static int coord_xz(int x, int y, Sint16 *res_x, Sint16 *res_y)
}
+static inline int yz_z_model2screen(int z)
+{
+ /* center at cursor in right 1/4 of screen */
+ return XRES-(XRES >> 3) - ((z-cursor->z) >> zoom);
+}
+
+
+static int coord_yz(int x, int y, Sint16 *res_x, Sint16 *res_y)
+{
+ x = yz_z_model2screen(x);
+ y = y_model2screen(y);
+
+ if (x < -32768 || x > 32767)
+ return -1;
+ if (y < -32768 || y > 32767)
+ return -1;
+
+ *res_x = x;
+ *res_y = y;
+
+ return x >= 0 && x < XRES && y >= 0 && y < YRES;
+}
+
+
/* ----- Draw the mesh ----------------------------------------------------- */
@@ -335,6 +359,36 @@ static void show_xz_profile(SDL_Surface *s)
}
+static void show_yz_profile(SDL_Surface *s)
+{
+ struct point *yz;
+ unsigned n, i;
+ Sint16 xa = 0, ya = 0, xb = 0, yb = 0;
+ /* initialize because gcc seems to "see" that they can be left
+ uninitialized by coord_xz but then fails to notice that
+ we track this via the return value. */
+ int a, b;
+
+ yz = plane_yz(cursor->x, &n);
+
+ a = coord_yz(yz[0].x, yz[0].y, &xa, &ya);
+ change(s, XRES - (XRES >> 2)-1, 0, XRES >> 2, YRES);
+ SDL_LockSurface(s);
+ for (i = 1; i < n; i++) {
+ b = coord_yz(yz[i].x, yz[i].y, &xb, &yb);
+ if (a >= 0 && b >= 0 && (a || b))
+ aalineColor(s, xa, ya, xb, yb, PROFILE_RGBA);
+ xa = xb;
+ ya = yb;
+ a = b;
+ }
+ aalineColor(s, XRES - (XRES >> 3), 0, XRES - (XRES >> 3), YRES-1, PROFILE_RGBA);
+ SDL_UnlockSurface(s);
+
+ free(yz);
+}
+
+
/* ----- Find nearest vertex ----------------------------------------------- */
@@ -411,6 +465,7 @@ static void motion_event(SDL_Surface *s, SDL_MouseMotionEvent *motion)
cursor = find_nearest(x, y);
show_cursor(s, cursor);
show_xz_profile(s);
+ show_yz_profile(s);
apply_changes(s);
}
diff --git a/meme/plane.c b/meme/plane.c
index a2a392c..4fdcaf3 100644
--- a/meme/plane.c
+++ b/meme/plane.c
@@ -45,6 +45,9 @@ static void add_point(struct cut *cut, int x, int y)
}
+/* ----- XZ plane ---------------------------------------------------------- */
+
+
static bool cut_xz(const struct vertex *a, const struct vertex *b, void *user)
{
struct cut *cut = user;
@@ -96,3 +99,59 @@ struct point *plane_xz(int y, unsigned *n)
*n = cut.n-1;
return cut.res;
}
+
+
+/* ----- YZ plane ---------------------------------------------------------- */
+
+
+static bool cut_yz(const struct vertex *a, const struct vertex *b, void *user)
+{
+ struct cut *cut = user;
+ int x = cut->coord;
+ int dx, dy, dz;
+ float f;;
+
+ if ((a->x > x || b->x < x) && (a->x < x || b->x > x))
+ return 0;
+
+ if (a->x == b->x) {
+ add_point(cut, (a->z + b->z) / 2, (a->y + b->y) / 2);
+ return 0;
+ }
+
+ dx = a->x - b->x;
+ dy = a->y - b->y;
+ dz = a->z - b->z;
+ f = (x - b->x)/(float) dx;
+ add_point(cut, b->z + f * dz, b->y + f * dy);
+
+ return 0;
+}
+
+
+static int comp_y(const void *a, const void *b)
+{
+ const struct point *pa = a;
+ const struct point *pb = b;
+
+ if (pa->y < pb->y)
+ return -1;
+ if (pa->y > pb->y)
+ return 1;
+ return 0;
+}
+
+
+struct point *plane_yz(int x, unsigned *n)
+{
+ struct cut cut = {
+ .coord = x,
+ .res = NULL,
+ .n = 0,
+ };
+
+ edge_foreach(cut_yz, &cut);
+ qsort(cut.res, cut.n, sizeof(struct point), comp_y);
+ *n = cut.n-1;
+ return cut.res;
+}
diff --git a/meme/plane.h b/meme/plane.h
index 2389504..fb810f3 100644
--- a/meme/plane.h
+++ b/meme/plane.h
@@ -23,5 +23,6 @@ struct point {
struct point *plane_xz(int y, unsigned *n);
+struct point *plane_yz(int x, unsigned *n);
#endif /* !PLANE_H */