Antialiasing filled polygon with openGL

I'm new to the exciting world of calling opengl from matlab. My filled polygons have
pixelated edges. Enabling 'polygon smooth' doesn't seem to make the difference that it
should. Please help! Thanks.

Here's the relevant bit of my code and the PTB output (pasted below):

Screen('BeginOpenGL', window);
glEnable(GL.BLEND);
glBlendFunc(GL.ONE, GL.SRC_ALPHA);
%glBlendFunc(GL.SRC_ALPHA_SATURATE, GL.ONE);

glClearColor(0, 0, 0, 0);
glClear(GL.COLOR_BUFFER_BIT);

glEnable(GL.POLYGON_SMOOTH);
glHint(GL.POLYGON_SMOOTH_HINT, GL.NICEST);
glDisable(GL.DEPTH_TEST);

for(i=1:length(screenLX))
glColor4f(1, 0, 0, 1);
%glBegin(GL.QUADS);
glBegin(GL.POLYGON);
glVertex2f(screenLX(i, 1), screenLY(i, 1));
glVertex2f(screenLX(i, 2), screenLY(i, 2));
glVertex2f(screenLX(i, 3), screenLY(i, 3));
glVertex2f(screenLX(i, 4), screenLY(i, 4));
glEnd;

glColor4f(0, tex_luminanceR(i), 0, 1);
glBegin(GL.QUADS);
glVertex2f(screenRX(i, 1), screenRY(i, 1));
glVertex2f(screenRX(i, 2), screenRY(i, 2));
glVertex2f(screenRX(i, 3), screenRY(i, 3));
glVertex2f(screenRX(i, 4), screenRY(i, 4));
glEnd;

end;
Screen('EndOpenGL', window);

PTB-INFO: This is the OpenGL-Psychtoolbox version 3.0.8. Type 'PsychtoolboxVersion' for
more detailed version information.
PTB-INFO: Psychtoolbox is licensed to you under terms of the GNU General Public License
(GPL). See file 'License.txt' in the
PTB-INFO: Psychtoolbox root folder for a copy of the GPL license.


PTB-INFO: OpenGL-Renderer is ATI Technologies Inc. :: ATI Radeon 9600 XT OpenGL
Engine :: 1.5 ATI-1.4.18
PTB-INFO: Renderer has 128 MB of VRAM and a maximum 122 MB of texture memory.
PTB-Info: VBL startline = 960 , VBL Endline = 975
PTB-Info: Measured monitor refresh interval from beamposition = 16.717702 ms
[59.816833 Hz].
PTB-Info: Will use beamposition query for accurate Flip time stamping.
PTB-Info: Measured monitor refresh interval from VBLsync = 16.773761 ms [59.616921
Hz]. (298 valid samples taken, stddev=0.969760 ms.)
PTB-Info: Small deviations between reported values are normal and no reason to worry.
PTB-INFO: Support for OpenGL 3D graphics rendering enabled: 24 bit depth-buffer and 8
bit stencil buffer attached.
PTB-INFO: Using OpenGL GL_TEXTURE_RECTANGLE_EXT extension for efficient high-
performance texture mapping...

I'm using a PowerBook G4.
Welcome ;-)

You're (almost) doing the right thing, except usually one
would use ...

glBlendFunc(GL.SRC_ALPHA,GL.ONE_MINUS_SRC_ALPHA);

... for optimal results. The real problem is that polygon
smoothing via glEnable(GL.POLYGON_SMOOTH); isn't
well supported - or not supported at all - on many pieces
of gfx-hardware, so it may have no effect at all.

Luckily there's a new mechanism called multisampling...
<http://www.opengl.org/registry/specs/ARB/multisample.txt>
...which is easier to use and of higher quality, as
long as you're not only drawing smoothed lines
and dots.

Simply set the optional 'multisample' parameter of
Screen('OpenWindow', ...., ); to some high value, e.g., 6.

this will enable anti-aliasing via multisampling for all
2D screen drawing commands. Then you must enable
it as well in your GL code via:

