44 lines
1.4 KiB
C++
44 lines
1.4 KiB
C++
#include "gridmesh.h"
|
|
|
|
GridMesh::GridMesh(Material* mat, int width, int height, bool alternate)
|
|
{
|
|
addGroup(mat);
|
|
for(int i=0; i<=width; ++i)
|
|
{
|
|
for(int j=0; j<=height; ++j)
|
|
{
|
|
float x = (float)i/(float)width;
|
|
float y = (float)j/(float)height;
|
|
addPosition(x-0.5f, 0, y-0.5f);
|
|
addNormal(0, 1, 0);
|
|
addTexCoord(x, y);
|
|
if(i > 0 && j > 0)
|
|
{
|
|
if(alternate && (i+j)%2)
|
|
{
|
|
addTriangle(getVertexId(i, j, height),
|
|
getVertexId(i, j-1, height),
|
|
getVertexId(i-1, j, height));
|
|
addTriangle(getVertexId(i-1, j-1, height),
|
|
getVertexId(i-1, j, height),
|
|
getVertexId(i, j-1, height));
|
|
}
|
|
else
|
|
{
|
|
addTriangle(getVertexId(i, j, height),
|
|
getVertexId(i-1, j-1, height),
|
|
getVertexId(i-1, j, height));
|
|
addTriangle(getVertexId(i, j, height),
|
|
getVertexId(i, j-1, height),
|
|
getVertexId(i-1, j-1, height));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int GridMesh::getVertexId(int i, int j, int height)
|
|
{
|
|
return i*(height+1) + j;
|
|
}
|