Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • gamedev/fggl
  • onuralpsezer/fggl
2 results
Show changes
Showing
with 858 additions and 637 deletions
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 10/12/22.
//
#include "fggl/grid/hexagon.hpp"
namespace fggl::grid {
std::vector<IntHex> lineTo(const IntHex& start, const IntHex& end) {
const int distance = start.distance(end);
std::vector<IntHex> line;
for (auto i=0; i < distance; ++i) {
line.push_back( round2(hexLerp(start, end, 1.0F/distance * i)) );
}
return line;
}
} // namespace fggl:grid
target_sources( ${PROJECT_NAME}
PRIVATE
widget.cpp
widgets.cpp
containers.cpp
fonts.cpp
model/parser.cpp
model/structure.cpp
renderer/renderer.cpp
)
find_package(Freetype)
target_link_libraries(${PROJECT_NAME} PUBLIC Freetype::Freetype)
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include <fggl/gui/containers.hpp>
#include <spdlog/spdlog.h>
namespace fggl::gui {
Widget *Container::getChildAt(const math::vec2 &point) {
for ( auto& child : m_children ){
if ( child->contains(point) ){
auto Container::getChildAt(const math::vec2 &point) -> Widget * {
for (auto &child : m_children) {
if (child->contains(point)) {
return child->getChildAt(point);
}
}
......@@ -13,21 +27,99 @@ namespace fggl::gui {
return nullptr;
}
bool Container::contains(const math::vec2 &point) {
auto Container::contains(const math::vec2 &point) -> bool {
return true;
}
void Container::render(gfx::Paint &paint) {
for( auto& child : m_children ){
child->render( paint );
for (auto &child : m_children) {
child->render(paint);
}
}
void Container::add(std::unique_ptr<Widget> widget) {
m_children.push_back( std::move(widget) );
m_children.push_back(std::move(widget));
m_dirty = true;
}
void Container::onMouseOver(math::vec2 pos) {
Widget::onMouseOver(pos);
auto* childHover = getChildAt(pos);
for ( auto& child : m_children) {
if ( child.get() != childHover ) {
child->onExit(pos);
}
}
if ( childHover != nullptr ) {
childHover->onMouseOver(pos);
}
}
void Container::onEnter(math::vec2i pos) {
Widget::onEnter(pos);
for ( auto& child : m_children ) {
if ( child->contains(pos)) {
child->onEnter(pos);
}
}
}
void Container::onExit(math::vec2i pos) {
Widget::onExit(pos);
for ( auto& child : m_children) {
child->onExit(pos);
}
}
GridBox::GridBox(uint32_t rows, uint32_t cols, uint32_t padx, uint32_t pady) : m_rows(rows), m_cols(cols), m_padding(padx, pady) {}
void GridBox::layout() {
assert( m_rows != 0 || m_cols != 0 );
if ( m_rows == 0 ) {
int rows = m_children.size() / m_cols;
// figure out the width and heights
auto* widths = new float[m_cols]{0.0F};
auto* heights = new float[rows]{0.0F};
for ( auto idx = 0U; idx < m_children.size(); ++idx) {
auto& child = m_children[idx];
int col = idx % m_cols;
int row = idx / m_cols;
widths[col] = std::max( child->size().x, widths[col] );
heights[row] = std::max( child->size().y, heights[row] );
}
// populate the grid
fggl::math::vec2i pos{0, 0};
unsigned int row = 0;
unsigned int col = 0;
for ( auto& child : m_children ) {
fggl::math::vec2i size{ widths[col], heights[row] };
child->size(pos, size);
child->layout();
// next iter
pos.x += size.x + m_padding.x;
col++;
if ( col == m_cols ) {
col = 0;
row++;
pos.x = 0;
pos.y += size.y + m_padding.y;
}
}
// cleanup variables
delete[] widths;
delete[] heights;
}
}
/*
Box::Box( LayoutAxis axis ) : m_axis( axis ) {}
......@@ -56,7 +148,7 @@ namespace fggl::gui {
void Panel::render(gfx::Paint &paint) {
// background painting time
gfx::Path2D background(topLeft());
background.colour(math::vec3(32.0f/255.0f, 74.0F/255.0F, 135.0F/255.0F));
background.colour(math::vec3(32.0F / 255.0F, 74.0F / 255.0F, 135.0F / 255.0F));
draw_box(background, topLeft(), bottomRight());
paint.fill(background);
......
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include "fggl/gui/fonts.hpp"
......@@ -29,29 +23,29 @@ namespace fggl::gui {
FT_Done_Face(m_face);
}
FontLibrary::FontLibrary() {
FontLibrary::FontLibrary(data::Storage *storage) : m_context(nullptr), m_storage(storage) {
FT_Init_FreeType(&m_context);
m_defaultFont = getFont(DEFAULT_FONT_NAME);
}
FontLibrary::~FontLibrary() {
// free all fonts
for (auto& face : m_cache ){
face.second = nullptr;
}
m_defaultFont = nullptr;
m_cache.clear();
// shut the library down
FT_Done_FreeType(m_context);
}
GlyphMetrics &FontFace::populateMetrics(char letter) {
if (FT_Load_Char(m_face, letter, FT_LOAD_RENDER) ) {
auto FontFace::populateMetrics(char letter) -> GlyphMetrics & {
if (FT_Load_Char(m_face, letter, FT_LOAD_RENDER)) {
// something bad happened
return m_metrics['?'];
}
GlyphMetrics metrics {
{ m_face->glyph->bitmap.width, m_face->glyph->bitmap.rows},
{ m_face->glyph->bitmap_left, m_face->glyph->bitmap_top },
GlyphMetrics metrics{
{m_face->glyph->bitmap.width, m_face->glyph->bitmap.rows},
{m_face->glyph->bitmap_left, m_face->glyph->bitmap_top},
m_face->glyph->advance.x
};
......@@ -60,8 +54,8 @@ namespace fggl::gui {
return it.first->second;
}
void FontFace::texture(char letter, int &width, int &height, void **buff) {
if (FT_Load_Char(m_face, letter, FT_LOAD_RENDER) ) {
void FontFace::texture(char letter, int &width, int &height, unsigned char **buff) {
if (FT_Load_Char(m_face, letter, FT_LOAD_RENDER)) {
// something bad happened
return;
}
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 11/03/23.
//
#include "fggl/gui/model/parser.hpp"
#include <yaml-cpp/yaml.h>
namespace fggl::gui::model {
Widget* YamlToWidgetTree(WidgetFactory& factory, const YAML::Node& config) {
Widget* root;
if ( config["template"] ) {
root = factory.build( config["template"].as<std::string>());
} else {
root = factory.buildEmpty();
}
// deal with attrs
for ( auto attr : config["attrs"] ) {
root->set(attr.first.as<std::string>(), attr.second.as<std::string>());
}
// deal with child nodes
for ( auto child : config["children"] ) {
Widget* childWidget = YamlToWidgetTree(factory, child);
root->addChild(*childWidget);
}
// are we a template definition?
if ( config["define"] ) {
factory.push( config["define"].as<std::string>(), std::move(*root) );
return factory.getTemplate( config["define"].as<std::string>() );
}
return root;
}
inline Widget* parseFile(WidgetFactory& factory, const std::string& path) {
YAML::Node root = YAML::LoadFile(path);
if ( !root ){
return nullptr;
}
return YamlToWidgetTree(factory, root);
}
}
\ No newline at end of file
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 11/03/23.
//
#include "fggl/gui/model/structure.hpp"
#include "fggl/gui/fonts.hpp"
#include <algorithm>
#include <utility>
#include <string>
namespace fggl::gui::model {
math::vec2 calcTextBounds(const std::string& value, std::shared_ptr<FontFace> face) {
if ( face == nullptr ){
debug::warning("No preferred font sent, cowardly refusing to process text");
return {};
}
math::vec2 max{0, 0};
for (auto letter : value) {
auto metrics = face->metrics(letter);
max.x += metrics.size.x + (metrics.advance >> 6);
max.y = std::max(max.y, metrics.size.y);
}
return max;
}
inline math::vec2 calcBoxContrib(const Widget& widget, const std::string& name) {
return math::vec2{
widget.get_or_default<float>( name + "::left") + widget.get_or_default<float>(name + "::right"),
widget.get_or_default<float>( name + "::top" ) + widget.get_or_default<float>( name + "::bottom" )
};
}
void Widget::calcPrefSize(std::shared_ptr<FontFace> face) {
if ( !m_dirty ){
return;
}
auto padding = calcBoxContrib( *this, "padding");
auto border = calcBoxContrib( *this, "border");
auto content = math::vec2{0,0};
if (hasAttr("text")) {
content += calcTextBounds( get<std::string>("text"), std::move(face) );
}
m_cachedSize = padding + content + content;
debug::info("my preferred size is: ({}, {})", m_cachedSize.x, m_cachedSize.y);
m_dirty = false;
}
}
\ No newline at end of file
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 11/03/23.
//
#include "fggl/gui/renderer/renderer.hpp"
namespace fggl::gui::renderer {
constexpr int PADDING = 15;
void draw_box(gfx::Path2D &path, glm::vec2 topLeft, glm::vec2 bottomRight) {
path.moveTo({topLeft.x, topLeft.y});
path.pathTo({bottomRight.x, topLeft.y});
path.pathTo({bottomRight.x, bottomRight.y});
path.pathTo({topLeft.x, bottomRight.y});
path.pathTo({topLeft.x, topLeft.y});
}
void draw_border_patch(gfx::Paint& paint, Box& bounds, Box& size, math::vec3 colour) {
gfx::Path2D path({0,0});
path.colour(colour);
// draw edges
draw_box(path, {bounds.left + size.left, bounds.top}, {bounds.right - size.right, bounds.top + size.top} );
draw_box(path, {bounds.right - size.right, bounds.top + size.top}, {bounds.right, bounds.bottom - size.bottom} );
draw_box(path, {bounds.left + size.left, bounds.bottom - size.bottom}, {bounds.right - size.right, bounds.bottom} );
draw_box(path, {bounds.left, bounds.top + size.top}, {bounds.left + size.left, bounds.bottom - size.bottom} );
// draw-corners
draw_box(path, {bounds.left, bounds.top}, {bounds.left + size.left, bounds.top + size.top} );
draw_box(path, {bounds.right - size.right, bounds.top}, {bounds.right, bounds.top + size.top} );
draw_box(path, {bounds.left, bounds.bottom - size.bottom}, {bounds.left + size.left, bounds.bottom} );
draw_box(path, {bounds.right - size.right, bounds.bottom - size.bottom}, {bounds.right, bounds.bottom});
paint.fill(path);
}
void draw_border_solid(gfx::Paint& paint, Box& bounds, Box& size, math::vec3 colour) {
gfx::Path2D path({0,0});
path.colour(colour);
// draw edges
draw_box(path, {bounds.left, bounds.top}, {bounds.right, bounds.top + size.top} );
draw_box(path, {bounds.right - size.right, bounds.top + size.top}, {bounds.right, bounds.bottom - size.bottom} );
draw_box(path, {bounds.left, bounds.bottom - size.bottom}, {bounds.right, bounds.bottom} );
draw_box(path, {bounds.left, bounds.top + size.top}, {bounds.left + size.left, bounds.bottom - size.bottom} );
paint.fill(path);
}
void draw_background_solid(gfx::Paint& paint, Box& bounds, math::vec3 colour) {
gfx::Path2D path({0,0});
path.colour(colour);
draw_box(path, {bounds.left, bounds.top}, {bounds.right, bounds.bottom} );
paint.fill(path);
}
void layout(model::Widget& current) {
if ( current.isLeaf() ) {
// if the widget has a defined size, use that
if ( current.hasAttr("size") && !current.hasAttr("text") ) {
return;
}
// else, use the model's preferred size
auto preferred = current.preferredSize();
if ( preferred.has_value() ) {
current.set<math::vec2>("size", preferred.value() );
}
} else {
auto topPad = current.get_or_default<float>("border:top") + current.get_or_default<float>("padding::top");
auto leftPad = current.get_or_default<float>("border::left") + current.get_or_default<float>("padding::left");
math::vec2 size = {topPad, leftPad};
// layout all children
for ( auto& child : current ) {
layout(child);
auto childSize = child.get_or_default<math::vec2>("size");
size.x = std::max( childSize.x, size.x );
size.y += childSize.y;
child.set<math::vec2>("position", {leftPad, size.y});
}
// set our size based on that
current.set<math::vec2>("size", size );
}
}
void visit(const model::Widget& root, gfx::Paint& paint, Box offset) {
// get border size
auto border = get_box(root, "border");
// calculate box bounds
auto pos = get_vec2(root, "position");
auto size = get_vec2(root, "size");
auto bounds = getBounds(pos, size);
bounds.top += offset.top;
bounds.left += offset.left;
// deal with right hand size bounds
//bounds.right = std::min( size.x, offset.width() );
//bounds.right += offset.left;
// deal with bottom bounds
//bounds.bottom = std::min( size.y, offset.height() );
//bounds.bottom += offset.top;
auto background = bounds.trim(border);
draw_background_solid(paint, background, get_vec3_rgb(root, "colour"));
draw_border_patch(paint, bounds, border, get_vec3_rgb(root, "border::colour"));
auto padding = get_box(root, "padding");
background = background.trim(padding);
if ( root.hasAttr("text") ) {
auto text = root.get<std::string>("text");
paint.text(text, {background.left, background.top + PADDING});
}
for (const auto& child : root) {
visit(child, paint, background);
}
}
} // namespace fggl::gui:;renderer
\ No newline at end of file
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 17/04/22.
//
......@@ -6,44 +20,44 @@
namespace fggl::gui {
void buttonBorder( gfx::Path2D& path, glm::vec2 pos, glm::vec2 size ) {
void button_border(gfx::Path2D &path, glm::vec2 pos, glm::vec2 size) {
// outer box
path.colour( {1.0f, 0.0f, 0.0f} );
path.pathTo( { pos.x + size.x, pos.y } );
path.pathTo( { pos.x + size.x, pos.y + size.y } );
path.pathTo( { pos.x, pos.y + size.y } );
path.colour({1.0F, 0.0F, 0.0F});
path.pathTo({pos.x + size.x, pos.y});
path.pathTo({pos.x + size.x, pos.y + size.y});
path.pathTo({pos.x, pos.y + size.y});
path.close();
// inner box
math::vec2 innerTop { pos.x + 5, pos.y + 5 };
math::vec2 innerBottom { pos.x + size.x - 5, pos.y + size.y - 5 };
path.colour( {1.0f, 1.0f, 0.0f} );
path.moveTo( { innerTop.x, innerTop.y } );
path.pathTo( { innerBottom.x, innerTop.y } );
path.pathTo( { innerBottom.x, innerBottom.y } );
path.pathTo( { innerTop.x, innerBottom.y } );
path.pathTo( { innerTop.x, innerTop.y } );
math::vec2 innerTop{pos.x + 5, pos.y + 5};
math::vec2 innerBottom{pos.x + size.x - 5, pos.y + size.y - 5};
path.colour({1.0F, 1.0F, 0.0F});
path.moveTo({innerTop.x, innerTop.y});
path.pathTo({innerBottom.x, innerTop.y});
path.pathTo({innerBottom.x, innerBottom.y});
path.pathTo({innerTop.x, innerBottom.y});
path.pathTo({innerTop.x, innerTop.y});
}
void draw_box( gfx::Path2D& path, glm::vec2 topLeft, glm::vec2 bottomRight ) {
path.moveTo( { topLeft.x, topLeft.y } );
path.pathTo( { bottomRight.x, topLeft.y } );
path.pathTo( { bottomRight.x, bottomRight.y } );
path.pathTo( { topLeft.x, bottomRight.y } );
path.pathTo( { topLeft.x, topLeft.y } );
void draw_box(gfx::Path2D &path, glm::vec2 topLeft, glm::vec2 bottomRight) {
path.moveTo({topLeft.x, topLeft.y});
path.pathTo({bottomRight.x, topLeft.y});
path.pathTo({bottomRight.x, bottomRight.y});
path.pathTo({topLeft.x, bottomRight.y});
path.pathTo({topLeft.x, topLeft.y});
}
void draw_progress( gfx::Path2D& path, glm::vec2 topLeft, glm::vec2 size, float value ) {
const auto bottomRight { topLeft + size };
void draw_progress(gfx::Path2D &path, glm::vec2 topLeft, glm::vec2 size, float value) {
const auto bottomRight{topLeft + size};
// background
path.colour( {0.5f, 0.5f, 0.5f} );
draw_box( path, topLeft, bottomRight );
path.colour({0.5F, 0.5F, 0.5F});
draw_box(path, topLeft, bottomRight);
// fill
math::vec2 innerTop { topLeft.x + 5, topLeft.y + 5 };
math::vec2 innerBottom { bottomRight.x - 5, bottomRight.y - 5 };
math::vec2 innerTop{topLeft.x + 5, topLeft.y + 5};
math::vec2 innerBottom{bottomRight.x - 5, bottomRight.y - 5};
// figure out how wide the bar should be
float barWidth = (innerBottom.x - innerTop.x) * value;
......@@ -51,55 +65,55 @@ namespace fggl::gui {
innerBottom.x = innerTop.x + barWidth;
// draw the bar
path.colour( {0.8f, 0.0f, 0.0f} );
draw_box( path, innerTop, innerBottom );
path.colour({0.8F, 0.0F, 0.0F});
draw_box(path, innerTop, innerBottom);
// part of the bar that's not filled in
math::vec2 emptyTop { innerBottom.x, innerTop.y };
math::vec2 emptyBottom { trueBottom, innerBottom.y };
path.colour( {0.4f, 0.0f, 0.0f} );
draw_box( path, emptyTop, emptyBottom );
math::vec2 emptyTop{innerBottom.x, innerTop.y};
math::vec2 emptyBottom{trueBottom, innerBottom.y};
path.colour({0.4F, 0.0F, 0.0F});
draw_box(path, emptyTop, emptyBottom);
}
void draw_slider( gfx::Path2D& path, glm::vec2 topLeft, glm::vec2 size, float value ) {
draw_progress( path, topLeft, size, value );
void draw_slider(gfx::Path2D &path, glm::vec2 topLeft, glm::vec2 size, float value) {
draw_progress(path, topLeft, size, value);
// dimensions
const auto bottomRight { topLeft + size };
const math::vec2 innerTop { topLeft.x + 5, topLeft.y + 5 };
const math::vec2 innerBottom { bottomRight.x - 5, bottomRight.y - 5 };
const auto bottomRight{topLeft + size};
const math::vec2 innerTop{topLeft.x + 5, topLeft.y + 5};
const math::vec2 innerBottom{bottomRight.x - 5, bottomRight.y - 5};
// selector bar
float trackWidth = innerBottom.x - innerTop.x;
float selectorValue = trackWidth * value;
float selectorWidth = 6;
math::vec2 selectorTop { innerTop.x + selectorValue - ( selectorWidth/2), topLeft.y };
math::vec2 selectorBottom { selectorTop.x + selectorWidth, bottomRight.y };
path.colour( {1.0f, 1.0f, 1.0f} );
draw_box( path, selectorTop, selectorBottom );
math::vec2 selectorTop{innerTop.x + selectorValue - (selectorWidth / 2), topLeft.y};
math::vec2 selectorBottom{selectorTop.x + selectorWidth, bottomRight.y};
path.colour({1.0F, 1.0F, 1.0F});
draw_box(path, selectorTop, selectorBottom);
}
void draw_button( gfx::Path2D& path, glm::vec2 pos, glm::vec2 size, bool active, bool pressed) {
void draw_button(gfx::Path2D &path, glm::vec2 pos, glm::vec2 size, bool active, bool pressed) {
// locations
math::vec2 outerTop { pos };
math::vec2 outerBottom { pos + size };
math::vec2 innerTop { pos.x + 5, pos.y + 5 };
math::vec2 innerBottom { pos.x + size.x - 5, pos.y + size.y - 5 };
math::vec2 outerTop{pos};
math::vec2 outerBottom{pos + size};
math::vec2 innerTop{pos.x + 5, pos.y + 5};
math::vec2 innerBottom{pos.x + size.x - 5, pos.y + size.y - 5};
math::vec3 baseColour{ 0.5f, 0.5f, 0.5f };
math::vec3 baseColour{0.5F, 0.5F, 0.5F};
if ( active ) {
baseColour *= 1.2f;
if (active) {
baseColour *= 1.2F;
}
if ( pressed ) {
baseColour *= 0.8f;
if (pressed) {
baseColour *= 0.8F;
}
math::vec3 lightColour{ baseColour * 1.2f };
math::vec3 darkColour{ baseColour * 0.8f };
if ( pressed ) {
math::vec3 lightColour{baseColour * 1.2F};
math::vec3 darkColour{baseColour * 0.8F};
if (pressed) {
// flip light and dark for selected buttons
auto tmp = darkColour;
darkColour = lightColour;
......@@ -107,39 +121,39 @@ namespace fggl::gui {
}
// bottom side
path.colour( lightColour );
path.moveTo( outerTop );
path.pathTo( innerTop );
path.pathTo( { innerBottom.x, innerTop.y } );
path.pathTo( { outerBottom.x, outerTop.y } );
path.pathTo( outerTop );
path.colour(lightColour);
path.moveTo(outerTop);
path.pathTo(innerTop);
path.pathTo({innerBottom.x, innerTop.y});
path.pathTo({outerBottom.x, outerTop.y});
path.pathTo(outerTop);
// left side
path.colour( lightColour );
path.moveTo( outerTop );
path.pathTo( innerTop );
path.pathTo( { innerTop.x, innerBottom.y } );
path.pathTo( { outerTop.x, outerBottom.y } );
path.pathTo( outerTop );
path.colour(lightColour);
path.moveTo(outerTop);
path.pathTo(innerTop);
path.pathTo({innerTop.x, innerBottom.y});
path.pathTo({outerTop.x, outerBottom.y});
path.pathTo(outerTop);
// top side
path.colour( darkColour );
path.moveTo( { outerTop.x, outerBottom.y} );
path.pathTo( { innerTop.x, innerBottom.y} );
path.pathTo( innerBottom );
path.pathTo( outerBottom );
path.pathTo( { outerTop.x, outerBottom.y} );
path.colour(darkColour);
path.moveTo({outerTop.x, outerBottom.y});
path.pathTo({innerTop.x, innerBottom.y});
path.pathTo(innerBottom);
path.pathTo(outerBottom);
path.pathTo({outerTop.x, outerBottom.y});
// right side
path.colour( darkColour );
path.moveTo( outerBottom );
path.pathTo( innerBottom );
path.pathTo( { innerBottom.x, innerTop.y } );
path.pathTo( { outerBottom.x, outerTop.y } );
path.pathTo( outerBottom );
path.colour(darkColour);
path.moveTo(outerBottom);
path.pathTo(innerBottom);
path.pathTo({innerBottom.x, innerTop.y});
path.pathTo({outerBottom.x, outerTop.y});
path.pathTo(outerBottom);
// inner box
path.colour( baseColour );
draw_box( path, innerTop, innerBottom );
path.colour(baseColour);
draw_box(path, innerTop, innerBottom);
}
}
\ No newline at end of file
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 17/04/22.
//
......@@ -9,10 +23,10 @@
namespace fggl::gui {
Button::Button( math::vec2 pos, math::vec2 size) : Widget(pos, size), m_label(pos, size), m_hover(false), m_active(false) {}
Button::Button(math::vec2 pos, math::vec2 size) : Widget(pos, size), m_label(pos, size), m_active(false) {}
void Button::render(gfx::Paint &paint) {
gfx::Path2D path{ topLeft() };
gfx::Path2D path{topLeft()};
draw_button(path, topLeft(), size(), m_hover, m_active);
paint.fill(path);
......@@ -21,41 +35,37 @@ namespace fggl::gui {
void Button::activate() {
m_active = !m_active;
if ( m_active ) {
for( auto& callback : m_callbacks ) {
if (m_active) {
for (auto &callback : m_callbacks) {
callback();
m_active = false;
}
m_active = false;
}
}
void Button::onEnter() {
m_hover = true;
}
void Button::onExit() {
m_hover = false;
}
void Button::addCallback(Callback cb) {
m_callbacks.push_back( cb );
m_callbacks.push_back(cb);
}
void Button::label(const std::string &value) {
m_label.text(value);
}
std::string Button::label() const {
void Button::layout() {
m_label.size(topLeft(), size());
}
auto Button::label() const -> std::string {
return m_label.text();
}
void Label::layout() {
if ( m_font == nullptr ) {
if (m_font == nullptr) {
return;
}
math::vec2 size;
for (const auto& letter : m_value) {
for (const auto &letter : m_value) {
auto metrics = m_font->metrics(letter);
size.x += (metrics.advance << 6);
size.y = std::max(size.y, metrics.size.y);
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 20/11/2021.
//
#include <fggl/ecs3/ecs.hpp>
#include <fggl/input/input.hpp>
#include <fggl/gfx/camera.hpp>
#include <fggl/input/camera_input.h>
#include <fggl/input/camera_input.hpp>
namespace fggl::input {
void process_arcball(fggl::ecs3::World &ecs, const Input &input, fggl::ecs::entity_t cam) {
// see https://asliceofrendering.com/camera/2019/11/30/ArcballCamera/
auto *camTransform = ecs.get<fggl::math::Transform>(cam);
auto *camComp = ecs.get<fggl::gfx::Camera>(cam);
auto &mouse = input.mouse;
glm::vec4 position(camTransform->origin(), 1.0f);
glm::vec4 pivot(camComp->target, 1.0f);
glm::mat4 view = glm::lookAt(camTransform->origin(), camComp->target, camTransform->up());
glm::vec3 viewDir = -glm::transpose(view)[2];
glm::vec3 rightDir = glm::transpose(view)[0];
float deltaAngleX = (2 * M_PI);
float deltaAngleY = (M_PI);
float xAngle = (-mouse.axisDelta(fggl::input::MouseAxis::X)) * deltaAngleX;
float yAngle = (-mouse.axisDelta(fggl::input::MouseAxis::Y)) * deltaAngleY;
// rotate the camera around the pivot on the first axis
glm::mat4x4 rotationMatrixX(1.0f);
rotationMatrixX = glm::rotate(rotationMatrixX, xAngle, fggl::math::UP);
position = (rotationMatrixX * (position - pivot)) + pivot;
// rotate the camera aroud the pivot on the second axis
glm::mat4x4 rotationMatrixY(1.0f);
rotationMatrixY = glm::rotate(rotationMatrixY, yAngle, rightDir);
glm::vec3 finalPos = (rotationMatrixY * (position - pivot)) + pivot;
camTransform->origin(finalPos);
}
void process_freecam(fggl::ecs3::World &ecs, const Input &input, fggl::ecs::entity_t cam) {
float rotationValue = 0.0f;
glm::vec3 translation(0.0f);
auto &keyboard = input.keyboard;
auto* settings = ecs.get<FreeCamKeys>(cam);
// calculate rotation (user input)
if (keyboard.down(settings->rotate_cw)) {
rotationValue = ROT_SPEED;
} else if (keyboard.down(settings->rotate_ccw)) {
rotationValue = -ROT_SPEED;
}
// calculate movement (user input)
if (keyboard.down(settings->forward)) {
translation -= fggl::math::RIGHT;
}
if (keyboard.down(settings->backward)) {
translation += fggl::math::RIGHT;
}
if (keyboard.down(settings->right)) {
translation += fggl::math::FORWARD;
}
if (keyboard.down(settings->left)) {
translation -= fggl::math::FORWARD;
}
// apply rotation/movement
auto camTransform = ecs.get<fggl::math::Transform>(cam);
auto camComp = ecs.get<fggl::gfx::Camera>(cam);
glm::vec4 position(camTransform->origin(), 1.0f);
glm::vec4 pivot(camComp->target, 1.0f);
// apply movement
if (translation != glm::vec3(0.0f)) {
const auto rotation = (position - pivot);
const float angle = atan2f(rotation.x, rotation.z);
const auto rotationMat = glm::rotate(MAT_IDENTITY, angle, fggl::math::UP);
auto deltaMove = (rotationMat * glm::vec4(translation, 1.0f)) * PAN_SPEED;
deltaMove.w = 0.0f;
position += deltaMove;
pivot += deltaMove;
}
// apply rotation
if (rotationValue != 0.0f) {
glm::mat4 rotation = glm::rotate(MAT_IDENTITY, rotationValue, fggl::math::UP);
position = (rotation * (position - pivot)) + pivot;
}
camTransform->origin(position);
camComp->target = pivot;
}
void process_edgescroll(fggl::ecs3::World &ecs, const Input &input, fggl::ecs::entity_t cam) {
glm::vec3 translation(0.0f);
auto &mouse = input.mouse;
// calculate movement (user input)
if (mouse.axis(MouseAxis::Y) < 0.9f) {
translation -= fggl::math::RIGHT;
}
if (mouse.axis(MouseAxis::Y) > -0.9f) {
translation += fggl::math::RIGHT;
}
if (mouse.axis(MouseAxis::X) > -0.9f) {
translation += fggl::math::FORWARD;
}
if (mouse.axis(MouseAxis::X) < 0.9f) {
translation -= fggl::math::FORWARD;
}
// apply rotation/movement
auto camTransform = ecs.get<fggl::math::Transform>(cam);
auto camComp = ecs.get<fggl::gfx::Camera>(cam);
glm::vec4 position(camTransform->origin(), 1.0f);
glm::vec4 pivot(camComp->target, 1.0f);
// apply movement
if (translation != glm::vec3(0.0f)) {
const auto rotation = (position - pivot);
const float angle = atan2f(rotation.x, rotation.z);
const auto rotationMat = glm::rotate(MAT_IDENTITY, angle, fggl::math::UP);
auto deltaMove = (rotationMat * glm::vec4(translation, 1.0f)) * PAN_SPEED;
deltaMove.w = 0.0f;
position += deltaMove;
pivot += deltaMove;
}
// move camera
camTransform->origin(position);
camComp->target = pivot;
}
void process_arcball(entity::EntityManager &ecs, const Input &input, entity::EntityID cam) {
// see https://asliceofrendering.com/camera/2019/11/30/ArcballCamera/
auto &camTransform = ecs.get<fggl::math::Transform>(cam);
auto &camComp = ecs.get<fggl::gfx::Camera>(cam);
const auto &mouse = input.mouse;
glm::vec4 position(camTransform.origin(), 1.0F);
glm::vec4 pivot(camComp.target, 1.0F);
glm::mat4 view = glm::lookAt(camTransform.origin(), camComp.target, camTransform.up());
glm::vec3 viewDir = -glm::transpose(view)[2];
glm::vec3 rightDir = glm::transpose(view)[0];
float deltaAngleX = (2 * M_PI);
float deltaAngleY = (M_PI);
float xAngle = (-mouse.axisDelta(fggl::input::MouseAxis::X)) * deltaAngleX;
float yAngle = (-mouse.axisDelta(fggl::input::MouseAxis::Y)) * deltaAngleY;
// rotate the camera around the pivot on the first axis
glm::mat4x4 rotationMatrixX(1.0F);
rotationMatrixX = glm::rotate(rotationMatrixX, xAngle, fggl::math::UP);
position = (rotationMatrixX * (position - pivot)) + pivot;
// rotate the camera around the pivot on the second axis
glm::mat4x4 rotationMatrixY(1.0F);
rotationMatrixY = glm::rotate(rotationMatrixY, yAngle, rightDir);
glm::vec3 finalPos = (rotationMatrixY * (position - pivot)) + pivot;
camTransform.origin(finalPos);
}
void process_scroll(entity::EntityManager &ecs,
const Input &input,
entity::EntityID cam,
float minZoom,
float maxZoom) {
auto &camTransform = ecs.get<fggl::math::Transform>(cam);
auto &camComp = ecs.get<fggl::gfx::Camera>(cam);
const glm::vec3 dir = (camTransform.origin() - camComp.target);
const glm::vec3 forward = glm::normalize(dir);
glm::vec3 motion(0.0F);
float delta = input.mouse.axis(fggl::input::MouseAxis::SCROLL_Y);
if ((glm::length(dir) < maxZoom && delta < 0.0F) || (glm::length(dir) > minZoom && delta > 0.0F)) {
motion -= (forward * delta);
camTransform.origin(camTransform.origin() + motion);
}
}
void process_freecam(entity::EntityManager &ecs, const Input &input, entity::EntityID cam) {
float rotationValue = 0.0F;
glm::vec3 translation(0.0F);
const auto &keyboard = input.keyboard;
auto &settings = ecs.get<FreeCamKeys>(cam);
// calculate rotation (user input)
if (keyboard.down(settings.rotate_cw)) {
rotationValue = ROT_SPEED;
} else if (keyboard.down(settings.rotate_ccw)) {
rotationValue = -ROT_SPEED;
}
// calculate movement (user input)
if (keyboard.down(settings.forward)) {
translation -= fggl::math::RIGHT;
}
if (keyboard.down(settings.backward)) {
translation += fggl::math::RIGHT;
}
if (keyboard.down(settings.right)) {
translation += fggl::math::FORWARD;
}
if (keyboard.down(settings.left)) {
translation -= fggl::math::FORWARD;
}
// apply rotation/movement
auto camTransform = ecs.get<fggl::math::Transform>(cam);
auto camComp = ecs.get<fggl::gfx::Camera>(cam);
glm::vec4 position(camTransform.origin(), 1.0F);
glm::vec4 pivot(camComp.target, 1.0F);
// apply movement
if (translation != glm::vec3(0.0F)) {
const auto rotation = (position - pivot);
const float angle = atan2f(rotation.x, rotation.z);
const auto rotationMat = glm::rotate(MAT_IDENTITY, angle, fggl::math::UP);
auto deltaMove = (rotationMat * glm::vec4(translation, 1.0F)) * PAN_SPEED;
deltaMove.w = 0.0F;
position += deltaMove;
pivot += deltaMove;
}
// apply rotation
if (rotationValue != 0.0F) {
glm::mat4 rotation = glm::rotate(MAT_IDENTITY, rotationValue, fggl::math::UP);
position = (rotation * (position - pivot)) + pivot;
}
camTransform.origin(position);
camComp.target = pivot;
}
void process_edgescroll(entity::EntityManager &ecs, const Input &input, entity::EntityID cam) {
glm::vec3 translation(0.0F);
const auto &mouse = input.mouse;
// calculate movement (user input)
if (mouse.axis(MouseAxis::Y) < 0.9F) {
translation -= fggl::math::RIGHT;
}
if (mouse.axis(MouseAxis::Y) > -0.9F) {
translation += fggl::math::RIGHT;
}
if (mouse.axis(MouseAxis::X) > -0.9F) {
translation += fggl::math::FORWARD;
}
if (mouse.axis(MouseAxis::X) < 0.9F) {
translation -= fggl::math::FORWARD;
}
// apply rotation/movement
auto &camTransform = ecs.get<fggl::math::Transform>(cam);
auto &camComp = ecs.get<fggl::gfx::Camera>(cam);
glm::vec4 position(camTransform.origin(), 1.0F);
glm::vec4 pivot(camComp.target, 1.0F);
// apply movement
if (translation != glm::vec3(0.0F)) {
const auto rotation = (position - pivot);
const float angle = atan2f(rotation.x, rotation.z);
const auto rotationMat = glm::rotate(MAT_IDENTITY, angle, fggl::math::UP);
auto deltaMove = (rotationMat * glm::vec4(translation, 1.0F)) * PAN_SPEED;
deltaMove.w = 0.0F;
position += deltaMove;
pivot += deltaMove;
}
// move camera
camTransform.origin(position);
camComp.target = pivot;
}
}
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include <fggl/input/input.hpp>
namespace fggl::input {
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include <fggl/input/mouse.hpp>
namespace fggl::input {
void MouseState::operator=(const MouseState& rhs) {
for ( int i=0; i<4; i++ ) {
axis[i] = rhs.axis[i];
}
buttons = rhs.buttons;
}
void MouseState::operator=(const MouseState &rhs) {
for (int i = 0; i < 4; i++) {
axis[i] = rhs.axis[i];
}
buttons = rhs.buttons;
}
} // namespace fggl::input
# math
find_package(glm CONFIG REQUIRED)
target_link_libraries(fggl PUBLIC glm::glm)
target_sources(fggl
PRIVATE
shapes.cpp
triangulation.cpp
)
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include "fggl/math/shapes.hpp"
......@@ -25,21 +19,17 @@ namespace fggl::math::phs3d {
void AABB::emtpy() {
min = {FLT_MAX, FLT_MAX, FLT_MAX};
max = { - FLT_MAX, - FLT_MAX, -FLT_MAX};
max = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
}
void AABB::add(const math::vec3 &p) {
if ( p.x < min.x ) min.x = p.x;
if ( p.x > max.x ) max.x = p.x;
if ( p.y < min.y ) min.y = p.y;
if ( p.y > min.y ) max.y = p.y;
if ( p.z < min.z ) min.z = p.z;
if ( p.z > max.z ) max.z = p.z;
void AABB::add(const math::vec3 &point) {
min = minElm(min, point);
max = maxElm(max, point);
}
AABB AABB::fromPoints(const std::vector<math::vec3> &points) {
auto AABB::fromPoints(const std::vector<math::vec3> &points) -> AABB {
AABB box;
for (const auto& point : points) {
for (const auto &point : points) {
box.add(point);
}
return box;
......@@ -50,9 +40,9 @@ namespace fggl::math::phs3d {
min = max = m[3]; // should be the translation component of the matrix
// this feels like something that should be vectorizable...
for ( int i = 0; i < 3; i++) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if ( m[i][j] > 0.0f ) {
if (m[i][j] > 0.0F) {
min[j] += m[i][j] * other.min[j];
max[j] += m[i][j] * other.max[j];
} else {
......@@ -63,7 +53,7 @@ namespace fggl::math::phs3d {
}
}
Plane Plane::fromPoints(const math::vec3 p1, const math::vec3 p2, const math::vec3 p3) {
auto Plane::fromPoints(const math::vec3 p1, const math::vec3 p2, const math::vec3 p3) -> Plane {
const auto e3 = p2 - p1;
const auto e1 = p3 - p2;
auto normal = glm::normalize(glm::cross(e3, e1));
......@@ -71,13 +61,13 @@ namespace fggl::math::phs3d {
return {normal, d};
}
static math::vec3 bestFitNormal(const std::vector<math::vec3>& points) {
static auto bestFitNormal(const std::vector<math::vec3> &points) -> math::vec3 {
assert(!points.empty());
math::vec3 result;
math::vec3 p = points.back();
for ( std::size_t i = 0; i < points.size(); ++i ){
for (std::size_t i = 0; i < points.size(); ++i) {
math::vec3 c = points[i];
result.x += (p.z + c.z) * (p.y - c.y);
......@@ -90,13 +80,13 @@ namespace fggl::math::phs3d {
return glm::normalize(result);
};
static float bestFitD(const std::vector<math::vec3>& points, glm::vec3 normal) {
static auto bestFitD(const std::vector<math::vec3> &points, glm::vec3 normal) -> float {
math::vec3 sum;
for (auto& point : points) {
for (const auto &point : points) {
sum += point;
}
sum *= 1.0F/points.size();
return glm::dot( sum, normal );
sum *= 1.0F / points.size();
return glm::dot(sum, normal);
}
const char X = 0;
......@@ -108,11 +98,11 @@ namespace fggl::math::phs3d {
const char b;
};
static bary_axis baryCalcAxis(const math::vec3& normal) {
if ( (fabs(normal.x) >= fabs(normal.y)) && (fabs(normal.x) >= fabs(normal.z))) {
static auto baryCalcAxis(const math::vec3 &normal) -> bary_axis {
if ((fabs(normal.x) >= fabs(normal.y)) && (fabs(normal.x) >= fabs(normal.z))) {
// discard x
return {Y, Z};
} else if ( fabs(normal.y) >= fabs(normal.z) ) {
} else if (fabs(normal.y) >= fabs(normal.z)) {
// discard y
return {Z, X};
} else {
......@@ -121,7 +111,7 @@ namespace fggl::math::phs3d {
}
}
bool Triangle::CartToBarycentric(const math::vec3& cart, Barycentric& outVal) {
auto Triangle::CartToBarycentric(const math::vec3 &cart, Barycentric &outVal) -> bool {
// everything is const because I'm paying the compiler is smarter than me...
const auto d1 = v[1] - v[0];
......@@ -143,20 +133,20 @@ namespace fggl::math::phs3d {
const float v3 = cart[ax.b] - v[0][ax.b];
const float v4 = cart[ax.b] - v[2][ax.b];
const float denom = v1*u2 - v2*u1;
if ( denom == 0.0f) {
const float denom = v1 * u2 - v2 * u1;
if (denom == 0.0F) {
return false;
}
// finally, we can work it out
const float oneOverDenom = 1.0f / denom;
outVal.b[0] = (v4*u2 - v2*u4) * oneOverDenom;
outVal.b[1] = (v1*u3 - v3*u1) * oneOverDenom;
outVal.b[2] = 1.0f - outVal.b[0] - outVal.b[1];
const float oneOverDenom = 1.0F / denom;
outVal.b[0] = (v4 * u2 - v2 * u4) * oneOverDenom;
outVal.b[1] = (v1 * u3 - v3 * u1) * oneOverDenom;
outVal.b[2] = 1.0F - outVal.b[0] - outVal.b[1];
return true;
}
bool Triangle::CartToBarycentric2(const math::vec3& cart, Barycentric& outVal) {
auto Triangle::CartToBarycentric2(const math::vec3 &cart, Barycentric &outVal) -> bool {
const auto e1 = v[2] - v[1];
const auto e2 = v[0] - v[2];
const auto e3 = v[1] - v[0];
......@@ -167,7 +157,7 @@ namespace fggl::math::phs3d {
const auto normal = glm::normalize(glm::cross(e1, e2));
const auto denom = glm::dot(glm::cross(e1, e2), normal);
assert( denom != 0.0f);
assert(denom != 0.0F);
outVal.b[0] = glm::dot(glm::cross(e1, d3), normal) / denom;
outVal.b[1] = glm::dot(glm::cross(e2, d1), normal) / denom;
......
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include "fggl/math/triangulation.hpp"
#include <iostream>
namespace fggl::math {
/**
* Fast Triangulation for convex polygons.
*/
void fan_triangulation(const PolygonVertex& polygon, data::Mesh2D &mesh) {
void fan_triangulation(const PolygonVertex &polygon, data::Mesh2D &mesh) {
assert(polygon.size() >= 3);
// add the first two points to the mesh
......@@ -35,7 +28,7 @@ namespace fggl::math {
// deal with the indices
const auto nTris = polygon.size() - 2;
for (auto i = 0u; i < nTris; i++) {
for (auto i = 0U; i < nTris; i++) {
mesh.add_index(firstIdx);
mesh.add_index(prevIdx);
......
target_sources(fggl
PRIVATE
null.cpp
)
\ No newline at end of file
# bullet integration support
find_package( Bullet REQUIRED )
target_link_libraries(fggl PUBLIC Bullet::Bullet )
#
target_sources(fggl
PRIVATE
simulation.cpp
phys_draw.cpp
)
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "fggl/phys/bullet/types.hpp"
namespace fggl::phys::bullet {
BulletPhysicsEngine::BulletPhysicsEngine(ecs3::World* world) : m_ecs(world), m_config(), m_world(nullptr) {
m_config.broadphase = new btDbvtBroadphase();
m_config.collisionConfiguration = new btDefaultCollisionConfiguration();
m_config.dispatcher = new btCollisionDispatcher(m_config.collisionConfiguration);
m_config.solver = new btSequentialImpulseConstraintSolver();
m_world = new btDiscreteDynamicsWorld(
m_config.dispatcher,
m_config.broadphase,
m_config.solver,
m_config.collisionConfiguration);
m_debug = std::make_unique<debug::BulletDebugDrawList>();
m_world->setDebugDrawer( m_debug.get() );
m_debug->setDebugMode(1);
m_ecs->addDeathListener( [this](auto entity) { this->onEntityDeath(entity);} );
}
void BulletPhysicsEngine::step() {
checkForPhys();
auto dynamicEnts = m_ecs->findMatching<phys::Dynamics>();
for (auto& ent : dynamicEnts) {
auto* dynamicComp = m_ecs->get<phys::Dynamics>(ent);
auto* bulletProxy = m_ecs->get<phys::bullet::BulletBody>(ent);
if ( bulletProxy == nullptr ) {
std::cerr << "entity has dynamics but isn't a physics object!" << std::endl;
continue;
}
const auto forceVec = dynamicComp->force;
if ( glm::length(forceVec) != 0.F ) {
bulletProxy->body->applyCentralForce({forceVec.x, forceVec.y, forceVec.z});
bulletProxy->body->activate();
dynamicComp->force = math::VEC3_ZERO;
}
}
m_world->stepSimulation(60.0f);
syncToECS();
dealWithCollisions();
m_world->debugDrawWorld();
}
static void build_motation_state(math::Transform* myState, BulletBody* btState) {
auto myPos = myState->origin();
btVector3 position{myPos.x, myPos.y, myPos.z};
auto myRot = myState->euler();
btQuaternion rotation;
rotation.setEulerZYX(myRot.x, myRot.y, myRot.z);
btTransform transform;
transform.setIdentity();
transform.setRotation(rotation);
transform.setOrigin(position);
btState->motion = new btDefaultMotionState(transform);
}
inline btCollisionShape* shapeToBullet(const phys::RigidBody* fgglBody) {
if ( fgglBody->shape == nullptr ) {
// they forgot to put a shape, we'll assume a unit sphere to avoid crashes...
return new btSphereShape(0.5f);
}
switch (fgglBody->shape->type) {
case phys::ShapeType::BOX: {
auto extents = ((phys::Box*)fgglBody->shape)->extents;
return new btBoxShape({extents.x, extents.y, extents.z});
}
case phys::ShapeType::SPHERE: {
float radius = ((phys::Sphere*)fgglBody->shape)->radius;
return new btSphereShape(radius);
}
default:
return new btSphereShape(0.5f);
}
}
void BulletPhysicsEngine::checkForPhys() {
// FIXME without reactive-based approaches this is very slow
auto entsWantPhys = m_ecs->findMatching<phys::RigidBody>();
for (auto ent : entsWantPhys) {
auto* btComp = m_ecs->get<BulletBody>(ent);
if ( btComp != nullptr ) {
// they are already in the simluation
continue;
}
btComp = m_ecs->add<BulletBody>(ent);
const auto* fgBody = m_ecs->get<phys::RigidBody>(ent);
// setup the starting motion state
auto* transform = m_ecs->get<math::Transform>(ent);
build_motation_state(transform, btComp);
btCollisionShape* colShape = shapeToBullet(fgBody);
btVector3 localInt(0, 0, 0);
if ( !fgBody->isStatic() ) {
colShape->calculateLocalInertia(fgBody->mass, localInt);
}
// setup the construction information
btRigidBody::btRigidBodyConstructionInfo bodyCI(
fgBody->mass,
btComp->motion,
colShape,
localInt
);
auto* body = new btRigidBody(bodyCI);
body->setUserIndex(static_cast<int>(ent));
btComp->body = body;
if (!fgBody->isStatic()) {
body->setRestitution(0.5f);
body->setRollingFriction(0.0f);
}
if ( fgBody->type == BodyType::KINEMATIC ) {
body->setCollisionFlags( body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT );
}
m_world->addRigidBody(btComp->body );
}
}
void BulletPhysicsEngine::syncToECS() {
auto physEnts = m_ecs->findMatching<BulletBody>();
for (auto& ent : physEnts) {
auto* transform = m_ecs->get<math::Transform>(ent);
auto* physBody = m_ecs->get<BulletBody>(ent);
if ( physBody->body->isKinematicObject() ) {
continue;
}
btTransform bTransform;
physBody->motion->getWorldTransform(bTransform);
// set calculate and set ecs position
auto& btPos = bTransform.getOrigin();
math::vec3 pos{btPos.x(), btPos.y(), btPos.z()};
transform->origin( pos );
// set the rotation
const auto& btRot = bTransform.getRotation();
math::vec3 rot;
btRot.getEulerZYX(rot.x, rot.y, rot.z);
transform->euler(rot);
}
}
BulletPhysicsEngine::~BulletPhysicsEngine() {
// clean up the rigid bodies
auto entRB = m_ecs->findMatching<BulletBody>();
for (auto& ent : entRB) {
auto* bulletBody = m_ecs->get<BulletBody>(ent);
delete bulletBody->motion;
m_world->removeCollisionObject(bulletBody->body);
delete bulletBody->body;
m_ecs->remove<BulletBody>(ent);
}
// delete the world
delete m_world;
// cleanup the configuration object
delete m_config.solver;
delete m_config.broadphase;
delete m_config.dispatcher;
delete m_config.collisionConfiguration;
}
static void handleCollisionCallbacks(ecs3::World* world, ecs3::entity_t owner, ecs3::entity_t other) {
auto* callbacks = world->tryGet<CollisionCallbacks>(owner);
if ( callbacks == nullptr ) {
return;
}
auto* cache = world->tryGet<CollisionCache>(owner);
if ( cache != nullptr ) {
auto itr = cache->collisions.find(other);
if ( itr == cache->collisions.end() ) {
if ( callbacks->onEnter != nullptr ) {
callbacks->onEnter(owner, other);
}
cache->collisions.insert( other );
} else {
if ( callbacks->onStay != nullptr ) {
callbacks->onStay(owner, other);
}
}
}
}
void BulletPhysicsEngine::dealWithCollisions() {
// flush collision caches
auto caches = m_ecs->findMatching<CollisionCache>();
for( auto& ent : caches) {
auto* cache = m_ecs->get<CollisionCache>(ent);
std::swap(cache->collisions, cache->lastFrame);
cache->collisions.clear();
}
// deal with the number of manifolds
const auto numManifolds = m_config.dispatcher->getNumManifolds();
for ( int i=0; i< numManifolds; ++i) {
auto* contactManifold = m_config.dispatcher->getManifoldByIndexInternal(i);
const auto* body0 = contactManifold->getBody0();
const auto* body1 = contactManifold->getBody1();
// FIXME rigid body didn't die according to plan!
if ( body0->getUserIndex() == ecs::NULL_ENTITY || body1->getUserIndex() == ecs::NULL_ENTITY) {
continue;
}
if ( !m_ecs->alive( body0->getUserIndex()) || !m_ecs->alive(body1->getUserIndex()) ) {
continue;
}
std::cerr << "contact: " << body0->getUserIndex() << " on " << body1->getUserIndex() << std::endl;
handleCollisionCallbacks(m_ecs, body0->getUserIndex(), body1->getUserIndex());
handleCollisionCallbacks(m_ecs, body1->getUserIndex(), body0->getUserIndex());
}
// note conacts that have ended
caches = m_ecs->findMatching<CollisionCache>(); // re-fetch, entities can (and probably do) die in handlers.
for( auto& ent : caches) {
auto* cache = m_ecs->get<CollisionCache>(ent);
// if we don't have an exit callback, it's pointless checking the cache
auto* callbacks = m_ecs->tryGet<CollisionCallbacks>(ent);
if ( callbacks == nullptr || callbacks->onExit == nullptr){
continue;
}
// check if a collision has ended
for (const auto& other : cache->lastFrame) {
auto itr = cache->collisions.find(other);
if (itr == cache->collisions.end()) {
callbacks->onExit( ent,other);
}
}
}
}
void BulletPhysicsEngine::onEntityDeath(ecs::entity_t entity) {
auto* btPhysics = m_ecs->tryGet<BulletBody>(entity);
if ( btPhysics != nullptr) {
btPhysics->body->setUserIndex( ecs::NULL_ENTITY );
m_world->removeRigidBody( btPhysics->body );
}
}
} // namespace fggl::phys::bullet
\ No newline at end of file
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 20/08/22.
//
#include "fggl/phys/null.hpp"
namespace fggl::phys {
auto NullPhysics::factory(modules::ServiceName serviceName, modules::Services &serviceManager) -> bool {
if (serviceName == phys::PhysicsProvider::service) {
serviceManager.bind<phys::PhysicsProvider, NullPhysicsProvider>();
return true;
}
return false;
}
}
\ No newline at end of file
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
add_subdirectory(linux)
else ()
add_subdirectory(fallback)
endif ()
\ No newline at end of file