qtorus.c 6.75 KB
/*
 *	Generate a mesh of quads
 */

#include <stdio.h>
#include <stdlib.h>
#include <gl/image.h>
#include "math.h"
#define DEG2RAD( a) ((a)*(float)(3.141/180.))


#define TRUE -1
#define FALSE 0

#define	XMIN	(-300)
#define	XMAX	300
#define	YMIN	(-300)
#define	YMAX	300

#define WIDTH 100
#define RADIUS 600

#define	TEX_ONE	4096
#define	TILE_MAXX	32
#define	TILE_MAXY	32

#define VTX_ELS 13 /* x, y, z, s, t, w, r, g, b, a */
#define VX 0
#define VY 1
#define VZ 2
#define VS 3
#define VT 4
#define VW 5
#define VR 6
#define VG 7
#define VB 8
#define VA 9
#define	VNX 10
#define	VNY 11
#define	VNZ 12

int dup_color = FALSE;
int dup_texture = FALSE;
char default_file[] = "six.t";
char *inputfile = default_file;

struct Vertex {
	float e[VTX_ELS];	
};

struct Vertex	vtx[4] = {
	{XMIN, YMIN, 0, 	0, 4095, 1,		255, 0, 0, 255},
	{XMAX, YMIN, 0, 	4095, 4095, 1,		255, 255, 0, 255},
	{XMIN, YMAX, 0, 	0, 0, 1,		0, 255, 0, 255},
	{XMAX, YMAX, 0, 	4095, 0, 1,		0, 0, 255, 255},
};

extern sortst(struct Vertex *, struct Vertex *, struct Vertex *);

main(argc, argv)
int	argc;
char	**argv;
{
	int xstep = 1, ystep = 1;
	struct Vertex vx0, vx1, vx2, vx3;
	struct Vertex vy0, vy1, vy2, vy3;
	struct Vertex st0, st1;
	int	x, y;
	float	xdel, ydel;
	int	sdel, tdel;
	int i, j;

	int tex_w, tex_h;

	float xang, yang;
	float xangd, yangd;
	float sin_val, cos_val;
	float xa, za, xb, zb;
	float nxa, nza, nxb, nzb;
	float width, radius;

	width = WIDTH;
	radius = RADIUS;

while (--argc) {
        if ((*++argv)[0] == '-') {
        switch ((*argv)[1]) {
        case 'c':       dup_color = TRUE; break;
        case 't':       dup_texture = TRUE; break;
	case 'f':       argc--; argv++;
			inputfile = *argv;
			break;
	case 'x':       argc--; argv++;
                        sscanf( *argv, "%d", &xstep); break;
	case 'y':       argc--; argv++;
                        sscanf( *argv, "%d", &ystep); break;

       }}}

	get_texdim(inputfile, &tex_w, &tex_h);
	do_init();

	xangd = (float)360/(xstep);
	yangd = (float)360/(ystep);
	xdel = (XMAX-XMIN)/xstep;
	ydel = (YMAX-YMIN)/ystep;
	sdel = (float)tex_w/xstep + 0.5;
	tdel = (float)tex_h/ystep + 0.5;

	/* XXX 4/4/4/4 for now */
	/* must be <= 31 for edges of bilinear filter*/
	if (sdel >= TILE_MAXX || tdel >= TILE_MAXY) {
		fprintf(stderr, "texture tile does not fit\n");
		exit(0);
	}

	yang = 0;
	for (y=0; y<ystep; y++) {

                ydel = 1.0 - fabs( (float)2*(yang)/360 - 1.0);
		lerp_vtx( 3, &vtx[0], &vtx[2], ydel, &vy0);
		lerp_vtx( 3, &vtx[1], &vtx[3], ydel, &vy1);
                ydel = 1.0 - fabs( (float)2*(yang+yangd)/360 - 1.0);
		lerp_vtx( 3, &vtx[0], &vtx[2], ydel, &vy2);
		lerp_vtx( 3, &vtx[1], &vtx[3], ydel, &vy3);

		xang = -xangd/2;
	for (x=0; x<xstep; x++) {

		xdel = xang;
		if (xdel < 0) xdel = 360 + xdel;
                xdel = 1.0 - fabs( (float)2*(xdel)/360 - 1.0);
		lerp_vtx( 3, &vy0, &vy1, xdel, &vx0);
		lerp_vtx( 3, &vy2, &vy3, xdel, &vx2);
                xdel = 1.0 - fabs( (float)2*(xang+xangd)/360 - 1.0);
		lerp_vtx( 3, &vy0, &vy1, xdel, &vx1);
		lerp_vtx( 3, &vy2, &vy3, xdel, &vx3);

	/* rotate [W,0,0] vector around y axis */ 
		cos_val = cos( DEG2RAD( xang));
		sin_val = sin( DEG2RAD( xang));
		xa = cos_val * width + radius;
		za = sin_val * width;
		nxa = cos_val;
		nza = sin_val;
		cos_val = cos( DEG2RAD( xang+xangd));
		sin_val = sin( DEG2RAD( xang+xangd));
		xb = cos_val * width + radius;
		zb = sin_val * width;
		nxb = cos_val;
		nzb = sin_val;
	/* rotate [X,0,Z] vector around z axis */
		cos_val = cos( DEG2RAD( yang));
		sin_val = sin( DEG2RAD( yang));
		vx0.e[VX] = xa * cos_val;
		vx0.e[VY] = xa * sin_val;
		vx0.e[VZ] = za;
		vx1.e[VX] = xb * cos_val;
		vx1.e[VY] = xb * sin_val;
		vx1.e[VZ] = zb;
		vx0.e[VNX] = nxa * cos_val;
		vx0.e[VNY] = nxa * sin_val;
		vx0.e[VNZ] = nza;
		vx1.e[VNX] = nxb * cos_val;
		vx1.e[VNY] = nxb * sin_val;
		vx1.e[VNZ] = nzb;
		cos_val = cos( DEG2RAD( yang+yangd));
		sin_val = sin( DEG2RAD( yang+yangd));
		vx2.e[VX] = xa * cos_val;
		vx2.e[VY] = xa * sin_val;
		vx2.e[VZ] = za;
		vx3.e[VX] = xb * cos_val;
		vx3.e[VY] = xb * sin_val;
		vx3.e[VZ] = zb;
		vx2.e[VNX] = nxa * cos_val;
		vx2.e[VNY] = nxa * sin_val;
		vx2.e[VNZ] = nza;
		vx3.e[VNX] = nxb * cos_val;
		vx3.e[VNY] = nxb * sin_val;
		vx3.e[VNZ] = nzb;
		if (dup_color) {
			copy_color( &vtx[0], &vx0);
			copy_color( &vtx[1], &vx1);
			copy_color( &vtx[2], &vx2);
			copy_color( &vtx[3], &vx3);
			}
		if (dup_texture) {
			copy_texture( &vtx[0], &vx0);
			copy_texture( &vtx[1], &vx1);
			copy_texture( &vtx[2], &vx2);
			copy_texture( &vtx[3], &vx3);
			}
		sortst(&vx0, &vx1, &st0);
		sortst(&vx2, &vx3, &st1);
		sortst(&st0, &st1, &st0);
		do_tramload(&st0, sdel, tdel, tex_w, tex_h);
		do_quad();
		do_vertex( &vx0);
		do_vertex( &vx1);
		do_vertex( &vx2);
		do_vertex( &vx3);
		xang += xangd;
		}
		yang += yangd;
		}
}

