mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-24 16:48:18 -05:00
Added 23-vectordisplay example.
This commit is contained in:
parent
bb1fd097ad
commit
57715c3cc9
9 changed files with 1978 additions and 0 deletions
13
examples/23-vectordisplay/fs_vectordisplay_blit.sc
Normal file
13
examples/23-vectordisplay/fs_vectordisplay_blit.sc
Normal file
|
@ -0,0 +1,13 @@
|
|||
$input v_color0, v_texcoord0
|
||||
|
||||
#include <bgfx_shader.sh>
|
||||
|
||||
SAMPLER2D(s_textureSampler, 0);
|
||||
|
||||
uniform float u_compose_alpha;
|
||||
uniform float u_compose_mult;
|
||||
|
||||
void main() {
|
||||
gl_FragColor = texture2D(s_textureSampler, v_texcoord0.xy) *
|
||||
vec4(u_compose_mult, u_compose_mult, u_compose_mult, u_compose_alpha*u_compose_mult);
|
||||
}
|
24
examples/23-vectordisplay/fs_vectordisplay_blur.sc
Normal file
24
examples/23-vectordisplay/fs_vectordisplay_blur.sc
Normal file
|
@ -0,0 +1,24 @@
|
|||
$input v_color0, v_texcoord0
|
||||
|
||||
#include <bgfx_shader.sh>
|
||||
|
||||
SAMPLER2D(s_textureSampler, 0);
|
||||
|
||||
uniform vec2 u_blur_scale;
|
||||
|
||||
uniform float u_compose_alpha;
|
||||
uniform float u_compose_mult;
|
||||
|
||||
void main() {
|
||||
vec4 color = texture2D(s_textureSampler, vec2(v_texcoord0.x-4.0*u_blur_scale.x, v_texcoord0.y-4.0*u_blur_scale.y))*0.05;
|
||||
color += texture2D(s_textureSampler, vec2(v_texcoord0.x-3.0*u_blur_scale.x, v_texcoord0.y-3.0*u_blur_scale.y))*0.09;
|
||||
color += texture2D(s_textureSampler, vec2(v_texcoord0.x-2.0*u_blur_scale.x, v_texcoord0.y-2.0*u_blur_scale.y))*0.12;
|
||||
color += texture2D(s_textureSampler, vec2(v_texcoord0.x-1.0*u_blur_scale.x, v_texcoord0.y-1.0*u_blur_scale.y))*0.15;
|
||||
color += texture2D(s_textureSampler, vec2(v_texcoord0.x+0.0*u_blur_scale.x, v_texcoord0.y+0.0*u_blur_scale.y))*0.16;
|
||||
color += texture2D(s_textureSampler, vec2(v_texcoord0.x+1.0*u_blur_scale.x, v_texcoord0.y+1.0*u_blur_scale.y))*0.15;
|
||||
color += texture2D(s_textureSampler, vec2(v_texcoord0.x+2.0*u_blur_scale.x, v_texcoord0.y+2.0*u_blur_scale.y))*0.12;
|
||||
color += texture2D(s_textureSampler, vec2(v_texcoord0.x+3.0*u_blur_scale.x, v_texcoord0.y+3.0*u_blur_scale.y))*0.09;
|
||||
color += texture2D(s_textureSampler, vec2(v_texcoord0.x+4.0*u_blur_scale.x, v_texcoord0.y+4.0*u_blur_scale.y))*0.05;
|
||||
|
||||
gl_FragColor = color * vec4(u_compose_mult, u_compose_mult, u_compose_mult, u_compose_alpha*u_compose_mult);
|
||||
}
|
12
examples/23-vectordisplay/fs_vectordisplay_fb.sc
Normal file
12
examples/23-vectordisplay/fs_vectordisplay_fb.sc
Normal file
|
@ -0,0 +1,12 @@
|
|||
$input v_color0, v_texcoord0
|
||||
|
||||
#include <bgfx_shader.sh>
|
||||
|
||||
SAMPLER2D(s_lineTexture, 0);
|
||||
|
||||
uniform float u_compose_alpha;
|
||||
|
||||
void main() {
|
||||
vec4 texColor = texture2D(s_lineTexture, v_texcoord0.xy);
|
||||
gl_FragColor = v_color0 * texColor * vec4(1.0, 1.0, 1.0, u_compose_alpha);
|
||||
}
|
171
examples/23-vectordisplay/main.cpp
Normal file
171
examples/23-vectordisplay/main.cpp
Normal file
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
* Copyright 2014 Kai Jourdan. All rights reserved.
|
||||
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "bgfx_utils.h"
|
||||
|
||||
#include "vectordisplay.h"
|
||||
|
||||
struct PosColorVertex
|
||||
{
|
||||
float m_x;
|
||||
float m_y;
|
||||
float m_z;
|
||||
uint32_t m_abgr;
|
||||
|
||||
static void init()
|
||||
{
|
||||
ms_decl
|
||||
.begin()
|
||||
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
||||
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
|
||||
.end();
|
||||
}
|
||||
|
||||
static bgfx::VertexDecl ms_decl;
|
||||
};
|
||||
|
||||
bgfx::VertexDecl PosColorVertex::ms_decl;
|
||||
|
||||
int _main_(int /*_argc*/, char** /*_argv*/)
|
||||
{
|
||||
uint32_t width = 1280;
|
||||
uint32_t height = 720;
|
||||
uint32_t debug = BGFX_DEBUG_TEXT;
|
||||
uint32_t reset = BGFX_RESET_VSYNC;
|
||||
|
||||
bgfx::init();
|
||||
bgfx::reset(width, height, reset);
|
||||
|
||||
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;
|
||||
VectorDisplay vectorDisplay(originBottomLeft, texelHalf);
|
||||
vectorDisplay.setup(width, height);
|
||||
|
||||
// Enable debug text.
|
||||
bgfx::setDebug(debug);
|
||||
|
||||
// Set view 0 clear state.
|
||||
bgfx::setViewClear(0, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT, 0x303030ff, 1.0f, 0);
|
||||
|
||||
// Create vertex stream declaration.
|
||||
PosColorVertex::init();
|
||||
|
||||
float at[3] = { 0.0f, 0.0f, 0.0f };
|
||||
float eye[3] = { 0.0f, 0.0f, -35.0f };
|
||||
|
||||
uint32_t oldWidth = width;
|
||||
uint32_t oldHeight = height;
|
||||
|
||||
while (!entry::processEvents(width, height, debug, reset) )
|
||||
{
|
||||
if ( (oldWidth != width)
|
||||
|| (oldHeight != height) )
|
||||
{
|
||||
oldWidth = width;
|
||||
oldHeight = height;
|
||||
vectorDisplay.resize(width, height);
|
||||
}
|
||||
|
||||
float view[16];
|
||||
float proj[16];
|
||||
bx::mtxLookAt(view, eye, at);
|
||||
bx::mtxProj(proj, 60.0f, float(width) / float(height), 0.1f, 100.0f);
|
||||
// Set view and projection matrix for view 0.
|
||||
bgfx::setViewTransform(0, view, proj);
|
||||
|
||||
// Set view 0 default viewport.
|
||||
bgfx::setViewRect(0, 0, 0, width, height);
|
||||
|
||||
// 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::submit(0);
|
||||
|
||||
int64_t now = bx::getHPCounter();
|
||||
static int64_t last = now;
|
||||
const int64_t frameTime = now - last;
|
||||
last = now;
|
||||
const double freq = double(bx::getHPFrequency() );
|
||||
const double toMs = 1000.0 / freq;
|
||||
|
||||
// Use debug font to print information about this example.
|
||||
bgfx::dbgTextClear();
|
||||
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/23-vectordisplay");
|
||||
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Rendering lines as oldschool vectors.");
|
||||
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime) * toMs);
|
||||
|
||||
vectorDisplay.beginFrame();
|
||||
|
||||
//simplex test
|
||||
vectorDisplay.setDrawColor(0.7f, 0.7f, 1.0f);
|
||||
vectorDisplay.drawSimplexFont(50, 80, 1.5, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
vectorDisplay.drawSimplexFont(50, 140, 1.5, "abcdefghijklmnopqrstuvwxyz");
|
||||
vectorDisplay.drawSimplexFont(50, 200, 1.5, "!@#$%^&*()-=<>/?;:'\"{}[]|\\+=-_");
|
||||
|
||||
vectorDisplay.setDrawColor(1.0f, 0.7f, 0.7f);
|
||||
|
||||
//test pattern for lines
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
for (int j = 0; j < i; j++) //draw more intensive lines
|
||||
{
|
||||
vectorDisplay.drawLine(50.0f, 350.0f + 40 * i, 200.0f, 350.0f + 40 * i);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
vectorDisplay.drawLine(50.0f + 40 * i, 600.0f, 50.0f + 40 * i, 700.0f);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// test pattern for shapes
|
||||
//
|
||||
vectorDisplay.setDrawColor(0.7f, 0.7f, 1.0f);
|
||||
vectorDisplay.drawCircle(250, 450, 10, 32);
|
||||
vectorDisplay.drawCircle(300, 450, 30, 32);
|
||||
vectorDisplay.drawCircle(400, 450, 60, 32);
|
||||
vectorDisplay.drawCircle(500, 450, 80, 64);
|
||||
|
||||
vectorDisplay.setDrawColor(0.7f, 1.0f, 0.7f);
|
||||
vectorDisplay.drawBox(250, 600, 10, 10);
|
||||
vectorDisplay.drawBox(300, 600, 30, 30);
|
||||
vectorDisplay.drawBox(350, 600, 60, 60);
|
||||
vectorDisplay.drawBox(450, 600, 80, 80);
|
||||
|
||||
vectorDisplay.setDrawColor(1.0f, 0.7f, 1.0f);
|
||||
vectorDisplay.drawWheel(bx::pi, 800, 450, 80);
|
||||
vectorDisplay.drawWheel(3 * bx::pi / 4, 950, 450, 60);
|
||||
vectorDisplay.drawWheel(bx::pi / 2, 1150, 450, 30);
|
||||
vectorDisplay.drawWheel(bx::pi / 4, 1250, 450, 10);
|
||||
|
||||
// draw moving shape
|
||||
static float counter = 0.0f;
|
||||
counter += 0.01f;
|
||||
float posX = width / 2 + sin(counter * 3.18378f) * (width / 2);
|
||||
float posY = height / 2 + cos(counter) * (height / 2);
|
||||
vectorDisplay.drawCircle(posX, posY, 5, 10);
|
||||
|
||||
vectorDisplay.endFrame();
|
||||
|
||||
// Advance to next frame. Rendering thread will be kicked to
|
||||
// process submitted rendering primitives.
|
||||
bgfx::frame();
|
||||
}
|
||||
|
||||
// Cleanup.
|
||||
vectorDisplay.teardown();
|
||||
|
||||
// Shutdown bgfx.
|
||||
bgfx::shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
17
examples/23-vectordisplay/makefile
Normal file
17
examples/23-vectordisplay/makefile
Normal file
|
@ -0,0 +1,17 @@
|
|||
#
|
||||
# Copyright 2011-2014 Branimir Karadzic. All rights reserved.
|
||||
# License: http://www.opensource.org/licenses/BSD-2-Clause
|
||||
#
|
||||
|
||||
BGFX_DIR=../..
|
||||
RUNTIME_DIR=$(BGFX_DIR)/examples/runtime
|
||||
BUILD_DIR=../../.build
|
||||
|
||||
include $(BGFX_DIR)/scripts/shader.mk
|
||||
|
||||
rebuild:
|
||||
@make -s --no-print-directory TARGET=0 clean all
|
||||
@make -s --no-print-directory TARGET=1 clean all
|
||||
@make -s --no-print-directory TARGET=2 clean all
|
||||
@make -s --no-print-directory TARGET=3 clean all
|
||||
@make -s --no-print-directory TARGET=4 clean all
|
6
examples/23-vectordisplay/varying.def.sc
Normal file
6
examples/23-vectordisplay/varying.def.sc
Normal file
|
@ -0,0 +1,6 @@
|
|||
vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
|
||||
vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
|
||||
vec3 a_position : POSITION;
|
||||
vec2 a_texcoord0 : TEXCOORD0;
|
||||
vec4 a_color0 : COLOR0;
|
1529
examples/23-vectordisplay/vectordisplay.cpp
Normal file
1529
examples/23-vectordisplay/vectordisplay.cpp
Normal file
File diff suppressed because it is too large
Load diff
196
examples/23-vectordisplay/vectordisplay.h
Normal file
196
examples/23-vectordisplay/vectordisplay.h
Normal file
|
@ -0,0 +1,196 @@
|
|||
|
||||
/*
|
||||
* Copyright 2014 Kai Jourdan. All rights reserved.
|
||||
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
||||
*
|
||||
* based on code from Brian Luczkiewicz
|
||||
* https://github.com/blucz/Vector
|
||||
*
|
||||
* uses the SIMPLEX-Font which is a variant of the Hershey font (public domain)
|
||||
* http://paulbourke.net/dataformats/hershey/
|
||||
*/
|
||||
|
||||
#ifndef __BGFX_VECTORDISPLAY_H__
|
||||
#define __BGFX_VECTORDISPLAY_H__
|
||||
|
||||
#include "bgfx.h"
|
||||
|
||||
#include <tinystl/allocator.h>
|
||||
#include <tinystl/vector.h>
|
||||
namespace stl = tinystl;
|
||||
|
||||
class VectorDisplay {
|
||||
public:
|
||||
VectorDisplay(bool _originBottomLeft, float _texelHalf);
|
||||
~VectorDisplay()
|
||||
{
|
||||
}
|
||||
|
||||
void setup(uint16_t _width, uint16_t _height, int _view = 2);
|
||||
void resize(uint16_t _width, uint16_t _height);
|
||||
void teardown();
|
||||
|
||||
void beginFrame();
|
||||
void endFrame();
|
||||
|
||||
// Draw a series of connected line segments.
|
||||
void beginDraw(float _x, float _y);
|
||||
void drawTo(float _x, float _y);
|
||||
void endDraw();
|
||||
|
||||
//HighLevel draw functions
|
||||
void drawLine(float _x0, float _y0, float _x1, float _y1);
|
||||
void drawBox(float _x, float _y, float _w, float _h);
|
||||
void drawCircle(float _x, float _y, float _radius, float _steps);
|
||||
void drawWheel(float _spokeangle, float _x, float _y, float _radius);
|
||||
|
||||
//Font Stuff (Simplex)
|
||||
//void simplexMeasure(float _scale, const char *_string, float *_outWidth, float *_outHeight);
|
||||
void drawSimplexFont(float _x, float _y, float _scale, const char* _string);
|
||||
|
||||
// Set the current drawing color
|
||||
void setDrawColor(float _r, float _g, float _b, float _a = 1.0f);
|
||||
|
||||
// Set the number of frames of decay/fade to apply to the scene.
|
||||
bool setDecaySteps(int _steps);
|
||||
|
||||
// Set the brightness multipler applied on each decay frame after the first.
|
||||
bool setDecay(float _decay);
|
||||
|
||||
// Set the brightness multipler applied on the first decay frame.
|
||||
bool setInitialDecay(float _initialDecay);
|
||||
|
||||
// Set a 2d transformation for the display.
|
||||
//
|
||||
// This relates logical coordinates, as passed to vector_display_begin_draw,
|
||||
// vector_display_draw_to, and vector_display_draw, to coordinates from (0,0)
|
||||
// to (width,height) in the destination framebuffer.
|
||||
//
|
||||
// The parameters impact coordinates as follows:
|
||||
//
|
||||
// framebuffer_x = x * scale + offset_x
|
||||
// framebuffer_y = y * scale + offset_y
|
||||
//
|
||||
void setTransform(float _offsetX, float _offsetY, float _scale);
|
||||
|
||||
// Set the line thickness.
|
||||
//
|
||||
// The line thickness is measured in scene coordinates, and includes all pixels lit by
|
||||
// the line before any post-processing. The apparent width of the line to the viewer
|
||||
// is significantly narrower, since brightness decays exponentially to zero within the
|
||||
// bounds of the line.
|
||||
//
|
||||
// Thickness, by default, is guessed based on width and height.
|
||||
//
|
||||
// This function clears the display.
|
||||
bool setThickness(float _thickness);
|
||||
void setDefaultThickness();
|
||||
|
||||
// Set the "brightness" of the display
|
||||
//
|
||||
// useful values range from [0.5, 1.5]. 0.0 disables all glow effects.
|
||||
//
|
||||
// Due to implementation details of the glow effect, glow is related to
|
||||
// the pixel density of the framebuffer. It may require adjustment,
|
||||
// particularly when moving between devices of very different pixel density.
|
||||
//
|
||||
void setBrightness(float _brightness);
|
||||
|
||||
// Get the size from a vector display.
|
||||
void getSize(float* _outWidth, float* _outHeight);
|
||||
protected:
|
||||
void screenSpaceQuad(float _textureWidth, float _textureHeight, float _width = 1.0f, float _height = 1.0f);
|
||||
|
||||
typedef struct //has to match the spec submitted to the 3d-api!
|
||||
{ float x, y, z;
|
||||
float u, v;
|
||||
uint32_t color; } point_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float x, y;
|
||||
} pending_point_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float x0, y0, x1, y1; // nominal points
|
||||
float a; // angle
|
||||
float sin_a, cos_a; // precomputed trig
|
||||
|
||||
float xl0, yl0, xl1, yl1; // left side of the box
|
||||
float xr0, yr0, xr1, yr1; // right side of the box
|
||||
|
||||
int is_first, is_last;
|
||||
int has_next, has_prev; // booleans indicating whether this line connects to prev/next
|
||||
|
||||
float xlt0, ylt0, xlt1, ylt1; // coordinates of endcaps (if !has_prev/!has_next)
|
||||
float xrt0, yrt0, xrt1, yrt1; // coordinates of endcaps (if !has_prev/!has_next)
|
||||
|
||||
float tl0, tl1, tr0, tr1;
|
||||
|
||||
float s0, s1; // shorten line by this amount
|
||||
|
||||
float len;
|
||||
} line_t;
|
||||
|
||||
float effectiveThickness();
|
||||
void setupResDependent();
|
||||
void teardownResDependent();
|
||||
|
||||
void appendTexpoint(float _x, float _y, float _u, float _v);
|
||||
|
||||
float normalizef(float _a);
|
||||
|
||||
void drawFan(float _cx, float _cy, float _pa, float _a, float _t, float _s, float _e);
|
||||
void drawLines(line_t* _lines, int _numberLines);
|
||||
void genLinetex();
|
||||
|
||||
bool m_originBottomLeft;
|
||||
float m_texelHalf;
|
||||
|
||||
bgfx::ProgramHandle m_drawToScreenShader; // program for drawing to the framebuffer
|
||||
bgfx::UniformHandle u_compose_alpha;
|
||||
|
||||
bgfx::FrameBufferHandle m_sceneFrameBuffer;
|
||||
|
||||
bgfx::ProgramHandle m_blurShader; // program for gaussian blur
|
||||
bgfx::UniformHandle u_blur_scale;
|
||||
bgfx::UniformHandle u_compose_mult;
|
||||
bgfx::UniformHandle s_textureSampler; //texture handle for blur
|
||||
|
||||
bgfx::ProgramHandle m_blitShader;
|
||||
|
||||
bgfx::FrameBufferHandle m_glow0FrameBuffer; // framebuffer for glow pass 0
|
||||
bgfx::FrameBufferHandle m_glow1FrameBuffer; // framebuffer for glow pass 1
|
||||
|
||||
int m_view;
|
||||
|
||||
uint16_t m_screenWidth, m_screenHeight;
|
||||
uint16_t m_glowWidth, m_glowHeight;
|
||||
|
||||
int m_numberDecaySteps;
|
||||
float m_decayValue;
|
||||
uint8_t m_drawColorR, m_drawColorG, m_drawColorB, m_drawColorA;
|
||||
|
||||
stl::vector<point_t> m_points;
|
||||
stl::vector<pending_point_t> m_pendingPoints;
|
||||
|
||||
int m_currentDrawStep;
|
||||
stl::vector<bgfx::DynamicVertexBufferHandle> m_vertexBuffers;
|
||||
stl::vector<int> m_vertexBuffersSize;
|
||||
|
||||
bgfx::TextureHandle m_lineTexId;
|
||||
bgfx::UniformHandle s_lineTexture;
|
||||
|
||||
float m_initialDecay;
|
||||
|
||||
float m_thickness;
|
||||
bool m_customThicknessEnabled;
|
||||
|
||||
float m_brightness;
|
||||
|
||||
float m_drawOffsetX, m_drawOffsetY;
|
||||
float m_drawScale;
|
||||
};
|
||||
|
||||
#endif //#ifndef __BGFX_VECTORDISPLAY_H__
|
10
examples/23-vectordisplay/vs_vectordisplay_fb.sc
Normal file
10
examples/23-vectordisplay/vs_vectordisplay_fb.sc
Normal file
|
@ -0,0 +1,10 @@
|
|||
$input a_position, a_texcoord0, a_color0
|
||||
$output v_texcoord0, v_color0
|
||||
|
||||
#include <bgfx_shader.sh>
|
||||
|
||||
void main() {
|
||||
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) );
|
||||
v_color0 = a_color0;
|
||||
v_texcoord0 = a_texcoord0;
|
||||
}
|
Loading…
Reference in a new issue