This commit is contained in:
Branimir Karadžić 2015-12-13 09:48:21 -08:00
parent 7c24a058ff
commit 8493775a8b
4 changed files with 479 additions and 443 deletions

View file

@ -137,18 +137,30 @@ class Terrain : public entry::AppI
imguiDestroy(); imguiDestroy();
if (bgfx::isValid(m_ibh) ) if (bgfx::isValid(m_ibh) )
{
bgfx::destroyIndexBuffer(m_ibh); bgfx::destroyIndexBuffer(m_ibh);
}
if (bgfx::isValid(m_vbh) ) if (bgfx::isValid(m_vbh) )
{
bgfx::destroyVertexBuffer(m_vbh); bgfx::destroyVertexBuffer(m_vbh);
}
if (bgfx::isValid(m_dibh) ) if (bgfx::isValid(m_dibh) )
{
bgfx::destroyDynamicIndexBuffer(m_dibh); bgfx::destroyDynamicIndexBuffer(m_dibh);
}
if (bgfx::isValid(m_dvbh) ) if (bgfx::isValid(m_dvbh) )
{
bgfx::destroyDynamicVertexBuffer(m_dvbh); bgfx::destroyDynamicVertexBuffer(m_dvbh);
}
bgfx::destroyUniform(s_heightTexture); bgfx::destroyUniform(s_heightTexture);
if (bgfx::isValid(m_heightTexture) ) if (bgfx::isValid(m_heightTexture) )
{
bgfx::destroyTexture(m_heightTexture); bgfx::destroyTexture(m_heightTexture);
}
bgfx::destroyProgram(m_terrainProgram); bgfx::destroyProgram(m_terrainProgram);
bgfx::destroyProgram(m_terrainHeightTextureProgram); bgfx::destroyProgram(m_terrainHeightTextureProgram);
@ -187,7 +199,7 @@ class Terrain : public entry::AppI
uint32_t y_offset = (y * s_terrainSize); uint32_t y_offset = (y * s_terrainSize);
for (uint32_t x = 0; x < (s_terrainSize - 1); x++) for (uint32_t x = 0; x < (s_terrainSize - 1); x++)
{ {
m_terrain.m_indices[m_terrain.m_indexCount] = y_offset + x + 1; m_terrain.m_indices[m_terrain.m_indexCount + 0] = y_offset + x + 1;
m_terrain.m_indices[m_terrain.m_indexCount + 1] = y_offset + x + s_terrainSize; m_terrain.m_indices[m_terrain.m_indexCount + 1] = y_offset + x + s_terrainSize;
m_terrain.m_indices[m_terrain.m_indexCount + 2] = y_offset + x; m_terrain.m_indices[m_terrain.m_indexCount + 2] = y_offset + x;
m_terrain.m_indices[m_terrain.m_indexCount + 3] = y_offset + x + s_terrainSize + 1; m_terrain.m_indices[m_terrain.m_indexCount + 3] = y_offset + x + s_terrainSize + 1;
@ -209,13 +221,17 @@ class Terrain : public entry::AppI
updateTerrainMesh(); updateTerrainMesh();
if (bgfx::isValid(m_vbh) ) if (bgfx::isValid(m_vbh) )
{
bgfx::destroyVertexBuffer(m_vbh); bgfx::destroyVertexBuffer(m_vbh);
}
mem = bgfx::makeRef(&m_terrain.m_vertices[0], sizeof(PosTexCoord0Vertex) * m_terrain.m_vertexCount); mem = bgfx::makeRef(&m_terrain.m_vertices[0], sizeof(PosTexCoord0Vertex) * m_terrain.m_vertexCount);
m_vbh = bgfx::createVertexBuffer(mem, PosTexCoord0Vertex::ms_decl); m_vbh = bgfx::createVertexBuffer(mem, PosTexCoord0Vertex::ms_decl);
if (bgfx::isValid(m_ibh) ) if (bgfx::isValid(m_ibh) )
{
bgfx::destroyIndexBuffer(m_ibh); bgfx::destroyIndexBuffer(m_ibh);
}
mem = bgfx::makeRef(&m_terrain.m_indices[0], sizeof(uint16_t) * m_terrain.m_indexCount); mem = bgfx::makeRef(&m_terrain.m_indices[0], sizeof(uint16_t) * m_terrain.m_indexCount);
m_ibh = bgfx::createIndexBuffer(mem); m_ibh = bgfx::createIndexBuffer(mem);
@ -227,13 +243,17 @@ class Terrain : public entry::AppI
updateTerrainMesh(); updateTerrainMesh();
if (!bgfx::isValid(m_dvbh) ) if (!bgfx::isValid(m_dvbh) )
{
m_dvbh = bgfx::createDynamicVertexBuffer(m_terrain.m_vertexCount, PosTexCoord0Vertex::ms_decl); m_dvbh = bgfx::createDynamicVertexBuffer(m_terrain.m_vertexCount, PosTexCoord0Vertex::ms_decl);
}
mem = bgfx::makeRef(&m_terrain.m_vertices[0], sizeof(PosTexCoord0Vertex) * m_terrain.m_vertexCount); mem = bgfx::makeRef(&m_terrain.m_vertices[0], sizeof(PosTexCoord0Vertex) * m_terrain.m_vertexCount);
bgfx::updateDynamicVertexBuffer(m_dvbh, 0, mem); bgfx::updateDynamicVertexBuffer(m_dvbh, 0, mem);
if (!bgfx::isValid(m_dibh) ) if (!bgfx::isValid(m_dibh) )
{
m_dibh = bgfx::createDynamicIndexBuffer(m_terrain.m_indexCount); m_dibh = bgfx::createDynamicIndexBuffer(m_terrain.m_indexCount);
}
mem = bgfx::makeRef(&m_terrain.m_indices[0], sizeof(uint16_t) * m_terrain.m_indexCount); mem = bgfx::makeRef(&m_terrain.m_indices[0], sizeof(uint16_t) * m_terrain.m_indexCount);
bgfx::updateDynamicIndexBuffer(m_dibh, 0, mem); bgfx::updateDynamicIndexBuffer(m_dibh, 0, mem);
@ -254,7 +274,9 @@ class Terrain : public entry::AppI
} }
if (!bgfx::isValid(m_heightTexture) ) if (!bgfx::isValid(m_heightTexture) )
{
m_heightTexture = bgfx::createTexture2D(s_terrainSize, s_terrainSize, 1, bgfx::TextureFormat::R8); m_heightTexture = bgfx::createTexture2D(s_terrainSize, s_terrainSize, 1, bgfx::TextureFormat::R8);
}
mem = bgfx::makeRef(&m_terrain.m_heightMap[0], sizeof(uint8_t) * s_terrainSize * s_terrainSize); mem = bgfx::makeRef(&m_terrain.m_heightMap[0], sizeof(uint8_t) * s_terrainSize * s_terrainSize);
bgfx::updateTexture2D(m_heightTexture, 0, 0, 0, s_terrainSize, s_terrainSize, mem); bgfx::updateTexture2D(m_heightTexture, 0, 0, 0, s_terrainSize, s_terrainSize, mem);
@ -268,9 +290,18 @@ class Terrain : public entry::AppI
for (int32_t area_x = -m_brush.m_size; area_x < m_brush.m_size; ++area_x) for (int32_t area_x = -m_brush.m_size; area_x < m_brush.m_size; ++area_x)
{ {
int32_t brush_x = _x + area_x; int32_t brush_x = _x + area_x;
if (brush_x < 0 || brush_x > (int32_t)s_terrainSize) continue; if (brush_x < 0
|| brush_x > (int32_t)s_terrainSize)
{
continue;
}
int32_t brush_y = _y + area_y; int32_t brush_y = _y + area_y;
if (brush_y < 0 || brush_y > (int32_t)s_terrainSize) continue; if (brush_y < 0
|| brush_y > (int32_t)s_terrainSize)
{
continue;
}
uint32_t heightMapPos = (brush_y * s_terrainSize) + brush_x; uint32_t heightMapPos = (brush_y * s_terrainSize) + brush_x;
float height = (float)m_terrain.m_heightMap[heightMapPos]; float height = (float)m_terrain.m_heightMap[heightMapPos];
@ -323,8 +354,13 @@ class Terrain : public entry::AppI
{ {
bx::vec3Add(pos, pos, ray_dir); bx::vec3Add(pos, pos, ray_dir);
if (pos[0] < 0 || pos[0] > s_terrainSize || pos[2] < 0 || pos[2] > s_terrainSize) if (pos[0] < 0
|| pos[0] > s_terrainSize
|| pos[2] < 0
|| pos[2] > s_terrainSize)
{
continue; continue;
}
uint32_t heightMapPos = ( (uint32_t)pos[2] * s_terrainSize) + (uint32_t)pos[0]; uint32_t heightMapPos = ( (uint32_t)pos[2] * s_terrainSize) + (uint32_t)pos[0];
if ( pos[1] < m_terrain.m_heightMap[heightMapPos] ) if ( pos[1] < m_terrain.m_heightMap[heightMapPos] )