/*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* A spherical version of the Realtime Optimally-Adapting Meshes (ROAM)
* algorithm is use for drawing the surface of the planet. ROAM provide a
- * continuous level-of-detail mesh of the planet which is used by #GisOpenGL
- * when drawing surface textures for GisTiles.
+ * continuous level-of-detail mesh of the planet which is used by #GritsOpenGL
+ * when drawing surface textures for GritsTiles.
*
* This implementation of the ROAM algorithm is based on an octahedron as the
* base model.
#include <GL/glu.h>
#include "gpqueue.h"
-#include "gis-util.h"
+#include "grits-util.h"
#include "roam.h"
/*
*
* Returns: the new triangle
*/
-RoamTriangle *roam_triangle_new(RoamPoint *l, RoamPoint *m, RoamPoint *r)
+RoamTriangle *roam_triangle_new(RoamPoint *l, RoamPoint *m, RoamPoint *r,
+ RoamDiamond *parent)
{
RoamTriangle *triangle = g_new0(RoamTriangle, 1);
- triangle->error = 0;
- triangle->p.l = l;
- triangle->p.m = m;
- triangle->p.r = r;
- triangle->split = roam_point_new(
+ triangle->error = 0;
+ triangle->p.l = l;
+ triangle->p.m = m;
+ triangle->p.r = r;
+ triangle->parent = parent;
+ triangle->split = roam_point_new(
(l->lat + r->lat)/2,
(ABS(l->lat) == 90 ? r->lon :
ABS(r->lat) == 90 ? l->lon :
// triangle->split->lat, l->lat);
/* Update normal */
- double pa[3];
- double pb[3];
- pa[0] = triangle->p.l->x - triangle->p.m->x;
- pa[1] = triangle->p.l->y - triangle->p.m->y;
- pa[2] = triangle->p.l->z - triangle->p.m->z;
-
- pb[0] = triangle->p.r->x - triangle->p.m->x;
- pb[1] = triangle->p.r->y - triangle->p.m->y;
- pb[2] = triangle->p.r->z - triangle->p.m->z;
-
- triangle->norm[0] = pa[1] * pb[2] - pa[2] * pb[1];
- triangle->norm[1] = pa[2] * pb[0] - pa[0] * pb[2];
- triangle->norm[2] = pa[0] * pb[1] - pa[1] * pb[0];
-
- double total = sqrt(triangle->norm[0] * triangle->norm[0] +
- triangle->norm[1] * triangle->norm[1] +
- triangle->norm[2] * triangle->norm[2]);
-
- triangle->norm[0] /= total;
- triangle->norm[1] /= total;
- triangle->norm[2] /= total;
+ crossd3((gdouble*)triangle->p.l,
+ (gdouble*)triangle->p.m,
+ (gdouble*)triangle->p.r, triangle->norm);
+ normd(triangle->norm);
/* Store bounding box, for get_intersect */
RoamPoint *p[] = {l,m,r};
g_pqueue_remove(sphere->triangles, triangle->handle);
}
-static void roam_triangle_sync_neighbors(RoamTriangle *new, RoamTriangle *old, RoamTriangle *neigh)
+/* (neight->t.? == old) = new */
+static void roam_triangle_sync_neighbors(RoamTriangle *neigh, RoamTriangle *old, RoamTriangle *new)
{
if (neigh->t.l == old) neigh->t.l = new;
else if (neigh->t.b == old) neigh->t.b = new;
else if (neigh->t.r == old) neigh->t.r = new;
- else g_assert_not_reached();
}
static gboolean roam_triangle_visible(RoamTriangle *triangle, RoamSphere *sphere)
l->pz < 1 && m->pz < 1 && r->pz < 1;
}
+static gboolean roam_triangle_backface(RoamTriangle *triangle, RoamSphere *sphere)
+{
+ RoamPoint *l = triangle->p.l;
+ RoamPoint *m = triangle->p.m;
+ RoamPoint *r = triangle->p.r;
+ roam_point_update_projection(l, sphere->view);
+ roam_point_update_projection(m, sphere->view);
+ roam_point_update_projection(r, sphere->view);
+ double size = -( l->px * (m->py - r->py) +
+ m->px * (r->py - l->py) +
+ r->px * (l->py - m->py) ) / 2.0;
+ return size < 0;
+}
+
/**
* roam_triangle_update_errors:
* @triangle: the triangle
triangle->error = -1;
} else {
roam_point_update_projection(triangle->split, sphere->view);
- RoamPoint *l = triangle->p.l;
- RoamPoint *m = triangle->p.m;
- RoamPoint *r = triangle->p.r;
+ RoamPoint *l = triangle->p.l;
+ RoamPoint *m = triangle->p.m;
+ RoamPoint *r = triangle->p.r;
RoamPoint *split = triangle->split;
/* l-r midpoint projected l-r midpoint */
r->px * (l->py - m->py) ) / 2.0;
/* Size < 0 == backface */
- //if (size < 0)
- // triangle->error *= -1;
triangle->error *= size;
+
+ /* Give some preference to "edge" faces */
+ if (roam_triangle_backface(triangle->t.l, sphere) ||
+ roam_triangle_backface(triangle->t.b, sphere) ||
+ roam_triangle_backface(triangle->t.r, sphere))
+ triangle->error *= 50;
}
}
* @sphere: the sphere
*
* Split a triangle into two child triangles and update the sphere.
+ * triangle
*/
void roam_triangle_split(RoamTriangle *triangle, RoamSphere *sphere)
{
if (triangle != triangle->t.b->t.b)
g_assert_not_reached();
- RoamTriangle *base = triangle->t.b;
+ RoamTriangle *s = triangle; // Self
+ RoamTriangle *b = triangle->t.b; // Base
+
+ RoamDiamond *dia = roam_diamond_new(s, b);
/* Add new triangles */
RoamPoint *mid = triangle->split;
- RoamTriangle *sl = triangle->kids[0] = roam_triangle_new(triangle->p.m, mid, triangle->p.l); // triangle Left
- RoamTriangle *sr = triangle->kids[1] = roam_triangle_new(triangle->p.r, mid, triangle->p.m); // triangle Right
- RoamTriangle *bl = base->kids[0] = roam_triangle_new(base->p.m, mid, base->p.l); // Base Left
- RoamTriangle *br = base->kids[1] = roam_triangle_new(base->p.r, mid, base->p.m); // Base Right
+ RoamTriangle *sl = s->kids[0] = roam_triangle_new(s->p.m, mid, s->p.l, dia); // Self Left
+ RoamTriangle *sr = s->kids[1] = roam_triangle_new(s->p.r, mid, s->p.m, dia); // Self Right
+ RoamTriangle *bl = b->kids[0] = roam_triangle_new(b->p.m, mid, b->p.l, dia); // Base Left
+ RoamTriangle *br = b->kids[1] = roam_triangle_new(b->p.r, mid, b->p.m, dia); // Base Right
/* triangle,l, base, r, sphere */
- roam_triangle_add(sl, sr, triangle->t.l, br, sphere);
- roam_triangle_add(sr, bl, triangle->t.r, sl, sphere);
- roam_triangle_add(bl, br, base->t.l, sr, sphere);
- roam_triangle_add(br, sl, base->t.r, bl, sphere);
+ roam_triangle_add(sl, sr, s->t.l, br, sphere);
+ roam_triangle_add(sr, bl, s->t.r, sl, sphere);
+ roam_triangle_add(bl, br, b->t.l, sr, sphere);
+ roam_triangle_add(br, sl, b->t.r, bl, sphere);
- roam_triangle_sync_neighbors(sl, triangle, triangle->t.l);
- roam_triangle_sync_neighbors(sr, triangle, triangle->t.r);
- roam_triangle_sync_neighbors(bl, base, base->t.l);
- roam_triangle_sync_neighbors(br, base, base->t.r);
+ roam_triangle_sync_neighbors(s->t.l, s, sl);
+ roam_triangle_sync_neighbors(s->t.r, s, sr);
+ roam_triangle_sync_neighbors(b->t.l, b, bl);
+ roam_triangle_sync_neighbors(b->t.r, b, br);
/* Remove old triangles */
- roam_triangle_remove(triangle, sphere);
- roam_triangle_remove(base, sphere);
+ roam_triangle_remove(s, sphere);
+ roam_triangle_remove(b, sphere);
/* Add/Remove diamonds */
- RoamDiamond *diamond = roam_diamond_new(triangle, base, sl, sr, bl, br);
- roam_diamond_update_errors(diamond, sphere);
- roam_diamond_add(diamond, sphere);
- roam_diamond_remove(triangle->parent, sphere);
- roam_diamond_remove(base->parent, sphere);
+ roam_diamond_update_errors(dia, sphere);
+ roam_diamond_add(dia, sphere);
+ roam_diamond_remove(s->parent, sphere);
+ roam_diamond_remove(b->parent, sphere);
}
/**
*
* Returns: the new diamond
*/
-RoamDiamond *roam_diamond_new(
- RoamTriangle *parent0, RoamTriangle *parent1,
- RoamTriangle *kid0, RoamTriangle *kid1,
- RoamTriangle *kid2, RoamTriangle *kid3)
+RoamDiamond *roam_diamond_new(RoamTriangle *parent0, RoamTriangle *parent1)
{
RoamDiamond *diamond = g_new0(RoamDiamond, 1);
-
- diamond->kids[0] = kid0;
- diamond->kids[1] = kid1;
- diamond->kids[2] = kid2;
- diamond->kids[3] = kid3;
-
- kid0->parent = diamond;
- kid1->parent = diamond;
- kid2->parent = diamond;
- kid3->parent = diamond;
-
diamond->parents[0] = parent0;
diamond->parents[1] = parent1;
-
return diamond;
}
{
//g_message("roam_diamond_merge: %p, e=%f\n", diamond, diamond->error);
+ /* TODO: pick the best split */
sphere->polys -= 2;
- /* TODO: pick the best split */
- RoamTriangle **kids = diamond->kids;
+ /* Use nicer temp names */
+ RoamTriangle *s = diamond->parents[0]; // Self
+ RoamTriangle *b = diamond->parents[1]; // Base
+
+ RoamTriangle *sl = s->kids[0];
+ RoamTriangle *sr = s->kids[1];
+ RoamTriangle *bl = b->kids[0];
+ RoamTriangle *br = b->kids[1];
- /* Create triangles */
- RoamTriangle *triangle = diamond->parents[0];
- RoamTriangle *base = diamond->parents[1];
+ s->kids[0] = s->kids[1] = NULL;
+ b->kids[0] = b->kids[1] = NULL;
- roam_triangle_add(triangle, kids[0]->t.b, base, kids[1]->t.b, sphere);
- roam_triangle_add(base, kids[2]->t.b, triangle, kids[3]->t.b, sphere);
+ /* Add original triangles */
+ roam_triangle_sync_neighbors(s->t.l, sl, s);
+ roam_triangle_sync_neighbors(s->t.r, sr, s);
+ roam_triangle_sync_neighbors(b->t.l, bl, b);
+ roam_triangle_sync_neighbors(b->t.r, br, b);
- roam_triangle_sync_neighbors(triangle, kids[0], kids[0]->t.b);
- roam_triangle_sync_neighbors(triangle, kids[1], kids[1]->t.b);
- roam_triangle_sync_neighbors(base, kids[2], kids[2]->t.b);
- roam_triangle_sync_neighbors(base, kids[3], kids[3]->t.b);
+ roam_triangle_add(s, sl->t.b, b, sr->t.b, sphere);
+ roam_triangle_add(b, bl->t.b, s, br->t.b, sphere);
- /* Remove triangles */
- roam_triangle_remove(kids[0], sphere);
- roam_triangle_remove(kids[1], sphere);
- roam_triangle_remove(kids[2], sphere);
- roam_triangle_remove(kids[3], sphere);
+ roam_triangle_sync_neighbors(sl->t.b, sl, s);
+ roam_triangle_sync_neighbors(sr->t.b, sr, s);
+ roam_triangle_sync_neighbors(bl->t.b, bl, b);
+ roam_triangle_sync_neighbors(br->t.b, br, b);
- /* Clear kids */
- triangle->kids[0] = triangle->kids[1] = NULL;
- base->kids[0] = base->kids[1] = NULL;
+ /* Remove child triangles */
+ roam_triangle_remove(sl, sphere);
+ roam_triangle_remove(sr, sphere);
+ roam_triangle_remove(bl, sphere);
+ roam_triangle_remove(br, sphere);
/* Add/Remove triangles */
- if (triangle->t.l->t.l == triangle->t.r->t.r &&
- triangle->t.l->t.l != triangle && triangle->parent) {
- roam_diamond_update_errors(triangle->parent, sphere);
- roam_diamond_add(triangle->parent, sphere);
+ if (s->t.l->t.l == s->t.r->t.r &&
+ s->t.l->t.l != s && s->parent) {
+ roam_diamond_update_errors(s->parent, sphere);
+ roam_diamond_add(s->parent, sphere);
}
- if (base->t.l->t.l == base->t.r->t.r &&
- base->t.l->t.l != base && base->parent) {
- roam_diamond_update_errors(base->parent, sphere);
- roam_diamond_add(base->parent, sphere);
+ if (b->t.l->t.l == b->t.r->t.r &&
+ b->t.l->t.l != b && b->parent) {
+ roam_diamond_update_errors(b->parent, sphere);
+ roam_diamond_add(b->parent, sphere);
}
/* Remove and free diamond and child triangles */
roam_diamond_remove(diamond, sphere);
- g_assert(diamond->kids[0]->p.m == diamond->kids[1]->p.m &&
- diamond->kids[1]->p.m == diamond->kids[2]->p.m &&
- diamond->kids[2]->p.m == diamond->kids[3]->p.m);
- g_assert(diamond->kids[0]->p.m->tris == 0);
- roam_triangle_free(diamond->kids[0]);
- roam_triangle_free(diamond->kids[1]);
- roam_triangle_free(diamond->kids[2]);
- roam_triangle_free(diamond->kids[3]);
+ g_assert(sl->p.m == sr->p.m &&
+ sr->p.m == bl->p.m &&
+ bl->p.m == br->p.m);
+ g_assert(sl->p.m->tris == 0);
+ roam_triangle_free(sl);
+ roam_triangle_free(sr);
+ roam_triangle_free(bl);
+ roam_triangle_free(br);
g_free(diamond);
}
sphere->roots[i] = roam_triangle_new(
vertexes[_triangles[i][0][0]],
vertexes[_triangles[i][0][1]],
- vertexes[_triangles[i][0][2]]);
+ vertexes[_triangles[i][0][2]],
+ NULL);
for (int i = 0; i < 8; i++)
roam_triangle_add(sphere->roots[i],
sphere->roots[_triangles[i][1][0]],
gint roam_sphere_split_merge(RoamSphere *sphere)
{
gint iters = 0, max_iters = 500;
+ //gint target = 20000;
//gint target = 4000;
gint target = 2000;
//gint target = 500;