I’d like to share a simple tip on enabling anti aliased line drawing in OpenGL. I hope you’ve basic understanding of OpenGL and how to use it.
To smooth a line, you will have to enable 3 OpenGL capabilities.
- Enable line smooth option (GL_LINE_SMOOTH)
- Enable Blending( GL_BLEND)
- Specify pixel arithmetic(GL_SRC_ALPHA)
Draw the lines after enabling the above properties. See the sample below.
// Function to enable smoothing
void EnableSmoothing( bool bEnable )
{
if( bEnable )
{
// Smooth out lines
glEnable( GL_LINE_SMOOTH );
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
// Enable Blending
glEnable(GL_BLEND);
// Specifies pixel arithmetic
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
printf( "\nEnabled" );
}
else // if the options are disabled
{
glDisable(GL_LINE_SMOOTH); // Smooth out lines
glDisable(GL_BLEND);
printf( "\nDisabled" );
}
}
// Render function
void RenderScene()
{
glClear( GL_COLOR_BUFFER_BIT );
glRotatef( z, 0, 0, 1 );
/* Draw line for testing */
glBegin( GL_LINES );
{
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex2f( -0.75f, 0.25f );
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex2f( 0.75f,-0.25f );
}
glEnd();
glutSwapBuffers();
}
See the images before and after conversion. Click on the image to see it in original size.
I’m also attaching a working sample source written using GLUT.
// LineSmoothSample.cpp : How to smooth line using OpenGL
#include "stdafx.h"
#include <windows.h>
#include <gl/glut.h>
// z rotation angle
GLfloat z = 0.0f;
// Function to enable smoothing
void EnableSmoothing( bool bEnable )
{
if( bEnable )
{
// Smooth out lines
glEnable( GL_LINE_SMOOTH );
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
// Enable Blending
glEnable(GL_BLEND);
// Specifies pixel arithmetic
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
printf( "\nEnabled" );
}
else // if the options are disabled
{
glDisable(GL_LINE_SMOOTH); // Smooth out lines
glDisable(GL_BLEND);
printf( "\nDisabled" );
}
}
// Render function
void RenderScene()
{
glClear( GL_COLOR_BUFFER_BIT );
glRotatef( z, 0, 0, 1 );
/* Draw line for testing */
glBegin( GL_LINES );
{
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex2f( -0.75f, 0.25f );
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex2f( 0.75f,-0.25f );
}
glEnd();
glutSwapBuffers();
}
// Initial setup for rendering context
void SetupRC()
{
glOrtho( -1,1,-1,1,-1,1);
glClearColor( 0,0,0,1);
glLineWidth( 5 );
// Enable smoothing by default
EnableSmoothing( true );
}
// Function to handle change in window size
void ChangeSize( GLsizei w, GLsizei h )
{
glViewport(0,0,w,h);
}
// Keyboard callback
void SpecialKey(unsigned char key, int x, int y)
{
if( ' ' == key )
{
static bool bEnable = true;
bEnable = !bEnable;
EnableSmoothing( bEnable );
}
else if( 'w' == key )
{
z += 0.01f;
if( z >= 360.0f )
z = 0;
}
else
return;
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 800,600 );
glutCreateWindow( "Line Smooth Sample");
glutReshapeFunc( ChangeSize );
glutDisplayFunc( RenderScene);
glutKeyboardFunc( SpecialKey );
SetupRC();
glutMainLoop();
return 0;
}