Пример #1
0
void mesh_map_xy_to_uv(struct mesh *m)
{
	float u0, v0, u1, v1, u2, v2, r;
	int i;

	r = mesh_calculate_xy_radius(m);
	if (m->tex)
		free(m->tex);

	m->tex = malloc(sizeof(*m->tex) * m->ntriangles * 3);
	if (!m->tex)
		return;

	for (i = 0; i < m->ntriangles; i++) {
		struct vertex *vtx0, *vtx1, *vtx2;

		vtx0 = m->t[i].v[0];
		vtx1 = m->t[i].v[1];
		vtx2 = m->t[i].v[2];

		u0 = vtx0->x / (r * 2.0) + 0.5;
		v0 = vtx0->y / (r * 2.0) + 0.5;
		u1 = vtx1->x / (r * 2.0) + 0.5;
		v1 = vtx1->y / (r * 2.0) + 0.5;
		u2 = vtx2->x / (r * 2.0) + 0.5;
		v2 = vtx2->y / (r * 2.0) + 0.5;

		mesh_set_triangle_texture_coords(m, i, u0, v0, u1, v1, u2, v2);
	}
	mesh_graph_dev_init(m);
}
Пример #2
0
/* this has a known issue mapping vertices of tris that span the "international date line" */
void mesh_sphere_uv_map(struct mesh *m)
{
	float u0, v0, u1, v1, u2, v2;
	int i;

	if (m->tex)
		free(m->tex);

	m->tex = malloc(sizeof(*m->tex) * m->ntriangles * 3);
	if (!m->tex)
		return;

	for (i = 0; i < m->ntriangles; i++) {
		struct vertex *vtx0, *vtx1, *vtx2;

		vtx0 = m->t[i].v[0];
		vtx1 = m->t[i].v[1];
		vtx2 = m->t[i].v[2];

		u0 = acosf(vtx0->z) / M_PI;
		v0 = (atan2f(vtx0->y, vtx0->x) + M_PI) / (2.0 * M_PI);
		u1 = acosf(vtx1->z) / M_PI;
		v1 = (atan2f(vtx1->y, vtx1->x) + M_PI) / (2.0 * M_PI);
		u2 = acosf(vtx2->z) / M_PI;
		v2 = (atan2f(vtx2->y, vtx2->x) + M_PI) / (2.0 * M_PI);

		mesh_set_triangle_texture_coords(m, i, u0, v0, u1, v1, u2, v2);
	}
	mesh_graph_dev_init(m);
}
Пример #3
0
static void mesh_uv_map_planetary_ring(struct mesh *m)
{
	float u0, u1, u2, r;
	int i;

	r = mesh_calculate_xy_radius(m);
	if (m->tex)
		free(m->tex);

	m->tex = malloc(sizeof(*m->tex) * m->ntriangles * 3);
	if (!m->tex)
		return;

	for (i = 0; i < m->ntriangles; i++) {
		struct vertex *vtx0, *vtx1, *vtx2;

		vtx0 = m->t[i].v[0];
		vtx1 = m->t[i].v[1];
		vtx2 = m->t[i].v[2];

		/* v will be a per-instance constant passed as uniform to the shader */
		u0 = (float) outside_edge_of_ring(vtx0, r);
		u1 = (float) outside_edge_of_ring(vtx1, r);
		u2 = (float) outside_edge_of_ring(vtx2, r);

		mesh_set_triangle_texture_coords(m, i, u0, 0.0f, u1, 0.0f, u2, 0.0f);
	}
	mesh_graph_dev_init(m);
}
Пример #4
0
struct mesh *init_line_mesh(double x1, double y1, double z1, double x2, double y2, double z2)
{
	struct mesh *my_mesh = malloc(sizeof(*my_mesh));

	if (!my_mesh)
		return my_mesh;
	memset(my_mesh, 0, sizeof(*my_mesh));

