diff options
Diffstat (limited to '3rdparty/bgfx/examples/13-stencil/stencil.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/13-stencil/stencil.cpp | 188 |
1 files changed, 96 insertions, 92 deletions
diff --git a/3rdparty/bgfx/examples/13-stencil/stencil.cpp b/3rdparty/bgfx/examples/13-stencil/stencil.cpp index 5308757e87f..cfbf0615c9a 100644 --- a/3rdparty/bgfx/examples/13-stencil/stencil.cpp +++ b/3rdparty/bgfx/examples/13-stencil/stencil.cpp @@ -1,6 +1,6 @@ /* * Copyright 2013-2014 Dario Manesku. All rights reserved. - * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE */ #include <string> @@ -15,7 +15,7 @@ namespace bgfx { - int32_t read(bx::ReaderI* _reader, bgfx::VertexDecl& _decl, bx::Error* _err = NULL); + int32_t read(bx::ReaderI* _reader, bgfx::VertexLayout& _layout, bx::Error* _err); } namespace @@ -43,7 +43,7 @@ struct PosNormalTexcoordVertex 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) @@ -51,10 +51,10 @@ struct PosNormalTexcoordVertex .end(); } - static bgfx::VertexDecl ms_decl; + static bgfx::VertexLayout ms_layout; }; -bgfx::VertexDecl PosNormalTexcoordVertex::ms_decl; +bgfx::VertexLayout PosNormalTexcoordVertex::ms_layout; static const float s_texcoord = 5.0f; static PosNormalTexcoordVertex s_hplaneVertices[] = @@ -131,8 +131,9 @@ static bgfx::UniformHandle s_texColor; void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil) { - for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) ) + for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 ) { + const uint32_t ntz = bx::uint32_cnttz(viewMask); viewMask >>= ntz; view += ntz; @@ -142,8 +143,9 @@ void setViewClearMask(uint32_t _viewMask, uint8_t _flags, uint32_t _rgba, float void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _proj) { - for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) ) + for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 ) { + const uint32_t ntz = bx::uint32_cnttz(viewMask); viewMask >>= ntz; view += ntz; @@ -153,8 +155,9 @@ void setViewTransformMask(uint32_t _viewMask, const void* _view, const void* _pr void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height) { - for (uint32_t view = 0, viewMask = _viewMask, ntz = bx::uint32_cnttz(_viewMask); 0 != viewMask; viewMask >>= 1, view += 1, ntz = bx::uint32_cnttz(viewMask) ) + for (uint32_t view = 0, viewMask = _viewMask; 0 != viewMask; viewMask >>= 1, view += 1 ) { + const uint32_t ntz = bx::uint32_cnttz(viewMask); viewMask >>= ntz; view += ntz; @@ -162,40 +165,39 @@ void setViewRectMask(uint32_t _viewMask, uint16_t _x, uint16_t _y, uint16_t _wid } } -void mtxReflected(float*__restrict _result - , const float* __restrict _p /* plane */ - , const float* __restrict _n /* normal */ - ) +void mtxReflected(float* _result, const bx::Vec3& _pos, const bx::Vec3& _normal) { - float dot = bx::vec3Dot(_p, _n); - - _result[ 0] = 1.0f - 2.0f * _n[0] * _n[0]; //1-2Nx^2 - _result[ 1] = -2.0f * _n[0] * _n[1]; //-2*Nx*Ny - _result[ 2] = -2.0f * _n[0] * _n[2]; //-2*NxNz - _result[ 3] = 0.0f; //0 - - _result[ 4] = -2.0f * _n[0] * _n[1]; //-2*NxNy - _result[ 5] = 1.0f - 2.0f * _n[1] * _n[1]; //1-2*Ny^2 - _result[ 6] = -2.0f * _n[1] * _n[2]; //-2*NyNz - _result[ 7] = 0.0f; //0 - - _result[ 8] = -2.0f * _n[0] * _n[2]; //-2*NxNz - _result[ 9] = -2.0f * _n[1] * _n[2]; //-2NyNz - _result[10] = 1.0f - 2.0f * _n[2] * _n[2]; //1-2*Nz^2 - _result[11] = 0.0f; //0 - - _result[12] = 2.0f * dot * _n[0]; //2*dot*Nx - _result[13] = 2.0f * dot * _n[1]; //2*dot*Ny - _result[14] = 2.0f * dot * _n[2]; //2*dot*Nz - _result[15] = 1.0f; //1 + const float nx = _normal.x; + const float ny = _normal.y; + const float nz = _normal.z; + + _result[ 0] = 1.0f - 2.0f * nx * nx; + _result[ 1] = - 2.0f * nx * ny; + _result[ 2] = - 2.0f * nx * nz; + _result[ 3] = 0.0f; + + _result[ 4] = - 2.0f * nx * ny; + _result[ 5] = 1.0f - 2.0f * ny * ny; + _result[ 6] = - 2.0f * ny * nz; + _result[ 7] = 0.0f; + + _result[ 8] = - 2.0f * nx * nz; + _result[ 9] = - 2.0f * ny * nz; + _result[10] = 1.0f - 2.0f * nz * nz; + _result[11] = 0.0f; + + const float dot = bx::dot(_pos, _normal); + + _result[12] = 2.0f * dot * nx; + _result[13] = 2.0f * dot * ny; + _result[14] = 2.0f * dot * nz; + _result[15] = 1.0f; } -void mtxShadow(float* __restrict _result - , const float* __restrict _ground - , const float* __restrict _light - ) +void mtxShadow(float* _result, const float* _ground, const float* _light) { - float dot = _ground[0] * _light[0] + const float dot = + _ground[0] * _light[0] + _ground[1] * _light[1] + _ground[2] * _light[2] + _ground[3] * _light[3] @@ -222,10 +224,7 @@ void mtxShadow(float* __restrict _result _result[15] = dot - _light[3] * _ground[3]; } -void mtxBillboard(float* __restrict _result - , const float* __restrict _view - , const float* __restrict _pos - , const float* __restrict _scale) +void mtxBillboard(float* _result, const float* _view, const float* _pos, const float* _scale) { _result[ 0] = _view[0] * _scale[0]; _result[ 1] = _view[4] * _scale[0]; @@ -610,6 +609,9 @@ struct Group { m_vbh.idx = bgfx::kInvalidHandle; m_ibh.idx = bgfx::kInvalidHandle; + m_sphere={}; + m_aabb={}; + m_obb={}; m_prims.clear(); } @@ -623,15 +625,15 @@ struct Group struct Mesh { - void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices) + void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexLayout _layout, const uint16_t* _indices, uint32_t _numIndices) { Group group; const bgfx::Memory* mem; uint32_t size; - size = _numVertices*_decl.getStride(); + size = _numVertices*_layout.getStride(); mem = bgfx::makeRef(_vertices, size); - group.m_vbh = bgfx::createVertexBuffer(mem, _decl); + group.m_vbh = bgfx::createVertexBuffer(mem, _layout); size = _numIndices*2; mem = bgfx::makeRef(_indices, size); @@ -651,35 +653,37 @@ struct Mesh Group group; + bx::Error err; + uint32_t chunk; - while (4 == bx::read(reader, chunk) ) + while (4 == bx::read(reader, chunk, &err) ) { switch (chunk) { case BGFX_CHUNK_MAGIC_VB: { - bx::read(reader, group.m_sphere); - bx::read(reader, group.m_aabb); - bx::read(reader, group.m_obb); + bx::read(reader, group.m_sphere, &err); + bx::read(reader, group.m_aabb, &err); + bx::read(reader, group.m_obb, &err); - bgfx::read(reader, m_decl); - uint16_t stride = m_decl.getStride(); + bgfx::read(reader, m_layout, &err); + uint16_t stride = m_layout.getStride(); uint16_t numVertices; - bx::read(reader, numVertices); + bx::read(reader, numVertices, &err); const bgfx::Memory* mem = bgfx::alloc(numVertices*stride); - bx::read(reader, mem->data, mem->size); + bx::read(reader, mem->data, mem->size, &err); - group.m_vbh = bgfx::createVertexBuffer(mem, m_decl); + group.m_vbh = bgfx::createVertexBuffer(mem, m_layout); } break; case BGFX_CHUNK_MAGIC_IB: { uint32_t numIndices; - bx::read(reader, numIndices); + bx::read(reader, numIndices, &err); const bgfx::Memory* mem = bgfx::alloc(numIndices*2); - bx::read(reader, mem->data, mem->size); + bx::read(reader, mem->data, mem->size, &err); group.m_ibh = bgfx::createIndexBuffer(mem); } break; @@ -687,31 +691,31 @@ struct Mesh case BGFX_CHUNK_MAGIC_PRI: { uint16_t len; - bx::read(reader, len); + bx::read(reader, len, &err); std::string material; material.resize(len); - bx::read(reader, const_cast<char*>(material.c_str() ), len); + bx::read(reader, const_cast<char*>(material.c_str() ), len, &err); uint16_t num; - bx::read(reader, num); + bx::read(reader, num, &err); for (uint32_t ii = 0; ii < num; ++ii) { - bx::read(reader, len); + bx::read(reader, len, &err); std::string name; name.resize(len); - bx::read(reader, const_cast<char*>(name.c_str() ), len); + bx::read(reader, const_cast<char*>(name.c_str() ), len, &err); Primitive prim; - bx::read(reader, prim.m_startIndex); - bx::read(reader, prim.m_numIndices); - bx::read(reader, prim.m_startVertex); - bx::read(reader, prim.m_numVertices); - bx::read(reader, prim.m_sphere); - bx::read(reader, prim.m_aabb); - bx::read(reader, prim.m_obb); + bx::read(reader, prim.m_startIndex, &err); + bx::read(reader, prim.m_numIndices, &err); + bx::read(reader, prim.m_startVertex, &err); + bx::read(reader, prim.m_numVertices, &err); + bx::read(reader, prim.m_sphere, &err); + bx::read(reader, prim.m_aabb, &err); + bx::read(reader, prim.m_obb, &err); group.m_prims.push_back(prim); } @@ -776,12 +780,12 @@ struct Mesh // Submit bgfx::submit(_id, _program); - // Keep track of submited view ids + // Keep track of submitted view ids s_viewMask |= 1 << _id; } } - bgfx::VertexDecl m_decl; + bgfx::VertexLayout m_layout; typedef std::vector<Group> GroupArray; GroupArray m_groups; }; @@ -790,8 +794,8 @@ struct Mesh class ExampleStencil : public entry::AppI { public: - ExampleStencil(const char* _name, const char* _description) - : entry::AppI(_name, _description) + ExampleStencil(const char* _name, const char* _description, const char* _url) + : entry::AppI(_name, _description, _url) { } @@ -808,6 +812,9 @@ public: bgfx::Init init; init.type = args.m_type; init.vendorId = args.m_pciId; + init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle); + init.platformData.ndt = entry::getNativeDisplayHandle(); + init.platformData.type = entry::getNativeWindowHandleType(entry::kDefaultWindowHandle); init.resolution.width = m_viewState.m_width; init.resolution.height = m_viewState.m_height; init.resolution.reset = m_reset; @@ -823,7 +830,7 @@ public: s_uniforms.init(); - s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1); + s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler); m_programTextureLighting = loadProgram("vs_stencil_texture_lighting", "fs_stencil_texture_lighting"); m_programColorLighting = loadProgram("vs_stencil_color_lighting", "fs_stencil_color_lighting" ); @@ -833,9 +840,9 @@ public: m_bunnyMesh.load("meshes/bunny.bin"); m_columnMesh.load("meshes/column.bin"); - m_cubeMesh.load(s_cubeVertices, BX_COUNTOF(s_cubeVertices), PosNormalTexcoordVertex::ms_decl, s_cubeIndices, BX_COUNTOF(s_cubeIndices) ); - m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordVertex::ms_decl, s_planeIndices, BX_COUNTOF(s_planeIndices) ); - m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordVertex::ms_decl, s_planeIndices, BX_COUNTOF(s_planeIndices) ); + m_cubeMesh.load(s_cubeVertices, BX_COUNTOF(s_cubeVertices), PosNormalTexcoordVertex::ms_layout, s_cubeIndices, BX_COUNTOF(s_cubeIndices) ); + m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordVertex::ms_layout, s_planeIndices, BX_COUNTOF(s_planeIndices) ); + m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordVertex::ms_layout, s_planeIndices, BX_COUNTOF(s_planeIndices) ); m_figureTex = loadTexture("textures/figure-rgba.dds"); m_flareTex = loadTexture("textures/flare.dds"); @@ -866,9 +873,8 @@ public: const bgfx::Caps* caps = bgfx::getCaps(); bx::mtxProj(m_viewState.m_proj, 60.0f, aspect, 0.1f, 100.0f, caps->homogeneousDepth); - float initialPos[3] = { 0.0f, 18.0f, -40.0f }; cameraCreate(); - cameraSetPosition(initialPos); + cameraSetPosition({ 0.0f, 18.0f, -40.0f }); cameraSetVerticalAngle(-0.35f); cameraGetViewMtx(m_viewState.m_view); @@ -994,7 +1000,7 @@ public: s_uniforms.m_time = time; // Update camera. - cameraUpdate(deltaTime, m_mouseState); + cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() ); cameraGetViewMtx(m_viewState.m_view); static float lightTimeAccumulator = 0.0f; @@ -1125,18 +1131,15 @@ public: // Compute reflected matrix. float reflectMtx[16]; - float plane_pos[3] = { 0.0f, 0.01f, 0.0f }; - float normal[3] = { 0.0f, 1.0f, 0.0f }; - mtxReflected(reflectMtx, plane_pos, normal); + mtxReflected(reflectMtx, { 0.0f, 0.01f, 0.0f }, { 0.0f, 1.0f, 0.0f }); // Reflect lights. - float reflectedLights[MAX_NUM_LIGHTS][4]; for (uint8_t ii = 0; ii < numLights; ++ii) { - bx::vec3MulMtx(reflectedLights[ii], lightPosRadius[ii], reflectMtx); - reflectedLights[ii][3] = lightPosRadius[ii][3]; + bx::Vec3 reflected = bx::mul(bx::load<bx::Vec3>(lightPosRadius[ii]), reflectMtx); + bx::store(&s_uniforms.m_lightPosRadius[ii], reflected); + s_uniforms.m_lightPosRadius[ii][3] = lightPosRadius[ii][3]; } - bx::memCopy(s_uniforms.m_lightPosRadius, reflectedLights, numLights * 4*sizeof(float) ); // Reflect and submit bunny. float mtxReflectedBunny[16]; @@ -1226,11 +1229,7 @@ public: } // Ground plane. - float ground[4]; - float plane_pos[3] = { 0.0f, 0.0f, 0.0f }; - float normal[3] = { 0.0f, 1.0f, 0.0f }; - bx::memCopy(ground, normal, sizeof(float) * 3); - ground[3] = -bx::vec3Dot(plane_pos, normal) - 0.01f; // - 0.01 against z-fighting + float ground[4] = { 0.0f, 1.0f, 0.0f, -bx::dot(bx::Vec3{ 0.0f, 0.0f, 0.0f }, bx::Vec3{ 0.0f, 1.0f, 0.0f }) - 0.01f }; for (uint8_t ii = 0, viewId = RENDER_VIEWID_RANGE5_PASS_6; ii < numLights; ++ii, ++viewId) { @@ -1411,4 +1410,9 @@ public: } // namespace -ENTRY_IMPLEMENT_MAIN(ExampleStencil, "13-stencil", "Stencil reflections and shadows."); +ENTRY_IMPLEMENT_MAIN( + ExampleStencil + , "13-stencil" + , "Stencil reflections and shadows." + , "https://bkaradzic.github.io/bgfx/examples.html#stencil" + ); |