glEnable(GL.MULTISAMPLE_ARB);

for your code. The 'multisample' value is the minimum
number of samples to compute for each final pixel. PTB
will choose the smallest value greater or equal to your
requested sample count, if supported by hardware.

Higher number == higher quality, but more ressource
consumption and lowered frame redraw rates -- this
is compute intense!

N.B. Your example code does exactly what Screen('FillPoly')
would do - just less efficient and general ;-) -- If you'd
only need to draw anti-aliased 2D polygons, that would
be the better way. E.g., it can handle concave polygons,
whereas GL only handles convex polygons correctly...

-mario



--- In psychtoolbox@yahoogroups.com, "erichwgraf" <erichwgraf@...> wrote:
>
> I'm new to the exciting world of calling opengl from matlab. My filled polygons have
> pixelated edges. Enabling 'polygon smooth' doesn't seem to make the difference that it
> should. Please help! Thanks.
>
> Here's the relevant bit of my code and the PTB output (pasted below):
>
> Screen('BeginOpenGL', window);
> glEnable(GL.BLEND);
> glBlendFunc(GL.ONE, GL.SRC_ALPHA);
> %glBlendFunc(GL.SRC_ALPHA_SATURATE, GL.ONE);
>
> glClearColor(0, 0, 0, 0);
> glClear(GL.COLOR_BUFFER_BIT);
>
> glEnable(GL.POLYGON_SMOOTH);
> glHint(GL.POLYGON_SMOOTH_HINT, GL.NICEST);
> glDisable(GL.DEPTH_TEST);
>
> for(i=1:length(screenLX))
> glColor4f(1, 0, 0, 1);
> %glBegin(GL.QUADS);
> glBegin(GL.POLYGON);
> glVertex2f(screenLX(i, 1), screenLY(i, 1));
> glVertex2f(screenLX(i, 2), screenLY(i, 2));
> glVertex2f(screenLX(i, 3), screenLY(i, 3));
> glVertex2f(screenLX(i, 4), screenLY(i, 4));
> glEnd;
>
> glColor4f(0, tex_luminanceR(i), 0, 1);
> glBegin(GL.QUADS);
> glVertex2f(screenRX(i, 1), screenRY(i, 1));
> glVertex2f(screenRX(i, 2), screenRY(i, 2));
> glVertex2f(screenRX(i, 3), screenRY(i, 3));
> glVertex2f(screenRX(i, 4), screenRY(i, 4));
> glEnd;
>
> end;
> Screen('EndOpenGL', window);
>
> PTB-INFO: This is the OpenGL-Psychtoolbox version 3.0.8. Type 'PsychtoolboxVersion'
for
> more detailed version information.
> PTB-INFO: Psychtoolbox is licensed to you under terms of the GNU General Public
License
> (GPL). See file 'License.txt' in the
> PTB-INFO: Psychtoolbox root folder for a copy of the GPL license.
>
>
> PTB-INFO: OpenGL-Renderer is ATI Technologies Inc. :: ATI Radeon 9600 XT OpenGL
> Engine :: 1.5 ATI-1.4.18
> PTB-INFO: Renderer has 128 MB of VRAM and a maximum 122 MB of texture memory.
> PTB-Info: VBL startline = 960 , VBL Endline = 975
> PTB-Info: Measured monitor refresh interval from beamposition = 16.717702 ms
> [59.816833 Hz].
> PTB-Info: Will use beamposition query for accurate Flip time stamping.
> PTB-Info: Measured monitor refresh interval from VBLsync = 16.773761 ms [59.616921
> Hz]. (298 valid samples taken, stddev=0.969760 ms.)
> PTB-Info: Small deviations between reported values are normal and no reason to worry.
> PTB-INFO: Support for OpenGL 3D graphics rendering enabled: 24 bit depth-buffer and
8
> bit stencil buffer attached.
> PTB-INFO: Using OpenGL GL_TEXTURE_RECTANGLE_EXT extension for efficient high-
> performance texture mapping...
>
> I'm using a PowerBook G4.
>