Help end child hunger
Nov 232012
 

Both FreeGLUT and GLUT allow us to define an OpenGL context with multisampling. However the number of samples is fixed (4) and I’ve not found a way to change it using the API.

In here we’re going to see how to hack FreeGLUT so that we can change the default number of samples. This can be achieved either by changing the default value, or by adding a new function to set this value.

Note: This hack should be used only for testing purposes, not for redistribution, as FreeGLUT has a large base of users which already have the official version installed.

A simple solution is to simply change the number of samples in the code. In the source code, in file “freeglut_init.c”, there is a data structure that holds the definitions for creation of the OpenGL context. Some of these values can be altered through the API, but not the number of samples.

Bellow is the relevant part of the file, with the number of samples highlighted.

/*
 * The settings for the current freeglut session
 */
SFG_State fgState = { { -1, -1, GL_FALSE },  /* Position */
                      { 300, 300, GL_TRUE }, /* Size */
                      GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH,  /* DisplayMode */
                      GL_FALSE,              /* Initialised */
                      GLUT_TRY_DIRECT_CONTEXT,  /* DirectContext */
                      GL_FALSE,              /* ForceIconic */
                      GL_FALSE,              /* UseCurrentContext */
                      GL_FALSE,              /* GLDebugSwitch */
                      GL_FALSE,              /* XSyncSwitch */
                      GLUT_KEY_REPEAT_ON,    /* KeyRepeat */
                      INVALID_MODIFIERS,     /* Modifiers */
                      0,                     /* FPSInterval */
                      0,                     /* SwapCount */
                      0,                     /* SwapTime */
                      0,                     /* Time */
                      { NULL, NULL },         /* Timers */
                      { NULL, NULL },         /* FreeTimers */
                      NULL,                   /* IdleCallback */
                      0,                      /* ActiveMenus */
                      NULL,                   /* MenuStateCallback */
                      NULL,                   /* MenuStatusCallback */
                      { 640, 480, GL_TRUE },  /* GameModeSize */
                      16,                     /* GameModeDepth */
                      72,                     /* GameModeRefresh */
                      GLUT_ACTION_EXIT,       /* ActionOnWindowClose */
                      GLUT_EXEC_STATE_INIT,   /* ExecState */
                      NULL,                   /* ProgramName */
                      GL_FALSE,               /* JoysticksInitialised */
                      0,                      /* NumActiveJoysticks */
                      GL_FALSE,               /* InputDevsInitialised */
                      0,                      /* MouseWheelTicks */
                      1,                      /* AuxiliaryBufferNumber */
                      4,                      /* SampleNumber */
                      1,                      /* MajorVersion */
                      0,                      /* MinorVersion */
                      0,                      /* ContextFlags */
                      0,                      /* ContextProfile */
                      NULL,                   /* ErrorFunc */
                      NULL                    /* WarningFunc */
};

We could simply modify the value from 4 to 16 for instance, and get a context with 16 samples, instead of the default 4. Just recompile freeGLUT and we’re done.

However, this would imply that every time we run an OpenGL-freeGLUT application we have 16 samples whether we need/want them or not.

A better solution is to add a function to the API, having as an argument the number of requested samples.

The code for our new function is pretty simple, as it just sets one of the members of the window initialization structure.

void FGAPIENTRY glutSetSamples(unsigned int n) {

	fgState.SampleNumber = n;
}

We’re going to add the signature of this function to the header file that contains the initialization functions “freeglut_ext.h”, placing it near the other initialization functions. Search the header file for:

/*
 * Initialization functions, see freeglut_init.c
 */

And add the following line below it :

FGAPI void		FGAPIENTRY glutSetSamples(unsigned int n);

The source code for the function will be added to the file “freeglut_init.c”. Again to place the function near the other interface functions, look for the following comment on the file:

/* -- INTERFACE FUNCTIONS -------------------------------------------------- */

And add the new function below it. Recompile and that’s it.

Usage is pretty straightforward, just call the new function before glutCreateWindow. Here goes an example:

int main(int argc, char **argv) {

//  GLUT initialization
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA|GLUT_MULTISAMPLE);

	glutInitContextVersion (4, 2);
	glutInitContextProfile (GLUT_CORE_PROFILE );
	glutInitContextFlags(GLUT_DEBUG);

	glutInitWindowPosition(100,100);
	glutInitWindowSize(512,512);
	glutSetSamples(16);
	glutCreateWindow("An OpenGL window with 16 samples!");
	...

Oh, and don’t forget to set the flag GLUT_MULTISAMPLE in glutInitDisplayMode.

The images below show the result with the default number of samples, 4, on the left, and with 16 samples on the right.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: