Skip to content

Commit

Permalink
Set camera to preserve aspect ratio.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasper Peeters committed Nov 12, 2024
1 parent 9ca5dd2 commit 96b4841
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
55 changes: 49 additions & 6 deletions frontend/gtkmm/GraphicsView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
#include <iostream>
#include <fstream>
#include <gdk/gdkx.h>
#include <filament/Viewport.h>
#include <filament/ColorGrading.h>

using namespace cadabra;

// LIBGL_ALWAYS_SOFTWARE=true cadabra2-gtk
// a=server.send("hello", "graphics_view", 0, 123, False)
// https://github.com/nitronoid/qt_filament/

static constexpr uint8_t RESOURCES_BAKEDCOLOR_DATA[] = {
#include "bakedcolor.inc"
Expand All @@ -31,7 +35,7 @@ GraphicsView::GraphicsView(filament::Engine *engine_)
}

GraphicsView::GLView::GLView(filament::Engine *engine_)
: engine(engine_)
: engine(engine_), zoom(1.0)
{
}

Expand Down Expand Up @@ -96,26 +100,33 @@ void GraphicsView::GLView::first_render()

scene = engine->createScene();
view = engine->createView();
view->setViewport(filament::Viewport(0, 0, 400, 400));
scene->addEntity(renderable);
camera = utils::EntityManager::get().create();
cam = engine->createCamera(camera);
view->setCamera(cam);
view->setScene(scene);

skybox = filament::Skybox::Builder().color({0.1, 0.125, 0.25, 1.0}).build(*engine);
// skybox = filament::Skybox::Builder().color({0.1, 0.125, 0.25, 1.0}).build(*engine);
skybox = filament::Skybox::Builder().color({4.0, 4.0, 4.0, 1.0}).build(*engine);
scene->setSkybox(skybox);
filament::PBRNeutralToneMapper tone_mapper;
auto color_grading = filament::ColorGrading::Builder().toneMapper(&tone_mapper).build(*engine);
view->setColorGrading(color_grading);

renderer = engine->createRenderer();
view->setPostProcessingEnabled(true);
filament::Renderer::ClearOptions co;
co.clear=true;
co.discard=true;
co.clearColor=filament::math::float4({1.0,1.0,0.0,0.5});
// co.clearColor=filament::math::float4({1.0,1.0,0.0,0.5});
co.clearColor=filament::math::float4({1.0,1.0,1.0,1.0});
renderer->setClearOptions(co);
// view->setClearTargets(filament::backend::TargetBufferFlags::COLOR | filament::backend::TargetBufferFlags::DEPTH);
}

bool GraphicsView::GLView::on_render(const Glib::RefPtr< Gdk::GLContext > &context)
//bool GraphicsView::GLView::on_draw(const Cairo::RefPtr<Cairo::Context>& context)
//bool GraphicsView::GLView::on_render(const Glib::RefPtr< Gdk::GLContext > &context)
bool GraphicsView::GLView::on_draw(const Cairo::RefPtr<Cairo::Context>& context)
{
static bool first=true;

Expand All @@ -128,7 +139,9 @@ bool GraphicsView::GLView::on_render(const Glib::RefPtr< Gdk::GLContext > &conte
std::cerr << "first render" << std::endl;
first_render();
}


setup_camera();

if(renderer->beginFrame(swapChain) || true /* always render */) {
std::cerr << "filament rendering" << std::endl;
renderer->render(view);
Expand Down Expand Up @@ -172,3 +185,33 @@ bool GraphicsView::on_button_release_event(GdkEventButton *event)

return true;
}

void GraphicsView::GLView::setup_camera()
{
// Get the width and height of our window, scaled by the pixel ratio
const auto pixel_ratio = 1.0; // FIXME: devicePixelRatio();
const uint32_t w = static_cast<uint32_t>(get_width() * pixel_ratio);
const uint32_t h = static_cast<uint32_t>(get_height() * pixel_ratio);

// Set our view-port size
view->setViewport({0, 0, w, h});

// setup view matrix
const filament::math::float3 eye(0.f, 0.f, 1.f);
const filament::math::float3 target(0.f, 0.f, 0.f);
const filament::math::float3 up(0.f, 1.f, 0.f);
cam->lookAt(eye, target, up);

// setup projection matrix
const float aspect = float(w) / h;
std::cerr << "aspect = " << aspect << ", zoom = " << zoom << std::endl;
cam->setProjection(filament::Camera::Projection::ORTHO,
-aspect * zoom,
aspect * zoom,
-zoom,
zoom,
0,
10.0);
}


11 changes: 8 additions & 3 deletions frontend/gtkmm/GraphicsView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ namespace cadabra {
virtual bool on_button_press_event(GdkEventButton *event) override;
virtual bool on_button_release_event(GdkEventButton *event) override;

class GLView : public /* Gtk::DrawingArea */ Gtk::GLArea {
class GLView :
public Gtk::DrawingArea {
// public Gtk::GLArea {
public:
GLView(filament::Engine *);
virtual bool on_render (const Glib::RefPtr< Gdk::GLContext > &context);
//virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
//virtual bool on_render (const Glib::RefPtr< Gdk::GLContext > &context);
virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;

private:
void first_render();
void setup_camera();

// Filament things. The engine is owned by the NotebookWindow and passed
// in on creation of GraphicsView. The swapchain, on the other hand, is
Expand All @@ -62,6 +65,8 @@ namespace cadabra {
utils::Entity camera;
utils::Entity renderable;
filament::Renderer *renderer;

float zoom;
};

private:
Expand Down
3 changes: 2 additions & 1 deletion frontend/gtkmm/NotebookWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,8 @@ NotebookWindow::NotebookWindow(Cadabra *c, bool ro)
// filament::Engine::Config engineConfig = {};
// engineConfig.stereoscopicEyeCount = 0;
// engineConfig.stereoscopicType = filament::Engine::StereoscopicType::NONE;
filament_engine = filament::Engine::create(filament::Engine::Backend::OPENGL); // VULKAN);
// filament_engine = filament::Engine::create(filament::Engine::Backend::OPENGL);
filament_engine = filament::Engine::create(filament::Engine::Backend::VULKAN);
}

NotebookWindow::~NotebookWindow()
Expand Down

0 comments on commit 96b4841

Please sign in to comment.