	my_mesh->geometry_mode = MESH_GEOMETRY_LINES;
	my_mesh->nvertices = 2;
	my_mesh->ntriangles = 0;
	my_mesh->nlines = 1;
	my_mesh->t = 0;
	my_mesh->v = malloc(sizeof(*my_mesh->v) * 2);
	my_mesh->l = malloc(sizeof(*my_mesh->l) * 1);
	my_mesh->tex = 0;
	my_mesh->radius = fmax(dist3d(x1, y1, z1), dist3d(x2, y2, z2));
	my_mesh->graph_ptr = 0;

	my_mesh->v[0].x = x1;
	my_mesh->v[0].y = y1;
	my_mesh->v[0].z = -z1;
	my_mesh->v[1].x = x2;
	my_mesh->v[1].y = y2;
	my_mesh->v[1].z = -z2;

	my_mesh->l[0].start = &my_mesh->v[0];
	my_mesh->l[0].end = &my_mesh->v[1];
	my_mesh->l[0].flag = 0;

	mesh_graph_dev_init(my_mesh);
	return my_mesh;
}
Пример #5
0
void mesh_rotate(struct mesh *m, union quat *q)
{
	int i;

	for (i = 0; i < m->nvertices; i++)
		vertex_rotate(&m->v[i], q);
	for (i = 0; i < m->ntriangles; i++)
		triangle_rotate_normals(&m->t[i], q);
	mesh_graph_dev_init(m);
}
Пример #6
0
void mesh_scale(struct mesh *m, float scale)
{
	int i;

	for (i = 0; i < m->nvertices; i++) {
		m->v[i].x *= scale;
		m->v[i].y *= scale;
		m->v[i].z *= scale;
	}
	m->radius = mesh_compute_radius(m);
	mesh_graph_dev_init(m);
}
Пример #7
0
static void copy_mesh_contents(struct mesh *copy, struct mesh *original)
{
	int i;

	copy->geometry_mode = original->geometry_mode;
	copy->ntriangles = original->ntriangles;
	copy->nvertices = original->nvertices;
	copy->nlines = original->nlines;
	copy->graph_ptr = 0;

	for (i = 0; i < original->nvertices; i++)
		copy->v[i] = original->v[i];

	for (i = 0; i < original->ntriangles; i++) {
		int v0, v1, v2;

		v0 = lookup_vertex(original, original->t[i].v[0]);
		v1 = lookup_vertex(original, original->t[i].v[1]);
		v2 = lookup_vertex(original, original->t[i].v[2]);

		copy->t[i].v[0] = &copy->v[v0];
		copy->t[i].v[1] = &copy->v[v1];
		copy->t[i].v[2] = &copy->v[v2];
		copy->t[i].n = original->t[i].n; 
		copy->t[i].vnormal[0] = original->t[i].vnormal[0];
		copy->t[i].vnormal[1] = original->t[i].vnormal[1];
		copy->t[i].vnormal[2] = original->t[i].vnormal[2];
	}

	for (i = 0; i < original->nlines; i++) {
		int v0, v1;

		v0 = lookup_vertex(original, original->l[i].start);
		v1 = lookup_vertex(original, original->l[i].end);

		copy->l[i].start = &copy->v[v0];
		copy->l[i].end = &copy->v[v1];
		copy->l[i].flag = original->l[i].flag;
		copy->l[i].additivity = original->l[i].additivity;
		copy->l[i].opacity = original->l[i].opacity;
		copy->l[i].tint_color = original->l[i].tint_color;
		copy->l[i].time_offset = original->l[i].time_offset;
	}
	if (original->tex)
		memcpy(copy->tex, original->tex, sizeof(*copy->tex) * original->ntriangles * 3);
	copy->radius = original->radius;
	if (original->material) {
		copy->material = malloc(sizeof(*copy->material));
		*copy->material = *original->material;
	}
	mesh_graph_dev_init(copy);
}
Пример #8
0
struct mesh *init_burst_rod_mesh(int streaks, double h, double r1, double r2)
{
	struct mesh *my_mesh = malloc(sizeof(*my_mesh));

	if (!my_mesh)
		return my_mesh;
	memset(my_mesh, 0, sizeof(*my_mesh));

	my_mesh->geometry_mode = MESH_GEOMETRY_PARTICLE_ANIMATION;

	my_mesh->nlines = streaks;
	my_mesh->nvertices = my_mesh->nlines * 2;
	my_mesh->ntriangles = 0;
	my_mesh->t = 0;
	my_mesh->v = malloc(sizeof(*my_mesh->v) * my_mesh->nvertices);
	my_mesh->l = malloc(sizeof(*my_mesh->l) * my_mesh->nlines);
	my_mesh->tex = 0;
	my_mesh->radius = fmax(sqrt(h * h / 4.0 + r1 * r1), sqrt(h * h / 4.0 + r2 * r2));
	my_mesh->graph_ptr = 0;

	int line_index;
	for (line_index = 0; line_index < streaks; line_index++) {

		float p = fabs(snis_random_float());
		float pr = p * (r2 - r1) + r1;
		float ph = p * h - h / 2.0;
		float pa = fabs(snis_random_float()) * 2.0 * M_PI;

		int v_index = line_index * 2;
		my_mesh->v[v_index + 0].x = ph;
		my_mesh->v[v_index + 0].y = 0;
		my_mesh->v[v_index + 0].z = 0;
		my_mesh->v[v_index + 1].x = ph;
		my_mesh->v[v_index + 1].y = sin(pa) * pr;
		my_mesh->v[v_index + 1].z = cos(pa) * pr;

		my_mesh->l[line_index].start = &my_mesh->v[v_index + 0];
		my_mesh->l[line_index].end = &my_mesh->v[v_index + 1];
		my_mesh->l[line_index].flag = 0;
		my_mesh->l[line_index].additivity = 1.0;
		my_mesh->l[line_index].opacity = 1.0;
		my_mesh->l[line_index].tint_color.red = 1.0;
		my_mesh->l[line_index].tint_color.green = 1.0;
		my_mesh->l[line_index].tint_color.blue = 1.0;
		my_mesh->l[line_index].time_offset = fabs(snis_random_float());
	}

	mesh_graph_dev_init(my_mesh);
	return my_mesh;
}
Пример #9
0
struct mesh *init_radar_circle_xz_plane_mesh(double x, double z, double r, int ticks, double tick_radius)
{
	int i;
	struct mesh *my_mesh = malloc(sizeof(*my_mesh));

	if (!my_mesh)
		return my_mesh;
	memset(my_mesh, 0, sizeof(*my_mesh));

	my_mesh->geometry_mode = MESH_GEOMETRY_LINES;
	my_mesh->nvertices = 0;
	my_mesh->ntriangles = 0;
	my_mesh->nlines = 0;
	my_mesh->t = 0;
	my_mesh->v = malloc(sizeof(*my_mesh->v) * (360 / 2 + 1 + ticks*2));
	my_mesh->l = malloc(sizeof(*my_mesh->l) * (1 + ticks));
	my_mesh->radius = dist3d(x, 0, z) + r;
	my_mesh->tex = 0;
	my_mesh->graph_ptr = 0;

	for (i = 0; i <= 360; i += 2) {
		my_mesh->v[my_mesh->nvertices].x = x + cos(i * M_PI / 180.0) * r;
		my_mesh->v[my_mesh->nvertices].y = 0;
		my_mesh->v[my_mesh->nvertices].z = z + sin(i * M_PI / 180.0) * r;
		my_mesh->nvertices++;
	}
	my_mesh->l[0].start = &my_mesh->v[0];
	my_mesh->l[0].end = &my_mesh->v[my_mesh->nvertices - 1];
	my_mesh->l[0].flag = MESH_LINE_STRIP;
	my_mesh->nlines++;

