#ifndef _VERTEX_H_
#define _VERTEX_H_

#include "getglut.h"
#include "texture.h"

/* Verticies and triangles */
/* For the sake of simplicity, normals are handle on a per-triangle, not per-vertex, basis */

typedef struct {
	/* Position */
	GLfloat x;
	GLfloat y;
	GLfloat z;
	/* Texturing */
	GLfloat u;
	GLfloat v;
	/* Colour / alpha (diffuse) */
	GLfloat r;
	GLfloat g;
	GLfloat b;
	GLfloat a;
} vertex;

void vertex_init(vertex* v);

typedef struct triangle_s {
	/* A triangle is simply a triplet of verticies */
	vertex p;
	vertex q;
	vertex r;
	/* And a texture to be applied first, or 0 for previous */
	texture* tex;
	/* Better have a normal, too */
	GLfloat n[3];
	/* A 'next' pointer allows construction of meshes. Movables can be */
	/* used to add mesh positional information.                        */
	struct triangle_s* next;
} triangle;

void triangle_init(triangle* t);
void triangle_setv(triangle* t,
                   GLfloat x1, GLfloat y1, GLfloat z1,
                   GLfloat x2, GLfloat y2, GLfloat z2,
                   GLfloat x3, GLfloat y3, GLfloat z3);
/* tx/ty is the texture scale (number of repeats in that direction): */
/* the other dimension is calculated automatically */
/* Make a wall for the given points, with floor y1 and ceiling y2 */
triangle* triangle_makeroom (GLfloat x1, GLfloat z1, GLfloat x2, GLfloat z2,
                             GLfloat x3, GLfloat z3, GLfloat x4, GLfloat z4,
			                             texture *tex, GLfloat tx);
triangle* triangle_makeroomx(GLfloat x1, GLfloat z1, GLfloat x2, GLfloat z2,
                             GLfloat x3, GLfloat z3, GLfloat x4, GLfloat z4,
			     GLfloat y1, GLfloat y2, texture *tex, GLfloat tx);
/* Make wall from point (x1,z1) to (x2,z2) with floor y1 and ceiling y2 */
triangle* triangle_makewall (GLfloat x1, GLfloat z1, GLfloat x2, GLfloat z2,
                                                     texture *tex, GLfloat ty);
triangle* triangle_makewallx(GLfloat x1, GLfloat z1, GLfloat x2, GLfloat z2,
                             GLfloat y1, GLfloat y2, texture *tex, GLfloat ty);
/* Find the last triangle in a chain */
triangle* triangle_tail(triangle* t);
/* Autogenerate a triangle's normal. */
void triangle_autonormal(triangle* t);
/* Render a triangle. Doesn't adjust the matrix. */
void triangle_draw(triangle* t);

extern long polycount;

#endif
