OpenGL antialiasing with freeglut
Asked Answered
U

3

6

I am using freeglut. I'm trying to get FSAA working, but nothing seems to work. Sample buffers is 1 and Samples is 4. But I'm not seeing any anti-aliasing. Am I missing something? Currently, I am running Ubuntu 12.04; not sure if that changes anything.

#include <GL/glut.h>
#include <GL/glext.h>
#include <stdio.h>


void render(void);

int main(int argc, char **argv){    
    glutInit(&argc,argv);

    //Initialize the window
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_MULTISAMPLE);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(200,200);
    glutCreateWindow("Testing");
    glutDisplayFunc(render);

    //Enable FSAA       
    glEnable(GL_MULTISAMPLE);

    //2D graphics
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT), 0, 0, 1);
    glMatrixMode(GL_MODELVIEW);


    GLint buf, sbuf;
    glGetIntegerv(GL_SAMPLE_BUFFERS, &buf);
    printf("number of sample buffers is %d\n", buf);
    glGetIntegerv(GL_SAMPLES, &sbuf);
    printf("number of samples is %d\n", sbuf);

    glutMainLoop();
    return 0;
}

//Draw some stuff
void render(void){  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glColor3f(0.0f,1.0f,0.0f);
    glBegin(GL_LINE_LOOP);
        glVertex2f(10.0,10.0);
        glVertex2f(170.0,60.0);
        glVertex2f(50.0,130.0);
        glVertex2f(50.0,60.0);
    glEnd();    

    glutSwapBuffers();
}

I am well aware of SDL and GLFW. I'd like to get it working in freeglut though.

More info:
Graphics card:ATI Radeon HD 4250
OpenGL version: 3.3.11627 Compatibility Profile Context

Unstable answered 2/6, 2012 at 18:30 Comment(0)
B
0

To antialiase your points and lines in 2D, you can also do this:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH, GL_NICEST);

glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH, GL_NICEST);
Bontebok answered 2/6, 2012 at 20:3 Comment(4)
This doesn't seem to work for GL_TRIANGLE_FAN or GL_QUAD_STRIP. Isn't multisampling supposed to antialias those?Unstable
in your code you just draw a line loop, which should be antialiased with my code.Bontebok
Well, that was just an example. I intend to do more advanced graphics than just lines. I really just want to get FSAA working.Unstable
oh sorry. you didn't mention that :)Bontebok
D
0

Your code did work properly on my computer (running Windows). Just make sure that you are using freeglut and not just regular GLUT. Thus, you should be linking to the properly library files (like freeglut.lib/freeglut.dll on windows). Additionally, it would not be a bad idea to include freeglut.h instead of glut.h to be sure that you are actually including the header file you intend to.

Also, there is no need to call glClearColor every frame. You only have to call it when you want to change the clear color. Just remember though, like glColor, glClearColor only applies to function calls that occur after it.

Deadening answered 5/6, 2012 at 19:47 Comment(2)
Hmmm. Do I need to set a different build option than "-lglut"?Unstable
@fintelia: Actually calling glClearColor every frame is good style. Eventually one wants to implement fancier rendering techniques that involve multiple passes or FBOs and once you do that, you have to make sure that at the beginning of rendering a frame everything is set properly.Sonasonant
A
0

GLUT on my Windows 7 NVIDIA is somehow not working either. I got 0 for both Sample buffers and Samples.

However, freeglut solves it and gives you the option to set Samples.

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

To turn it on

void glexMultisample(int msaa)
{
    if (msaa)
    {
        glEnable(GL_MULTISAMPLE);

        // 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");
    }   
}
Albertype answered 2/3, 2015 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.