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 1413 additions and 717 deletions
......@@ -13,10 +13,6 @@
*/
#include <fggl/gfx/input.hpp>
#include <cassert>
#include <bitset>
#include <iostream>
using fggl::gfx::Input;
......@@ -34,10 +30,10 @@ void Input::clear() {
m_mouse_last = m_mouse_curr;
// reset pad data
for ( auto& pad : m_pad_curr ) {
for (auto &pad : m_pad_curr) {
pad.present = false;
pad.buttons.reset();
for (float & axe : pad.axes){
for (float &axe : pad.axes) {
axe = 0.0F;
}
}
......@@ -57,15 +53,15 @@ void Input::mousePos(double x, double y) {
m_mouse_curr.cursor[1] = y;
}
double Input::cursorDeltaX() const {
auto Input::cursorDeltaX() const -> double {
return m_mouse_last.cursor[0] - m_mouse_curr.cursor[0];
}
double Input::cursorDeltaY() const {
auto Input::cursorDeltaY() const -> double {
return m_mouse_last.cursor[1] - m_mouse_curr.cursor[1];
}
const double* Input::mousePos() const {
auto Input::mousePos() const -> const double * {
return m_mouse_curr.scroll.data();
}
......@@ -74,78 +70,78 @@ void Input::mouseScroll(double deltaX, double deltaY) {
m_mouse_curr.scroll[1] = deltaY;
}
const double* Input::mouseScroll() const {
auto Input::mouseScroll() const -> const double * {
return m_mouse_curr.scroll.data();
}
double Input::scrollDeltaX() const {
auto Input::scrollDeltaX() const -> double {
return m_mouse_curr.scroll[0];
}
double Input::scrollDeltaY() const {
auto Input::scrollDeltaY() const -> double {
return m_mouse_curr.scroll[1];
}
void Input::mouseBtn(const MouseButton btn, bool state) {
if ( state ) {
if (state) {
m_mouse_curr.buttons |= btn;
} else {
m_mouse_curr.buttons &= ~btn;
}
}
bool Input::mouseDown(const MouseButton btn) const {
auto Input::mouseDown(const MouseButton btn) const -> bool {
return m_mouse_curr.buttons & btn;
}
bool Input::mousePressed(const MouseButton btn) const {
auto Input::mousePressed(const MouseButton btn) const -> bool {
return (m_mouse_curr.buttons & btn) && !(m_mouse_last.buttons & btn);
}
bool Input::mouseReleased(const MouseButton btn) const {
auto Input::mouseReleased(const MouseButton btn) const -> bool {
return !(m_mouse_curr.buttons & btn) && (m_mouse_last.buttons & btn);
}
void Input::joystickConnect(int id, Joystick &data){
void Input::joystickConnect(int id, Joystick &data) {
// joystick data is polled, so we need to call this every frame >.<
m_joysticks[id] = true;
m_joydata[id] = data;
}
void Input::joystickDisconnect( int id ){
void Input::joystickDisconnect(int id) {
// reset to empty joystick
m_joysticks[id] = false;
m_joydata[id] = Joystick();
}
bool Input::hasJoystick(int id) const {
auto Input::hasJoystick(int id) const -> bool {
return m_joysticks[id];
}
const fggl::gfx::Joystick& Input::joystick(int id) const {
auto Input::joystick(int id) const -> const fggl::gfx::Joystick & {
return m_joydata[id];
}
void Input::padState(int id, const PadState& state) {
void Input::padState(int id, const PadState &state) {
m_pad_curr[id] = state;
}
bool Input::padDown(int id, PadButton btn) {
return m_pad_curr[id].buttons[(int)btn];
auto Input::padDown(int id, PadButton btn) -> bool {
return m_pad_curr[id].buttons[(int) btn];
}
bool Input::padPressed(int id, PadButton btn) {
return m_pad_curr[id].buttons[(int)btn] && !m_pad_last[id].buttons[(int)btn];
auto Input::padPressed(int id, PadButton btn) -> bool {
return m_pad_curr[id].buttons[(int) btn] && !m_pad_last[id].buttons[(int) btn];
}
bool Input::padReleased(int id, PadButton btn) {
return !m_pad_curr[id].buttons[(int)btn] && m_pad_last[id].buttons[(int)btn];
auto Input::padReleased(int id, PadButton btn) -> bool {
return !m_pad_curr[id].buttons[(int) btn] && m_pad_last[id].buttons[(int) btn];
}
float Input::padAxis(int id, PadAxis axis) {
return m_pad_curr[id].axes[(int)axis];
auto Input::padAxis(int id, PadAxis axis) -> float {
return m_pad_curr[id].axes[(int) axis];
}
float Input::padAxisDelta(int id, PadAxis axis) {
return m_pad_last[id].axes[(int)axis] - m_pad_curr[id].axes[(int)axis];
auto Input::padAxisDelta(int id, PadAxis axis) -> float {
return m_pad_last[id].axes[(int) axis] - m_pad_curr[id].axes[(int) axis];
}
# Sources
target_sources(fggl
PRIVATE
glad.c
backend.cpp
shader.cpp
renderer.cpp
types.cpp
)
PRIVATE
backend.cpp
shader.cpp
renderer.cpp
types.cpp
)
# OpenGL Backend
find_package( OpenGL REQUIRED )
if ( MSVC )
target_link_libraries(${PROJECT_NAME} PUBLIC OpenGL::GL)
else()
target_link_libraries(${PROJECT_NAME} PRIVATE OpenGL::OpenGL)
endif()
find_package(OpenGL REQUIRED)
if (MSVC)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenGL::GL)
else ()
target_link_libraries(${PROJECT_NAME} PRIVATE OpenGL::OpenGL)
endif ()
# FreeType
......
......@@ -27,7 +27,6 @@
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <memory>
......@@ -64,24 +63,15 @@ constexpr auto fggl_ogl_source(GLenum source) -> const char * {
constexpr auto static fggl_ogl_type(GLenum type) -> const char * {
switch (type) {
case GL_DEBUG_TYPE_ERROR: return "Error";
break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: return "Deprecated Behaviour";
break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: return "Undefined Behaviour";
break;
case GL_DEBUG_TYPE_PERFORMANCE: return "Performance";
break;
case GL_DEBUG_TYPE_PORTABILITY: return "Portability";
break;
case GL_DEBUG_TYPE_MARKER: return "Marker";
break;
case GL_DEBUG_TYPE_PUSH_GROUP: return "Push Group";
break;
case GL_DEBUG_TYPE_POP_GROUP: return "Pop Group";
break;
default:
case GL_DEBUG_TYPE_OTHER: return "Other";
break;
}
assert(false);
return "unknown";
......@@ -90,25 +80,30 @@ constexpr auto static fggl_ogl_type(GLenum type) -> const char * {
PRAGMA_DIAGNOSTIC_PUSH
#pragma ide diagnostic ignored "bugprone-easily-swappable-parameters"
constexpr const char* OGL_LOG_FMT {"[GL] {}, {}: [{}]: {}"};
constexpr const char *OGL_LOG_FMT{"[GL] {}, {}: [{}]: {}"};
void static GLAPIENTRY fggl_ogl_to_spdlog(GLenum source, GLenum type, unsigned int msgId, GLenum severity, GLsizei /*length*/,
const char *message, const void * /*userParam*/) {
void static GLAPIENTRY fggl_ogl_log(GLenum source,
GLenum type,
unsigned int msgId,
GLenum severity,
GLsizei /*length*/,
const char *message,
const void * /*userParam*/) {
const auto *const sourceStr = fggl_ogl_source(source);
const auto *const typeStr = fggl_ogl_type(type);
// table 20.3, GL spec 4.5
switch (severity) {
case GL_DEBUG_SEVERITY_HIGH: fggl::debug::error(OGL_LOG_FMT, sourceStr, typeStr, msgId, message);
break;
default:
case GL_DEBUG_SEVERITY_MEDIUM: fggl::debug::warning(OGL_LOG_FMT, sourceStr, typeStr, msgId, message);
break;
case GL_DEBUG_SEVERITY_LOW: fggl::debug::info(OGL_LOG_FMT, sourceStr, typeStr, msgId, message);
break;
case GL_DEBUG_SEVERITY_NOTIFICATION: fggl::debug::debug(OGL_LOG_FMT, sourceStr, typeStr, msgId, message);
break;
case GL_DEBUG_SEVERITY_HIGH: fggl::debug::error(OGL_LOG_FMT, sourceStr, typeStr, msgId, message);
break;
default:
case GL_DEBUG_SEVERITY_MEDIUM: fggl::debug::warning(OGL_LOG_FMT, sourceStr, typeStr, msgId, message);
break;
case GL_DEBUG_SEVERITY_LOW: fggl::debug::info(OGL_LOG_FMT, sourceStr, typeStr, msgId, message);
break;
case GL_DEBUG_SEVERITY_NOTIFICATION: fggl::debug::debug(OGL_LOG_FMT, sourceStr, typeStr, msgId, message);
break;
}
}
......@@ -134,22 +129,83 @@ namespace fggl::gfx {
using data::Mesh2D;
using data::Vertex2D;
OpenGL4Backend::OpenGL4Backend(data::Storage* storage, gui::FontLibrary* fonts) : fggl::gfx::Graphics(), m_canvasPipeline(INVALID_SHADER_ID), m_storage(storage) {
// initialise GLEW, or fail
// FIXME this binds the graphics stack to GLFW :'(
int version = gladLoadGLLoader( (GLADloadproc)glfwGetProcAddress );
static void setup_shaders(ShaderCache* cache) {
// FIXME this should not be hard-coded, it should be part of the scene loading process
cache->load(ShaderConfig::named("phong"));
cache->load(ShaderConfig::named("redbook/lighting"));
cache->load(ShaderConfig::named("redbook/debug"));
// debug shaders
cache->load(ShaderConfig::named("normals", true));
cache->load(ShaderConfig::named("debug"));
}
static void splat_checkerboard(GLuint* memory, unsigned int width = 128, unsigned int height = 128) {
int counter = 0;
auto colour = ogl4::TEX_CHECKER;
for ( auto i = 0u; i < width * height; ++i) {
memory[i] = ogl4::TEX_CHECKER;
counter++;
if (counter == 5) {
counter = 0;
colour = colour == ogl4::TEX_CHECKER ? ogl4::TEX_WHITE : ogl4::TEX_CHECKER;
}
}
}
static auto make_solid(uint8_t width, uint8_t height, GLuint colour) -> ogl::Image {
ogl::PixelDataArray data(ogl::PixelFormat::UNSIGNED_INT, width * height);
for ( auto i = 0u; i < width * height; ++i) {
data.ui[i] = colour;
}
return ogl::Image(
ogl::ImageFormat::RGBA,
{width, height},
std::move(data)
);
}
static void setup_fallback_texture(assets::AssetManager* assets) {
{
// generate the image
ogl::Image image(ogl::ImageFormat::RGBA, ogl::PixelFormat::UNSIGNED_INT, {128,128});
splat_checkerboard( image.data.ui );
// setup the texture
auto *fallback2D = new ogl::Texture(ogl::TextureType::Tex2D);
fallback2D->setData(ogl::InternalImageFormat::RedGreenBlueAlpha, image, ogl::PixelFormat::UNSIGNED_BYTE);
assets->set(ogl4::FALLBACK_TEX, fallback2D);
}
{
ogl::Image image = make_solid(128, 128, ogl4::TEX_WHITE);
auto *solid2D = new ogl::Texture(ogl::TextureType::Tex2D);
solid2D->setData(ogl::InternalImageFormat::RedGreenBlueAlpha, image);
assets->set(ogl4::SOLID_TEX, solid2D);
}
}
OpenGL4Backend::OpenGL4Backend(data::Storage *storage, gui::FontLibrary *fonts, assets::AssetManager *assets, GlFunctionLoader loader)
: fggl::gfx::Graphics(), m_canvasPipeline(nullptr), m_storage(storage) {
// load OpenGL context, or fail.
int version = gladLoadGLLoader(loader);
if (version == 0) {
printf("Failed to initialize OpenGL context\n");
debug::error("Failed to initialize OpenGL context\n");
return;
}
// OpenGL debug Support
GLint flags = 0;
glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
if ( (flags & GL_CONTEXT_FLAG_DEBUG_BIT) == GL_CONTEXT_FLAG_DEBUG_BIT) { // NOLINT(hicpp-signed-bitwise)
if ((flags & GL_CONTEXT_FLAG_DEBUG_BIT) == GL_CONTEXT_FLAG_DEBUG_BIT) { // NOLINT(hicpp-signed-bitwise)
debug::info("enabling OpenGL debug output");
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(fggl_ogl_to_spdlog, nullptr);
glDebugMessageCallback(fggl_ogl_log, nullptr);
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
}
......@@ -159,26 +215,25 @@ namespace fggl::gfx {
// setup the shader cache
m_cache = std::make_unique<ShaderCache>(m_storage);
setup_shaders(m_cache.get());
// setup 2D rendering system
ShaderConfig shader2DConfig = ShaderFromName("canvas");
ShaderConfig shader2DConfig = ShaderConfig::named("canvas");
m_canvasPipeline = m_cache->load(shader2DConfig);
if ( m_canvasPipeline == INVALID_SHADER_ID) {
if (m_canvasPipeline == nullptr) {
debug::error("failed to load shader2D - using fallback");
m_canvasPipeline = m_cache->get(ogl4::FALLBACK_CANVAS_PIPELINE);
}
// FIXME this should not be hard-coded, it should be part of the scene loading process
m_cache->load(ShaderFromName("phong"));
m_cache->load(ShaderFromName("redbook/lighting"));
m_cache->load(ShaderFromName("redbook/debug"));
// fallback textures
setup_fallback_texture(assets);
// rendering helpers
m_canvasRenderer = std::make_unique<ogl4::CanvasRenderer>(fonts);
m_modelRenderer = std::make_unique<ogl4::StaticModelRenderer>(m_cache.get());
m_modelRenderer = std::make_unique<ogl4::StaticModelRenderer>(m_cache.get(), assets);
m_debugRenderer = std::make_unique<ogl4::DebugRenderer>(m_cache->getOrLoad(ShaderFromName("debug")));
if ( m_debugRenderer ) {
m_debugRenderer = std::make_unique<ogl4::DebugRenderer>(m_cache->getOrLoad(ShaderConfig::named("debug")));
if (m_debugRenderer) {
dd::initialize(m_debugRenderer.get());
}
};
......@@ -189,31 +244,32 @@ namespace fggl::gfx {
}
void OpenGL4Backend::draw2D(const gfx::Paint &paint) {
if ( !m_canvasRenderer ) {
if (!m_canvasRenderer) {
return;
}
m_canvasRenderer->render(m_canvasPipeline, paint);
m_canvasRenderer->render(*m_canvasPipeline, paint);
}
void OpenGL4Backend::drawScene(entity::EntityManager &world) {
if ( m_modelRenderer ) {
m_modelRenderer->render(world);
void OpenGL4Backend::drawScene(entity::EntityManager &world, bool debugMode) {
if (m_modelRenderer) {
m_modelRenderer->render(world, debugMode);
}
if ( m_debugRenderer ) {
if (m_debugRenderer) {
auto cameras = world.find<gfx::Camera>();
if ( cameras.empty() ) {
if (cameras.empty()) {
return;
}
auto cameraEnt = cameras.front();
const auto& camTransform = world.get<math::Transform>(cameraEnt);
const auto& camComp = world.get<gfx::Camera>(cameraEnt);
const auto &camTransform = world.get<math::Transform>(cameraEnt);
const auto &camComp = world.get<gfx::Camera>(cameraEnt);
const math::mat4 projectionMatrix = glm::perspective(camComp.fov, camComp.aspectRatio, camComp.nearPlane, camComp.farPlane);
const math::mat4 viewMatrix = glm::lookAt( camTransform.origin(), camComp.target, camTransform.up() );
const math::mat4 projectionMatrix =
glm::perspective(camComp.fov, camComp.aspectRatio, camComp.nearPlane, camComp.farPlane);
const math::mat4 viewMatrix = glm::lookAt(camTransform.origin(), camComp.target, camTransform.up());
m_debugRenderer->mvpMatrix = projectionMatrix * viewMatrix;
dd::flush();
......
......@@ -17,362 +17,367 @@
#include "fggl/gfx/ogl4/fallback.hpp"
#include <iostream>
#include <fstream>
#include <vector>
#include <spdlog/spdlog.h>
using namespace fggl::gfx;
namespace fggl::gfx {
bool ShaderCache::compileShaderFromSource(const std::string& source, GLuint sid) {
// upload and compile shader
const char* src = source.c_str();
glShaderSource(sid, 1, &src, nullptr);
glCompileShader(sid);
auto ShaderCache::compileShaderFromSource(const std::string &source, GLuint sid) -> bool {
// upload and compile shader
const char *src = source.c_str();
glShaderSource(sid, 1, &src, nullptr);
glCompileShader(sid);
// check it worked
GLint compiled = GL_FALSE;
glGetShaderiv(sid, GL_COMPILE_STATUS, &compiled);
if ( compiled == GL_FALSE ) {
// check it worked
GLint compiled = GL_FALSE;
glGetShaderiv(sid, GL_COMPILE_STATUS, &compiled);
if (compiled == GL_FALSE) {
GLint maxLength = 0;
glGetShaderiv(sid, GL_INFO_LOG_LENGTH, &maxLength);
char* infoLog = new char[maxLength];
glGetShaderInfoLog(sid, maxLength, &maxLength, infoLog);
GLint maxLength = 0;
glGetShaderiv(sid, GL_INFO_LOG_LENGTH, &maxLength);
char *infoLog = new char[maxLength];
glGetShaderInfoLog(sid, maxLength, &maxLength, infoLog);
spdlog::warn("could not compile shader source: {}", infoLog);
delete[] infoLog;
return false;
spdlog::warn("could not compile shader source: {}", infoLog);
delete[] infoLog;
return false;
}
return true;
}
return true;
}
auto ShaderCache::readAndCompileShader(const std::string &filename, GLuint shader) -> bool {
std::string source;
bool result = m_storage->load(fggl::data::Data, filename, &source);
if (!result) {
spdlog::warn("could not load shader source from disk: {}", filename.c_str());
return false;
}
bool ShaderCache::readAndCompileShader(const std::string &filename, GLuint shader) {
std::string source;
bool result = m_storage->load(fggl::data::Data, filename, &source);
if ( !result ) {
spdlog::warn("could not load shader source from disk: {}", filename.c_str());
return false;
return compileShaderFromSource(source, shader);
}
return compileShaderFromSource(source, shader);
}
ShaderCache::ShaderCache(fggl::data::Storage *storage) : m_storage(storage), m_shaders(), m_binary(true) {
ShaderCache::ShaderCache(fggl::data::Storage* storage) : m_storage(storage), m_shaders(), m_binary(true) {
if (!GLAD_GL_ARB_get_program_binary) {
spdlog::warn("the graphics card doesn support shader caching, disabling");
m_binary = false;
} else {
// debug - disable shader cache
m_binary = false;
}
if ( !GLAD_GL_ARB_get_program_binary ) {
spdlog::warn("the graphics card doesn support shader caching, disabling");
m_binary = false;
} else {
// debug - disable shader cache
m_binary = false;
if (GLAD_GL_ARB_shading_language_include) {
setupIncludes();
}
initFallbackPipelines();
}
if ( GLAD_GL_ARB_shading_language_include ) {
setupIncludes();
}
initFallbackPipelines();
}
void ShaderCache::setupIncludes() {
auto root = m_storage->resolvePath(data::StorageType::Data, "include");
auto paths = m_storage->findResources(root, ".glsl");
void ShaderCache::setupIncludes() {
auto root = m_storage->resolvePath( data::StorageType::Data, "include" );
auto paths = m_storage->findResources( root, ".glsl");
for (auto &path : paths) {
std::string source;
m_storage->load(fggl::data::Data, path.string(), &source);
for ( auto& path : paths ){
std::string source;
m_storage->load( fggl::data::Data, path.string(), &source );
auto relPath = std::filesystem::relative(path, root);
const auto relPathStr = "/" + relPath.string();
glNamedStringARB(GL_SHADER_INCLUDE_ARB, -1, relPathStr.c_str(), -1, source.c_str());
}
auto relPath = std::filesystem::relative(path, root);
const auto relPathStr = "/" + relPath.string();
glNamedStringARB(GL_SHADER_INCLUDE_ARB, -1, relPathStr.c_str(), -1, source.c_str() );
}
}
auto ShaderCache::loadFromDisk(GLuint pid, const std::string &pipelineName) -> bool {
bool ShaderCache::loadFromDisk(GLuint pid, const std::string& pipelineName) {
BinaryCache cache;
auto fname = "shader_" + pipelineName + ".bin";
bool status = m_storage->load(fggl::data::Cache, fname, &cache);
BinaryCache cache;
auto fname = "shader_" + pipelineName + ".bin";
bool status = m_storage->load( fggl::data::Cache, fname, &cache );
if (!status) {
spdlog::info("cached shader '{}' could not be loaded from disk", pipelineName);
return false;
}
if ( !status ) {
spdlog::info("cached shader '{}' could not be loaded from disk", pipelineName);
return false;
bool result = cacheLoad(pid, &cache);
std::free(cache.data);
return result;
}
bool result = cacheLoad(pid, &cache);
std::free(cache.data);
return result;
}
void ShaderCache::saveToDisk(GLuint pid, const std::string &pipelineName) {
BinaryCache cache;
cacheSave(pid, &cache);
void ShaderCache::saveToDisk(GLuint pid, const std::string& pipelineName) {
BinaryCache cache;
cacheSave(pid, &cache);
auto fname = "shader_" + pipelineName + ".bin";
m_storage->save(fggl::data::Cache, fname, &cache);
}
auto fname = "shader_" + pipelineName + ".bin";
m_storage->save( fggl::data::Cache, fname, &cache);
}
auto ShaderCache::getOrLoad(const ShaderConfig &config) -> ShaderCache::ShaderPtr {
try {
return m_shaders.at(config.name);
} catch (std::out_of_range &e) {
return load(config);
}
}
GLuint ShaderCache::getOrLoad(const ShaderConfig& config) {
try {
return m_shaders.at(config.name);
} catch ( std::out_of_range& e) {
return load(config);
auto ShaderCache::get(const std::string &name) -> ShaderCache::ShaderPtr {
auto itr = m_shaders.find(name);
if ( itr != m_shaders.end() ){
return itr->second;
}
return nullptr;
}
}
GLuint ShaderCache::get(const std::string& name) {
return m_shaders.at(name);
}
auto ShaderCache::load(const ShaderConfig &config) -> ShaderCache::ShaderPtr {
spdlog::debug("starting shader program generation for {}", config.name);
GLuint pid = glCreateProgram();
GLuint ShaderCache::load(const ShaderConfig& config) {
if (m_binary) {
// if we have support for shader cache, give that a go
bool worked = loadFromDisk(pid, config.name);
if (worked) {
auto shader = std::make_shared<ogl::Shader>(pid);
m_shaders[config.name] = shader;
return shader;
}
spdlog::debug("could not use cached shader for '{}', doing full compile.", config.name);
}
spdlog::debug("starting shader program generation for {}", config.name);
// TODO actual shader loading
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
readAndCompileShader(config.vertex, vertShader);
glAttachShader(pid, vertShader);
GLuint pid = glCreateProgram();
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
readAndCompileShader(config.fragment, fragShader);
glAttachShader(pid, fragShader);
if ( m_binary ) {
// if we have support for shader cache, give that a go
bool worked = loadFromDisk(pid, config.name);
if ( worked ) {
m_shaders[config.name] = pid;
return pid;
GLuint geomShader = 0;
if (config.hasGeom) {
geomShader = glCreateShader(GL_GEOMETRY_SHADER);
readAndCompileShader(config.geometry, geomShader);
glAttachShader(pid, geomShader);
}
spdlog::debug("could not use cached shader for '{}', doing full compile.", config.name);
}
glLinkProgram(pid);
glDetachShader(pid, vertShader);
glDetachShader(pid, fragShader);
// TODO actual shader loading
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
readAndCompileShader(config.vertex, vertShader);
glAttachShader(pid, vertShader);
if (config.hasGeom) {
glDetachShader(pid, geomShader);
}
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
readAndCompileShader(config.fragment, fragShader);
glAttachShader(pid, fragShader);
GLint linked = GL_FALSE;
glGetProgramiv(pid, GL_LINK_STATUS, &linked);
if (linked == GL_FALSE) {
GLuint geomShader = 0;
if ( config.hasGeom ) {
geomShader = glCreateShader(GL_GEOMETRY_SHADER);
readAndCompileShader( config.geometry, geomShader );
glAttachShader(pid, geomShader);
}
// get the error
std::array<char, 512> infoLog;
glGetProgramInfoLog(pid, infoLog.size(), nullptr, infoLog.data());
spdlog::warn("linking shader program '{}' failed: {}", config.name, infoLog.data());
glLinkProgram(pid);
glDetachShader(pid, vertShader);
glDetachShader(pid, fragShader);
// cleanup
glDeleteProgram(pid);
glDeleteShader(vertShader);
glDeleteShader(fragShader);
if (config.hasGeom) {
glDeleteShader(geomShader);
}
if ( config.hasGeom ) {
glDetachShader(pid, geomShader);
}
return nullptr;
}
GLint linked = GL_FALSE;
glGetProgramiv(pid, GL_LINK_STATUS, &linked);
if ( linked == GL_FALSE ) {
// get the error
std::array<char, 512> infoLog;
glGetProgramInfoLog(pid, infoLog.size(), nullptr, infoLog.data());
spdlog::warn("linking shader program '{}' failed: {}", config.name, infoLog.data());
// cleanup
glDeleteProgram( pid );
glDeleteShader( vertShader );
glDeleteShader( fragShader );
if ( config.hasGeom ) {
glDeleteShader( geomShader );
if (m_binary) {
saveToDisk(pid, config.name);
}
return INVALID_SHADER_ID;
}
if ( m_binary ) {
saveToDisk(pid, config.name);
// update the cache and return
auto shaderPtr = std::make_shared<ogl::Shader>(pid);
m_shaders[config.name] = shaderPtr;
return shaderPtr;
}
// update the cache and return
m_shaders[ config.name ] = pid;
return pid;
}
auto ShaderCache::load(const ShaderSources &sources, bool allowBinaryCache) -> ShaderCache::ShaderPtr {
GLuint ShaderCache::load(const ShaderSources& sources, bool allowBinaryCache) {
spdlog::debug("starting shader program generation for {}", sources.name);
spdlog::debug("starting shader program generation for {}", sources.name);
GLuint pid = glCreateProgram();
GLuint pid = glCreateProgram();
if (m_binary && allowBinaryCache) {
// if we have support for shader cache, give that a go
bool worked = loadFromDisk(pid, sources.name);
if (worked) {
auto shader = std::make_shared<ogl::Shader>(pid);
m_shaders[sources.name] = shader;
return shader;
}
if ( m_binary && allowBinaryCache ) {
// if we have support for shader cache, give that a go
bool worked = loadFromDisk(pid, sources.name);
if ( worked ) {
m_shaders[sources.name] = pid;
return pid;
spdlog::debug("could not use cached shader for '{}', doing full compile.", sources.name);
}
spdlog::debug("could not use cached shader for '{}', doing full compile.", sources.name);
}
// TODO actual shader loading
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
compileShaderFromSource(sources.vertexSource, vertShader);
glAttachShader(pid, vertShader);
// TODO actual shader loading
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
compileShaderFromSource(sources.vertexSource, vertShader);
glAttachShader(pid, vertShader);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
compileShaderFromSource(sources.fragmentSource, fragShader);
glAttachShader(pid, fragShader);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
compileShaderFromSource(sources.fragmentSource, fragShader);
glAttachShader(pid, fragShader);
GLuint geomShader = INVALID_SHADER_ID;
if (!sources.geometrySource.empty()) {
geomShader = glCreateShader(GL_GEOMETRY_SHADER);
compileShaderFromSource(sources.geometrySource, geomShader);
glAttachShader(pid, geomShader);
}
GLuint geomShader = INVALID_SHADER_ID;
if ( !sources.geometrySource.empty() ) {
geomShader = glCreateShader(GL_GEOMETRY_SHADER);
compileShaderFromSource(sources.geometrySource, geomShader);
glAttachShader(pid, geomShader);
}
glLinkProgram(pid);
glDetachShader(pid, vertShader);
glDetachShader(pid, fragShader);
glLinkProgram(pid);
glDetachShader(pid, vertShader);
glDetachShader(pid, fragShader);
if (geomShader != INVALID_SHADER_ID) {
glDetachShader(pid, geomShader);
}
if ( geomShader != INVALID_SHADER_ID ) {
glDetachShader(pid, geomShader);
}
GLint linked = GL_FALSE;
glGetProgramiv(pid, GL_LINK_STATUS, &linked);
if (linked == GL_FALSE) {
// get the error
std::array<char, 512> infoLog{};
glGetProgramInfoLog(pid, infoLog.size(), nullptr, infoLog.data());
spdlog::warn("linking shader program '{}' failed: {}", sources.name, infoLog.data());
GLint linked = GL_FALSE;
glGetProgramiv(pid, GL_LINK_STATUS, &linked);
if ( linked == GL_FALSE ) {
// get the error
std::array<char, 512> infoLog;
glGetProgramInfoLog(pid, infoLog.size(), nullptr, infoLog.data());
spdlog::warn("linking shader program '{}' failed: {}", sources.name, infoLog.data());
// cleanup
glDeleteProgram( pid );
glDeleteShader( vertShader );
glDeleteShader( fragShader );
if ( geomShader != INVALID_SHADER_ID ) {
glDeleteShader( geomShader );
// cleanup
glDeleteProgram(pid);
glDeleteShader(vertShader);
glDeleteShader(fragShader);
if (geomShader != INVALID_SHADER_ID) {
glDeleteShader(geomShader);
}
return nullptr;
}
return INVALID_SHADER_ID;
}
if (m_binary && allowBinaryCache) {
saveToDisk(pid, sources.name);
}
if ( m_binary && allowBinaryCache ) {
saveToDisk(pid, sources.name);
// update the cache and return
m_shaders[sources.name] = std::make_shared<ogl::Shader>(pid);
return m_shaders[sources.name];
}
// update the cache and return
m_shaders[ sources.name ] = pid;
return pid;
}
void ShaderCache::cacheSave(GLuint pid, BinaryCache *cache) {
GLsizei length;
glGetProgramiv(pid, GL_PROGRAM_BINARY_LENGTH, &length);
void ShaderCache::cacheSave(GLuint pid, BinaryCache* cache) {
GLsizei length;
glGetProgramiv(pid, GL_PROGRAM_BINARY_LENGTH, &length);
cache->data = std::malloc(length);
cache->size = length;
cache->data = std::malloc(length);
cache->size = length;
glGetProgramBinary(pid, length, &cache->size, &cache->format, cache->data);
}
glGetProgramBinary(pid, length, &cache->size, &cache->format, cache->data);
}
auto ShaderCache::cacheLoad(GLuint pid, const BinaryCache *cache) -> bool {
if (!m_binary) {
return false;
}
glProgramBinary(pid, cache->format, cache->data, cache->size);
bool ShaderCache::cacheLoad(GLuint pid, const BinaryCache* cache) {
if ( !m_binary ) {
return false;
// check it loaded correctly
GLint status = GL_FALSE;
glGetProgramiv(pid, GL_LINK_STATUS, &status);
return status == GL_TRUE;
}
glProgramBinary(pid, cache->format, cache->data, cache->size);
// check it loaded correctly
GLint status = GL_FALSE;
glGetProgramiv(pid, GL_LINK_STATUS, &status);
return status == GL_TRUE;
}
void ShaderCache::initFallbackPipelines() {
// canvas fallback pipeline
load({
.name = ogl4::FALLBACK_CANVAS_PIPELINE,
.vertexSource = ogl4::FALLBACK_CANVAS_VERTEX_SHADER,
.fragmentSource = ogl4::FALLBACK_CANVAS_FRAGMENT_SHADER,
.geometrySource = ""
}, false);
}
void ShaderCache::initFallbackPipelines() {
// canvas fallback pipeline
load({
.name = ogl4::FALLBACK_CANVAS_PIPELINE,
.vertexSource = ogl4::FALLBACK_CANVAS_VERTEX_SHADER,
.fragmentSource = ogl4::FALLBACK_CANVAS_FRAGMENT_SHADER,
.geometrySource = ""
}, false);
}
template<>
bool fggl::data::fggl_deserialize(std::filesystem::path &data, fggl::gfx::BinaryCache *out) {
auto f =
#ifdef _MSC_VER
_wfopen(data.c_str(), L"r");
#else
std::fopen( data.c_str(), "r");
#endif
template<>
auto fggl::data::fggl_deserialize(std::filesystem::path &data, fggl::gfx::BinaryCache *out) -> bool {
auto* f =
#ifdef _MSC_VER
_wfopen(data.c_str(), L"r");
#else
std::fopen(data.c_str(), "r");
#endif
if ( f == nullptr ) {
spdlog::warn("could not load cached shader, fp was null");
return false;
}
if (f == nullptr) {
spdlog::warn("could not load cached shader, fp was null");
return false;
}
auto rsize = std::fread( &out->format, sizeof(GLenum), 1, f);
if ( rsize != 1 ) {
spdlog::warn("could not load cached shader: type read failed");
std::fclose(f);
return false;
}
auto rsize = std::fread(&out->format, sizeof(GLenum), 1, f);
if (rsize != 1) {
spdlog::warn("could not load cached shader: type read failed");
std::fclose(f);
return false;
}
out->size = 0;
rsize = std::fread( &out->size, sizeof(GLsizei), 1, f);
if ( rsize != 1 ) {
spdlog::warn("could not load cached shader: size read failed");
std::fclose(f);
return false;
}
out->size = 0;
rsize = std::fread(&out->size, sizeof(GLsizei), 1, f);
if (rsize != 1) {
spdlog::warn("could not load cached shader: size read failed");
std::fclose(f);
return false;
}
out->data = std::malloc(out->size);
auto readSize = std::fread( out->data, out->size, 1, f );
out->data = std::malloc(out->size);
auto readSize = std::fread(out->data, out->size, 1, f);
auto result = true;
if ( readSize != 1 ) {
spdlog::warn("could not load cached shader: reading failed!");
std::free( out->data );
result = false;
}
auto result = true;
if (readSize != 1) {
spdlog::warn("could not load cached shader: reading failed!");
std::free(out->data);
result = false;
}
std::fclose(f);
return result;
}
std::fclose(f);
return result;
}
#include <iostream>
#include <fstream>
template<>
bool fggl::data::fggl_deserialize(std::filesystem::path& data, std::string *out) {
auto fggl::data::fggl_deserialize(std::filesystem::path &data, std::string *out) -> bool {
std::ifstream ifs(data);
out->assign( (std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()) );
out->assign((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
return true;
}
template<>
bool fggl::data::fggl_serialize(std::filesystem::path &data, const fggl::gfx::BinaryCache *out) {
auto fggl::data::fggl_serialize(std::filesystem::path &data, const fggl::gfx::BinaryCache *out) -> bool {
// try and write
auto f =
auto *f =
#ifdef _MSC_VER
_wfopen( data.c_str(), L"w");
_wfopen( data.c_str(), L"w");
#else
std::fopen( data.c_str(), "w");
#endif
std::fopen(data.c_str(), "w");
#endif
if ( f == nullptr ){
if (f == nullptr) {
return false;
}
std::fwrite( &out->format, sizeof(GLenum), 1, f);
std::fwrite( &out->size, sizeof(GLsizei), 1, f);
std::fwrite( out->data, out->size, 1, f);
std::fwrite(&out->format, sizeof(GLenum), 1, f);
std::fwrite(&out->size, sizeof(GLsizei), 1, f);
std::fwrite(out->data, out->size, 1, f);
std::fclose(f);
std::free( out->data );
std::free(out->data);
return true;
}
......@@ -40,12 +40,12 @@ namespace fggl::gfx::ogl {
release();
}
VertexArray::VertexArray(VertexArray &&other) noexcept : m_obj(other.m_obj) {
VertexArray::VertexArray(VertexArray &&other) noexcept: m_obj(other.m_obj) {
other.m_obj = 0;
}
VertexArray &VertexArray::operator=(VertexArray &&other) {
if ( this != &other ){
auto VertexArray::operator=(VertexArray &&other) -> VertexArray & {
if (this != &other) {
release();
std::swap(m_obj, other.m_obj);
}
......@@ -57,76 +57,81 @@ namespace fggl::gfx::ogl {
m_obj = 0;
}
void VertexArray::setAttribute(const ArrayBuffer& buff, GLuint idx, AttributeF& attr) {
assert( 0 <= idx && idx < GL_MAX_VERTEX_ATTRIBS);
assert( 1 <= attr.elmCount && attr.elmCount <= 4);
assert( buff.isValid() );
void VertexArray::setAttribute(const ArrayBuffer &buff, GLuint idx, AttributeF &attr) {
assert(0 <= idx && idx < GL_MAX_VERTEX_ATTRIBS);
assert(1 <= attr.elmCount && attr.elmCount <= 4);
assert(buff.isValid());
#ifndef FGGL_GL_I_BOUND
bind();
GLuint boundVertexArray = 0;
bind_buffer(&boundVertexArray, buff);
bind();
GLuint boundVertexArray = 0;
bind_buffer(&boundVertexArray, buff);
#endif
glEnableVertexAttribArray(idx);
glVertexAttribPointer( idx, attr.elmCount, (GLenum)attr.attrType, GL_FALSE, attr.stride, (void*)attr.offset);
glVertexAttribPointer(idx, attr.elmCount, (GLenum) attr.attrType, GL_FALSE, attr.stride, (void *) attr.offset);
#ifndef FGGL_GL_I_BOUND
unbind_buffer(&boundVertexArray, buff);
unbind_buffer(&boundVertexArray, buff);
#endif
}
void VertexArray::setAttribute(const ArrayBuffer& buff, GLuint idx, AttributeI& attr, bool normalized) {
assert( 0 <= idx && idx < GL_MAX_VERTEX_ATTRIBS);
assert( 1 <= attr.elmCount && attr.elmCount <= 4);
assert( buff.isValid() );
void VertexArray::setAttribute(const ArrayBuffer &buff, GLuint idx, AttributeI &attr, bool normalized) {
assert(0 <= idx && idx < GL_MAX_VERTEX_ATTRIBS);
assert(1 <= attr.elmCount && attr.elmCount <= 4);
assert(buff.isValid());
#ifndef FGGL_GL_I_BOUND
GLuint boundVertexArray = 0;
bind_buffer(&boundVertexArray, buff);
GLuint boundVertexArray = 0;
bind_buffer(&boundVertexArray, buff);
#endif
glVertexAttribPointer(idx, attr.elmCount, (GLenum)attr.attrType, (GLboolean)normalized, attr.stride, (void*)attr.offset);
glVertexAttribPointer(idx,
attr.elmCount,
(GLenum) attr.attrType,
(GLboolean) normalized,
attr.stride,
(void *) attr.offset);
#ifndef FGGL_GL_I_BOUND
unbind_buffer(&boundVertexArray, buff);
unbind_buffer(&boundVertexArray, buff);
#endif
}
void VertexArray::setAttributeI(const ArrayBuffer& buff, GLuint idx, AttributeI& attr) {
assert( 0 <= idx && idx < GL_MAX_VERTEX_ATTRIBS);
assert( 1 <= attr.elmCount && attr.elmCount <= 4);
assert( buff.isValid() );
void VertexArray::setAttributeI(const ArrayBuffer &buff, GLuint idx, AttributeI &attr) {
assert(0 <= idx && idx < GL_MAX_VERTEX_ATTRIBS);
assert(1 <= attr.elmCount && attr.elmCount <= 4);
assert(buff.isValid());
#ifndef FGGL_GL_I_BOUND
GLuint boundVertexArray = 0;
bind_buffer(&boundVertexArray, buff);
GLuint boundVertexArray = 0;
bind_buffer(&boundVertexArray, buff);
#endif
glVertexAttribIPointer( idx, attr.elmCount, (GLenum)attr.attrType, attr.stride, (void*)attr.offset);
glVertexAttribIPointer(idx, attr.elmCount, (GLenum) attr.attrType, attr.stride, (void *) attr.offset);
#ifndef FGGL_GL_I_BOUND
unbind_buffer(&boundVertexArray, buff);
unbind_buffer(&boundVertexArray, buff);
#endif
}
void VertexArray::drawElements(const ElementBuffer &buff, Primative drawType, std::size_t size) {
void VertexArray::drawElements(const ElementBuffer &buff, Primitive drawType, std::size_t size) {
bind();
#ifndef FGGL_I_BOUND
GLuint boundElementArray = 0;
bind_buffer(&boundElementArray, buff);
GLuint boundElementArray = 0;
bind_buffer(&boundElementArray, buff);
#endif
glDrawElements( (GLenum)drawType, (GLsizei)size, GL_UNSIGNED_INT, nullptr );
glDrawElements((GLenum) drawType, (GLsizei) size, GL_UNSIGNED_INT, nullptr);
#ifndef FGGL_I_BOUND
unbind_buffer(&boundElementArray, buff);
unbind_buffer(&boundElementArray, buff);
#endif
}
void VertexArray::draw(Primative drawType, int first, std::size_t count) {
glDrawArrays( (GLenum)drawType, first, count);
void VertexArray::draw(Primitive drawType, int first, std::size_t count) {
glDrawArrays((GLenum) drawType, first, count);
}
} // namespace fggl::gfx::ogl
\ No newline at end of file
# Sources
target_sources(fggl
PRIVATE
setup.cpp
canvas.cpp
models.cpp
debug.cpp
module.cpp
)
PRIVATE
setup.cpp
canvas.cpp
models.cpp
debug.cpp
meshes.cpp
module.cpp
)
......@@ -27,23 +27,23 @@
namespace fggl::gfx::ogl4 {
static void make_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 } );
static void make_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});
}
inline static void add_mesh_triangle(data::Mesh2D& mesh, const std::vector<data::Vertex2D>& verts) {
assert( verts.size() == 3);
for( const auto& vert : verts ) {
inline static void add_mesh_triangle(data::Mesh2D &mesh, const std::vector<data::Vertex2D> &verts) {
assert(verts.size() == 3);
for (const auto &vert : verts) {
auto idx = mesh.add_vertex(vert);
mesh.add_index(idx);
}
}
static void convert_to_mesh(const gfx::Paint& paint, data::Mesh2D& mesh) {
static void convert_to_mesh(const gfx::Paint &paint, data::Mesh2D &mesh) {
for (const auto &cmd : paint.cmds()) {
auto path = cmd.path;
......@@ -83,17 +83,17 @@ namespace fggl::gfx::ogl4 {
}
}
CanvasRenderer::CanvasRenderer(fggl::gui::FontLibrary* fonts) :
CanvasRenderer::CanvasRenderer(fggl::gui::FontLibrary *fonts) :
m_bounds({0.0F, 1920.F, 1080.0F, 0.0F}),
m_fonts(fonts),
m_fontTex(ogl::TextureType::Tex2D) {
m_vao.bind();
#ifdef FGGL_GL_I_BOUND
// user will handle binding themselves usually, so attribute won't bind itself.
// which means it's our problem right now...
GLuint originalVertexList;
ogl::bind_buffer( &originalVertexList, m_vertexList );
// user will handle binding themselves usually, so attribute won't bind itself.
// which means it's our problem right now...
GLuint originalVertexList;
ogl::bind_buffer( &originalVertexList, m_vertexList );
#endif
// define our attributes
......@@ -107,19 +107,19 @@ namespace fggl::gfx::ogl4 {
m_vao.setAttribute(m_vertexList, 2, texAttr);
#ifdef FGGL_GL_I_BOUND
// cool, rebind whatever happened before, or not
ogl::unbind_buffer( &originalVertexList, m_vertexList );
// cool, rebind whatever happened before, or not
ogl::unbind_buffer( &originalVertexList, m_vertexList );
#endif
glBindVertexArray(0);
}
void CanvasRenderer::renderShapes (const gfx::Paint& paint, GLuint shader) {
void CanvasRenderer::renderShapes(const gfx::Paint &paint, ogl::Shader& shader) {
data::Mesh2D mesh;
convert_to_mesh(paint, mesh);
// nothing to render? give up
if ( mesh.indexList.empty() ){
if (mesh.indexList.empty()) {
return;
}
......@@ -129,62 +129,65 @@ namespace fggl::gfx::ogl4 {
m_indexList.replace(mesh.indexList.size(), mesh.indexList.data());
// draw
glDisable( GL_DEPTH_TEST );
glDisable( GL_CULL_FACE );
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
// FIXME: this should be abstracted into the shader class
glUseProgram( shader );
auto projMat = glm::ortho(m_bounds.left, m_bounds.right, m_bounds.bottom, m_bounds.top);
glUniformMatrix4fv(glGetUniformLocation( shader, "projection"), 1, GL_FALSE,
glm::value_ptr(projMat));
shader.use();
shader.setUniformMtx(shader.uniform("projection"), projMat);
m_vao.drawElements(m_indexList, ogl::Primative::TRIANGLE, mesh.indexList.size());
glUseProgram( 0 );
// draw elements
m_vao.drawElements(m_indexList, ogl::Primitive::TRIANGLE, mesh.indexList.size());
// cleanup
glUseProgram(0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}
// slow version
void CanvasRenderer::renderText(const Paint& paint, GLuint shader) {
if ( paint.textCmds().empty() ){
void CanvasRenderer::renderText(const Paint &paint, ogl::Shader& shader) {
if (paint.textCmds().empty()) {
return;
}
// get the expected font
std::shared_ptr<gui::FontFace> face = m_fonts->getFont("LiberationSans-Regular.ttf");
if ( face == nullptr ){
std::shared_ptr<gui::FontFace> face = m_fonts->getDefaultFont();
if (face == nullptr) {
// we don't know about that font...
return;
}
// setup the shader
glUseProgram( shader );
auto projMat = glm::ortho(0.0f, 1920.0f, 1080.0f, 0.f);
glUniformMatrix4fv(glGetUniformLocation( shader, "projection"), 1, GL_FALSE,
glm::value_ptr(projMat));
// set up the shader
shader.use();
auto projMat = glm::ortho(0.0F, 1920.0F, 1080.0F, 0.F);
shader.setUniformMtx(shader.uniform("projection"), projMat);
// bind the vbo we'll use for writing
m_vao.bind();
// setup the openGL state we expect for rendering
glDisable( GL_DEPTH_TEST );
glDisable( GL_CULL_FACE );
// set up the openGL state we expect for rendering
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// for each text string, attempt to render it
for ( const auto& textCmd : paint.textCmds() ) {
for (const auto &textCmd : paint.textCmds()) {
const auto label = textCmd.text;
math::vec2 penPos(textCmd.pos);
for ( auto letter : label ) {
ogl::Image img{};
img.format = ogl::ImageFormat::R;
img.type = ogl::PixelFormat::UNSIGNED_BYTE;
// set up a non-owning holder for the characters
data::Texture2D tex;
tex.channels = 1;
for (auto letter : label) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
face->texture(letter, img.size.x, img.size.y, &img.data);
m_fontTex.setData(ogl::InternalImageFormat::Red, img);
face->texture(letter, tex.size.x, tex.size.y, &tex.data);
m_fontTex.setData(ogl::InternalImageFormat::Red, &tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
......@@ -192,32 +195,33 @@ namespace fggl::gfx::ogl4 {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
m_fontTex.bind(1);
glUniform1i( glGetUniformLocation(shader, "tex"), 1);
shader.setUniformI(shader.uniform("tex"), 1);
// this is why this is called the slow version, we render each quad as a single call
auto& metrics = face->metrics(letter);
float xPos = penPos.x + metrics.bearing.x;
float yPos = (penPos.y - metrics.bearing.y);
float w = metrics.size.x;
float h = metrics.size.y;
const math::vec3 texCol{ 1.0f, 1.0f, 1.0f};
auto &metrics = face->metrics(letter);
const float xPos = penPos.x + metrics.bearing.x;
const float yPos = (penPos.y - metrics.bearing.y);
const float w = metrics.size.x;
const float h = metrics.size.y;
std::array<data::Vertex2D, 6> verts{{
{{xPos, yPos + h}, texCol, {0.0F, 1.0F}},
{{xPos, yPos}, texCol, {0.0F, 0.0F}},
{{xPos + w, yPos}, texCol, {1.0F, 0.0F}},
{{xPos, yPos + h}, texCol, {0.0F, 1.0F}},
{{xPos + w, yPos}, texCol, {1.0F, 0.0F}},
{{xPos + w, yPos + h}, texCol, {1.0F, 1.0F}},
{{xPos, yPos + h}, textCmd.colour, {0.0F, 1.0F}},
{{xPos, yPos}, textCmd.colour, {0.0F, 0.0F}},
{{xPos + w, yPos}, textCmd.colour, {1.0F, 0.0F}},
{{xPos, yPos + h}, textCmd.colour, {0.0F, 1.0F}},
{{xPos + w, yPos}, textCmd.colour, {1.0F, 0.0F}},
{{xPos + w, yPos + h}, textCmd.colour, {1.0F, 1.0F}},
}};
m_vertexList.replace(verts.size(), verts.data());
m_vao.bind();
m_vao.draw(ogl::Primative::TRIANGLE, 0, verts.size());
m_vao.draw(ogl::Primitive::TRIANGLE, 0, verts.size());
penPos.x += (metrics.advance >> 6 );
penPos.x += (metrics.advance >> 6);
}
// textures assume they own their contained data, we need to make sure we clear it
tex.data = nullptr;
}
glDisable(GL_BLEND);
......@@ -275,7 +279,7 @@ namespace fggl::gfx::ogl4 {
glUseProgram( 0 );
}*/
void CanvasRenderer::render(GLuint shader, const gfx::Paint &paint) {
void CanvasRenderer::render(ogl::Shader& shader, const gfx::Paint &paint) {
renderShapes(paint, shader);
renderText(paint, shader);
}
......
......@@ -21,45 +21,41 @@
#include "fggl/gfx/ogl4/debug.hpp"
#include <cassert>
#include <utility>
namespace fggl::gfx::ogl4 {
DebugRenderer::DebugRenderer(GLuint shader) :
mvpMatrix(1.0f),
m_lineShader(shader),
m_lineShaderMVP( m_lineShader.uniform("u_MvpMatrix")) {
DebugRenderer::DebugRenderer(std::shared_ptr<ogl::Shader> shader) :
mvpMatrix(1.0F),
m_lineShader(std::move(shader)),
m_lineShaderMVP(m_lineShader->uniform("u_MvpMatrix")) {
// define our attributes
auto posAttr = ogl::attribute<dd::DrawVertex, math::vec3>( 0 );
auto colAttr = ogl::attribute<dd::DrawVertex, math::vec3>( sizeof(float) * 3 );
auto posAttr = ogl::attribute<dd::DrawVertex, math::vec3>(0);
auto colAttr = ogl::attribute<dd::DrawVertex, math::vec3>(sizeof(float) * 3);
// bind the attributes to the vao
m_lineVao.setAttribute(m_lineVbo, 0, posAttr);
m_lineVao.setAttribute(m_lineVbo, 1, colAttr);
}
void DebugRenderer::drawLineList(const dd::DrawVertex * lines, int count, bool depthEnabled)
{
void DebugRenderer::drawLineList(const dd::DrawVertex *lines, int count, bool depthEnabled) {
assert(lines != nullptr);
assert(count > 0 && count <= DEBUG_DRAW_VERTEX_BUFFER_SIZE);
m_lineVao.bind();
m_lineShader.use();
m_lineShader.setUniformMtx(m_lineShaderMVP, mvpMatrix);
m_lineShader->use();
m_lineShader->setUniformMtx(m_lineShaderMVP, mvpMatrix);
if (depthEnabled)
{
if (depthEnabled) {
glEnable(GL_DEPTH_TEST);
}
else
{
} else {
glDisable(GL_DEPTH_TEST);
}
m_lineVbo.bind();
m_lineVbo.replace<dd::DrawVertex>(count, lines);
m_lineVao.draw(ogl::Primative::LINE, 0, count);
m_lineVao.draw(ogl::Primitive::LINE, 0, count);
glUseProgram(0);
glBindVertexArray(0);
......
/*
* 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/gfx/ogl4/meshes.hpp"
#include "fggl/gfx/phong.hpp"
#include "fggl/data/texture.hpp"
//
// Created by webpigeon on 22/10/22.
//
namespace fggl::gfx::ogl4 {
static auto setup_array_buffer(std::shared_ptr<ogl::VertexArray> &vao,
const std::vector<mesh::Vertex3D> &data) -> std::shared_ptr<ogl::ArrayBuffer> {
// upload the data to the GPU
auto buff = std::make_shared<ogl::ArrayBuffer>();
buff->write(data.size() * sizeof(mesh::Vertex3D), data.data(), ogl::BufUsage::STATIC_DRAW);
// set up the vertex attributes
auto posAttr = ogl::attribute<mesh::Vertex3D, math::vec3>(offsetof(mesh::Vertex3D, position));
auto normalAttr = ogl::attribute<mesh::Vertex3D, math::vec3>(offsetof(mesh::Vertex3D, normal));
auto colAttr = ogl::attribute<mesh::Vertex3D, math::vec3>(offsetof(mesh::Vertex3D, colour));
auto texAttr = ogl::attribute<mesh::Vertex3D, math::vec2>(offsetof(mesh::Vertex3D, texPos));
vao->setAttribute(*buff, 0, posAttr);
vao->setAttribute(*buff, 1, normalAttr);
vao->setAttribute(*buff, 2, colAttr);
vao->setAttribute(*buff, 3, texAttr);
return buff;
}
static auto setup_index_buffer(const std::vector<uint32_t>& data) -> std::shared_ptr<ogl::ElementBuffer> {
auto elementBuffer = std::make_shared<ogl::ElementBuffer>();
elementBuffer->write(data.size() * sizeof(uint32_t),
data.data(), ogl::BufUsage::STATIC_DRAW);
return elementBuffer;
}
void setup_material(const std::shared_ptr<ogl::Shader>& shader, const PhongMaterial* material) {
if ( !shader->hasUniform("material.ambient") ) {
return;
}
// setup material block
shader->setUniformF(shader->uniform("material.emission"), material->emission);
shader->setUniformF(shader->uniform("material.ambient"), material->ambient);
shader->setUniformF(shader->uniform("material.diffuse"), material->diffuse);
shader->setUniformF(shader->uniform("material.specular"), material->specular);
//shader->setUniformF(shader->uniform("material.shininess"), material->shininess);
}
void setup_material(const std::shared_ptr<ogl::Shader>& shader, const Material* material) {
if ( shader->hasUniform("material.diffuse") ) {
// setup material block
// shader->setUniformF(shader->uniform("material.emission"), material->emission);
// shader->setUniformF(shader->uniform("material.ambient"), material->ambient);
shader->setUniformF(shader->uniform("material.diffuse"), material->m_diffCol);
shader->setUniformF(shader->uniform("material.specular"), material->m_specCol);
}
// setup diffuse texture
if (material->m_diffuse != nullptr) {
material->m_diffuse->bind(0);
if (shader->hasUniform("diffuseTexture")) {
shader->setUniformI(shader->uniform("diffuseTexture"), 0);
}
}
// setup specular texture
if (material->m_specular != nullptr) {
material->m_specular->bind(1);
if (shader->hasUniform("specularTexture")) {
shader->setUniformI(shader->uniform("specularTexture"), 1);
}
}
//shader->setUniformF(shader->uniform("material.shininess"), material->shininess);
}
void MeshData::draw(std::shared_ptr<ogl::Shader> shader) const {
vao->bind();
vertexData->bind();
if ( material != nullptr ) {
setup_material(shader, material);
} else {
debug::info("no material is active, cannot bind textures!");
}
if ( drawInfo.restartIndex != ogl::NO_RESTART_IDX) {
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex( drawInfo.restartIndex );
}
vao->drawElements(*elements, drawInfo.mode, elementCount);
if ( drawInfo.restartIndex != ogl::NO_RESTART_IDX ) {
glDisable(GL_PRIMITIVE_RESTART);
}
}
void StaticMultiMesh::draw() const {
for ( const auto& mesh : meshes ) {
mesh.draw(pipeline);
}
}
void setup_lighting(const std::shared_ptr<ogl::Shader>& shader, const DirectionalLight* light) {
assert( light != nullptr );
if ( !shader->hasUniform("light.direction") ) {
fggl::debug::warning("asked for directional lighting, but shader does not support!");
return;
}
shader->setUniformF( shader->uniform("light.direction"), light->position);
shader->setUniformF( shader->uniform("light.ambient"), light->ambient);
shader->setUniformF( shader->uniform("light.diffuse"), light->diffuse);
shader->setUniformF( shader->uniform("light.specular"), light->specular);
}
void setup_lighting(const std::shared_ptr<ogl::Shader>& shader, const math::mat4& viewMatrix, const math::Transform& camTransform, const math::Transform& transform, math::vec3 lightPos) {
if (shader->hasUniform("lightPos")) {
shader->setUniformF(shader->uniform("lightPos"), lightPos);
}
if ( !shader->hasUniform("lights[0].isEnabled") ) {
return;
}
bool local = true;
shader->setUniformI(shader->uniform("lights[0].isEnabled"), 1);
shader->setUniformI(shader->uniform("lights[0].isLocal"), local);
shader->setUniformI(shader->uniform("lights[0].isSpot"), 0);
shader->setUniformF(shader->uniform("lights[0].constantAttenuation"), 5.0F);
shader->setUniformF(shader->uniform("lights[0].linearAttenuation"), 0.0F);
shader->setUniformF(shader->uniform("lights[0].quadraticAttenuation"), 0.0F);
shader->setUniformF(shader->uniform("Strength"), 0.6F);
if (!local) {
lightPos = glm::normalize(lightPos);
auto viewDir = glm::normalize(camTransform.origin() - transform.origin());
auto halfVector = glm::normalize(lightPos + viewDir);
shader->setUniformF(shader->uniform("lights[0].halfVector"), halfVector);
shader->setUniformF(shader->uniform("EyeDirection"), viewDir);
shader->setUniformF(shader->uniform("lights[0].position"), lightPos);
} else {
auto camModelView = (viewMatrix * camTransform.model() * math::vec4(0.0F, 0.0F, 0.0F, 1.0F));
auto modelModelView = (viewMatrix * transform.model() * math::vec4(0.0F, 0.0F, 0.0F, 1.0F));
math::vec3 viewDir = glm::normalize(camModelView - modelModelView);
shader->setUniformF(shader->uniform("EyeDirection"), viewDir);
shader->setUniformF(shader->uniform("lights[0].position"),
math::vec3(viewMatrix * math::vec4(lightPos, 1.0F)));
}
shader->setUniformF(shader->uniform("lights[0].ambient"), {0.0F, 0.5F, 0.0F});
shader->setUniformF(shader->uniform("lights[0].colour"), {0.5F, 0.5F, 0.5F});
}
static auto upload_texture( assets::AssetID name, assets::AssetManager* manager ) -> ogl::Texture* {
debug::info("loading texture: {}", name.get());
auto uploadedTex = assets::make_asset_id_rt("ogl", std::to_string(name.get()) );
auto* texture = manager->get<ogl::Texture>( uploadedTex );
if ( texture != nullptr ) {
return texture;
}
// get the texture data we plan to load
auto *textureData = manager->get<data::Texture2D>(name);
// create a texture
texture = new ogl::Texture(ogl::TextureType::Tex2D);
texture->setData( ogl::InternalImageFormat::RedGreenBlue, textureData);
return manager->set(uploadedTex, texture);
}
static auto get_fallback_material(assets::AssetManager* manager) -> Material* {
auto* fallback = manager->get<Material>(FALLBACK_MAT);
if ( fallback != nullptr ) {
return fallback;
}
auto* mat = new Material();
mat->m_diffCol = FALLBACK_COLOUR;
mat->m_specCol= FALLBACK_COLOUR;
mat->m_diffuse = manager->get<ogl::Texture>(FALLBACK_TEX);
mat->m_specular = manager->get<ogl::Texture>(FALLBACK_TEX);
mat->m_normals = manager->get<ogl::Texture>(SOLID_TEX);
return manager->set(FALLBACK_MAT, mat);
}
static auto upload_material( assets::AssetID name, assets::AssetManager* manager ) -> Material* {
auto* meshMaterial = manager->get<mesh::Material>(name);
if ( meshMaterial == nullptr ){
debug::error("attempted to load material {}, but did not exist!", name.get());
return get_fallback_material(manager);
}
auto uploadedMat = assets::make_asset_id_rt("ogl", std::to_string(name.get()) );
auto* material = manager->get<Material>( uploadedMat );
if ( material != nullptr ) {
return material;
}
material = new Material();
material->m_diffCol = meshMaterial->diffuse;
material->m_specCol = meshMaterial->specular;
// do we have a diffuse texture?
if ( !meshMaterial->diffuseTextures.empty() ) {
material->m_diffuse = upload_texture(meshMaterial->getPrimaryDiffuse(), manager);
} else {
material->m_diffuse = manager->get<ogl::Texture>( ogl4::SOLID_TEX );
}
// do we have a normal Texture?
if ( !meshMaterial->normalTextures.empty() ) {
material->m_normals = upload_texture(meshMaterial->getPrimaryNormals(), manager);
} else {
material->m_normals = manager->get<ogl::Texture>( ogl4::SOLID_TEX );
}
// do we have a specular texture?
if ( !meshMaterial->specularTextures.empty() ) {
material->m_specular = upload_texture(meshMaterial->getPrimarySpecular(), manager);
} else {
material->m_specular = manager->get<ogl::Texture>( ogl4::SOLID_TEX );
}
return manager->set( uploadedMat, material);
}
auto upload_mesh(const mesh::Mesh3D& rawMesh, assets::AssetManager* manager) -> MeshData {
auto vao = std::make_shared<ogl::VertexArray>();
return {
.vao = vao,
.elements = setup_index_buffer( rawMesh.indices ),
.vertexData = setup_array_buffer(vao, rawMesh.data ),
.elementCount = rawMesh.indices.size(),
.drawInfo = { .mode = ogl::Primitive::TRIANGLE },
.material = upload_material( rawMesh.material, manager )
};
}
auto upload_multi_mesh(const mesh::MultiMesh3D& rawMesh, assets::AssetManager* manager) -> std::vector<MeshData> {
std::vector<MeshData> gpuMeshes;
for (const auto& mesh : rawMesh.meshes) {
gpuMeshes.push_back( upload_mesh(mesh, manager) );
}
return gpuMeshes;
}
}
......@@ -17,17 +17,19 @@
//
#include "fggl/gfx/ogl4/models.hpp"
#include "fggl/gfx/ogl4/meshes.hpp"
#include "fggl/data/heightmap.hpp"
#include "fggl/data/texture.hpp"
#include "fggl/debug/logging.hpp"
#include "fggl/gfx/camera.hpp"
#include "fggl/gfx/phong.hpp"
#include <spdlog/spdlog.h>
namespace fggl::gfx::ogl4 {
static std::shared_ptr<ogl::ArrayBuffer> setupArrayBuffer(std::shared_ptr<ogl::VertexArray>& vao,
std::vector<data::Vertex>& data) {
static auto setup_array_buffer(std::shared_ptr<ogl::VertexArray> &vao,
const std::vector<data::Vertex> &data) -> std::shared_ptr<ogl::ArrayBuffer> {
auto buff = std::make_shared<ogl::ArrayBuffer>();
buff->write(data.size() * sizeof(data::Vertex), data.data(), ogl::BufUsage::STATIC_DRAW);
......@@ -36,24 +38,25 @@ namespace fggl::gfx::ogl4 {
auto normalAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, normal));
auto colAttr = ogl::attribute<data::Vertex, math::vec3>(offsetof(data::Vertex, colour));
vao->setAttribute( *buff, 0, posAttr );
vao->setAttribute( *buff, 1, normalAttr );
vao->setAttribute( *buff, 2, colAttr );
vao->setAttribute(*buff, 0, posAttr);
vao->setAttribute(*buff, 1, normalAttr);
vao->setAttribute(*buff, 2, colAttr);
return buff;
}
static std::shared_ptr<ogl::ElementBuffer> setupIndexBuffer(std::shared_ptr<ogl::VertexArray>& vao,
std::vector<uint32_t>& data) {
static auto setup_index_buffer(std::shared_ptr<ogl::VertexArray> &vao,
const std::vector<uint32_t> &data) -> std::shared_ptr<ogl::ElementBuffer> {
auto elementBuffer = std::make_shared<ogl::ElementBuffer>();
elementBuffer->write(data.size() * sizeof(uint32_t),
data.data(), ogl::BufUsage::STATIC_DRAW);
return elementBuffer;
}
static void setupComponent(StaticModel& modelComp, std::shared_ptr<ogl::Shader>& shader, data::Mesh& mesh) {
auto vao = std::make_shared< ogl::VertexArray >();
auto meshBuffer = setupArrayBuffer(vao, mesh.vertexList());
auto elementBuffer = setupIndexBuffer(vao, mesh.indexList());
static void setup_component(StaticModel &modelComp, std::shared_ptr<ogl::Shader> &shader, data::Mesh &mesh) {
auto vao = std::make_shared<ogl::VertexArray>();
auto meshBuffer = setup_array_buffer(vao, mesh.vertexList());
auto elementBuffer = setup_index_buffer(vao, mesh.indexList());
// set up the element attributes
modelComp.vao = vao;
......@@ -61,184 +64,184 @@ namespace fggl::gfx::ogl4 {
modelComp.elements = elementBuffer;
modelComp.pipeline = shader;
modelComp.elementCount = mesh.indexCount();
modelComp.drawType = ogl::Primative::TRIANGLE;
modelComp.drawType = ogl::Primitive::TRIANGLE;
}
void StaticModelRenderer::resolveModels(entity::EntityManager &world) {
// FIXME: this needs something reactive or performance will suck.
auto renderables = world.find<data::StaticMesh>();
for (const auto& renderable : renderables){
auto* currModel = world.tryGet<StaticModel>( renderable );
if ( currModel != nullptr ){
continue;
}
auto& meshComp = world.get<data::StaticMesh>(renderable);
auto& modelComp = world.add<StaticModel>(renderable);
auto shader = m_phong;
try {
shader = std::make_shared<ogl::Shader>( m_shaders->get( meshComp.pipeline ) );
} catch ( std::out_of_range& e) {
debug::log(debug::Level::warning, "Could not find shader: {}", meshComp.pipeline);
}
auto StaticModelRenderer::uploadMesh(assets::AssetID guid, const data::Mesh &mesh, bool allowCache) -> StaticModel* {
assert( m_assets != nullptr );
setupComponent(modelComp, shader, meshComp.mesh);
debug::log(debug::Level::info, "Added static mesh to {}", (uint64_t)renderable);
// if the asset has already been uploaded, we don't need to do anything
if ( allowCache && m_assets->has(guid) ) {
m_assets->require(guid);
return m_assets->get<StaticModel>(guid);
}
// terrain
auto terrain = world.find<data::HeightMap>();
for (auto& renderable : terrain){
auto currModel = world.tryGet<StaticModel>( renderable );
if ( currModel != nullptr ){
continue;
}
// the asset does not exist, we need to upload it
auto* modelAsset = new StaticModel();
modelAsset->vao = std::make_shared<ogl::VertexArray>();
modelAsset->vertexData = setup_array_buffer(modelAsset->vao, mesh.vertexList());
modelAsset->elements = setup_index_buffer(modelAsset->vao, mesh.indexList());
modelAsset->elementCount = mesh.indexCount();
modelAsset->drawType = ogl::Primitive::TRIANGLE;
// if caching is enabled, then use the cache
if ( allowCache ) {
m_assets->set(guid, modelAsset);
}
auto& heightmap = world.get<data::HeightMap>(renderable);
data::Mesh heightMapMesh{};
data::generateHeightMesh(heightmap, heightMapMesh);
return modelAsset;
}
auto& modelComp = world.add<StaticModel>(renderable);
setupComponent(modelComp, m_phong, heightMapMesh);
/*MeshData* StaticModelRenderer::uploadMesh(assets::AssetGUID guid, const mesh::Mesh3D &mesh, bool allowCache) {
assert( m_assets != nullptr );
// we know this is a triangle strip with a restart vertex...
// FIXME the model should be telling us this...
modelComp.drawType = ogl::Primative::TRIANGLE_STRIP;
modelComp.restartIndex = heightMapMesh.restartVertex;
// if the asset has already been uploaded, we don't need to do anything
if ( allowCache && m_assets->has(guid) ) {
m_assets->require(guid);
return m_assets->get<MeshData>(guid);
}
// no active model, we need to resolve/load one.
//spdlog::info("looks like heightmap, {} needs a static mesh", renderable);
// the asset does not exist, we need to upload it
auto* modelAsset = new MeshData();
modelAsset->vao = std::make_shared<ogl::VertexArray>();
modelAsset->vertexData = setupArrayBuffer(modelAsset->vao, mesh.data);
modelAsset->elements = setupIndexBuffer(modelAsset->vao, mesh.indices);
modelAsset->elementCount = mesh.indices.size();
modelAsset->drawInfo.mode = ogl::Primitive::TRIANGLE;
// if caching is enabled, then use the cache
if ( allowCache ) {
m_assets->set(guid, modelAsset);
}
}
void StaticModelRenderer::renderModelsForward(const entity::EntityManager &world) {
return modelAsset;
}*/
// fetch cameras we will need to render with
auto cameras = world.find<gfx::Camera>();
auto StaticModelRenderer::uploadMesh2(const assets::AssetID& meshName, const data::Mesh &mesh) -> StaticModelGPU* {
assert( m_assets != nullptr );
// if there are no cameras, we can't do anything...
if ( cameras.empty() ) {
spdlog::warn("asked to render static models, but there were no cameras");
return;
if ( m_assets->has(meshName) ) {
// we've already uploaded it...
return m_assets->get<StaticModelGPU>(meshName);
}
// perform a rendering pass for each camera (will usually only be one...)
for ( const auto& cameraEnt : cameras ){
//TODO should be clipping this to only visible objects
auto* modelAsset = new StaticModelGPU();
modelAsset->vao = std::make_shared<ogl::VertexArray>();
modelAsset->vertices = setup_array_buffer(modelAsset->vao, mesh.vertexList());
modelAsset->elements = setup_index_buffer(modelAsset->vao, mesh.indexList());
modelAsset->elementCount = mesh.indexCount();
modelAsset->drawType = ogl::Primitive::TRIANGLE;
// enable required OpenGL state
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
return m_assets->set(meshName, modelAsset);
}
// enable depth testing
glEnable( GL_DEPTH_TEST );
#ifdef FGGL_ALLOW_DEFERRED_UPLOAD
static void setup_meshes(entity::EntityManager& world, ShaderCache* shaders, assets::AssetManager* manager) {
auto entsWithModels = world.find<mesh::StaticMesh3D>();
for ( const auto& mesher : entsWithModels ) {
// set-up camera matrices
const auto& camTransform = world.get<math::Transform>(cameraEnt);
const auto& camComp = world.get<gfx::Camera>(cameraEnt);
// check if this entity already has a mesh...
const auto* currModel = world.tryGet<StaticMesh>(mesher);
if ( currModel != nullptr ) {
continue;
}
const math::mat4 projectionMatrix = glm::perspective(camComp.fov, camComp.aspectRatio, camComp.nearPlane, camComp.farPlane);
const math::mat4 viewMatrix = glm::lookAt( camTransform.origin(), camComp.target, camTransform.up() );
// figure out the requirements
auto& rawMesh = world.get<mesh::StaticMesh3D>(mesher);
if ( rawMesh.guid != util::make_guid("__NO_CACHE__") ){
// TODO support fetching of loaded meshes
debug::warning("multiple entities sharing a mesh has not been implemented yet...");
}
// TODO lighting needs to not be this...
math::vec3 lightPos{0.0f, 10.0f, 0.0f};
std::shared_ptr<ogl::Shader> shader = nullptr;
auto& entityMesh = world.add<StaticMesh>(mesher);
entityMesh.pipeline = shaders->get( rawMesh.pipeline );
entityMesh.mesh = upload_mesh( rawMesh.mesh, manager );
auto renderables = world.find<StaticModel>();
for ( const auto& entity : renderables ){
}
}
// ensure that the model pipeline actually exists...
const auto& model = world.get<StaticModel>(entity);
if ( model.pipeline == nullptr ) {
spdlog::warn("shader was null, aborting render");
static void setup_multi_meshes(entity::EntityManager& world, ShaderCache* shaders, assets::AssetManager* manager) {
auto entsWithModels = world.find<mesh::StaticMultiMesh3D>();
for ( const auto& mesher : entsWithModels ) {
// check if this entity already has a mesh...
const auto* currModel = world.tryGet<StaticMultiMesh>(mesher);
if ( currModel != nullptr ) {
continue;
}
// check if we switched shaders
if ( shader != model.pipeline ) {
// new shader - need to re-send the view and projection matrices
shader = model.pipeline;
shader->use();
if ( shader->hasUniform("projection") ) {
shader->setUniformMtx(shader->uniform("view"), viewMatrix);
shader->setUniformMtx(shader->uniform("projection"), projectionMatrix);
}
// figure out the requirements
auto& multiMesh = world.get<mesh::StaticMultiMesh3D>(mesher);
if ( multiMesh.guid != util::make_guid("__NO_CACHE__") ){
// TODO support fetching of loaded meshes
debug::warning("multiple entities sharing a mesh has not been implemented yet...");
}
// set model transform
const auto& transform = world.get<math::Transform>(entity);
shader->setUniformMtx(shader->uniform("MVPMatrix"), projectionMatrix * viewMatrix * transform.model() );
shader->setUniformMtx(shader->uniform("MVMatrix"), viewMatrix * transform.model());
auto normalMatrix = glm::mat3(glm::transpose(inverse(transform.model())));
shader->setUniformMtx(shader->uniform("NormalMatrix"), normalMatrix);
// setup lighting mode
if ( shader->hasUniform("lights[0].isEnabled") ) {
bool local = true;
shader->setUniformI(shader->uniform("lights[0].isEnabled"), 1);
shader->setUniformI(shader->uniform("lights[0].isLocal"), local);
shader->setUniformI(shader->uniform("lights[0].isSpot"), 0);
shader->setUniformF(shader->uniform("lights[0].constantAttenuation"), 5.0f);
shader->setUniformF(shader->uniform("lights[0].linearAttenuation"), 0.0f);
shader->setUniformF(shader->uniform("lights[0].quadraticAttenuation"), 0.0f);
shader->setUniformF(shader->uniform("Strength"), 0.6F);
if (!local) {
lightPos = glm::normalize(lightPos);
auto viewDir = glm::normalize(camTransform.origin() - transform.origin());
auto halfVector = glm::normalize(lightPos + viewDir);
shader->setUniformF(shader->uniform("lights[0].halfVector"), halfVector );
shader->setUniformF(shader->uniform("EyeDirection"), viewDir);
shader->setUniformF(shader->uniform("lights[0].position"), lightPos);
} else {
auto camModelView = (viewMatrix * camTransform.model() * math::vec4(0.0f, 0.0f, 0.0f, 1.0f));
auto modelModelView = (viewMatrix * transform.model() * math::vec4(0.0f, 0.0f, 0.0f, 1.0f));
math::vec3 viewDir = glm::normalize(camModelView - modelModelView);
shader->setUniformF(shader->uniform("EyeDirection"), viewDir);
shader->setUniformF(shader->uniform("lights[0].position"), math::vec3(viewMatrix * math::vec4(lightPos, 1.0f)));
}
shader->setUniformF(shader->uniform("lights[0].ambient"), {0.0f, 0.5f, 0.0f});
shader->setUniformF(shader->uniform("lights[0].colour"), {0.5f, 0.5f, 0.5f});
}
auto& entityMesh = world.add<StaticMultiMesh>(mesher);
entityMesh.pipeline = shaders->get( multiMesh.pipeline );
entityMesh.meshes = upload_multi_mesh(multiMesh.mesh, manager);
//entityMesh.material = setup_material( multiMesh.mesh.materials[0], manager);
}
}
// material detection with fallback
const auto& material = world.get<PhongMaterial>(entity);
void StaticModelRenderer::resolveModels(entity::EntityManager &world) {
// new mesh formats
setup_meshes(world, m_shaders, m_assets);
setup_multi_meshes(world, m_shaders, m_assets);
if ( shader->hasUniform("materials[0].ambient") ) {
shader->setUniformF(shader->uniform("materials[0].emission"), material.emission);
shader->setUniformF(shader->uniform("materials[0].ambient"), material.ambient);
shader->setUniformF(shader->uniform("materials[0].diffuse"), material.diffuse);
shader->setUniformF(shader->uniform("materials[0].specular"), material.specular);
shader->setUniformF(shader->uniform("materials[0].shininess"), material.shininess);
// terrain
auto terrain = world.find<data::HeightMap>();
for (const auto &renderable : terrain) {
auto *currModel = world.tryGet<StaticModel>(renderable);
if (currModel != nullptr) {
continue;
}
if ( shader->hasUniform("lightPos")) {
shader->setUniformF( shader->uniform("lightPos"), lightPos);
}
auto &heightmap = world.get<data::HeightMap>(renderable);
data::Mesh heightMapMesh{};
data::generateHeightMesh(heightmap, heightMapMesh);
auto vao = model.vao;
vao->bind();
auto &modelComp = world.add<StaticModel>(renderable);
setup_component(modelComp, m_phong, heightMapMesh);
model.vertexData->bind();
if ( model.restartIndex != NO_RESTART_IDX) {
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(model.restartIndex);
}
// we know this is a triangle strip with a restart vertex...
// FIXME the model should be telling us this...
modelComp.drawType = ogl::Primitive::TRIANGLE_STRIP;
modelComp.restartIndex = heightMapMesh.restartVertex;
auto* elements = model.elements.get();
vao->drawElements( *elements, model.drawType, model.elementCount);
if ( model.restartIndex != NO_RESTART_IDX) {
glDisable(GL_PRIMITIVE_RESTART);
}
// no active model, we need to resolve/load one.
debug::info("looks like {} needs a static mesh", (uint64_t)renderable);
}
}
#endif
void StaticModelRenderer::renderModelsForward(const entity::EntityManager &world, bool debugMode) {
// fetch cameras we will need to render with
auto cameras = world.find<gfx::Camera>();
// if there are no cameras, we can't do anything...
if (cameras.empty()) {
debug::warning("asked to render static models, but there were no cameras");
return;
}
// perform a rendering pass for each camera (will usually only be one...)
for (const auto &cameraEnt : cameras) {
//TODO should be clipping this to only visible objects
//forward_camera_pass(cameraEnt, world);
forward_pass<ogl4::StaticMesh>(cameraEnt, world, m_assets);
forward_pass<ogl4::StaticMultiMesh>(cameraEnt, world, m_assets);
// enable rendering normals
if ( debugMode ) {
auto normalShader = m_shaders->get("normals");
forward_pass_normals<StaticMesh>(cameraEnt, world, normalShader);
forward_pass_normals<StaticMultiMesh>(cameraEnt, world, normalShader);
//forward_normal_pass(cameraEnt, world, normalShader);
}
}
}
......
......@@ -18,83 +18,177 @@
#include "fggl/gfx/ogl4/module.hpp"
#include "fggl/gfx/phong.hpp"
#include "fggl/mesh/components.hpp"
#include "fggl/data/procedural.hpp"
#include "fggl/assets/loader.hpp"
#include <string>
namespace fggl::gfx {
constexpr uint32_t DEFAULT_STACKS = 16;
constexpr uint32_t DEFAULT_SLICES = 16;
constexpr const char* SHAPE_SPHERE{"sphere"};
constexpr const char* SHAPE_BOX{"box"};
static void process_shape(const YAML::Node& node, data::Mesh& mesh) {
auto transform = data::OFFSET_NONE;
auto offset = node["offset"].as<math::vec3>(math::VEC3_ZERO);
transform = glm::translate(transform, offset);
auto scale = node["scale"].as<math::vec3>(math::VEC3_ONES);
transform = glm::scale(transform, scale);
debug::debug("scale: {}, {}, {}", scale.x, scale.y, scale.z);
// now the shape itself
auto type = node["type"].as<std::string>();
if ( type == SHAPE_BOX ) {
data::make_cube(mesh, transform);
} else if ( type == SHAPE_SPHERE ) {
auto stacks = node["stacks"].as<uint32_t>(DEFAULT_STACKS);
auto slices = node["slices"].as<uint32_t>(DEFAULT_SLICES);
data::make_sphere(mesh, transform, stacks, slices);
} else {
debug::log(debug::Level::warning, "unknown shape type requested: {}", type);
constexpr const char *SHAPE_SPHERE{"sphere"};
constexpr const char *SHAPE_BOX{"box"};
namespace {
void process_shape(const YAML::Node &node, mesh::Mesh3D &mesh) {
auto transform = data::OFFSET_NONE;
auto offset = node["offset"].as<math::vec3>(math::VEC3_ZERO);
transform = glm::translate(transform, offset);
auto scale = node["scale"].as<math::vec3>(math::VEC3_ONES);
transform = glm::scale(transform, scale);
debug::debug("scale: {}, {}, {}", scale.x, scale.y, scale.z);
// now the shape itself
auto type = node["type"].as<std::string>();
if (type == SHAPE_BOX) {
data::make_cube(mesh, transform);
} else if (type == SHAPE_SPHERE) {
auto stacks = node["stacks"].as<uint32_t>(DEFAULT_STACKS);
auto slices = node["slices"].as<uint32_t>(DEFAULT_SLICES);
data::make_sphere(mesh, transform, stacks, slices);
} else {
debug::log(debug::Level::warning, "unknown shape type requested: {}", type);
}
}
}
void attach_mesh(const entity::ComponentSpec& spec, entity::EntityManager& manager, const entity::EntityID& id) {
auto& meshComp = manager.add<data::StaticMesh>(id);
meshComp.pipeline = spec.get<std::string>("pipeline", "");
if ( spec.has("shape") ) {
// procedural mesh
data::Mesh mesh;
if ( spec.config["shape"].IsSequence() ) {
for( const auto& node : spec.config["shape"] ) {
process_shape(node, mesh);
void attach_mesh(const entity::ComponentSpec &spec, entity::EntityManager &manager, const entity::EntityID &id, modules::Services &services) {
// check for the asset service
auto* assetService = services.get<assets::AssetManager>();
auto* assetLoader = services.get<assets::Loader>();
if ( assetService == nullptr || assetLoader == nullptr ) {
// no asset service, give up
return;
}
// asset is a procedural mesh description
if (spec.has("shape")) {
auto pipeline = spec.get<std::string>("pipeline", "");
// check if we had previously loaded this asset
/*const auto shapeName = spec.get<std::string>("shape_id", "");
if ( !shapeName.empty() ) {
meshAsset = assetService->get<data::Mesh>(shapeName);
}*/
// we've not loaded this before - generate mesh
if ( true ) {
// procedural meshes are build as static meshes first
if (spec.config["shape"].IsSequence()) {
mesh::MultiMesh3D* multiMesh;
for (const auto &node : spec.config["shape"]) {
auto* meshAsset = new mesh::Mesh3D();
process_shape(node, *meshAsset);
multiMesh->meshes.push_back(*meshAsset);
delete meshAsset;
}
#ifdef FGGL_ALLOW_DEFERRED_UPLOAD
// the graphics stack can detect static meshes without a rendering proxy at runtime and fix it but this
// requires loading the whole model into the ECS (and should be removed in the future). instead we should
// be triggering the upload at this point (if needed).
auto &entityMesh = manager.add<mesh::StaticMultiMesh3D>(id);
entityMesh.mesh = *multiMesh;
entityMesh.pipeline = pipeline;
debug::warning("HACKY: Triggered proc mesh - using deferred upload");
#endif
} else {
auto* meshAsset = new mesh::Mesh3D();
process_shape(spec.config["shape"], *meshAsset);
#ifdef FGGL_ALLOW_DEFERRED_UPLOAD
// the graphics stack can detect static meshes without a rendering proxy at runtime and fix it but this
// requires loading the whole model into the ECS (and should be removed in the future). instead we should
// be triggering the upload at this point (if needed).
auto &entityMesh = manager.add<mesh::StaticMesh3D>(id);
entityMesh.mesh = *meshAsset;
entityMesh.pipeline = pipeline;
debug::warning("HACKY: Triggered proc mesh - using deferred upload");
#endif
}
} else {
process_shape(spec.config["shape"], mesh);
//assetService->set(shapeName, meshTmp);
}
mesh.removeDups();
meshComp.mesh = mesh;
// TODO we need to trigger an upload to the GPU (somehow)
return;
}
// asset is a model from the resource cache
if ( spec.has("model") ) {
// figure out what model we want
auto assetStr = spec.get<std::string>("model", "");
auto assetId = assets::asset_from_user( assetStr );
auto* asset = assetService->get<ogl4::StaticModel>(assetId);
if ( asset == nullptr ) {
// the asset is not loaded/does not exist
debug::error("requested model {} but it was not loaded.", assetStr);
return;
}
// copy the asset to the model
auto& model = manager.add<ogl4::StaticModel>(id);
model = *asset;
}
}
void attach_material(const entity::ComponentSpec& spec, entity::EntityManager& manager, const entity::EntityID& id) {
auto& mat = manager.add<gfx::PhongMaterial>(id);
void attach_material(const entity::ComponentSpec &spec,
entity::EntityManager &manager,
const entity::EntityID &id,
modules::Services& /*services*/ ) {
auto &mat = manager.add<gfx::PhongMaterial>(id);
mat.ambient = spec.get<math::vec3>("ambient", gfx::DEFAULT_AMBIENT);
mat.diffuse = spec.get<math::vec3>("diffuse", gfx::DEFAULT_DIFFUSE);
mat.specular = spec.get<math::vec3>("ambient", gfx::DEFAULT_SPECULAR);
mat.shininess = spec.get<float>("ambient", gfx::DEFAULT_SHININESS);
}
void attach_light(const entity::ComponentSpec& spec, entity::EntityManager& manager, const entity::EntityID& id) {
auto& light = manager.add<gfx::Light>(id);
void attach_light_directional(const entity::ComponentSpec &spec, entity::EntityManager &manager, const entity::EntityID &id, modules::Services& /*services*/) {
auto &light = manager.add<gfx::DirectionalLight>(id);
light.position = spec.get<math::vec3>("direction", -math::UP);
light.ambient = spec.get<math::vec3>("ambient", gfx::colours::WHITE);
light.specular = spec.get<math::vec3>("specular", gfx::colours::WHITE);
light.diffuse = spec.get<math::vec3>("diffuse", gfx::colours::WHITE);
}
bool OpenGL4::factory(modules::ModuleService service, modules::Services& services) {
void attach_light_point(const entity::ComponentSpec &spec, entity::EntityManager &manager, const entity::EntityID &id, modules::Services& /*services*/) {
auto &light = manager.add<gfx::PointLight>(id);
light.position = spec.get<math::vec3>("position", math::VEC3_ZERO);
light.constant = spec.get<float>("constant", 1.0F);
light.linear = spec.get<float>("linear", 0.0014F);
light.quadratic = spec.get<float>("quadratic", 0.000007F);
}
auto OpenGL4::factory(modules::ServiceName service, modules::Services &services) -> bool {
if (service == WindowGraphics::service) {
// setup the thing responsible for graphics
auto* storage = services.get<data::Storage>();
auto* fontLibrary = services.get<gui::FontLibrary>();
services.bind<WindowGraphics, ogl4::WindowGraphics>(storage, fontLibrary);
auto *storage = services.get<data::Storage>();
auto *fontLibrary = services.get<gui::FontLibrary>();
auto *assets = services.get<assets::AssetManager>();
services.bind<WindowGraphics, ogl4::WindowGraphics>(storage, fontLibrary, assets);
// register as responsible for creating rendering components
auto* entityFactory = services.get<entity::EntityFactory>();
auto *entityFactory = services.get<entity::EntityFactory>();
entityFactory->bind(data::StaticMesh::guid, attach_mesh);
entityFactory->bind(mesh::StaticMesh3D::guid, attach_mesh);
entityFactory->bind(mesh::StaticMultiMesh3D::guid, attach_mesh);
entityFactory->bind(gfx::PhongMaterial::guid, attach_material);
entityFactory->bind(gfx::Light::guid, attach_light);
entityFactory->bind(gfx::DirectionalLight::guid, attach_light_directional);
entityFactory->bind(gfx::PointLight::guid, attach_light_point);
return true;
}
......
......@@ -20,8 +20,8 @@
namespace fggl::gfx::ogl4 {
Graphics *WindowGraphics::create(display::Window &window) {
return new OpenGL4Backend(m_storage, m_fonts);
auto WindowGraphics::create(display::Window &window) -> Graphics * {
return new OpenGL4Backend(m_storage, m_fonts, m_assets, (GlFunctionLoader)glfwGetProcAddress);
}
}
\ No newline at end of file
......@@ -21,7 +21,6 @@
#include <string>
#include <stdexcept>
#include <GLFW/glfw3.h>
#include <spdlog/spdlog.h>
namespace fggl::display::glfw {
......@@ -34,37 +33,37 @@ namespace fggl::display::glfw {
fgglWindow->framesize(width, height);
}
static void fggl_input_cursor(GLFWwindow *window, double x, double y) {
static void fggl_input_cursor(GLFWwindow *window, double xPos, double yPos) {
auto &input = GlfwInputManager::instance();
auto *fgglWin = static_cast<Window *>(glfwGetWindowUserPointer(window));
#ifndef FGGL_INPUT_SCREEN_COORDS
// convert to nice ranges...
x = (x / fgglWin->width() * 2) - 1.0; // [-1, 1]
y = (y / fgglWin->height() * 2) - 1.0; // [-1, 1]
xPos = (xPos / fgglWin->width() * 2) - 1.0; // [-1, 1]
yPos = (yPos / fgglWin->height() * 2) - 1.0; // [-1, 1]
#endif
// inform the input system
input.onMouseMove(x, y);
input.onMouseMove(xPos, yPos);
}
static void fggl_input_scroll(GLFWwindow *window, double x, double y) {
static void fggl_input_scroll(GLFWwindow */*window*/, double xPos, double yPos) {
auto &input = GlfwInputManager::instance();
input.onMouseScroll(x, y);
input.onMouseScroll(xPos, yPos);
}
static void fggl_input_mouse_btn(GLFWwindow *window, int btn, int action, int mods) {
static void fggl_input_mouse_btn(GLFWwindow */*window*/, int btn, int action, int /*mods*/) {
auto &input = GlfwInputManager::instance();
input.onMouseButton(btn, action == GLFW_PRESS);
}
static void fggl_input_keyboard(GLFWwindow *window, int key, int scancode, int action, int mods) {
static void fggl_input_keyboard(GLFWwindow */*window*/, int /*key*/, int scancode, int action, int /*mods*/) {
auto &input = GlfwInputManager::instance();
input.onKeyEvent(scancode, action == GLFW_PRESS || action == GLFW_REPEAT);
}
static void fggl_update_joystick(fggl::input::GamepadInput &input, int jid) {
bool isGamepad = glfwJoystickIsGamepad(jid);
bool isGamepad = (glfwJoystickIsGamepad(jid) == GLFW_TRUE);
if (isGamepad) {
if (!input.present(jid)) {
......@@ -99,7 +98,7 @@ namespace fggl::display::glfw {
auto &gamepadCtl = input.gamepads();
for (int jid = 0; jid < GLFW_JOYSTICK_LAST; jid++) {
if (glfwJoystickPresent(jid)) {
if (glfwJoystickPresent(jid) == GLFW_TRUE) {
fggl_update_joystick(gamepadCtl, jid);
} else {
gamepadCtl.setActive(jid, false);
......@@ -144,7 +143,7 @@ namespace fggl::display::glfw {
GlfwContext::~GlfwContext() {
glfwTerminate();
spdlog::debug("[glfw] context terminated");
debug::trace("[glfw] context terminated");
}
void GlfwContext::pollEvents() {
......@@ -155,9 +154,11 @@ namespace fggl::display::glfw {
fggl_joystick_poll();
}
Window::Window(std::shared_ptr<GlfwContext> context, gfx::WindowGraphics* graphics)
Window::Window(std::shared_ptr<GlfwContext> context, gfx::WindowGraphics *graphics)
: m_context(std::move(context)), m_window(nullptr), m_framesize() {
spdlog::debug("[glfw] creating window");
// don't iconify when focus is lost.
glfwWindowHint( GLFW_AUTO_ICONIFY, GLFW_FALSE );
// FIXME - this ties the graphics API before window creation
auto graphicsConfig = graphics->config();
......@@ -184,12 +185,15 @@ namespace fggl::display::glfw {
// bind the graphics API
glfwMakeContextCurrent(m_window);
m_graphics = std::unique_ptr<gfx::Graphics>(graphics->create(*this));
spdlog::debug("[glfw] window creation complete");
m_graphics = graphics->createMain(*this);
}
Window::~Window() {
if ( m_graphics != nullptr ) {
delete m_graphics;
m_graphics = nullptr;
}
if (m_window != nullptr) {
// prevent dangling pointers
glfwSetWindowUserPointer(m_window, nullptr);
......@@ -215,14 +219,14 @@ namespace fggl::display::glfw {
glfwMakeContextCurrent(m_window);
}
fggl::math::vec2i Window::frameSize() const {
auto Window::frameSize() const -> fggl::math::vec2i {
assert(m_window != nullptr);
math::vec2i size;
glfwGetFramebufferSize(m_window, &size.x, &size.y);
return size;
}
bool Window::wantClose() const {
auto Window::wantClose() const -> bool {
assert(m_window != nullptr);
return glfwWindowShouldClose(m_window) == GLFW_TRUE;
}
......
target_sources( fggl
PRIVATE
hexagon.cpp
)
\ 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 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)
......@@ -17,9 +17,9 @@
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);
}
}
......@@ -27,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 ) {}
......@@ -70,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);
......
......@@ -23,29 +23,29 @@ namespace fggl::gui {
FT_Done_Face(m_face);
}
FontLibrary::FontLibrary(data::Storage* storage) : m_context(nullptr), m_storage(storage) {
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
};
......@@ -54,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