Created
September 12, 2014 15:28
-
-
Save tgfrerer/a0ebe97b37bc7e008c2a to your computer and use it in GitHub Desktop.
square mesh generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void generateMesh () { | |
int cMeshWidth = 200; // mesh dimensions | |
int cMeshHeight = 200; | |
// setup mesh | |
vector<ofVec3f> vertices; | |
vector<ofIndexType> indices; | |
vector<ofVec2f> texCoords; | |
{ | |
int delta = 2; // mesh density | |
float oneOverWidth = 1.0 / float(cMeshWidth); | |
float oneOverHeight = 1.0 / float(cMeshHeight); | |
int z = 0; | |
int x = 0; | |
// note that we say <= so that we make sure to capture the full width | |
for (z=0; z * delta <= cMeshHeight; z++){ | |
for (x=0; x * delta <= cMeshWidth; x++){ | |
vertices.push_back(ofVec3f(x * delta, 0, z * delta)); | |
texCoords.push_back(ofVec2f(x * delta, z * delta) * ofVec2f(oneOverWidth, oneOverHeight)); | |
ofLogNotice() << vertices.back(); | |
} | |
} | |
// invariant: x and z are at their respective maximum values | |
// so keep these values for the indexing | |
int maxZ = z; | |
int maxX = x; | |
// now index the mesh | |
for (z = 0; z+1 < maxZ; z++) { | |
for (x = 0; x+1 < maxX; x++) { | |
indices.push_back(x + 0 + (z + 0) * maxX); | |
indices.push_back(x + 0 + (z + 1) * maxX); | |
indices.push_back(x + 1 + (z + 1) * maxX); | |
indices.push_back(x + 0 + (z + 0) * maxX); | |
indices.push_back(x + 1 + (z + 1) * maxX); | |
indices.push_back(x + 1 + (z + 0) * maxX); | |
} | |
} | |
{ | |
int counter = 0; | |
ostringstream s; | |
s << endl; | |
for (auto const &i : indices){ | |
s << setw(2) << i << ", " ; | |
if (++counter % 3 == 0) s << endl; | |
} | |
ofLogNotice() << s.str(); | |
} | |
// now build the mesh | |
mMsh.addVertices(vertices); | |
mMsh.addTexCoords(texCoords); | |
mMsh.addIndices(indices); | |
mMsh.setUsage(GL_STATIC_DRAW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment