Hello,
I am trying to create a terrain editor for my RTS game I am making with OpenGL 3+.
To generate the terrain, I am using a function called genLandscape() that is called once in the initialization of the application.
EngineModel TerrainEngine::genLandscape(Loader loader, std::string heightMap)
{
GLuint textureID = 0;
int texture_width = 0, texture_height = 0, channels = 0;
…
here /\ I loaded an image. Is insignificant information…
//allocate texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
int VERTEX_COUNT = texture_height;
//free SOIL image data
int count = VERTEX_COUNT * VERTEX_COUNT;
std::vector<float> vertices(count * 3);
std::vector<float> normals(count * 3);
std::vector<float> textureCoords(count * 2);
std::vector<int> indices(6 * (VERTEX_COUNT - 1)*(VERTEX_COUNT - 1));
int vertexPointer = 0;
heightsLength = VERTEX_COUNT;
heights = new GLfloat*[VERTEX_COUNT];
for (int i = 0; i < VERTEX_COUNT; i++) {
heights[i] = new GLfloat[VERTEX_COUNT];
}
for (int i = 0; i < VERTEX_COUNT; i++) {
for (int j = 0; j < VERTEX_COUNT; j++) {
vertices[vertexPointer * 3] = (float)j / ((float)VERTEX_COUNT - 1) * SIZE;
GLfloat height = getHeight(j, i, pData, texture_height, texture_width, channels);
heights[j][i] = height;
vertices[vertexPointer * 3 + 1] = height;
vertices[vertexPointer * 3 + 2] = (float)i / ((float)VERTEX_COUNT - 1) * SIZE;
normals[vertexPointer * 3] = 0;
normals[vertexPointer * 3 + 1] = 1;
normals[vertexPointer * 3 + 2] = 0;
textureCoords[vertexPointer * 2] = (float)j / ((float)VERTEX_COUNT - 1);
textureCoords[vertexPointer * 2 + 1] = (float)i / ((float)VERTEX_COUNT - 1);
vertexPointer++;
}
}
int pointer = 0;
for (int gz = 0; gz < VERTEX_COUNT - 1; gz++) {
for (int gx = 0; gx < VERTEX_COUNT - 1; gx++) {
int topLeft = (gz*VERTEX_COUNT) + gx;
int topRight = topLeft + 1;
int bottomLeft = ((gz + 1)*VERTEX_COUNT) + gx;
int bottomRight = bottomLeft + 1;
indices[pointer++] = topLeft;
indices[pointer++] = bottomLeft;
indices[pointer++] = topRight;
indices[pointer++] = topRight;
indices[pointer++] = bottomLeft;
indices[pointer++] = bottomRight;
}
}
free(pData);
return loader.postToVAO(vertices, textureCoords, normals, indices);
}
This setup, however, is creating issues for me, as when I want to edit the terrain, the testing process is slowed down because when I try to generate the landscape every frame, it, of course, slows the frame rate because of all the calculations that are being carried out. The same thing happens if I create another heights vector to extract the height data. So my question is how could I make the terrain one that could be changed in, say, a heightmap editor? (that is what I am going for here) What would be the most efficient way of going about such calculations?
Thanks in advance!
-yaboiryan