Well,when I read OpenGL Programming Guide(the fifth Edition),I found something very interesting about how to use multisample easily with the help of glut libary.It said that the only thing I have to do is add the GLUT_MULTISAMPLE flag to the initialization flag of glutInitDisplayMode function.However,when I tried what the book said,the multisample didn't seem to work.My display card is Nvidia 7600GT,so the hardware limitation is not a problem.To find out what happened ,I read glut specification very carefully.OK,it said glut's multisample function would be of cource working if your display device supported GLX_SAMPLE_SGIS extension.I checked the OpenGL extensions supported by my display card,well,it did'nt support this rare extention.But my device really supported multisample,so I must have some way to force glut implement multisample for me.
As every OpenGL programmer on Win32 platform knows,whether a window support multisample depends on how it is selected ,or say,the exact number of second parameter of SetPixelFormat function.To determine the number of this parameter,we must invoke the ChoosePixelFormat or wglChoosePixelFormatARB to see what it return.But wglChoosePixelFormatARB is a extend OpenGL function of Win32 platform,we can't get it address in address space until we create a window and its a resource context ,and make the device context of the window and resource context in the current state.In other word,if we want OpenGL work properly on the Win32 platform,we have to use ChoosePixelFormat and SetPixelFormat once or more.Glut libary must also invoke ChoosePixelFormat function to select a window it want,and use its return value as the second parameter of SetPixelFormat function.So we can use a platform dependent method to force the ChoosePixelFormat function to return a value which will both enable multisample and have no effect on other features we wanted.Its name is API Hook.
Well,when some of us hear API Hook,they think that is a method to hack or to crack,and is a method that few horrible virus take the advantage of.But here I will use it properly to force glut support multisample in Win32 platform.I won't use this method to hook API globally,which will affect the entire operating system.Because it's not necessary and very dangerous.Why not just add some tiny code in each program you write,that won't increase much space and make it slower.My thinking is that my code will just affect the IAT(Import Address Table) of glut in the address space of the process of my program,and it will restore the IAT when the job is done.In c++ programming,a c++ object to manage this is a wise choise,which will modify the IAT when it's constructed,and restore the IAT when it's destructored.
I implement my thinking and it works well,as the photoes showed below.
My API_HOOK code is shown below:
HMODULE hGlut=GetModuleHandle(_T("gdi32.dll"));
if(hGlut==0)
throw Exception_GLUT_Not_Ready();
if( (this->GetSuper()=(PFNChoosePixelFormat) GetProcAddress(hGlut,"ChoosePixelFormat"))==0)
throw Exception_Thunk_Not_Found();
PBYTE pBase=(PBYTE) GetModuleHandle(_T("glut32.dll"));
PIMAGE_DOS_HEADER pDos=(PIMAGE_DOS_HEADER)pBase;
if(pDos->e_magic!=IMAGE_DOS_SIGNATURE)
throw Exception_Unknow(__LINE__);
PIMAGE_NT_HEADERS pNT=(PIMAGE_NT_HEADERS)(pBase+pDos->e_lfanew);
if(pNT->Signature!=IMAGE_NT_SIGNATURE)
throw Exception_Unknow(__LINE__);
PIMAGE_IMPORT_DESCRIPTOR pImort=(PIMAGE_IMPORT_DESCRIPTOR)
(pBase+pNT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
for(;pImort->Characteristics;pImort++)
{
if(strcmpi( (char*)(pImort->Name+pBase),"gdi32.dll")==0)
{
PIMAGE_THUNK_DATA pThunk=(PIMAGE_THUNK_DATA)(pBase+pImort->FirstThunk);
for(;pThunk->u1.Function;pThunk++)
{
if(pThunk->u1.Function==(DWORD)GetSuper())
{
DWORD flag;
MEMORY_BASIC_INFORMATION mi;
VirtualQuery(pThunk,&mi,sizeof(MEMORY_BASIC_INFORMATION));
if(!VirtualProtect(mi.BaseAddress,mi.RegionSize,PAGE_READWRITE,&flag))
throw Exception_Unknow(__LINE__);
pThunk->u1.Function=(DWORD)this->MyChoosePixelFormat;
if(!VirtualProtect(mi.BaseAddress,mi.RegionSize,flag,&flag))
throw Exception_Unknow(__LINE__);
break;
}
}
break;
}
}