Antialias in OpenGL

BlitzMax Forums/BlitzMax Module Tweaks/Antialias in OpenGL

JoshK(Posted 2006) [#1]
I modified BRL.graphics and BRL.glgraphics to handle antialiasing in OpenGL. Thanks to "John J." for handling most of the C programming.

First, open BRL.graphics ("graphics.bmx") and add these flags to the beginning. These are flags you can pass to SetGraphicsDriver(). If you don't want to modify BRL.graphics, you can just declare these in your main program:
Const GRAPHICS_MULTISAMPLE2X=	$40
Const GRAPHICS_MULTISAMPLE4X=	$80
Const GRAPHICS_MULTISAMPLE8X=	$100
Const GRAPHICS_MULTISAMPLE16X=	$200


Now replace the file "BRL.mod\glgraphics.mod\glgraphics.win32.c" with this:


Recompile BRL.graphics and BRL.glgraphics modules.

To set Antialias in OpenGL, use this:
SetGraphicsDriver GLGraphicsDriver(),GRAPHICS_BACKBUFFER+GRAPHICS_MULTISAMPLE2X

You will need to recreate any graphics windows or canvases for the change to take effect. For a seamless transition, try creating the new canvas before deleting the old one. The user will never suspect anything happened.

To turn antialiasing off, just call this command and rebuild the graphics window or canvas:
SetGraphicsDriver GLGraphicsDriver(),GRAPHICS_BACKBUFFER



Filax(Posted 2006) [#2]
Nice tweak :) !


Gavin Beard(Posted 2006) [#3]
is it only possible to change aa level by rebuild gfx window or canvas?

Excellent tweak thx


JoshK(Posted 2006) [#4]
Yes, you have to rebuild the graphics to change between multisample levels. However, I believe you can use glEnable GL_MULTISAMPLE to turn antialias on and off.

This is an annoying but unavoidable caveat of OpenGL. Trust me, I researched it a lot!


ninjarat(Posted 2006) [#5]
do i have to apply aa using glEnable (GL_LINE_SMOOTH), or is all automatic with the aa on the setgraphicsdriver command?


JoshK(Posted 2006) [#6]
You might have to use glEnable GL_MULTISAMPLE_EXT. You don't need to use LINE_SMOOTH or anything like that; that's a completely different (and crap) method.


Filax(Posted 2006) [#7]
The same thing is possible with DirectX ?
because vista seem incompatible with
opengl and vista is coming soon ... ?


ziggy(Posted 2007) [#8]
Vista is compatible with OpenGL, and it will not be emulated as it was planned by Microsoft. It will support native OpenGL in full screen mode and in windowed mode.


kfprimm(Posted 2007) [#9]
Yeah, I got Vista last friday. I runs OpenGL just fine.


Barbapapa(Posted 2007) [#10]
I was wondering why this module tweak isn't implemented in the original module. I would need AA but haven't done any module tweaks, because they are lost every time I sync.


Dreamora(Posted 2007) [#11]
Because there is no such thing for the DX7 side, it would need a DX9 replacement ... (at least I assume so)


Barbapapa(Posted 2007) [#12]
Yes but this AA is about OpenGL only, so it's multiplatform, it's not for DirectX, so two different pair of shows, or am I missing something?


Dreamora(Posted 2007) [#13]
Yes you miss the most obvious point. It must be crossplattform and full Max2D compatible and thus DX would need to have it as well, as Max2D = OpenGL AND DX, not OpenGL only.
As GLGraphics is Max2D Core module for OpenGL this directly influences Max2D just in case this question would be the next :)
(but as mentioned, this is only an assumption why it didn't get included into the official modules)


Gavin Beard(Posted 2007) [#14]
Hi

Just tried building this update into max 1.24 and it compiles fine. however when ever i run a$ program i get an error : wglChoosePixelFormatARB() failed. what causes this?

thx


JoshK(Posted 2007) [#15]
I don't know, post your program's code.


SofaKng(Posted 2007) [#16]
I'm getting the same "wglChoosePixelFormatARB()" error...

The modules re-compile just fine, but when I try to run a simple program like this:

SetGraphicsDriver GLGraphicsDriver(),GRAPHICS_BACKBUFFER+GRAPHICS_MULTISAMPLE2X
Graphics 640, 480
Cls
Flip
WaitKey

...it displays that error. :(


JoshK(Posted 2007) [#17]
I updated the code to work with the latest version of brl.glgraphics.

Multisample mode is set by choosing a pixel format with a special function, wglChoosePixelFormat(). But the pointer to that function can not be retrieved until an OpenGL window is created. Therefore multisample mode can not be set on the first graphics context created. Fortunately, BRL's Graphics command set provides a way to create a dummy window. I have added a GRAPHICS_HIDDEN flag so you can create a hidden graphics window, and the end user never has a clue it's there.

This will use a hidden context to initialize OpenGL, and then create a multisampled graphics context. You can then use glEnable/Disable GL_MULTISAMPLE to toggle antialias mode, but it should be activated by default.
SetGraphicsDriver GLGraphicsDriver()
SetGraphics CreateGraphics(400,300,0,60,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER|GRAPHICS_HIDDEN)
SetGraphics CreateGraphics(1024,768,0,60,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER|GRAPHICS_MULTISAMPLE4X)


Don't forget to declare the multisample constants somewhere (preferably in brl.graphics):
	GRAPHICS_MULTISAMPLE2X=	$40
	GRAPHICS_MULTISAMPLE4X=	$80
	GRAPHICS_MULTISAMPLE8X=	$100
	GRAPHICS_MULTISAMPLE16X=$200
	GRAPHICS_HIDDEN=	$400


glgraphics.win32.c:



Kryzon(Posted 2010) [#18]
Sorry to bump this thread.

Regarding the need to create a dummy window. Is there a way to 'kill' the hidden dummy window after you created your official graphics window with multisample?

Also, every time you want to change the sample amount (if the player was changing AA options in the game menu, for instance), do you need to create that dummy window again, or just the first time you start your application?
In other words, is the pointer to wglChoosePixelFormat() kept when you delete your official graphics window to create a different one with a different sample value?


void(Posted 2012) [#19]
Ready to use modules here
http://www.leadwerks.com/werkspace/topic/5143-blitzmax-arb-fsaa-ready-to-use-modules-real-antialias/