~rabbits/moogle

8c164cd2531775b4186872e198806073a55895ef — neauoire 1 year, 5 months ago 90611a2
Optimized extrude/symmetry
2 files changed, 124 insertions(+), 65 deletions(-)

M lib/structures.c
M src/moogle.c
M lib/structures.c => lib/structures.c +67 -29
@@ 1,40 1,78 @@
Mesh *
createdoorway(Scene *s)
createdoorway(Scene *s, double width, double height, double depth, int color)
{
	Mesh *doorway = addmesh(s);

	/* outset */
	addarc(doorway, 14, 6, 180, 1);
	addline(doorway, Pt3d(14, 0, 0), Pt3d(14, -40, 0), 1);
	addline(doorway, Pt3d(-14, 0, 0), Pt3d(-14, -40, 0), 1);
	extrude(doorway, Pt3d(0, 0, 10), 1);

	/* joint */ addline(doorway, Pt3d(10, 0, 0), Pt3d(14, 0, 0), 1);
	/* joint */ addline(doorway, Pt3d(-10, 0, 0), Pt3d(-14, 0, 0), 1);
	/* ceil */ addline(doorway, Pt3d(-14, -40, 10), Pt3d(14, -40, 10), 1);

	/* inset */
	addarc(doorway, 10, 6, 180, 1);
	addarc(doorway, 4, 16, 360, 1);
	addline(doorway, Pt3d(10, 0, 0), Pt3d(10, -40, 0), 1);
	addline(doorway, Pt3d(-10, 0, 0), Pt3d(-10, -40, 0), 1);
	addline(doorway, Pt3d(-10, -40, 0), Pt3d(10, -40, 0), 1);
	addline(doorway, Pt3d(0, -4, 0), Pt3d(0, -40, 0), 1);
	addline(doorway, Pt3d(-14, -40, 0), Pt3d(-10, -40, 0), 1);
	addline(doorway, Pt3d(14, -40, 0), Pt3d(10, -40, 0), 1);
	/* origin */
	translate(doorway, 0, 20, 0);

	Point3d a = Pt3d(-width, 0, 0),
			b = Pt3d(-width, height, 0),
			c = Pt3d(0, height + width, 0),
			d = Pt3d(width, height, 0),
			e = Pt3d(width, 0, 0);
	addline(doorway, a, b, color);
	addline(doorway, b, c, color);
	addline(doorway, c, d, color);
	addline(doorway, d, e, color);
	addline(doorway, e, a, color);
	extrude(doorway, 0, 0, depth, color);
	return doorway;
}

Mesh *
createstairs(Scene *s)
createstairs(Scene *s, double width, int steps, int color)
{
	int i;
	Point3d a, b, c, d;
	Mesh *stairs = addmesh(s);
	rotate(createplane(s, 30, 4, 1, 1, 1), 90, 0, 0);
	translate(rotate(createplane(s, 30, 4, 1, 1, 1), 90, 0, 0), 0, -4, 4);
	translate(rotate(createplane(s, 30, 4, 1, 1, 1), 90, 0, 0), 0, -8, 8);
	translate(rotate(createplane(s, 30, 4, 1, 1, 1), 90, 0, 0), 0, -12, 12);
	for(i = 0; i < steps; ++i) {
		a = Pt3d(-width, i + 1, i), b = Pt3d(width, i + 1, i), c = Pt3d(-width, i + 1, 1 + i), d = Pt3d(width, i + 1, 1 + i);
		addline(stairs, a, b, color);
		addline(stairs, c, d, color);
		addline(stairs, a, c, color);
		addline(stairs, b, d, color);
		addline(stairs, a, Pt3d(a.x, a.y - 1, a.z), color);
		addline(stairs, b, Pt3d(b.x, b.y - 1, b.z), color);
	}
	addline(stairs, c, Pt3d(c.x, 0, c.z), color);
	addline(stairs, d, Pt3d(d.x, 0, d.z), color);
	addline(stairs, Pt3d(c.x, 0, c.z - steps), Pt3d(c.x, 0, c.z), color);
	addline(stairs, Pt3d(d.x, 0, d.z - steps), Pt3d(d.x, 0, d.z), color);
	return stairs;
}

Mesh *
createring(Scene *s, double radius, double thickness, double depth, int segs, double angle, int color)
{
	Mesh *ring = addmesh(s);
	/* front */
	addarc(ring, radius, segs, angle, color);
	addarc(ring, radius - thickness, segs, angle, color);
	addedge(ring, ring->vertices, ring->vertices + segs + 1, color);
	addedge(ring, ring->vertices + segs, ring->vertices + segs * 2 + 1, color);
	/* back */
	moveto(s, 0, 0, depth);
	addarc(ring, radius, segs, angle, color);
	addarc(ring, radius - thickness, segs, angle, color);
	addedge(ring, ring->vertices + segs * 3 + 2, ring->vertices + segs * 4 + 3, color);
	addedge(ring, ring->vertices + segs * 2 + 2, ring->vertices + segs * 3 + 3, color);
	/* connects */
	addedge(ring, ring->vertices, ring->vertices + segs * 2 + 2, color);
	addedge(ring, ring->vertices + segs, ring->vertices + segs * 3 + 2, color);
	addedge(ring, ring->vertices + segs + 1, ring->vertices + segs * 3 + 3, color);
	addedge(ring, ring->vertices + segs * 2 + 1, ring->vertices + segs * 4 + 3, color);
	reset(s);
	return ring;
}

