bgfx/examples/23-vectordisplay/main.cpp

217 lines
5.4 KiB
C++
Raw Normal View History

2014-11-16 03:10:44 +03:00
/*
* Copyright 2014 Kai Jourdan. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2014-11-16 03:10:44 +03:00
*
*/
#include "common.h"
#include "bgfx_utils.h"
#include "vectordisplay.h"
2017-06-26 07:44:04 +03:00
#include "imgui/imgui.h"
namespace
{
2014-11-16 03:10:44 +03:00
struct PosColorVertex
{
float m_x;
float m_y;
float m_z;
uint32_t m_abgr;
static void init()
{
ms_decl
2018-01-14 02:33:50 +03:00
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
2014-11-16 03:10:44 +03:00
}
static bgfx::VertexDecl ms_decl;
};
bgfx::VertexDecl PosColorVertex::ms_decl;
2017-06-16 23:10:30 +03:00
class ExampleVectorDisplay : public entry::AppI
2014-11-16 03:10:44 +03:00
{
2017-06-26 07:44:04 +03:00
public:
ExampleVectorDisplay(const char* _name, const char* _description)
: entry::AppI(_name, _description)
{
}
2017-07-15 10:17:29 +03:00
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
2014-11-16 03:10:44 +03:00
{
2017-06-16 23:10:30 +03:00
Args args(_argc, _argv);
2017-06-30 08:23:18 +03:00
m_width = _width;
m_height = _height;
2017-06-26 07:44:04 +03:00
m_debug = BGFX_DEBUG_NONE;
2017-06-16 23:10:30 +03:00
m_reset = BGFX_RESET_VSYNC;
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
bgfx::init(args.m_type, args.m_pciId);
bgfx::reset(m_width, m_height, m_reset);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
const bgfx::RendererType::Enum renderer = bgfx::getRendererType();
float texelHalf = bgfx::RendererType::Direct3D9 == renderer ? 0.5f : 0.0f;
bool originBottomLeft = bgfx::RendererType::OpenGL == renderer
|| bgfx::RendererType::OpenGLES == renderer;
m_vd.init(originBottomLeft, texelHalf);
m_vd.setup(uint16_t(m_width), uint16_t(m_height) );
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
// Enable debug text.
bgfx::setDebug(m_debug);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
// Set view 0 clear state.
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
// Create vertex stream declaration.
PosColorVertex::init();
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
m_oldWidth = m_width;
m_oldHeight = m_height;
2017-06-26 07:44:04 +03:00
imguiCreate();
2017-06-16 23:10:30 +03:00
}
2014-11-16 03:10:44 +03:00
2017-07-15 10:17:29 +03:00
virtual int shutdown() override
2017-06-16 23:10:30 +03:00
{
2017-06-26 07:44:04 +03:00
imguiDestroy();
2017-06-16 23:10:30 +03:00
// Cleanup.
m_vd.teardown();
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
// Shutdown bgfx.
bgfx::shutdown();
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
return 0;
}
2017-06-26 07:44:04 +03:00
2017-07-15 10:17:29 +03:00
bool update() override
2017-06-16 23:10:30 +03:00
{
2017-06-26 07:44:04 +03:00
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
2014-11-16 03:10:44 +03:00
{
2017-06-26 07:44:04 +03:00
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)
);
2017-06-30 09:19:20 +03:00
showExampleDialog(this);
2017-06-26 07:44:04 +03:00
imguiEndFrame();
2017-06-16 23:10:30 +03:00
if (m_oldWidth != m_width
2017-06-26 07:44:04 +03:00
|| m_oldHeight != m_height)
2014-11-16 03:10:44 +03:00
{
2017-06-16 23:10:30 +03:00
m_oldWidth = m_width;
m_oldHeight = m_height;
m_vd.resize(uint16_t(m_width), uint16_t(m_height) );
2014-11-16 03:10:44 +03:00
}
2017-06-26 07:44:04 +03:00
const float at[3] = { 0.0f, 0.0f, 0.0f };
2017-06-16 23:10:30 +03:00
const float eye[3] = { 0.0f, 0.0f, -35.0f };
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
float view[16];
float proj[16];
bx::mtxLookAt(view, eye, at);
bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
m_vd.beginFrame();
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
//simplex test
m_vd.setDrawColor(0.7f, 0.7f, 1.0f);
m_vd.drawSimplexFont(50.0f, 80.0f, 1.5f, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
m_vd.drawSimplexFont(50.0f, 140.0f, 1.5f, "abcdefghijklmnopqrstuvwxyz");
m_vd.drawSimplexFont(50.0f, 200.0f, 1.5f, "!@#$%^&*()-=<>/?;:'\"{}[]|\\+=-_");
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
m_vd.setDrawColor(1.0f, 0.7f, 0.7f);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
//test pattern for lines
for (int ii = 0; ii < 4; ii++)
{
for (int jj = 0; jj < ii; jj++) //draw more intensive lines
{
m_vd.drawLine(50.0f, 350.0f + 40 * ii, 200.0f, 350.0f + 40 * ii);
}
}
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
for (int ii = 0; ii < 4; ii++)
2014-11-16 03:10:44 +03:00
{
2017-06-16 23:10:30 +03:00
for (int jj = 0; jj <= ii; jj++)
{
m_vd.drawLine(50.0f + 40 * ii, 600.0f, 50.0f + 40 * ii, 700.0f);
}
2014-11-16 03:10:44 +03:00
}
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
//
// test pattern for shapes
//
m_vd.setDrawColor(0.7f, 0.7f, 1.0f);
m_vd.drawCircle(250.0f, 450.0f, 10.0f, 32.0f);
m_vd.drawCircle(300.0f, 450.0f, 30.0f, 32.0f);
m_vd.drawCircle(400.0f, 450.0f, 60.0f, 32.0f);
m_vd.drawCircle(500.0f, 450.0f, 80.0f, 64.0f);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
m_vd.setDrawColor(0.7f, 1.0f, 0.7f);
m_vd.drawBox(250.0f, 600.0f, 10.0f, 10.0f);
m_vd.drawBox(300.0f, 600.0f, 30.0f, 30.0f);
m_vd.drawBox(350.0f, 600.0f, 60.0f, 60.0f);
m_vd.drawBox(450.0f, 600.0f, 80.0f, 80.0f);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
m_vd.setDrawColor(1.0f, 0.7f, 1.0f);
m_vd.drawWheel(bx::kPi, 800.0f, 450.0f, 80.0f);
m_vd.drawWheel(3.0f * bx::kPi / 4.0f, 95.0f, 450.0f, 60.0f);
m_vd.drawWheel(bx::kPi / 2.0f, 1150.0f, 450.0f, 30.0f);
m_vd.drawWheel(bx::kPi / 4.0f, 1250.0f, 450.0f, 10.0f);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
// draw moving shape
static float counter = 0.0f;
counter += 0.01f;
2018-01-14 02:33:50 +03:00
const float posX = m_width / 2.0f + bx::sin(counter * 3.18378f) * (m_width / 2.0f);
const float posY = m_height / 2.0f + bx::cos(counter) * (m_height / 2.0f);
2017-06-16 23:10:30 +03:00
m_vd.drawCircle(posX, posY, 5.0f, 10.0f);
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
m_vd.endFrame();
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
2017-06-26 07:44:04 +03:00
2017-06-30 09:19:20 +03:00
return true;
2014-11-16 03:10:44 +03:00
}
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
return false;
2014-11-16 03:10:44 +03:00
}
2017-06-26 07:44:04 +03:00
entry::MouseState m_mouseState;
2017-06-16 23:10:30 +03:00
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
VectorDisplay m_vd;
2017-06-26 07:44:04 +03:00
2017-06-16 23:10:30 +03:00
uint32_t m_oldWidth;
uint32_t m_oldHeight;
};
2014-11-16 03:10:44 +03:00
2017-06-26 07:44:04 +03:00
} // namespace
ENTRY_IMPLEMENT_MAIN(ExampleVectorDisplay, "23-vectordisplay", "Rendering lines as oldschool vectors.");