I've been writing this code for polygon.c for the past week or so and I can't shift some bugs, so I hoped I'd be able to get some experienced eyes cast over it. Thanks in advance. =P
The intent is to generate a landscape from a series of heights. (There may be some small bugs in it because I recently unwrapped an array of arrays to form a single array to make my life easier and haven't fiddled everything else to compensate.)
It looks horrible because it was formatted for Notepad, because I was using that when drafting it. HTML is not kind to it.
The intent is to generate a landscape from a series of heights. (There may be some small bugs in it because I recently unwrapped an array of arrays to form a single array to make my life easier and haven't fiddled everything else to compensate.)
Code:
#include "sgl.h"
// function to calculate the normalised cross product of two vectors - why is this not part of the SGL already?
VECTOR slCrossProduct(VECTOR u, VECTOR v)
{
VECTOR cross =
{
slMulFX(u[Y],v[Z]) - slMulFX(u[Z],v[Y]),
slMulFX(u[Z],v[X]) - slMulFX(u[X],v[Z]),
slMulFX(u[X],v[Y]) - slMulFX(u[Y],v[X]),
};
// normalise X
if (cross[X] > toFIXED(0.0)) {cross[X] = toFIXED(1.0);}
else {cross[X] = toFIXED(-1.0);};
// normalise Y
if (cross[Y] > toFIXED(0.0)) {cross[Y] = toFIXED(1.0);}
else {cross[Y] = toFIXED(-1.0);};
// normalise Z
if (cross[Z] > toFIXED(0.0)) {cross[Z] = toFIXED(1.0);}
else {cross[Z] = toFIXED(-1.0);}
return cross;
}
// counters for loops
Uint32 m, n, t, u;
// size of grid in vertices
Uint32 nx = 5;
Uint32 nz = 5;
Uint32 r = 4;
Uint32 s = 4;
// size of grid in coordinate units
FIXED lx = toFIXED(50.0);
FIXED lz = toFIXED(50.0);
// elevation of each vertex
FIXED height[] =
{
{toFIXED(5.0), toFIXED(4.0), toFIXED(3.0), toFIXED(2.0), toFIXED(1.0),
toFIXED(4.0), toFIXED(3.5), toFIXED(3.0), toFIXED(2.5), toFIXED(2.0),
toFIXED(3.0), toFIXED(3.0), toFIXED(3.0), toFIXED(3.0), toFIXED(3.0),
toFIXED(2.0), toFIXED(2.5), toFIXED(3.0), toFIXED(3.5), toFIXED(4.0),
toFIXED(1.0), toFIXED(2.0), toFIXED(3.0), toFIXED(4.0), toFIXED(5.0)},
};
// vertex data
POINT vert[];
// normal vector data
NORMAL norm[];
// polygon data
POLYGON poly[];
// attribute data
ATTR attr[];
VECTOR v1,v2;
for (n = 0; n < nz; n++)
{
for(m = 0; m < nx; m++)
{
vert[q] = {slMulFX(toFIXED(m/nx), lx), height[((n-1)*nz)+m], slMulFX(toFIXED(n/nz), lz};
};
}
for(t = 0; t < r*s; t++)
{
v1 = vert[t+1] - vert[t];
v2 = vert[t+nz] - vert[t];
norm[t] = slCrossProduct(v1,v2);
poly[t] = {norm[t], VERTICES(t, t+1, t+nz, t+nz+1)};
// dummy attribute data for now
attr[t] = ATTRIBUTE(Dual_Plane, SORT_CEN, No_Texture, C_RGB(16, 16, 16), No_Gouraud, MESHoff, sprPolygon, No_Option);
}
// polygon model data - this bit only works if everything else works (fingers crossed!)
PDATA PD_SCAPE1 =
{
vert, sizeof(vert)/sizeof(POINT),
poly, sizeof(poly)/sizeof(POLYGON),
attr,
};
It looks horrible because it was formatted for Notepad, because I was using that when drafting it. HTML is not kind to it.