Mesh *
createrain(Scene *s, double width, double height, double depth, double min, double max, int seed, int density, int color)
{
	int x, y;
	Mesh *rain = addmesh(s);
	for(y = 0; y < density; ++y)
		for(x = 0; x < density; ++x)
			if((x + y * density) % seed == 0)
				addline(rain,
					Pt3d(x * 2 - width / 2, y * 2 - height / 2, cos(x + y * density) * depth / 10),
					Pt3d(x * 2 - width / 2, y * 2 - height / 2, sin(x + y * density) * depth / 10),
					1);
	return rain;
}
\ No newline at end of file

M src/moogle.c => src/moogle.c +57 -36
@@ 81,8 81,7 @@ static Uint32 theme[] = {
	0x000000,
	0XFFFFFF,
	0x72DEC2,
	0x666666,
	0x222222};
	0xFF0000};

static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL;


@@ 91,6 90,12 @@ static Uint32 *pixels;

/* helpers */

double
clamp(double val, double min, double max)
{
	return (val >= min) ? (val <= max) ? val : max : min;
}

static Point2d
Pt2d(double x, double y)
{


@@ 280,16 285,6 @@ addedge(Mesh *m, Point3d *a, Point3d *b, int color)
	return &m->edges[m->edgeslen++];
}

static Mesh *
addmesh(Scene *s)
{
	if(s->len == MLIMIT) {
		fprintf(stderr, "Warning: Reached mesh limit\n");
		return NULL;
	}
	return &s->meshes[s->len++];
}

/* Primitives */

static Mesh *


@@ 656,38 651,30 @@ rotate(Mesh *m, double pitch, double yaw, double roll)
Mesh *
extrude(Mesh *m, double x, double y, double z, int color)
{
	int i, j, vl = m->verticeslen, el = m->edgeslen;
	for(i = 0; i < vl; i++) {
		Point3d *a = &m->vertices[i];
		addedge(m, &m->vertices[i], addvertex(m, a->x + x, a->y + y, a->z + z), color);
	}
	for(i = 0; i < el; i++) {
		Edge *e0 = &m->edges[i];
		Edge *e1 = addedge(m, e0->a, e0->b, e0->color);
		for(j = 0; j < vl; j++) {
			if(e0->a == &m->vertices[j])
				e1->a = &m->vertices[vl + j];
			else if(e0->b == &m->vertices[j])
				e1->b = &m->vertices[vl + j];
		}
	}
	int i, vl = m->verticeslen, el = m->edgeslen;
	for(i = 0; i < vl; i++)
		addedge(m,
			&m->vertices[i],
			addvertex(m, m->vertices[i].x + x, m->vertices[i].y + y, m->vertices[i].z + z),
			color);
	for(i = 0; i < el; i++)
		addedge(m,
			&m->vertices[vl + m->edges[i].a - &m->vertices[0]],
			&m->vertices[vl + m->edges[i].b - &m->vertices[0]],
			color);
	return m;
}

Mesh *
symmetry(Mesh *m, double x, double y, double z, int color)
{
	int i, el, vl = m->verticeslen;
	for(i = 0; i < vl; i++)
		addvertex(m,
			m->vertices[i].x * x,
			m->vertices[i].y * y,
			m->vertices[i].z * z);
	el = m->edgeslen;
	int i, el = m->edgeslen, vl = m->verticeslen;
	for(i = 0; i < vl; ++i)
		m->vertices[vl + i] = Pt3d(m->vertices[i].x * x, m->vertices[i].y * y, m->vertices[i].z * z);
	for(i = 0; i < el; i++)
		addedge(m,
			el + 1 + m->edges[i].a,
			el + 1 + m->edges[i].b,
			&m->vertices[vl + m->edges[i].a - &m->vertices[0]],
			&m->vertices[vl + m->edges[i].b - &m->vertices[0]],
			color);
	return m;
}


@@ 695,6 682,40 @@ symmetry(Mesh *m, double x, double y, double z, int color)
/* Primitives */

Mesh *
addmesh(Scene *s)
{
	if(s->len == MLIMIT) {
		fprintf(stderr, "Warning: Reached mesh limit\n");
		return NULL;
	}
	return &s->meshes[s->len++];
}

Mesh *
copymesh(Scene *s, Mesh *a)
{
	int i;
	Mesh *b = addmesh(s);
	b->verticeslen = a->verticeslen;
	b->edgeslen = a->edgeslen;
	b->position = a->position;
	for(i = 0; i < a->verticeslen; ++i)
		b->vertices[i] = a->vertices[i];
	for(i = 0; i < a->edgeslen; ++i) {
		b->edges[i].color = a->edges[i].color;
		b->edges[i].a = &b->vertices[a->edges[i].a - &a->vertices[0]];
		b->edges[i].b = &b->vertices[a->edges[i].b - &a->vertices[0]];
	}
	return b;
}

Mesh *
createshape(Scene *s, double radius, double segs, int color)
{
	return addarc(addmesh(s), radius, segs, 360, color);
}

Mesh *
createfrustum(Scene *s, double radius, int segs, double depth, double cap, int color)
{
	int i;