Jump to content
  • Advertisement
Sign in to follow this  
MrDoomMaster

OpenGL Anti-Aliasing not working

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Normally in Anti-Aliasing, the pixels which "smooth" the line gradually fade from the color of the line to the back ground color. In my case this does not happen. The smooth factor stays the same color as the line until it just stops. This, in turn, makes my line appear thicker and still aliased. If someone could kindly check why my anti-aliasing isn't working I would greatly appreciate it. I have created my window using the GWL functions, I will paste the code below. Keep in mind that most of the variables here should be obvious, so I won't explain their declaration. This code does not support fullscreen mode at this time:
        RECT WinSize = {0};
        WNDCLASSEX cs = {0};

        WinSize.right   = x;
        WinSize.bottom  = y;

        AdjustWindowRectEx(&WinSize, WS_OVERLAPPEDWINDOW, false, 0);

        cs.cbSize           = sizeof(WNDCLASSEX);
        cs.hCursor          = LoadCursor(0, IDC_ARROW);
        cs.hInstance        = AppInstance;
        cs.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
        cs.lpszClassName    = "OGLIB_WINDOWCLASS";
        cs.lpfnWndProc      = Handler;
        cs.style            = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;

        if(!RegisterClassEx(&cs))
            throw 0;

        if(!(m_hWindow = CreateWindowEx(0, "OGLIB_WINDOWCLASS", Title,
            WS_OVERLAPPEDWINDOW, 50, 50, WinSize.right - WinSize.left,
            WinSize.bottom - WinSize.top, 0, 0, AppInstance, 0)))
        {
            throw 0;
        }

        if(!(m_DC = GetDC(m_hWindow)))
            throw 0;


        // Initialize Display Window
        PIXELFORMATDESCRIPTOR p = {0};

        p.nSize         = sizeof(PIXELFORMATDESCRIPTOR);
        p.nVersion      = 1;
        p.cColorBits    = 24;
        p.cDepthBits    = 24;
        p.dwFlags       = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | (DI.EnableDepth ? PFD_DOUBLEBUFFER : 0);
        p.iPixelType    = PFD_TYPE_RGBA;

        int pixform = 0;
        
        if(!(pixform = ChoosePixelFormat(m_Window->GetHDC(), &p)))
            throw 0;

        if(!SetPixelFormat(m_Window->GetHDC(), pixform, &p))
            throw 0;

        if(!(m_RC = wglCreateContext(m_Window->GetHDC())))
            throw 0;

        if(!wglMakeCurrent(m_Window->GetHDC(), m_RC))
            throw 0;


        // Initialize OpenGL
	    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	    glClearDepth(1.0f);

        //glEnable(GL_ALPHA_TEST);

        if(DI.EnableDepth)
        {
	        glEnable(GL_DEPTH_TEST);
	        glDepthFunc(GL_LEQUAL);
	        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	        glViewport(0, 0, m_Resolution.x, m_Resolution.y);

	        glMatrixMode(GL_PROJECTION);
	        glLoadIdentity();
	        gluPerspective(45.0f, (double)m_Resolution.x / m_Resolution.y, 0.1f, 1000.0f);

	        glMatrixMode(GL_MODELVIEW);
	        glLoadIdentity();
        }

        ShowWindow(m_hWindow, TRUE);
Code used to setup Anti Aliasing:
        // Initialize AA
        glEnable(GL_LINE_SMOOTH);
        glEnable(GL_POLYGON_SMOOTH);

        glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
        glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

Share this post


Link to post
Share on other sites
Advertisement
Sounds like you don't have Blending enabled, glEnable(GL_BLEND). You also need to make sure you have your blending Src and Dst type set correctly.

Share this post


Link to post
Share on other sites
Try increasing the width of the line (glLineWidth()). Depending on how much you increase the width by, it can make it look better, or worse.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mantear
Sounds like you don't have Blending enabled, glEnable(GL_BLEND). You also need to make sure you have your blending Src and Dst type set correctly.



Does this mean I must enable alpha testing as well?

Share this post


Link to post
Share on other sites
You were correct, setting up blending fixed the problem. Here is some code:


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


