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

@ -136,19 +136,31 @@ class Terrain : public entry::AppI
cameraDestroy(); cameraDestroy();
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)) }
bgfx::destroyVertexBuffer(m_vbh);
if (bgfx::isValid(m_dibh)) if (bgfx::isValid(m_vbh) )
{
bgfx::destroyVertexBuffer(m_vbh);
}
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;
@ -208,14 +220,18 @@ 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);
@ -226,14 +242,18 @@ 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);
@ -242,7 +262,7 @@ class Terrain : public entry::AppI
// Height Texture: Update a height texture that is sampled in the terrain vertex shader. // Height Texture: Update a height texture that is sampled in the terrain vertex shader.
if (m_terrain.m_mode == 2) if (m_terrain.m_mode == 2)
{ {
if (!bgfx::isValid(m_vbh) || !bgfx::isValid(m_ibh)) if (!bgfx::isValid(m_vbh) || !bgfx::isValid(m_ibh) )
{ {
updateTerrainMesh(); updateTerrainMesh();
@ -253,8 +273,10 @@ class Terrain : public entry::AppI
m_ibh = bgfx::createIndexBuffer(mem); m_ibh = bgfx::createIndexBuffer(mem);
} }
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];
@ -292,8 +323,8 @@ class Terrain : public entry::AppI
void mousePickTerrain() void mousePickTerrain()
{ {
float ray_clip[4]; float ray_clip[4];
ray_clip[0] = ((2.0f * m_mouseState.m_mx) / m_width - 1.0f) * -1.0f; ray_clip[0] = ( (2.0f * m_mouseState.m_mx) / m_width - 1.0f) * -1.0f;
ray_clip[1] = ((1.0f - (2.0f * m_mouseState.m_my) / m_height)) * -1.0f; ray_clip[1] = ( (1.0f - (2.0f * m_mouseState.m_my) / m_height) ) * -1.0f;
ray_clip[2] = -1.0; ray_clip[2] = -1.0;
ray_clip[3] = 1.0; ray_clip[3] = 1.0;
@ -323,13 +354,18 @@ 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] )
{ {
paintTerrainHeight((uint32_t)pos[0], (uint32_t)pos[2]); paintTerrainHeight( (uint32_t)pos[0], (uint32_t)pos[2]);
return; return;
} }
} }
@ -366,19 +402,19 @@ class Terrain : public entry::AppI
imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 3, &m_scrollArea); imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 3, &m_scrollArea);
imguiSeparatorLine(); imguiSeparatorLine();
if (imguiCheck("Vertex Buffer", (m_terrain.m_mode == 0)) ) if (imguiCheck("Vertex Buffer", (m_terrain.m_mode == 0) ) )
{ {
m_terrain.m_mode = 0; m_terrain.m_mode = 0;
m_terrain.m_dirty = true; m_terrain.m_dirty = true;
} }
if (imguiCheck("Dynamic Vertex Buffer", (m_terrain.m_mode == 1)) ) if (imguiCheck("Dynamic Vertex Buffer", (m_terrain.m_mode == 1) ) )
{ {
m_terrain.m_mode = 1; m_terrain.m_mode = 1;
m_terrain.m_dirty = true; m_terrain.m_dirty = true;
} }
if (imguiCheck("Height Texture", (m_terrain.m_mode == 2)) ) if (imguiCheck("Height Texture", (m_terrain.m_mode == 2) ) )
{ {
m_terrain.m_mode = 2; m_terrain.m_mode = 2;
m_terrain.m_dirty = true; m_terrain.m_dirty = true;
@ -386,7 +422,7 @@ class Terrain : public entry::AppI
imguiSeparatorLine(); imguiSeparatorLine();
if (imguiCheck("Raise Terrain", m_brush.m_raise)) if (imguiCheck("Raise Terrain", m_brush.m_raise) )
{ {
m_brush.m_raise = !m_brush.m_raise; m_brush.m_raise = !m_brush.m_raise;
} }