2

For some time I was trying to enable multisampling on QGLWidget in order to smooth endges of cuboids that I render but with no success. Here are the code parts that do the work.

1)I am passing a specified format to constructor of GLWidget (which inherits QGLWidget) in order to enable multisampling and set number of samples

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  QGLFormat glFormat;
  glFormat.setSampleBuffers(true);
  glFormat.setSamples(4);

  widget = new GLWidget(glFormat, ui->centralWidget);
  widget->setObjectName(QStringLiteral("widget"));
  ui->horizontalLayout->addWidget(widget);
}

GLWidget *widget is a member of mainwindow class.

2) I am enabling multisampling in initializeGL() method and test if multisampling works

void GLWidget::initializeGL()
{
  glClearColor(0,0,0,0);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_MULTISAMPLE);
  glEnable(GL_COLOR_MATERIAL);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_NORMALIZE);

  GLint bufs;
  GLint samples;
  glGetIntegerv(GL_SAMPLE_BUFFERS, &bufs);
  glGetIntegerv(GL_SAMPLES, &samples);
  qDebug("Have %d buffers and %d samples", bufs, samples);
}

Even though setSamples is set to 4 qDebug prints

Have 1 buffers and 8 samples

Nothing actually happens the cuboids still have jagged edges, lines are not smooth as well

3) Here is how cuboids are rendered

void GLWidget::placeBox(Dimension d, Position p)
{
  Position normal, v1, v2;

glColor3f(1, 0, 0);
glLineWidth(1.5);
glBegin(GL_LINE_LOOP);
    glVertex3f(p.x, p.y, p.z);
    glVertex3f(p.x, p.y+d.h, p.z);
    glVertex3f(p.x, p.y+d.h, p.z+d.w);
    glVertex3f(p.x, p.y, p.z+d.w);
glEnd();

glBegin(GL_LINE_LOOP);
    glVertex3f(p.x+d.l, p.y, p.z);
    glVertex3f(p.x+d.l, p.y+d.h, p.z);
    glVertex3f(p.x+d.l, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y, p.z+d.w);
glEnd();

glBegin(GL_LINE_LOOP);
    glVertex3f(p.x, p.y+d.h, p.z);
    glVertex3f(p.x, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y+d.h, p.z);
glEnd();

glBegin(GL_LINE_LOOP);
    glVertex3f(p.x, p.y, p.z);
    glVertex3f(p.x, p.y, p.z+d.w);
    glVertex3f(p.x+d.l, p.y, p.z+d.w);
    glVertex3f(p.x+d.l, p.y, p.z);
glEnd();

glBegin(GL_LINE_LOOP);
    glVertex3f(p.x, p.y, p.z+d.w);
    glVertex3f(p.x, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y, p.z+d.w);
glEnd();

glBegin(GL_LINE_LOOP);
    glVertex3f(p.x, p.y, p.z);
    glVertex3f(p.x, p.y+d.h, p.z);
    glVertex3f(p.x+d.l, p.y+d.h, p.z);
    glVertex3f(p.x+d.l, p.y, p.z);
glEnd();

// N = cross( (V1 = gl - gd), (V2 = dl - gd) )
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
    v1 = {0, 0, -d.w};
    v2 = {0, -d.h, -d.w};
    normal = crossProduct(v1,v2);
    glNormal3f(normal.x, normal.y, normal.z);
    glVertex3f(p.x, p.y, p.z);
    glVertex3f(p.x, p.y+d.h, p.z);
    glVertex3f(p.x, p.y+d.h, p.z+d.w);
    glVertex3f(p.x, p.y, p.z+d.w);

    v1 = {0, 0, d.w};
    v2 = {0,-d.h, d.w};
    normal = crossProduct(v1,v2);
    glNormal3f(normal.x, normal.y, normal.z);
    glVertex3f(p.x+d.l, p.y, p.z);
    glVertex3f(p.x+d.l, p.y+d.h, p.z);
    glVertex3f(p.x+d.l, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y, p.z+d.w);

    v1 = {-d.l, 0, 0};
    v2 = {0, 0, d.w};
    normal = crossProduct(v1,v2);
    glNormal3f(normal.x, normal.y, normal.z);
    glVertex3f(p.x, p.y+d.h, p.z);
    glVertex3f(p.x, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y+d.h, p.z);

    v1 = {d.l, 0, 0};
    v2 = {d.l, 0, d.w};
    normal = crossProduct(v1,v2);
    glNormal3f(normal.x, normal.y, normal.z);
    glVertex3f(p.x, p.y, p.z);
    glVertex3f(p.x, p.y, p.z+d.w);
    glVertex3f(p.x+d.l, p.y, p.z+d.w);
    glVertex3f(p.x+d.l, p.y, p.z);

    v1 = {-d.l, 0, 0};
    v2 = {-d.l, -d.h, 0};
    normal = crossProduct(v1, v2);
    glNormal3f(normal.x, normal.y, normal.z);
    glVertex3f(p.x, p.y, p.z+d.w);
    glVertex3f(p.x, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y+d.h, p.z+d.w);
    glVertex3f(p.x+d.l, p.y, p.z+d.w);

    v1 = {d.l, 0, 0};
    v2 = {d.l, -d.h, 0};
    normal = crossProduct(v1, v2);
    glNormal3f(normal.x, normal.y, normal.z);
    glVertex3f(p.x, p.y, p.z);
    glVertex3f(p.x, p.y+d.h, p.z);
    glVertex3f(p.x+d.l, p.y+d.h, p.z);
    glVertex3f(p.x+d.l, p.y, p.z);
glEnd();
}

GL_LINE_LOOP is used to create frames around cuboids and GL_QUADS for cuboid faces

I am working under Linux Mint, using Radeon HD 7790.

4

0 回答 0