Skip to content

RGB Sobel filter

Romain Milbert edited this page Dec 8, 2022 · 3 revisions

⚠️ This page is a work in progress; the code presented here may not work as expected, or at all.


As the time of writing (2022-12-08, commit b3646d3), the following files give the result shown in this video:

RaZ Playground - RGB Sobel filter

main.cpp

#include <RaZ/Application.hpp>
#include <RaZ/Data/Mesh.hpp>
#include <RaZ/Data/ObjFormat.hpp>
#include <RaZ/Math/Transform.hpp>
#include <RaZ/Render/Camera.hpp>
#include <RaZ/Render/GaussianBlurRenderProcess.hpp>
#include <RaZ/Render/Light.hpp>
#include <RaZ/Render/Material.hpp>
#include <RaZ/Render/MeshRenderer.hpp>
#include <RaZ/Render/RenderSystem.hpp>
#include <RaZ/Render/SobelFilterRenderProcess.hpp>

using namespace Raz::Literals;
using namespace std::literals;

int main() {
  Raz::Application app;
  Raz::World& world = app.addWorld(6);

  auto& renderSystem = world.addSystem<Raz::RenderSystem>(1280, 720, "RaZ");

  Raz::Window& window = renderSystem.getWindow();
  window.addKeyCallback(Raz::Keyboard::ESCAPE, [&app] (float) noexcept { app.quit(); });
  window.setCloseCallback([&app] () noexcept { app.quit(); });

  Raz::Entity& camera = world.addEntity();
  auto& cameraComp    = camera.addComponent<Raz::Camera>(window.getWidth(), window.getHeight());
  auto& cameraTrans   = camera.addComponent<Raz::Transform>(Raz::Vec3f(0.f, 5.f, 20.f));

  Raz::Entity& mesh = world.addEntity();
  mesh.addComponent<Raz::MeshRenderer>(Raz::ObjFormat::load("crytek_sponza.obj").second);
  mesh.addComponent<Raz::Transform>(Raz::Vec3f(0.f), Raz::Quaternionf(90_deg, Raz::Axis::Y), Raz::Vec3f(0.04f));

  world.addEntityWithComponent<Raz::Light>(Raz::LightType::DIRECTIONAL, Raz::Axis::X, 1.f);
  world.addEntityWithComponent<Raz::Light>(Raz::LightType::DIRECTIONAL, -Raz::Axis::X, 1.f);
  world.addEntityWithComponent<Raz::Light>(Raz::LightType::DIRECTIONAL, Raz::Axis::Z, 1.f);
  world.addEntityWithComponent<Raz::Light>(Raz::LightType::DIRECTIONAL, -Raz::Axis::Z, 1.f);

  Raz::RenderGraph& renderGraph = renderSystem.getRenderGraph();
  Raz::RenderPass& geometryPass = renderSystem.getGeometryPass();

  const auto depthBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::DEPTH);
  const auto colorBuffer = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);

  geometryPass.setWriteDepthTexture(depthBuffer);
  geometryPass.addWriteColorTexture(colorBuffer, 0);

  auto& blur = renderGraph.addRenderProcess<Raz::GaussianBlurRenderProcess>();
  blur.setInputBuffer(colorBuffer);
  blur.addParent(geometryPass);

  const auto colorBuffer2 = Raz::Texture2D::create(window.getWidth(), window.getHeight(), Raz::TextureColorspace::RGB);

  auto& sobel = renderGraph.addRenderProcess<Raz::SobelFilterRenderProcess>();
  sobel.setInputBuffer(colorBuffer2);
  sobel.addParent(blur);
  blur.setOutputBuffer(colorBuffer2);

  app.run();

  return EXIT_SUCCESS;
}
Clone this wiki locally