43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
#include "gridmesh.h"
|
|
|
|
GridMesh::GridMesh(int width, int height, bool alternate) : Mesh()
|
|
{
|
|
for(int i=0; i<=width; ++i)
|
|
{
|
|
for(int j=0; j<=height; ++j)
|
|
{
|
|
Vertex v;
|
|
v.position = glm::vec3((float)i/(float)width, (float)j/(float)height, 0);
|
|
v.normal = glm::vec3(0, 0, 1);
|
|
v.texCoord = glm::vec2(v.position.x, v.position.y);
|
|
addVertex(v);
|
|
if(i > 0 && j > 0)
|
|
{
|
|
if(alternate && (i+j)%2)
|
|
{
|
|
addFace(getVertexId(i, j),
|
|
getVertexId(i-1, j),
|
|
getVertexId(i, j-1));
|
|
addFace(getVertexId(i-1, j-1),
|
|
getVertexId(i, j-1),
|
|
getVertexId(i-1, j));
|
|
}
|
|
else
|
|
{
|
|
addFace(getVertexId(i, j),
|
|
getVertexId(i-1, j),
|
|
getVertexId(i-1, j-1));
|
|
addFace(getVertexId(i, j),
|
|
getVertexId(i-1, j-1),
|
|
getVertexId(i, j-1));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int GridMesh::getVertexId(int i, int j)
|
|
{
|
|
return i*(height+1) + j;
|
|
}
|