	for (i = 0; i < ticks; ++i) {
		double c = cos(i * 2.0 * M_PI / (double)ticks);
		double s = sin(i * 2.0 * M_PI / (double)ticks);
		mesh_add_point(my_mesh, x + c * (r - tick_radius), 0, z + s * (r - tick_radius));
		mesh_add_point(my_mesh, x + c * r, 0, z + s * r);
		mesh_add_line_last_2(my_mesh, MESH_LINE_DOTTED);
	}

	mesh_graph_dev_init(my_mesh);
	return my_mesh;
}
Пример #10
0
struct mesh *init_circle_mesh(double x, double z, double r, int npoints, double angle)
{
	int i;
	float increment;
	struct mesh *my_mesh = malloc(sizeof(*my_mesh));

	if (!my_mesh)
		return my_mesh;
	memset(my_mesh, 0, sizeof(*my_mesh));

	my_mesh->geometry_mode = MESH_GEOMETRY_LINES;
	my_mesh->nvertices = 0;
	my_mesh->ntriangles = 0;
	my_mesh->nlines = 1;
	my_mesh->t = 0;
	my_mesh->v = malloc(sizeof(*my_mesh->v) * (npoints + 1));
	my_mesh->l = malloc(sizeof(*my_mesh->l) * 1);
	my_mesh->tex = 0;
	my_mesh->radius = r;
	my_mesh->graph_ptr = 0;

	increment = angle / (float) npoints;
	for (i = 0; i <= npoints; i++) {
		float a = i * increment;
		my_mesh->v[my_mesh->nvertices].x = x + cos(a) * r;
		my_mesh->v[my_mesh->nvertices].y = 0;
		my_mesh->v[my_mesh->nvertices].z = z + sin(a) * r;
		my_mesh->nvertices++;
	}

	/* close the mesh */
	my_mesh->v[my_mesh->nvertices - 1].x = my_mesh->v[0].x;
	my_mesh->v[my_mesh->nvertices - 1].y = my_mesh->v[0].y;
	my_mesh->v[my_mesh->nvertices - 1].z = my_mesh->v[0].z;

	my_mesh->l[0].start = &my_mesh->v[0];
	my_mesh->l[0].end = &my_mesh->v[my_mesh->nvertices - 1];
	my_mesh->l[0].flag = MESH_LINE_STRIP;

	mesh_graph_dev_init(my_mesh);
	return my_mesh;
}
Пример #11
0
void mesh_derelict(struct mesh *m, float distortion)
{
	int i;

	for (i = 0; i < m->nvertices; i++) {
		float dx, dy, dz;

		dx = (float) (snis_randn(1000) / 1000.0 - 0.5) * distortion;
		dy = (float) (snis_randn(1000) / 1000.0 - 0.5) * (distortion / 10.0) - 0.5;
		dz = (float) (snis_randn(1000) / 1000.0 - 0.5) * (distortion / 10.0) - 0.5;

		if (m->v[i].x < 0) {
			m->v[i].x = dx;
			m->v[i].y += dy;
			m->v[i].z += dz;
		}
	}
	m->radius = mesh_compute_radius(m);
	for (i = 0; i < m->nvertices; i++)
		m->v[i].x -= m->radius / 2.0;
	m->radius = mesh_compute_radius(m);
	mesh_set_flat_shading_vertex_normals(m);
	mesh_graph_dev_init(m);
}
Пример #12
0
/* fabricate a tube of length h, radius r, with nfaces, parallel to x axis */
struct mesh *mesh_tube(float h, float r, float nfaces)
{
	struct mesh *m;
	int ntris = nfaces * 2;
	int nvertices = nfaces * 2;
	int i, j;
	float angle, da;

	float l = h / 2.0;

	m = allocate_mesh_for_copy(ntris, nvertices, 0, 1);
	if (!m)
		return m;

	m->geometry_mode = MESH_GEOMETRY_TRIANGLES;
	da = 2 * M_PI / (float) nfaces;
	angle = 0.0;
	for (i = 0; i < nvertices; i += 2) {
		m->v[i].x = -l;
		m->v[i].y = r * cos(angle);
		m->v[i].z = r * -sin(angle);
		m->v[i + 1].x = l;
		m->v[i + 1].y = m->v[i].y;
		m->v[i + 1].z = m->v[i].z;
		angle += da;
	}
	m->nvertices = nvertices;

	for (i = 0; i < ntris; i += 2) {
		m->t[i].v[2] = &m->v[i % nvertices];
		m->t[i].v[1] = &m->v[(i + 1) % nvertices];
		m->t[i].v[0] = &m->v[(i + 2) % nvertices];

		m->t[i + 1].v[2] = &m->v[(i + 2) % nvertices];
		m->t[i + 1].v[1] = &m->v[(i + 1) % nvertices];
		m->t[i + 1].v[0] = &m->v[(i + 3) % nvertices];
	}
	m->ntriangles = ntris;

	for (i = 0; i < ntris; i++) {
		union vec3 normal;
		normal = compute_triangle_normal(&m->t[i]);
		m->t[i].n.x = normal.v.x;
		m->t[i].n.y = normal.v.y;
		m->t[i].n.z = normal.v.z;
		for (j = 0; j < 3; j++) {
			union vec3 normal = { { 0, -m->t[i].v[j]->y, -m->t[i].v[j]->z } };
			vec3_normalize_self(&normal);
			m->t[i].vnormal[j].x = normal.v.x;
			m->t[i].vnormal[j].y = normal.v.y;
			m->t[i].vnormal[j].z = normal.v.z;
		}
	}
	mesh_set_flat_shading_vertex_normals(m);
	for (i = 0; i < ntris; i += 2) {
		mesh_set_triangle_texture_coords(m, i,
			0.0, (float) ((int) ((i + 2) / 2)) * 1.0 / (float) nfaces,
			1.0, (float) ((int) (i / 2)) / (float) nfaces,
			0.0, (float) ((int) (i / 2)) / (float) nfaces);
		mesh_set_triangle_texture_coords(m, i + 1,
			1.0, (float) ((int) ((i + 2) / 2)) / (float) nfaces,
			1.0, (float) ((int) (i / 2)) / (float) nfaces,
			0.0, (float) ((int) ((i + 2) / 2)) / (float) nfaces);
	}
	m->nlines = 0;
	m->radius = mesh_compute_radius(m);
	mesh_graph_dev_init(m);
	return m;
}
Пример #13
0
void mesh_distort_and_random_uv_map(struct mesh *m, float distortion)
{
	mesh_distort_helper(m, distortion);
	mesh_random_uv_map(m);
	mesh_graph_dev_init(m);
}
Пример #14
0
/* See: http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html */
struct mesh *mesh_unit_icosohedron(void)
{
	const double tau = (1.0 + sqrt(5.0)) / 2.0;
	const double scale = 1.0 / sqrt(1.0 + tau * tau);
	struct mesh *m;

	m = malloc(sizeof(*m));
	if (!m)
		return m;
	memset(m, 0, sizeof(*m));
	m->nvertices = 12;
	m->ntriangles = 20;

	m->t = malloc(sizeof(*m->t) * m->ntriangles);
	if (!m->t)
		goto bail;
	memset(m->t, 0, sizeof(*m->t) * m->ntriangles);
	m->v = malloc(sizeof(*m->v) * m->nvertices);
	if (!m->v)
		goto bail;
	memset(m->v, 0, sizeof(*m->v) * m->nvertices);
	m->tex = 0;
	/* m->tex = malloc(sizeof(*m->tex) * m->ntriangles * 3);
	if (!m->tex)
		goto bail;
	memset(m->tex, 0, sizeof(*m->tex) * m->ntriangles * 3); */
	m->l = NULL;

	m->geometry_mode = MESH_GEOMETRY_TRIANGLES;

	m->v[0].x = scale * -1.0;
	m->v[0].y = scale * tau;
	m->v[0].z = scale * 0.0;

