summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/06-bump
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/06-bump')
-rw-r--r--3rdparty/bgfx/examples/06-bump/bump.cpp27
-rw-r--r--3rdparty/bgfx/examples/06-bump/fs_bump.sc27
-rw-r--r--3rdparty/bgfx/examples/06-bump/makefile2
-rw-r--r--3rdparty/bgfx/examples/06-bump/vs_bump.sc37
-rw-r--r--3rdparty/bgfx/examples/06-bump/vs_bump_instanced.sc19
5 files changed, 64 insertions, 48 deletions
diff --git a/3rdparty/bgfx/examples/06-bump/bump.cpp b/3rdparty/bgfx/examples/06-bump/bump.cpp
index 2f299161926..7cf43f504aa 100644
--- a/3rdparty/bgfx/examples/06-bump/bump.cpp
+++ b/3rdparty/bgfx/examples/06-bump/bump.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
+ * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
@@ -22,7 +22,7 @@ struct PosNormalTangentTexcoordVertex
static void init()
{
- ms_decl
+ ms_layout
.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
@@ -31,10 +31,10 @@ struct PosNormalTangentTexcoordVertex
.end();
}
- static bgfx::VertexDecl ms_decl;
+ static bgfx::VertexLayout ms_layout;
};
-bgfx::VertexDecl PosNormalTangentTexcoordVertex::ms_decl;
+bgfx::VertexLayout PosNormalTangentTexcoordVertex::ms_layout;
static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
{
@@ -85,8 +85,8 @@ static const uint16_t s_cubeIndices[36] =
class ExampleBump : public entry::AppI
{
public:
- ExampleBump(const char* _name, const char* _description)
- : entry::AppI(_name, _description)
+ ExampleBump(const char* _name, const char* _description, const char* _url)
+ : entry::AppI(_name, _description, _url)
{
}
@@ -127,7 +127,7 @@ public:
calcTangents(s_cubeVertices
, BX_COUNTOF(s_cubeVertices)
- , PosNormalTangentTexcoordVertex::ms_decl
+ , PosNormalTangentTexcoordVertex::ms_layout
, s_cubeIndices
, BX_COUNTOF(s_cubeIndices)
);
@@ -135,15 +135,15 @@ public:
// Create static vertex buffer.
m_vbh = bgfx::createVertexBuffer(
bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
- , PosNormalTangentTexcoordVertex::ms_decl
+ , PosNormalTangentTexcoordVertex::ms_layout
);
// Create static index buffer.
m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
// Create texture sampler uniforms.
- s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
- s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
+ s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
+ s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Sampler);
m_numLights = 4;
u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4, m_numLights);
@@ -370,4 +370,9 @@ public:
} // namespace
-ENTRY_IMPLEMENT_MAIN(ExampleBump, "06-bump", "Loading textures.");
+ENTRY_IMPLEMENT_MAIN(
+ ExampleBump
+ , "06-bump"
+ , "Loading textures."
+ , "https://bkaradzic.github.io/bgfx/examples.html#bump"
+ );
diff --git a/3rdparty/bgfx/examples/06-bump/fs_bump.sc b/3rdparty/bgfx/examples/06-bump/fs_bump.sc
index 65154548ba2..3b07e9d8571 100644
--- a/3rdparty/bgfx/examples/06-bump/fs_bump.sc
+++ b/3rdparty/bgfx/examples/06-bump/fs_bump.sc
@@ -1,7 +1,7 @@
-$input v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0 // in...
+$input v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0// in...
/*
- * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
+ * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
@@ -15,7 +15,8 @@ uniform vec4 u_lightRgbInnerR[4];
vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
{
float ndotl = dot(_normal, _lightDir);
- vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal);
+ //vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal);
+ vec3 reflected = 2.0*ndotl*_normal - _lightDir;
float rdotv = dot(reflected, _viewDir);
return vec2(ndotl, rdotv);
}
@@ -45,25 +46,31 @@ vec3 calcLight(int _idx, mat3 _tbn, vec3 _wpos, vec3 _normal, vec3 _view)
{
vec3 lp = u_lightPosRadius[_idx].xyz - _wpos;
float attn = 1.0 - smoothstep(u_lightRgbInnerR[_idx].w, 1.0, length(lp) / u_lightPosRadius[_idx].w);
- vec3 lightDir = mul(_tbn, normalize(lp) );
+ vec3 lightDir = mul( normalize(lp), _tbn );
vec2 bln = blinn(lightDir, _normal, _view);
vec4 lc = lit(bln.x, bln.y, 1.0);
vec3 rgb = u_lightRgbInnerR[_idx].xyz * saturate(lc.y) * attn;
return rgb;
}
+mat3 mtx3FromCols(vec3 c0, vec3 c1, vec3 c2)
+{
+#if BGFX_SHADER_LANGUAGE_GLSL
+ return mat3(c0, c1, c2);
+#else
+ return transpose(mat3(c0, c1, c2));
+#endif
+}
+
+
void main()
{
- mat3 tbn = mat3(
- normalize(v_tangent),
- normalize(v_bitangent),
- normalize(v_normal)
- );
+ mat3 tbn = mtx3FromCols(v_tangent, v_bitangent, v_normal);
vec3 normal;
normal.xy = texture2D(s_texNormal, v_texcoord0).xy * 2.0 - 1.0;
normal.z = sqrt(1.0 - dot(normal.xy, normal.xy) );
- vec3 view = -normalize(v_view);
+ vec3 view = normalize(v_view);
vec3 lightColor;
lightColor = calcLight(0, tbn, v_wpos, normal, view);
diff --git a/3rdparty/bgfx/examples/06-bump/makefile b/3rdparty/bgfx/examples/06-bump/makefile
index 171709170a4..ef9203197d7 100644
--- a/3rdparty/bgfx/examples/06-bump/makefile
+++ b/3rdparty/bgfx/examples/06-bump/makefile
@@ -1,5 +1,5 @@
#
-# Copyright 2011-2018 Branimir Karadzic. All rights reserved.
+# Copyright 2011-2019 Branimir Karadzic. All rights reserved.
# License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
#
diff --git a/3rdparty/bgfx/examples/06-bump/vs_bump.sc b/3rdparty/bgfx/examples/06-bump/vs_bump.sc
index d41f87fc2fa..6975ccbbf38 100644
--- a/3rdparty/bgfx/examples/06-bump/vs_bump.sc
+++ b/3rdparty/bgfx/examples/06-bump/vs_bump.sc
@@ -2,36 +2,43 @@ $input a_position, a_normal, a_tangent, a_texcoord0
$output v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0
/*
- * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
+ * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "../common/common.sh"
+mat3 mtx3FromCols(vec3 c0, vec3 c1, vec3 c2)
+{
+#ifdef BGFX_SHADER_LANGUAGE_GLSL
+ return mat3(c0, c1, c2);
+#else
+ return transpose(mat3(c0, c1, c2));
+#endif
+}
+
void main()
{
vec3 wpos = mul(u_model[0], vec4(a_position, 1.0) ).xyz;
+ v_wpos = wpos;
+
gl_Position = mul(u_viewProj, vec4(wpos, 1.0) );
vec4 normal = a_normal * 2.0 - 1.0;
- vec3 wnormal = mul(u_model[0], vec4(normal.xyz, 0.0) ).xyz;
-
vec4 tangent = a_tangent * 2.0 - 1.0;
- vec3 wtangent = mul(u_model[0], vec4(tangent.xyz, 0.0) ).xyz;
-
- vec3 viewNormal = normalize(mul(u_view, vec4(wnormal, 0.0) ).xyz);
- vec3 viewTangent = normalize(mul(u_view, vec4(wtangent, 0.0) ).xyz);
- vec3 viewBitangent = cross(viewNormal, viewTangent) * tangent.w;
- mat3 tbn = mat3(viewTangent, viewBitangent, viewNormal);
- v_wpos = wpos;
+ vec3 wnormal = mul(u_model[0], vec4(normal.xyz, 0.0) ).xyz;
+ vec3 wtangent = mul(u_model[0], vec4(tangent.xyz, 0.0) ).xyz;
- vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz;
- v_view = mul(view, tbn);
+ v_normal = normalize(wnormal);
+ v_tangent = normalize(wtangent);
+ v_bitangent = cross(v_normal, v_tangent) * tangent.w;
- v_normal = viewNormal;
- v_tangent = viewTangent;
- v_bitangent = viewBitangent;
+ mat3 tbn = mtx3FromCols(v_tangent, v_bitangent, v_normal);
+ // eye position in world space
+ vec3 weyepos = mul(vec4(0.0, 0.0, 0.0, 1.0), u_view).xyz;
+ // tangent space view dir
+ v_view = mul(weyepos - wpos, tbn);
v_texcoord0 = a_texcoord0;
}
diff --git a/3rdparty/bgfx/examples/06-bump/vs_bump_instanced.sc b/3rdparty/bgfx/examples/06-bump/vs_bump_instanced.sc
index 24c509732b7..31929c4b648 100644
--- a/3rdparty/bgfx/examples/06-bump/vs_bump_instanced.sc
+++ b/3rdparty/bgfx/examples/06-bump/vs_bump_instanced.sc
@@ -2,7 +2,7 @@ $input a_position, a_normal, a_tangent, a_texcoord0, i_data0, i_data1, i_data2,
$output v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0
/*
- * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
+ * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
@@ -25,19 +25,16 @@ void main()
vec4 tangent = a_tangent * 2.0 - 1.0;
vec3 wtangent = instMul(model, vec4(tangent.xyz, 0.0) ).xyz;
- vec3 viewNormal = normalize(mul(u_view, vec4(wnormal, 0.0) ).xyz);
- vec3 viewTangent = normalize(mul(u_view, vec4(wtangent, 0.0) ).xyz);
- vec3 viewBitangent = cross(viewNormal, viewTangent) * tangent.w;
- mat3 tbn = mat3(viewTangent, viewBitangent, viewNormal);
+ v_normal = wnormal;
+ v_tangent = wtangent;
+ v_bitangent = cross(v_normal, v_tangent) * tangent.w;
- v_wpos = wpos;
+ mat3 tbn = mat3(v_tangent, v_bitangent, v_normal);
- vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz;
- v_view = instMul(view, tbn);
+ v_wpos = wpos;
- v_normal = viewNormal;
- v_tangent = viewTangent;
- v_bitangent = viewBitangent;
+ vec3 weyepos = mul(vec4(0.0, 0.0, 0.0, 1.0), u_view).xyz;
+ v_view = instMul(weyepos - wpos, tbn);
v_texcoord0 = a_texcoord0;
}