Now there's still 1 problem. Considering I have no idea what these values do or mean, even after reading MSDN (All the math is confusing, I don't know what to expect from the different flag configurations), I can't tell you what this does. I took it from the examples in MSDN.

Right now the anti aliasing goes from the line color to the background color, which is black. This means polygon anti aliasing turns out funny because the anti aliasing smooths to BLACK always, instead of smoothing to the color BEHIND that specific pixel.

Any way to configure the blending to blend to the color behind the specific pixel? Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by MrDoomMaster
Any way to configure the blending to blend to the color behind the specific pixel?
That's what blending does, but the pixel has to be there already. You must draw in back-to-front order with that blending equation for it to work "correctly." So make sure that everything that is behind the line/polygon/object/whatever that needs blending is rendered before you render that line/polygon/object/whatever.

Share this post


Link to post
Share on other sites
I see.

But, I cannot help that the clear is black. The "background", or clear color, cannot be put on top of my polygon. The polygon is fully red. The blending of the lines in the polygon go from red to black, even the ones that are not on the edge, which makes the polygon to appear as if it has black lines and red faces.

I need a way to restrict anti aliasing in polygons only the lines that compose of the orthogonal edges of the polygon.

Share this post


Link to post
Share on other sites
Quote:
Original post by MrDoomMaster
I see.

But, I cannot help that the clear is black. The "background", or clear color, cannot be put on top of my polygon. The polygon is fully red. The blending of the lines in the polygon go from red to black, even the ones that are not on the edge, which makes the polygon to appear as if it has black lines and red faces.

I need a way to restrict anti aliasing in polygons only the lines that compose of the orthogonal edges of the polygon.
Hmm, I think I'm misunderstanding you. Could you post your rendering code so we can see exactly what you're doing?

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalidor
Quote:
Original post by MrDoomMaster
I see.

But, I cannot help that the clear is black. The "background", or clear color, cannot be put on top of my polygon. The polygon is fully red. The blending of the lines in the polygon go from red to black, even the ones that are not on the edge, which makes the polygon to appear as if it has black lines and red faces.

I need a way to restrict anti aliasing in polygons only the lines that compose of the orthogonal edges of the polygon.
Hmm, I think I'm misunderstanding you. Could you post your rendering code so we can see exactly what you're doing?


Of course!

My code is very simple. I first draw a grid along the ZX plane, with a single line for the Y axis. I then draw my cylinder object.

I also notice that the depth testing isn't working correctly. I'll post the code for my depth testing as well:


// This is the code that initializes the depth testing.
// This does not work correctly because the lines of the
// grid are on TOP of the cylinder even when the cylinder
// is drawn on top of the grid.


glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);



Now my render code:

// First, draw grid along XZ Plane
int NumLines = 50;
double Spacing = 1.0f;
double Begin = NumLines * Spacing;

glBegin(GL_LINES);

SetColor(0, 50, 0); // This function simply calls glColor()
for(double i = -Begin; i <= Begin; i += Spacing)
{
glVertex3d(i, 0, Begin);
glVertex3d(i, 0, -Begin);
glVertex3d(Begin, 0, i);
glVertex3d(-Begin, 0, i);
}

// Draw Axes
SetColor(0, 150, 0);
glVertex3d(Begin, 0, 0);
glVertex3d(-Begin, 0, 0);
glVertex3d(0, Begin, 0);
glVertex3d(0, -Begin, 0);
glVertex3d(0, 0, Begin);
glVertex3d(0, 0, -Begin);

glEnd();

// Now draw the cylinder
SetColor(175, 0, 0);
glPushMatrix();
glTranslated(v1.x, v1.y, v1.z);
gluCylinder(m_QuadObj, .5, .5, 1, 16, 1);
glPopMatrix();

Share this post


Link to post
Share on other sites
Here is a screenshot of the polygon's anti aliasing. It doesn't look proper at all. As you can see, it fades from the color of the polgyon to the color of the background, hence making black lines appear in the polygon:

http://img.photobucket.com/albums/v356/MrDoomMaster/SS000.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!