	m->v[1].x = scale * 1.0;
	m->v[1].y = scale * tau;
	m->v[1].z = scale * 0.0;

	m->v[2].x = scale * -1.0;
	m->v[2].y = scale * -tau;
	m->v[2].z = scale * 0.0;

	m->v[3].x = scale * 1.0;
	m->v[3].y = scale * -tau;
	m->v[3].z = scale * 0.0;

	m->v[4].x = scale * 0.0;
	m->v[4].y = scale * -1.0;
	m->v[4].z = scale * tau;

	m->v[5].x = scale * 0.0;
	m->v[5].y = scale * 1.0;
	m->v[5].z = scale * tau;

	m->v[6].x = scale * 0.0;
	m->v[6].y = scale * -1.0;
	m->v[6].z = scale * -tau;

	m->v[7].x = scale * 0.0;
	m->v[7].y = scale * 1.0;
	m->v[7].z = scale * -tau;

	m->v[8].x = scale * tau;
	m->v[8].y = scale * 0.0;
	m->v[8].z = scale * -1.0;

	m->v[9].x = scale * tau;
	m->v[9].y = scale * 0.0;
	m->v[9].z = scale * 1.0;

	m->v[10].x = scale * -tau;
	m->v[10].y = scale * 0.0;
	m->v[10].z = scale * -1.0;

	m->v[11].x = scale * -tau;
	m->v[11].y = scale * 0.0;
	m->v[11].z = scale * 1.0;

	m->t[0].v[0] = &m->v[0];
	m->t[0].v[1] = &m->v[11];
	m->t[0].v[2] = &m->v[5];

	m->t[1].v[0] = &m->v[0];
	m->t[1].v[1] = &m->v[5];
	m->t[1].v[2] = &m->v[1];

	m->t[2].v[0] = &m->v[0];
	m->t[2].v[1] = &m->v[1];
	m->t[2].v[2] = &m->v[7];

	m->t[3].v[0] = &m->v[0];
	m->t[3].v[1] = &m->v[7];
	m->t[3].v[2] = &m->v[10];

	m->t[4].v[0] = &m->v[0];
	m->t[4].v[1] = &m->v[10];
	m->t[4].v[2] = &m->v[11];

	m->t[5].v[0] = &m->v[1];
	m->t[5].v[1] = &m->v[5];
	m->t[5].v[2] = &m->v[9];

	m->t[6].v[0] = &m->v[5];
	m->t[6].v[1] = &m->v[11];
	m->t[6].v[2] = &m->v[4];

	m->t[7].v[0] = &m->v[11];
	m->t[7].v[1] = &m->v[10];
	m->t[7].v[2] = &m->v[2];

	m->t[8].v[0] = &m->v[10];
	m->t[8].v[1] = &m->v[7];
	m->t[8].v[2] = &m->v[6];

	m->t[9].v[0] = &m->v[7];
	m->t[9].v[1] = &m->v[1];
	m->t[9].v[2] = &m->v[8];

	m->t[10].v[0] = &m->v[3];
	m->t[10].v[1] = &m->v[9];
	m->t[10].v[2] = &m->v[4];

	m->t[11].v[0] = &m->v[3];
	m->t[11].v[1] = &m->v[4];
	m->t[11].v[2] = &m->v[2];

	m->t[12].v[0] = &m->v[3];
	m->t[12].v[1] = &m->v[2];
	m->t[12].v[2] = &m->v[6];

	m->t[13].v[0] = &m->v[3];
	m->t[13].v[1] = &m->v[6];
	m->t[13].v[2] = &m->v[8];

	m->t[14].v[0] = &m->v[3];
	m->t[14].v[1] = &m->v[8];
	m->t[14].v[2] = &m->v[9];

	m->t[15].v[0] = &m->v[4];
	m->t[15].v[1] = &m->v[9];
	m->t[15].v[2] = &m->v[5];

	m->t[16].v[0] = &m->v[2];
	m->t[16].v[1] = &m->v[4];
	m->t[16].v[2] = &m->v[11];

