1

I make OpenGL application that draw cube. I want to add anti aliasing but have no success. Maybe someone can help (User Xcode 6.1.1, GLUT, OpenGL) ?

I need create antialiasing without ay external libraries.

Result of code below. enter image description here

GLfloat xRotated, yRotated, zRotated; void init(void){
glClearColor(0,0,0,0);

glEnable (GL_DEPTH_TEST);

// Turn on antialiasing, and give hint to do the best
// job possible.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_LINEAR);

glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_LINEAR);

glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_LINEAR);

glEnable(GL_MULTISAMPLE); } void DrawCube(void) {
glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0.0,0.0,-10.5);
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);

glBegin(GL_QUADS);        // Draw The Cube Using quads
glColor3f(0.0f,1.0f,0.0f);    // Color Blue
glVertex3f( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f);    // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f);    // Bottom Right Of The Quad (Top)
glColor3f(1.0f,0.5f,0.0f);    // Color Orange
glVertex3f( 1.0f,-1.0f, 1.0f);    // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f);    // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f);    // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f);    // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f,0.0f,0.0f);    // Color Red
glVertex3f( 1.0f, 1.0f, 1.0f);    // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f);    // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f);    // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f);    // Bottom Right Of The Quad (Front)
glColor3f(1.0f,1.0f,0.0f);    // Color Yellow
glVertex3f( 1.0f,-1.0f,-1.0f);    // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f);    // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f);    // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f);    // Bottom Right Of The Quad (Back)
glColor3f(0.0f,0.0f,1.0f);    // Color Blue
glVertex3f(-1.0f, 1.0f, 1.0f);    // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f);    // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f);    // Bottom Right Of The Quad (Left)
glColor3f(1.0f,0.0f,1.0f);    // Color Violet
glVertex3f( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f);    // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f);    // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f);    // Bottom Right Of The Quad (Right)
glEnd();            // End Drawing The Cube

glFlush(); } void animation(void) {
yRotated += 0.1;
xRotated += 0.2;
DrawCube(); } void reshape(int x, int y) {
if (y == 0 || x == 0) return;  //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y);  //Use the whole window for rendering } int main(int argc, char** argv){
glutInit(&argc, argv);
//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
//Set the function for the animation.
glutIdleFunc(animation);
glutMainLoop();
return 0; }
genpfault
  • 51,148
  • 11
  • 85
  • 139
MaeSTRo
  • 355
  • 2
  • 6
  • 18
  • 3
    What's the specific problem you are having? From the picture, it looks like it's working. At least it looks to me like there's some kind of anti-aliasing at the edges of the cube. – Reto Koradi Jan 08 '15 at 16:22
  • `glHint` should be giving an error for doing that, though it makes no difference in the actual output. The accepted values are `GL_DONT_CARE`, `GL_NICEST` and `GL_FASTEST`. You're also using the wrong blending function for polygon smoothing, it should be `GL_SRC_ALPHA_SATURATE, GL_ONE`. – Andon M. Coleman Jan 08 '15 at 16:25
  • https://www.opengl.org/wiki/Multisampling ? – n0rd Mar 02 '15 at 23:33

1 Answers1

5

GLUT lacks the setting of the number of samples. Freeglut does. To enable it, use

glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);

And remember to glEnable it

void enableMultisample(int msaa)
{
    if (msaa)
    {
        glEnable(GL_MULTISAMPLE);
        glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);

        // detect current settings
        GLint iMultiSample = 0;
        GLint iNumSamples = 0;
        glGetIntegerv(GL_SAMPLE_BUFFERS, &iMultiSample);
        glGetIntegerv(GL_SAMPLES, &iNumSamples);
        printf("MSAA on, GL_SAMPLE_BUFFERS = %d, GL_SAMPLES = %d\n", iMultiSample, iNumSamples);
    }
    else
    {
        glDisable(GL_MULTISAMPLE);
        printf("MSAA off\n");
    }   
}
leoly
  • 8,468
  • 6
  • 32
  • 33