How do you enable antialiasing in a Gtk::GLArea?

  antialiasing, c++, gtkmm, opengl

I’m doing some OpenGL within a Gtk::GLArea and I’d like to enable MULTISAMPLING antialiasing.

The docs say:

There are two pieces to rendering with multisampling:

  • Allocating a multisample render target (window or FBO), and
  • Enabling multisample rasterization (i.e. glEnable( GL_MULTISAMPLE ))

While the second step is standardized across all render target types and platforms, the first is only standardized for FBOs (as it is totally internal to GL). When rendering to a window, the allocation method depends on the platform-specific GL integration layer in-use (e.g. WGL, GLX, AGL, etc.). GLUT provides a wrapper around some of these so you don’t have to care.

The following sections describe how to perform MSAA render target allocation (#1) for various render target types and platforms.

I have done part 2. How do I do part 1? Some pseudocode to illustrate where I am:

#include <gtkmm/application.h>

void on_realize() {
  gl_area.make_current();
  if (GLenum res = glewInit(); res != GLEW_OK) {
    std::cerr << glewGetErrorString(res) << std::endl;
    exit(1);
  }
}

bool on_render(const Glib::RefPtr<Gdk::GLContext>& /*context*/) {
  glClearColor(0, 0, 0, 1);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glEnable(GL_MULTISAMPLE_ARB);  // I've tried GL_MULTISAMPLE as well.
  // Draw
}

main() {
  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create();
  Gtk::Window window;
  Gtk::GLArea gl_area;
  gl_area.signal_realize().connect(&on_realize);
  gl_area.signal_render().connect(&on_render);
  window.add(gl_area);
  gl_area.show();
  app->run(window);
}

I was expecting there to be a Gtk::GLArea::set_enable_multisampling(bool) that I could call from on_realize. I’ve checked Gtk::Window and Gdk::GLContext. Neither seem to be antialiasing-aware.

Source: Windows Questions C++

LEAVE A COMMENT