	m->t[17].v[0] = &m->v[6];
	m->t[17].v[1] = &m->v[2];
	m->t[17].v[2] = &m->v[10];

	m->t[18].v[0] = &m->v[8];
	m->t[18].v[1] = &m->v[6];
	m->t[18].v[2] = &m->v[7];

	m->t[19].v[0] = &m->v[9];
	m->t[19].v[1] = &m->v[8];
	m->t[19].v[2] = &m->v[1];

	m->radius = mesh_compute_radius(m);
	mesh_set_flat_shading_vertex_normals(m);
	mesh_graph_dev_init(m);

	return m;

bail:
	mesh_free(m);
	return NULL;
}
Пример #15
0
void mesh_distort(struct mesh *m, float distortion)
{
	mesh_distort_helper(m, distortion);
	mesh_graph_dev_init(m);
}
Пример #16
0
struct mesh *mesh_fabricate_billboard(float cx, float cy, float width, float height)
{
	struct mesh *m;

	m = malloc(sizeof(*m));
	if (!m)
		return m;
	memset(m, 0, sizeof(*m));
	m->nvertices = 4;
	m->ntriangles = 2;

	m->t = malloc(sizeof(*m->t) * m->ntriangles);
	if (!m->t)
		goto bail;
	memset(m->t, 0, sizeof(*m->t) * m->ntriangles);
	m->v = malloc(sizeof(*m->v) * m->nvertices);
	if (!m->v)
		goto bail;
	memset(m->v, 0, sizeof(*m->v) * m->nvertices);
	m->tex = malloc(sizeof(*m->tex) * m->ntriangles * 3);
	if (!m->tex)
		goto bail;
	memset(m->tex, 0, sizeof(*m->tex) * m->ntriangles * 3);
	m->l = NULL;

	m->geometry_mode = MESH_GEOMETRY_TRIANGLES;
	m->v[0].x = -width / 2.0f + cx;
	m->v[0].y = height / 2.0f + cy;
	m->v[0].z = 0;
	m->v[1].x = width / 2.0f + cx;
	m->v[1].y = height / 2.0f + cy;
	m->v[1].z = 0;
	m->v[2].x = width / 2.0f + cx;
	m->v[2].y = -height / 2.0f + cy;
	m->v[2].z = 0;
	m->v[3].x = -width / 2.0f + cx;
	m->v[3].y = -height / 2.0f + cy;
	m->v[3].z = 0;

	m->t[0].v[0] = &m->v[0];
	m->t[0].v[1] = &m->v[2];
	m->t[0].v[2] = &m->v[1];
	m->t[0].flag = TRIANGLE_0_1_COPLANAR;
	mesh_set_triangle_texture_coords(m, 0, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f);

	m->t[1].v[0] = &m->v[0];
	m->t[1].v[1] = &m->v[3];
	m->t[1].v[2] = &m->v[2];
	m->t[1].flag = TRIANGLE_0_2_COPLANAR;
	mesh_set_triangle_texture_coords(m, 1, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);

	m->radius = mesh_compute_radius(m);
	mesh_set_flat_shading_vertex_normals(m);
	mesh_graph_dev_init(m);

	return m;

bail:
	mesh_free(m);
	return NULL;
}
Пример #17
0
/* mesh_fabricate_crossbeam fabricates a mesh like so, out of 8 triangles:
 *          0
 *         |\
 *         | \
 *         |  \
 *  4______|   \__5
 *   \     \    \ \
 *    \     \   |3 \
 *     \     \  |   \
 *      \ 1   \ |    \
 *       \_____\|_____\6
 *      7   \   |
 *           \  |
 *            \ |
 *             \|
 *              2
 * centered on origin, length axis parallel to x axis.
 * length is the distance betwee 0 and 3, above, and
 * radius is the distance between the center of the cross
 * beam and 2,6,7,3 and 0,4,5,1.
 * 
 * 8 triangles are needed because we need to prevent backface
 * culling, so we wind one set of tris one way, and the other,
 * the other.
 */
