depth_offscreen_render.cpp 2.56 KB
Newer Older
1
#include <scigl_render/check_gl_error.hpp>
2
#include <scigl_render/example/depth_offscreen_render.hpp>
Tim Übelhör's avatar
Tim Übelhör committed
3
4
5
6

namespace scigl_render
{
// I know there is a lot of configuration but it is intendet to be flexible
7
8
9
10
11
12
DepthOffscreenRender::DepthOffscreenRender(std::shared_ptr<GLContext> context,
                                           std::shared_ptr<Texture2D> texture,
                                           size_t pixel_size)
    : rasterizer(context->get_width() / 2, context->get_height() / 2,
                 2, 2),
      image_buffer(context->get_width() * context->get_height() *
Tim Übelhör's avatar
Tim Übelhör committed
13
                   pixel_size),
14
15
      gl_context(context),
      fbo_texture(texture)
Tim Übelhör's avatar
Tim Übelhör committed
16
{
17
  framebuffer = std::make_shared<FrameBuffer>(fbo_texture);
Tim Übelhör's avatar
Tim Übelhör committed
18
  tex_reader.reset(new TextureReader(fbo_texture, pixel_size));
19
20
21
22
23
  // create second texture to copy the data to
  quad_texture = std::make_shared<Texture2D>(
      fbo_texture->get_width(), fbo_texture->get_height(),
      fbo_texture->get_format(), fbo_texture->get_internal_format(),
      fbo_texture->get_type());
Tim Übelhör's avatar
Tim Übelhör committed
24
25
}

26
27
28
29
void DepthOffscreenRender::next_frame(
    std::shared_ptr<DepthSimulator> depth_simulator,
    const CartesianPose &model_pose,
    const CartesianPose &camera_pose)
Tim Übelhör's avatar
Tim Übelhör committed
30
{
31
  using namespace std::placeholders;
Tim Übelhör's avatar
Tim Übelhör committed
32
  check_gl_error("next frame begin");
33
  // render to cleared fbo
34
35
  framebuffer->clear();
  framebuffer->activate();
36
  check_gl_error("activated fbo");
Tim Übelhör's avatar
Tim Übelhör committed
37
38
39
40
41
42
43
44
45
46
47
  // render 4 poses via rasterization
  for (int i = 0; i < 4; i++)
  {
    auto m_pose = model_pose;
    m_pose.orientation.x += i * M_PI_2;
    rasterizer.activate_view(i);
    depth_simulator->render_pose(m_pose.to_quaternion_pose(),
                                 camera_pose.to_quaternion_pose());
  }
  // back to default
  rasterizer.activate_all();
48
  framebuffer->deactivate();
49

Tim Übelhör's avatar
Tim Übelhör committed
50
51
  // read data from texture
  tex_reader->start_read();
52
  check_gl_error("asynchronous reading to pbo");
Tim Übelhör's avatar
Tim Übelhör committed
53
  auto data = tex_reader->do_read();
54
  check_gl_error("synchronous reading from pbo");
55
56

  // display the data on screen
57
  display_data(data);
Tim Übelhör's avatar
Tim Übelhör committed
58
59
  tex_reader->end_read();
  tex_reader->swap_buffers();
60
  check_gl_error("end synchronous reading");
Tim Übelhör's avatar
Tim Übelhör committed
61
62
}

63
void DepthOffscreenRender::display_data(const void *data)
Tim Übelhör's avatar
Tim Übelhör committed
64
65
{
  // Test copying the data, buffer_size is in bytes
66
  memcpy(image_buffer.data(), data, image_buffer.size());
67
  quad_texture->store_image(data);
68
  // draw the copy
Tim Übelhör's avatar
Tim Übelhör committed
69
70
  glClearColor(0, 0, 0, 0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71
  texture_render.draw(quad_texture);
72
  check_gl_error("drawing fullscreen quad");
Tim Übelhör's avatar
Tim Übelhör committed
73
74
75
76
  // update
  glfwSwapBuffers(gl_context->get_window());
}
} // namespace scigl_render