room101/SURFACE.CPP

69 lines
2.2 KiB
C++
Raw Permalink Normal View History

#include "surface.hpp"
#include "polygon.hpp"
#include "main.hpp"
/*******************************************************************************
** SurfaceCreate
*******************************************************************************/
void
SurfaceCreate(Surface* surface, Vector poly[], int n, int colour, float angle, float z)
{
Vector up = {0.0f, 1.0f, 0.0f};
Matrix reverse;
Vector lo, hi;
Vector* tmp = (Vector*) malloc(n * sizeof(Vector));
ThrowIf(!tmp, "Could not allocate temporary surface memory");
memset(surface, 0, sizeof(Surface));
surface->colour = colour;
TrianglePlane(poly, surface->plane);
/*
** Calculate surface matrix.
*/
MatrixView (surface->matrix, surface->plane, up, angle);
VectorNegate(surface->matrix[0], surface->matrix[0]);
VectorNegate(surface->matrix[1], surface->matrix[1]);
/*
** Transform polygon into surface space and calculate surface extents.
*/
PolygonTransform(tmp, poly, n, surface->matrix);
PolygonExtent(lo, hi, tmp, n);
/*
** Snap polygon extents to lightmap grid.
*/
surface->lo[0] = (float)floor(lo[0] / 16.0f) * 16.0f;
surface->lo[1] = (float)floor(lo[1] / 16.0f) * 16.0f;
surface->lo[2] = lo[2];
surface->hi[0] = (float)ceil (hi[0] / 16.0f) * 16.0f;
surface->hi[1] = (float)ceil (hi[1] / 16.0f) * 16.0f;
surface->hi[2] = hi[2];
/*
** Calculate surfaces width & height.
*/
surface->width = (int)surface->hi[0] - (int)surface->lo[0];
surface->height = (int)surface->hi[1] - (int)surface->lo[1];
ThrowIf(surface->width <= 0, "Zero width surface");
ThrowIf(surface->height <= 0, "Zero height surface");
/*
** Calculate surfaces PMN vectors.
*/
VectorCreate(surface->PMN[0], surface->lo[0], surface->lo[1], surface->lo[2] + z);
VectorCreate(surface->PMN[1], surface->hi[0] - surface->lo[0], 0.0f, 0.0f);
VectorCreate(surface->PMN[2], 0.0f, surface->hi[1] - surface->lo[1], 0.0f);
/*
** Transform PMN vectors back into world space.
*/
MatrixTranspose(reverse, surface->matrix);
TriangleTransform(surface->PMN, surface->PMN, reverse);
free(tmp);
}