summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/27-terrain/terrain.cpp
diff options
context:
space:
mode:
author Miodrag Milanovic <mmicko@gmail.com>2017-12-01 13:22:27 +0100
committer Miodrag Milanovic <mmicko@gmail.com>2017-12-01 13:22:27 +0100
commit39176274946d70ff520f265dee8fbd16d5fe0000 (patch)
tree318801d93146752050c9a492654ae3738820e3b9 /3rdparty/bgfx/examples/27-terrain/terrain.cpp
parent6829ecb3b037d6bfbe0b84e818d17d712f71bce6 (diff)
Updated GENie, BGFX, BX, added BIMG since it is separated now, updated all shader binaries and MAME part of code to support new interfaces [Miodrag Milanovic]
Diffstat (limited to '3rdparty/bgfx/examples/27-terrain/terrain.cpp')
-rw-r--r--3rdparty/bgfx/examples/27-terrain/terrain.cpp168
1 files changed, 81 insertions, 87 deletions
diff --git a/3rdparty/bgfx/examples/27-terrain/terrain.cpp b/3rdparty/bgfx/examples/27-terrain/terrain.cpp
index 3bcd8491dd6..82a6012ae8a 100644
--- a/3rdparty/bgfx/examples/27-terrain/terrain.cpp
+++ b/3rdparty/bgfx/examples/27-terrain/terrain.cpp
@@ -10,7 +10,10 @@
#include "bounds.h"
#include <bx/allocator.h>
#include <bx/debug.h>
-#include <bx/fpumath.h>
+#include <bx/math.h>
+
+namespace
+{
static const uint16_t s_terrainSize = 256;
@@ -38,7 +41,7 @@ bgfx::VertexDecl PosTexCoord0Vertex::ms_decl;
struct TerrainData
{
- uint32_t m_mode;
+ int32_t m_mode;
bool m_dirty;
float m_transform[16];
uint8_t* m_heightMap;
@@ -58,13 +61,19 @@ struct BrushData
class ExampleTerrain : public entry::AppI
{
- void init(int _argc, char** _argv) BX_OVERRIDE
+public:
+ ExampleTerrain(const char* _name, const char* _description)
+ : entry::AppI(_name, _description)
+ {
+ }
+
+ void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
- m_width = 1280;
- m_height = 720;
- m_debug = BGFX_DEBUG_TEXT;
+ m_width = _width;
+ m_height = _height;
+ m_debug = BGFX_DEBUG_NONE;
m_reset = BGFX_RESET_VSYNC;
bgfx::init(args.m_type, args.m_pciId);
@@ -75,11 +84,11 @@ class ExampleTerrain : public entry::AppI
// Set view 0 clear state.
bgfx::setViewClear(0
- , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
- , 0x303030ff
- , 1.0f
- , 0
- );
+ , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
+ , 0x303030ff
+ , 1.0f
+ , 0
+ );
// Create vertex stream declaration.
PosTexCoord0Vertex::init();
@@ -93,19 +102,17 @@ class ExampleTerrain : public entry::AppI
m_timeOffset = bx::getHPCounter();
- m_vbh.idx = bgfx::invalidHandle;
- m_ibh.idx = bgfx::invalidHandle;
- m_dvbh.idx = bgfx::invalidHandle;
- m_dibh.idx = bgfx::invalidHandle;
- m_heightTexture.idx = bgfx::invalidHandle;
+ m_vbh.idx = bgfx::kInvalidHandle;
+ m_ibh.idx = bgfx::kInvalidHandle;
+ m_dvbh.idx = bgfx::kInvalidHandle;
+ m_dibh.idx = bgfx::kInvalidHandle;
+ m_heightTexture.idx = bgfx::kInvalidHandle;
s_heightTexture = bgfx::createUniform("s_heightTexture", bgfx::UniformType::Int1);
m_oldWidth = 0;
m_oldHeight = 0;
m_oldReset = m_reset;
- m_scrollArea = 0;
-
m_brush.m_power = 0.5f;
m_brush.m_size = 10;
m_brush.m_raise = true;
@@ -125,10 +132,10 @@ class ExampleTerrain : public entry::AppI
const float initialPos[3] = { s_terrainSize/2.0f, 100.0f, 0.0f };
cameraSetPosition(initialPos);
- cameraSetVerticalAngle(-bx::pi/4.0f);
+ cameraSetVerticalAngle(-bx::kPi/4.0f);
}
- virtual int shutdown() BX_OVERRIDE
+ virtual int shutdown() override
{
// Cleanup.
cameraDestroy();
@@ -136,33 +143,33 @@ class ExampleTerrain : public entry::AppI
if (bgfx::isValid(m_ibh) )
{
- bgfx::destroyIndexBuffer(m_ibh);
+ bgfx::destroy(m_ibh);
}
if (bgfx::isValid(m_vbh) )
{
- bgfx::destroyVertexBuffer(m_vbh);
+ bgfx::destroy(m_vbh);
}
if (bgfx::isValid(m_dibh) )
{
- bgfx::destroyDynamicIndexBuffer(m_dibh);
+ bgfx::destroy(m_dibh);
}
if (bgfx::isValid(m_dvbh) )
{
- bgfx::destroyDynamicVertexBuffer(m_dvbh);
+ bgfx::destroy(m_dvbh);
}
- bgfx::destroyUniform(s_heightTexture);
+ bgfx::destroy(s_heightTexture);
if (bgfx::isValid(m_heightTexture) )
{
- bgfx::destroyTexture(m_heightTexture);
+ bgfx::destroy(m_heightTexture);
}
- bgfx::destroyProgram(m_terrainProgram);
- bgfx::destroyProgram(m_terrainHeightTextureProgram);
+ bgfx::destroy(m_terrainProgram);
+ bgfx::destroy(m_terrainHeightTextureProgram);
/// When data is passed to bgfx via makeRef we need to make
/// sure library is done with it before freeing memory blocks.
@@ -190,8 +197,8 @@ class ExampleTerrain : public entry::AppI
vert->m_x = (float)x;
vert->m_y = m_terrain.m_heightMap[(y * s_terrainSize) + x];
vert->m_z = (float)y;
- vert->m_u = (float)x / (float)s_terrainSize;
- vert->m_v = (float)y / (float)s_terrainSize;
+ vert->m_u = (x + 0.5f) / s_terrainSize;
+ vert->m_v = (y + 0.5f) / s_terrainSize;
m_terrain.m_vertexCount++;
}
@@ -226,14 +233,14 @@ class ExampleTerrain : public entry::AppI
if (bgfx::isValid(m_vbh) )
{
- bgfx::destroyVertexBuffer(m_vbh);
+ bgfx::destroy(m_vbh);
}
mem = bgfx::makeRef(&m_terrain.m_vertices[0], sizeof(PosTexCoord0Vertex) * m_terrain.m_vertexCount);
m_vbh = bgfx::createVertexBuffer(mem, PosTexCoord0Vertex::ms_decl);
if (bgfx::isValid(m_ibh) )
{
- bgfx::destroyIndexBuffer(m_ibh);
+ bgfx::destroy(m_ibh);
}
mem = bgfx::makeRef(&m_terrain.m_indices[0], sizeof(uint16_t) * m_terrain.m_indexCount);
@@ -291,14 +298,14 @@ class ExampleTerrain : public entry::AppI
{
int32_t brush_x = _x + area_x;
if (brush_x < 0
- || brush_x > (int32_t)s_terrainSize)
+ || brush_x >= (int32_t)s_terrainSize)
{
continue;
}
int32_t brush_y = _y + area_y;
if (brush_y < 0
- || brush_y > (int32_t)s_terrainSize)
+ || brush_y >= (int32_t)s_terrainSize)
{
continue;
}
@@ -312,7 +319,7 @@ class ExampleTerrain : public entry::AppI
float brushAttn = m_brush.m_size - bx::fsqrt(a2 + b2);
// Raise/Lower and scale by brush power.
- height += (bx::fclamp(brushAttn * m_brush.m_power, 0.0, m_brush.m_power) * m_brush.m_raise)
+ height += 0.0f < bx::fclamp(brushAttn*m_brush.m_power, 0.0f, m_brush.m_power) && m_brush.m_raise
? 1.0f
: -1.0f
;
@@ -358,9 +365,9 @@ class ExampleTerrain : public entry::AppI
bx::vec3Add(pos, pos, ray_dir);
if (pos[0] < 0
- || pos[0] > s_terrainSize
+ || pos[0] >= s_terrainSize
|| pos[2] < 0
- || pos[2] > s_terrainSize)
+ || pos[2] >= s_terrainSize)
{
continue;
}
@@ -374,7 +381,7 @@ class ExampleTerrain : public entry::AppI
}
}
- bool update() BX_OVERRIDE
+ bool update() override
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
@@ -383,60 +390,47 @@ class ExampleTerrain : public entry::AppI
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
- const double toMs = 1000.0/freq;
const float deltaTime = float(frameTime/freq);
- // Use m_debug font to print information about this example.
- bgfx::dbgTextClear();
- bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/27-terrain");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Terrain painting example.");
- bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
-
imguiBeginFrame(m_mouseState.m_mx
- , m_mouseState.m_my
- , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
- | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
- | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
- , m_mouseState.m_mz
- , uint16_t(m_width)
- , uint16_t(m_height)
- );
-
- imguiBeginScrollArea("Settings", m_width - m_width / 5 - 10, 10, m_width / 5, m_height / 3, &m_scrollArea);
- imguiSeparatorLine();
-
- if (imguiCheck("Vertex Buffer", (m_terrain.m_mode == 0) ) )
- {
- m_terrain.m_mode = 0;
- m_terrain.m_dirty = true;
- }
+ , m_mouseState.m_my
+ , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
+ | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
+ | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
+ , m_mouseState.m_mz
+ , uint16_t(m_width)
+ , uint16_t(m_height)
+ );
- if (imguiCheck("Dynamic Vertex Buffer", (m_terrain.m_mode == 1) ) )
- {
- m_terrain.m_mode = 1;
- m_terrain.m_dirty = true;
- }
+ showExampleDialog(this);
- if (imguiCheck("Height Texture", (m_terrain.m_mode == 2) ) )
- {
- m_terrain.m_mode = 2;
- m_terrain.m_dirty = true;
- }
+ ImGui::SetNextWindowPos(
+ ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
+ , ImGuiSetCond_FirstUseEver
+ );
+ ImGui::Begin("Settings"
+ , NULL
+ , ImVec2(m_width / 5.0f, m_height / 3.0f)
+ , ImGuiWindowFlags_AlwaysAutoResize
+ );
- imguiSeparatorLine();
+ ImGui::Separator();
- if (imguiCheck("Raise Terrain", m_brush.m_raise) )
- {
- m_brush.m_raise = !m_brush.m_raise;
- }
+ m_terrain.m_dirty |= ImGui::RadioButton("Vertex Buffer", &m_terrain.m_mode, 0);
+ m_terrain.m_dirty |= ImGui::RadioButton("Dynamic Vertex Buffer", &m_terrain.m_mode, 1);
+ m_terrain.m_dirty |= ImGui::RadioButton("Height Texture", &m_terrain.m_mode, 2);
+
+ ImGui::Separator();
- imguiSlider("Brush Size", m_brush.m_size, 1, 50);
- imguiSlider("Brush Power", m_brush.m_power, 0.0f, 1.0f, 0.01f);
+ ImGui::Checkbox("Raise Terrain", &m_brush.m_raise);
- imguiEndScrollArea();
+ ImGui::SliderInt("Brush Size", &m_brush.m_size, 1, 50);
+ ImGui::SliderFloat("Brush Power", &m_brush.m_power, 0.0f, 1.0f);
+
+ ImGui::End();
imguiEndFrame();
- if (!imguiMouseOverArea() )
+ if (!ImGui::MouseOverArea() )
{
// Update camera.
cameraUpdate(deltaTime, m_mouseState);
@@ -466,19 +460,19 @@ class ExampleTerrain : public entry::AppI
switch (m_terrain.m_mode)
{
default:
- bgfx::setVertexBuffer(m_vbh);
+ bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
bgfx::submit(0, m_terrainProgram);
break;
case 1:
- bgfx::setVertexBuffer(m_dvbh);
+ bgfx::setVertexBuffer(0, m_dvbh);
bgfx::setIndexBuffer(m_dibh);
bgfx::submit(0, m_terrainProgram);
break;
case 2:
- bgfx::setVertexBuffer(m_vbh);
+ bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
bgfx::setTexture(0, s_heightTexture, m_heightTexture);
bgfx::submit(0, m_terrainHeightTextureProgram);
@@ -516,8 +510,6 @@ class ExampleTerrain : public entry::AppI
uint32_t m_oldHeight;
uint32_t m_oldReset;
- int32_t m_scrollArea;
-
TerrainData m_terrain;
BrushData m_brush;
@@ -526,4 +518,6 @@ class ExampleTerrain : public entry::AppI
int64_t m_timeOffset;
};
-ENTRY_IMPLEMENT_MAIN(ExampleTerrain);
+} // namespace
+
+ENTRY_IMPLEMENT_MAIN(ExampleTerrain, "27-terrain", "Terrain painting example.");