|
Table of Contents
Main screen functionsmglOpen: Opens the screen
usage: mglOpen(whichScreen, <screenWidth>, <screenHeight>, <frameRate>, <bitDepth>)
Open last monitor in list with current window settings mglOpen Open with resolution 800×600 60Hz 32bit fullscreen mglOpen(1,800,600,60,32); Open in a window mglOpen(0);
mglFlush: Flips front and back buffer
purpose: swap front and back buffer (waits for one frame tick) mglClose: Closes the screen
purpose: close OpenGL screen Other screen functionsmglSwitchDisplay: Switch between multiple monitors
usage: mglSwitchDisplay(<displayID>)
You can use this to open up two separate screens and control them independently. For example, say you have two monitors 1 and 2. You open the first in the usual way: mglOpen(1) Then you switch monitors so that you can open up the other one mglSwitchDisplay mglOpen(2) Now if you want draw to the first display, you can do mglSwitchDisplay(1) mglClearScreen(0.5); mglFlush; Similarly, to draw to the second display mglSwitchDisplay(2); mglClearScreen(1); mglFlush; You can check the status of all open displays with: mglSwitchDisplay(-2); If you want to close all displays at once, you can do: mglSwitchDisplay(-1); mglMoveWindow: Moves windows created by mglOpen(0)
usage: mglMoveWindow(leftPos,topPos)
mglOpen(0); mglMoveWindow(100,100);
mglDescribeDisplays: Get information about your monitor and computer system
usage: [displayInfo computerInfo] = mglDescribeDisplays() mglFrameGrab: Frame grab to a matlab matrix
usage: mglFrameGrab(<frameRect>)
mglOpen();
mglScreenCoordinates;
mglClearScreen([0 0 0]);
global MGL;
mglPoints2(MGL.screenWidth*rand(5000,1),MGL.screenHeight*rand(5000,1));
mglPolygon([0 0 MGL.screenWidth MGL.screenWidth],[MGL.screenHeight/3 MGL.screenHeight*2/3 MGL.screenHeight*2/3 MGL.screenHeight/3],0);
mglTextSet('Helvetica',32,[1 1 1]);
mglTextDraw('Frame Grab',[MGL.screenWidth/2 MGL.screenHeight/2]);
frame = mglFrameGrab;
mglFlush
imagesc(mean(frame,3)');colormap('gray')
Functions to adjust the coordinate framemglVisualAngleCoordinates: Visual angle coordinates
purpose: Sets view transformation to correspond to visual angles (in degrees) given size and distance of display. Display must be open and have valid width and height (defined in MGL variable)
mglOpen mglVisualAngleCoordinates(57,[16 12]); mglScreenCoordinates: Pixel coordinate frame
purpose: Set coordinate frame so that it is in pixels with 0,0 in the top left hand corrner mglTransform: Low-level function to adjust transforms
purpose: applies view transformations
You can also specifiy one of GL_MODELVIEW, GL_PROJECTION, or GL_TEXTURE and a return variable current matrix values. If two outputs are specified, the result of the computation will be returned. This function is usually not called directly, but called by mglVisualAngleCoordinates or mglScreenCoordinates to set the transforms mglHFlip: Horizontally flip coordinates
purpose: flips coordinate frame horizontally, useful for when the display is viewed through a mirror mglOpen
mglVisualAngleCoordinates(57,[16 12]);
mglHFlip
mglTextSet('Helvetica',32,1,0,0,0,0,0,0,0);
mglTextDraw('Mirror reversed',[0 0]);
mglFlush;
mglVFlip: Vertically flip coordinates
purpose: flips coordinate frame vertically mglOpen
mglVisualAngleCoordinates(57,[16 12]);
mglVFlip
mglTextSet('Helvetica',32,[1 1 1],0,0,0,0,0,0,0);
mglTextDraw('Vertically flipped',[0 0]);
mglFlush;
Texture functions used for displaying imagesmglCreateTexture: Create a texture from a matrix
purpose: Create a texture for display on the screen with mglBltTexture image can either be grayscale nxm, color nxmx3 or color+alpha nxmx4
mglOpen; mglClearScreen mglScreenCoordinates texture = mglCreateTexture(round(rand(100,100)*255)); mglBltTexture(texture,[0 0]); mglFlush; mglBltTexture: Draw the texture to the screen
purpose: Draw a texture to the screen in desired position.
To display several textures at once, texture can be an array of n textures, position is nx2, or nx4 and hAlignment, vAlignment and rotation are either a single value or an array of n. multiple textures: mglOpen; mglVisualAngleCoordinates(57,[16 12]); image = rand(200,200)*255; imageTex = mglCreateTexture(image); mglBltTexture([imageTex imageTex],[-3 0;3 0],0,0,[-15 15]); mglFlush; single textures mglOpen; mglVisualAngleCoordinates(57,[16 12]); image = rand(200,200)*255; imageTex = mglCreateTexture(image); mglBltTexture(imageTex,[0 0]); mglFlush; mglDeleteTexture: Delete a texture
purpose: Deletes a texture. This will free up memory for textures that will not be drawn again. Note that when you call mglClose texture memory is freed up. You only need to call this if you are running out of memory and have textures that you do not need to use anymore.
mglOpen; mglClearScreen mglScreenCoordinates texture = mglCreateTexture(round(rand(100,100)*255)); mglBltTexture(texture,[0 0]); mglFlush; mglDeleteTexture(texture); Drawing textmglTextSet: Set parameters for drawing text
purpose: Set text properties for mglText
mglOpen;
mglVisualAngleCoordinates(57,[16 12]);
mglTextSet('Helvetica',32,[0 0.5 1 1],0,0,0,0,0,0,0);
mglTextDraw('Hello There',[0 0]);
mglFlush;
mglText: Create a texture from a string
purpose: Creates a texture from a string.
mglOpen;
mglVisualAngleCoordinates(57,[16 12]);
mglTextSet('Helvetica',32,[0 0.5 1 1],0,0,0,0,0,0,0);
thisText = mglText('hello')
mglBltTexture(thisText,[0 0],'left','top');
mglFlush;
Normally you will only set one output argument which is a texture usable by mglBltTexture. But if you have two output arguments [tex texMatrix] = mglText('hello');
texMatrix will contain a 2D matlab array that has a rendering of the text (i.e. it will have values from 0-255 that represent the string). You can modify this matrix as you want and then use mglCreateTexture to create it into a texture that can be displayed by mglBltTexture mglTextDraw: Draws text to screen (simple but slow)
purpose: wrapper around mglText and mglBltTexture to draw some text on the screen. If you need to draw text more quickly, you will have to pre-make the text textures with mglText and then use mglBltTexture when you want it. Otherwise, for non time-critical things this functions should be used.
mglOpen;
mglVisualAngleCoordinates(57,[16 12]);
mglTextSet('Helvetica',32,[0 0.5 1 1],0,0,0,0,0,0,0);
mglTextDraw('Hello There',[0 0]);
mglFlush;
mglStrokeText: Fast no-frills line-based text drawing (does not use texture memory)
purpose: Draws a stroked fixed-width character or string on MGL display. Default width is 1, default height 1.8 (in current screen coordinates)
mglOpen;
mglVisualAngleCoordinates(57,[16 12]);
mglStrokeText('Hello',0,0);
mglFlush;
Gamma tablesmglSetGammaTable: Sets the display card gamma table
purpose: Set the gamma table Setting a redMin, redMax, redGamma, greenMin, etc. mglSetGammaTable(0,1,0.8,0,1,0.9,0,1,0.75); or with a vector of length 9: mglSetGammaTable([0 1 0.8 0 1 0.9 0 1 0.75]); or set with a single table for all there colors. Note that the table values go from 0 to 1 (i.e. 0 is the darkest value and 1 is the brightest value). If you have a 10 bit gamma table (most cards do–see section on monitor calibration for a list), then the intermediate values will be interpreted with 10 bits of resolution. gammaTable = ((0:1/255:1).^0.8)'; mglSetGammaTable(gammaTable); or set all three colors with differnet tables redGammaTable = (0:1/255:1).^0.8; greenGammaTable = (0:1/255:1).^0.9; blueGammaTable = (0:1/255:1).^0.75; mglSetGammaTable(redGammaTable,greenGammaTable,blueGammaTable); can also be called with an nx3 table gammaTable(:,1) = (0:1/255:1).^0.8; gammaTable(:,2) = (0:1/255:1).^0.9; gammaTable(:,3) = (0:1/255:1).^0.75; mglSetGammaTable(gammaTable); can also be called with the structure returned by mglGetGammaTable mglSetGammaTable(mglGetGammaTable); Note that the gamma table will be restored to the original after mglClose. Timing. The setting of the gamma table is done by the OS in a way that seems to be asynchronous with mglFlush. For instance, the following code gives unexpected results: mglOpen(1); mglClearScreen(1); % set back buffer to white mglWaitSecs(2); mglSetGammaTable(zeros(1,256)); % now set the gamma table to all black, this should insure that nothing will be displayed mglFlush; % now the flush will bring the value 255, set by the mglClearScreen above, to the front buffer, but because the gamma table is set to black, nothing should be displayed mglWaitSecs(2); mglClose; This should keep the screen black, but on my machine, the screen temporarily flashes white. Presumably this is because the mglSetGammaTable happens after the mglFlush. It is recommended that you change the gamma while there is nothing displayed on the screen and wait for at least one screen refresh before assuming that the gamma table has actually changed. mglGetGammaTable: Gets the current gamma tablepurpose: returns what the gamma table is set to usage: table = mglGetGammaTable() mglOpen; gammaTable = mglGetGammaTable Drawing functionsmglClearScreen
purpose: sets the background color
set to the level of gray (0-1) mglClearScreen(gray) set to the given [r g b] mglClearScreen([r g b]) full example mglOpen; mglClearScreen([0.7 0.2 0.5]); mglFlush(); mglPoints2: 2D points
purpose: plot 2D points on an OpenGL screen opened with mglOpen
mglOpen; mglVisualAngleCoordinates(57,[16 12]); mglPoints2(16*rand(500,1)-8,12*rand(500,1)-6,2,1); mglFlush mglPoints3: 3D points
purpose: plot 2D points on an OpenGL screen opened with mglOpen
mglOpen; mglVisualAngleCoordinates(57,[16 12]); mglPoints3(16*rand(500,1)-8,12*rand(500,1)-6,zeros(500,1),2,1); mglFlush
mglLines2: 2D lines
purpose: mex function to plot lines on an OpenGL screen opened with glopen
mglOpen mglVisualAngleCoordinates(57,[16 12]); mglLines2(-4, -4, 4, 4, 2, [1 0.6 1]); mglFlush mglFillOval: Ovals
purpose: draw filled oval(s) centered at x,y with size [xsize ysize] and color [rgb]. the function is vectorized, so if you provide many x/y coordinates (identical) ovals will be plotted at all those locations.
mglOpen; mglVisualAngleCoordinates(57,[16 12]); x = [-1 -4 -3 0 3 4 1]; y = [-1 -4 -3 0 3 4 1]; sz = [1 1]; mglFillOval(x, y, sz, [1 0 0]); mglFlush(); mglFillRect: Rectangles
purpose: draw filled rectangles(s) centered at x,y with size [xsize ysize] and color [rgb]. the function is vectorized, so if you provide many x/y coordinates (identical) ovals will be plotted at all those locations.
mglOpen; mglVisualAngleCoordinates(57,[16 12]); x = [-1 -4 -3 0 3 4 1]; y = [-1 -4 -3 0 3 4 1]; sz = [1 1]; mglFillRect(x, y, sz, [1 1 0]); mglFlush();
mglFixationCross: Cross
purpose: draws a fixation cross with no arguments, draws a fixation cross at origin (default width 0.2 with linewidth 1 in white at [0,0])
mglOpen; mglVisualAngleCoordinates(57,[16 12]); mglFixationCross; mglFlush; mglGluAnnulus: Annuli, rings
purpose: for annuli and rings, e.g. for retinotopic stimuli. The function is vectorized, such that multiple annuli can be rendered in one call. In this case, x,y, isize, and osize need to have the same number of elements. Color is also vectorized.
mglOpen(0); mglVisualAngleCoordinates(57,[16 12]); x = zeros(10, 1); y = zeros(10, 1); isize = ones(10,1) osize = 3*ones(10,1); startAngles = linspace(0,180, 10) sweepAngles = ones(1,10).*10; colors = jet(10)'; % nb! transpose mglGluPartialDisk(x, y, isize, osize, startAngles, sweepAngles, colors, 60, 2); mglFlush(); mglGluDisk: Circular dots
purpose: for plotting circular (rather than square dots), use this function. on slower machines, large number of dots may lead to dropped frames. there may be a way to speed this up a bit in future.
mglOpen; mglVisualAngleCoordinates(57,[16 12]); x = 16*rand(100,1)-8; y = 12*rand(100,1)-6; mglGluDisk(x, y, 0.1, [0.1 0.6 1], 24, 2); mglFlush();
mglGluPartialDisk: Segments, wedges
purpose: for segments and wedges, e.g. for retinotopic stimuli. The function is vectorized, such that multiple segments can be rendered in one call. In this case, x,y, isize, osize, startAngles, and sweepAngles need to have the same number of elements. Color is also vectorized (see mglGluAnnulus and the example below).
mglOpen(0); mglVisualAngleCoordinates(57,[16 12]); x = zeros(10, 1); y = zeros(10, 1); isize = linspace(1, 5, 10); osize = 3+isize; startAngles = linspace(0,180, 10) sweepAngles = ones(1,10).*10; colors = jet(10)'; mglGluPartialDisk(x, y, isize, osize, startAngles, sweepAngles, colors, 60, 2); mglFlush(); mglPolygon: Polygons
purpose: mex function to draw a polygon in an OpenGL screen opened with mglOpen. x and y can be vectors (the polygon will be closed)
mglOpen; mglVisualAngleCoordinates(57,[16 12]); x = [-5 -6 -3 4 5]; y = [ 5 1 -4 -2 3]; mglPolygon(x, y, [1 0 0]); mglFlush(); mglQuads: Quads
usage: mglQuad( vX, vY, rgbColor, [antiAliasFlag] );
mglOpen; mglScreenCoordinates mglQuad([100; 600; 600; 100], [100; 200; 600; 100], [1; 1; 1], 1); mglFlush(); Stencils to control drawing only to specific parts of screenHere is a demonstration of how to use stencils using these these functions: mglOpen; mglScreenCoordinates; %Draw an oval stencil mglStencilCreateBegin(1); mglFillOval(300,400,[100 100]); mglStencilCreateEnd; mglClearScreen; % now draw some dots, masked by the oval stencil mglStencilSelect(1); mglPoints2(rand(1,5000)*500,rand(1,5000)*500); mglFlush; mglStencilSelect(0); mglStencilCreateBegin: Start drawing a stencil
purpose: Begin drawing to stencil. Until mglStencilCreateEnd is called, all drawing operations will also draw to the stencil. Check MGL.stencilBits to see how many stencil planes there are. If invert is set to one, then the inverse stencil is made
see example above. mglStencilCreateEnd: End drawing a stencilpurpose: Ends drawing to stencil usage: mglStencilCreateEnd see example above. mglStencilSelect: Select a stencilpurpose: Sets which stencil to use, 0 for no stencil usage: mglStencilSelect(stencilNumber)
See example above. Keyboard and mouse functionsmglDisplayCursor: Hide or display the mouse cursor
purpose: Hide or display the mouse cursor
When you call mglOpen the mouse cursor is hidden by default. You can get it to come back by doing: mglOpen mglDisplayCursor mglGetKeys: Get keyboard state
purpose: returns the status of the keyboard (regardless of whether the focus is on the mgl window)
mglGetMouse: Get mouse state
usage: mglGetMouse() mglGetKeyEvent: Get a key down event off of queuepurpose: returns a key down event waitTicks specifies how long to wait for a key press event in seconds. Note that the timing precision is system-dependent:
The default wait time is 0, which will return immediately and if no keypress event is found, will return an empty array []. The return structure contains the character (ASCII) code of the pressed key, the system-specific keycode, a keyboard identifier (on Linux, this is the keyboard state, or modifier field), and and the time (in secs) of the key press event. NOTE that to get a key event the focus *MUST* be on the mgl window. For faster timing, try mglGetKeys
mglOpen mglGetKeyEvent(0.5) mglGetMouseEvent: Get a mouse button down event off of queue
usage: mglGetMouseEvent(waitTicks)
The default wait time is 0, which will return immediately with the mouse position regardless of button state. The return structure contains the x,y coordinates of the mouse, the button identifier if pressed (on the button-challenged Mac this is always 1) and 0 otherwise, and the time (in secs) of the mouse event. NOTE that the mouse down event has to be *ON* the mgl window for this to work with waitTicks not equal to 0
mglOpen mglGetMouseEvent(0.5) mglCharToKeycode: Returns keycode of char
Purpose: Returns the keycodes of a (list of) keynames Note on special keys: On Linux (X), special keys and function keys have unique names, e.g., 'Escape', 'F1', etc., so obtaining the keycodes for these is done by mglCharToKeycode({'Escape','F1'}) etc. On Macs, this is not possible; instead, test for the keycode and name of a key using the mglShowKey function. The keycodes match those used by mglGetKeys and mglGetKeyEvent
Example: testing for specific keypresses: keycodes=mglCharToKeycode({'1','2' '3'}) % keys 1-3 on main keyboard
while (1); k=mglGetKeys(keycodes); if (any(k)),break;end;end
Technical note: the returned keycodes are identical to system keycodes+1 mglKeycodeToChar: Returns char of keycode
Purpose: Returns the keynames of a (list of) keycodes Note on special keys: This repeats the above entry, deleted.
Example: testing which keys were pressed: while (1); k=mglGetKeys; if (any(k)),break;end;end keycodes=find(k); keynames=mglKeycodeToChar(keycodes) Technical note: keycodes are identical to system keycodes+1 Timing functionsmglGetSecs: Get time in seconds
purpose: Get current or elapsed time
To get current time t=mglGetSecs Get elapsed time since t0 t0 = mglGetSecs; elapsedTime=mglGetSecs(t0) mglWaitSecs: Wait for a time in seconds
purpose: Wait for some time
Wait 3.3 seconds: mglWaitSecs(3.3); Sound functionsmglInstallSound: Install an .aiff file for playing with mglPlaySound
purpose: Install an .aiff file for playing with mglPlaySound
This will install sounds to be played using mglPlaySound. Note that if you just want to use systems sounds then you do not need to call this function directly, it will be called by mglOpen to install all your system sounds. Once the sound is installed you can play it with mglPlaySound soundNum = mglInstallSound('/System/Library/Sounds/Submarine.aiff');
mglPlaySound(soundNum);
With no arguments, mglInstallSound uninstalls all sounds mglInstallSound mglPlaySound: Play a system sound
purpose: Play a sound
Plays a system sound. After calling mglOpen, all of the system sounds will be installed and you can play a specific one as follows: mglOpen; global MGL; mglPlaySound(find(strcmp(MGL.soundNames,'Submarine'))); With no arguments mglPlaySound plays the system alert sound mglPlaySound Note that this function returns right after it starts playing the sound (it does not wait until the sound finishes playing). Test/Demo programsRun these test programs without any parameters and they should display on your second monitor. With an optional single argument you can pass the number of the display you want to display on.
|