do_init()
{
	printf("WINDOW\t\t640 480 0x7fff\n\n");
	printf("LD_TEXTURE\t%s\tRGBA4444\n\n", inputfile);
	printf("FRUSTUM\n");
	printf("-0.128 0.128 -0.096 0.096 1.0 32000.0\n\n");
	printf("LD_MATRIX\n");
	printf("1 1 1\n0 0 0\n0 0 -2000\n\n");
	printf("MARK\n");
	printf("CLEAR\t\t0x404040\n");
	printf("LIGHT\t0.1 0.1 0.1 0.1 0.5 0.5 0.6 0.5 0.7 0.7 0.7 0.7 0.58 -0.58 0.58\n");
}

do_quad()
{
	printf("QUAD\n");
}

do_vertex( v)
struct Vertex *v;
{
int i;
	for (i=0; i<VNX; i++) printf("%d ", (int)v->e[i]);
	for (i=VNX; i<VTX_ELS; i++) printf("%f ", v->e[i]);
	printf("\n");
}

do_tramload(vx0, sdel, tdel, w, h)
struct Vertex *vx0;
int	sdel;
int	tdel;
int	w;
int	h;
{
	int	s, t;

	s = (vx0->e[VS] / TEX_ONE * w);
	t = (vx0->e[VT] / TEX_ONE * h);

	/* XXX assume 4/4/4/4 & 4 samples per tram word */
	sdel = (sdel + 3) >> 2; sdel <<=2;
	tdel = (tdel + 3) >> 2; tdel <<=2;
	printf("LD_TRAM\t%s %d %d %d %d %d\n", inputfile, s, t, sdel, tdel, 0);
}

do_normals(struct Vertex *v)
{
	int	i;
	float	k;
	k = 1/sqrt(v->e[VX] * v->e[VX] + v->e[VY] * v->e[VY] + v->e[VZ] * v->e[VZ]);
	v->e[VNX] = v->e[VX] * k;
	v->e[VNY] = v->e[VY] * k;
	v->e[VNZ] = v->e[VZ] * k;

	/* preserve only 8 bits of precision */
	for (i=VNX; i<=VNZ; i++) v->e[i] = (int)(v->e[i]*256)/256.0;
}

sortst(struct Vertex *st0, struct Vertex *st1, struct Vertex *st2)
{
	int i;

	for (i=VS; i<=VT; i++)
		st2->e[i] = (st0->e[i] < st1->e[i]) ? st0->e[i] : st1->e[i];
}

get_texdim(char *file, int *w, int *h)
{
	IMAGE	*img;

	if ((img = iopen(file, "r")) == (IMAGE *)NULL) {
		fprintf(stderr, "can't open texture file %s\n", file);
		exit(EXIT_FAILURE);
	}

	*w = img->xsize;
	*h = img->ysize;
}

lerp_vtx( first, vn, vf, a, vo)
struct Vertex *vn, *vf, *vo;
float a;
{
int i;
for (i=first; i<9; i++)
vo->e[i] = vn->e[i] + (vf->e[i] - vn->e[i]) * a;
}
copy_color( vi, vo)
struct Vertex *vi, *vo;
{
	vo->e[VR] = vi->e[VR];
	vo->e[VG] = vi->e[VG];
	vo->e[VB] = vi->e[VB];
	vo->e[VA] = vi->e[VA];
	}
copy_texture( vi, vo)
struct Vertex *vi, *vo;
{
	vo->e[VS] = vi->e[VS];
	vo->e[VT] = vi->e[VT];
	vo->e[VW] = vi->e[VW];
	}