struct mesh *mesh_fabricate_crossbeam(float length, float radius)
{
	struct mesh *m;

	m = malloc(sizeof(*m));
	if (!m)
		return m;
	memset(m, 0, sizeof(*m));
	m->nvertices = 8;
	m->ntriangles = 8;

	m->t = malloc(sizeof(*m->t) * m->ntriangles);
	if (!m->t)
		goto bail;
	memset(m->t, 0, sizeof(*m->t) * m->ntriangles);
	m->v = malloc(sizeof(*m->v) * m->nvertices);
	if (!m->v)
		goto bail;
	memset(m->v, 0, sizeof(*m->v) * m->nvertices);
	m->tex = malloc(sizeof(*m->tex) * m->ntriangles * 3);
	if (!m->tex)
		goto bail;
	memset(m->tex, 0, sizeof(*m->tex) * m->ntriangles * 3);
	m->l = NULL;

	m->geometry_mode = MESH_GEOMETRY_TRIANGLES;
	m->v[0].x = -length / 2.0f;
	m->v[0].y = radius;
	m->v[0].z = 0.0f;
	m->v[1].x = -length / 2.0f;
	m->v[1].y = -radius;
	m->v[1].z = 0.0f;
	m->v[2].x = length / 2.0f;
	m->v[2].y = -radius;
	m->v[2].z = 0.0f;
	m->v[3].x = length / 2.0f;
	m->v[3].y = radius;
	m->v[3].z = 0.0f;
	m->v[4].x = -length / 2.0f;
	m->v[4].y = 0.0f;
	m->v[4].z = radius;
	m->v[5].x = -length / 2.0f;
	m->v[5].y = 0.0f;
	m->v[5].z = -radius;
	m->v[6].x = length / 2.0f;
	m->v[6].y = 0.0f;
	m->v[6].z = -radius;
	m->v[7].x = length / 2.0f;
	m->v[7].y = 0.0f;
	m->v[7].z = radius;

	m->t[0].v[0] = &m->v[0];
	m->t[0].v[1] = &m->v[1];
	m->t[0].v[2] = &m->v[2];
	mesh_set_triangle_texture_coords(m, 0, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);

	m->t[1].v[0] = &m->v[2];
	m->t[1].v[1] = &m->v[3];
	m->t[1].v[2] = &m->v[0];
	mesh_set_triangle_texture_coords(m, 1, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
	
	m->t[2].v[0] = &m->v[4];
	m->t[2].v[1] = &m->v[5];
	m->t[2].v[2] = &m->v[6];
	mesh_set_triangle_texture_coords(m, 2, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
	
	m->t[3].v[0] = &m->v[6];
	m->t[3].v[1] = &m->v[7];
	m->t[3].v[2] = &m->v[4];
	mesh_set_triangle_texture_coords(m, 3, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f);

	m->t[4].v[0] = &m->v[2];
	m->t[4].v[1] = &m->v[1];
	m->t[4].v[2] = &m->v[0];
	mesh_set_triangle_texture_coords(m, 4, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);

	m->t[5].v[0] = &m->v[0];
	m->t[5].v[1] = &m->v[3];
	m->t[5].v[2] = &m->v[2];
	mesh_set_triangle_texture_coords(m, 5, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
	
	m->t[6].v[0] = &m->v[6];
	m->t[6].v[1] = &m->v[5];
	m->t[6].v[2] = &m->v[4];
	mesh_set_triangle_texture_coords(m, 6, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
	
	m->t[7].v[0] = &m->v[4];
	m->t[7].v[1] = &m->v[7];
	m->t[7].v[2] = &m->v[6];
	mesh_set_triangle_texture_coords(m, 7, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f);

	mesh_compute_radius(m);
	mesh_set_flat_shading_vertex_normals(m);
	mesh_graph_dev_init(m);

	return m;

bail:
	mesh_free(m);
	return NULL;
}