PSYCHOPHYSICS TOOLBOX BASICS THE SCREEN COMMAND Screen Usage
PSYCHOPHYSICS TOOLBOX
BASICS
THE SCREEN COMMAND >> Screen Usage: % Activate compatibility mode: Try to behave like the old Mac. OS-9 Psychtoolbox: old. Enable. Flag=Screen('Preference', 'Emulate. Old. PTB', [enable. Flag]); % Open or close a window or texture: [window. Ptr, rect]=Screen('Open. Window', window. Ptr. Or. Screen. Number [, color] [, rect] [, pixel. Size] [, number. Of. Buffers] [, stereomode] [, multisample][, imagingmode][, special. Flags][, client. Rect]); [window. Ptr, rect]=Screen('Open. Offscreen. Window', window. Ptr. Or. Screen. Number [, color] [, rect] [, pixel. Size] [, special. Flags] [, multi. Sample]); texture. Index=Screen('Make. Texture', Window. Index, image. Matrix [, optimize. For. Draw. Angle=0] [, special. Flags=0] [, floatprecision=0] [, texture. Orientation=0] [, texture. Shader=0]); old. Params = Screen('Panel. Fitter', window. Ptr [, new. Params]); Screen('Close', [window. Or. Texture. Index or list of texture. Indices/offscreen. Window. Indices]); Screen('Close. All'); % Draw lines and solids like Quick. Draw and Direct. X (OS 9 and Windows): currentbuffer = Screen('Select. Stereo. Draw. Buffer', window. Ptr [, bufferid] [, param 1]); Screen('Draw. Line', window. Ptr [, color], from. H, from. V, to. H, to. V [, pen. Width]); Screen('Draw. Arc', window. Ptr, [color], [rect], start. Angle, arc. Angle) Screen('Frame. Arc', window. Ptr, [color], [rect], start. Angle, arc. Angle[, pen. Width] [, pen. Height] [, pen. Mode]) Screen('Fill. Arc', window. Ptr, [color], [rect], start. Angle, arc. Angle) Screen('Fill. Rect', window. Ptr [, color] [, rect] ); Screen('Frame. Rect', window. Ptr [, color] [, rect] [, pen. Width]); Screen('Fill. Oval', window. Ptr [, color] [, rect] [, perfect. Up. To. Max. Diameter]); Screen('Frame. Oval', window. Ptr [, color] [, rect] [, pen. Width] [, pen. Height] [, pen. Mode]); Screen('Frame. Poly', window. Ptr [, color], point. List [, pen. Width]);
DOUBLE BUFFERING Note that flipping clears the new back buffer A "Front" screen/buffer "Back" screen/buffer Swap or "Flip" buffers
USING THE SCREEN COMMAND Opening the screen [window. Ptr, rect]=Screen('Open. Window', Screen. Number) returns a number that we will use to refer to this screen in future commands returns a rectangle (a vector of four numbers) that describe the dimensions of the screen which screen you want to open (you may have multiple monitors)
[window. Ptr, rect]=Screen('Open. Window', Screen. Number) returns a rectangle (a vector of four numbers) that describe the dimensions of the screen >> rect = width 0 0 1680 (0, 0) height 1050 x y (1680, 1050 )
Understanding rects >> rect = 0 y 1 top x 1 left 0 1680 (x 1, y 1) x 2 right y 2 bottom 1050 x y (x 2, y 2)
HOW MONITORS WORK CRT (Cathode Ray Tube) LCD (Liquid Crystal Display)
HOW MONITORS WORK Frame Rate: The number of frames drawn per second A A A A A TIME Typical frame rate: 60 Hz (60 frames per second) 1 second / 60 frames == 16. 67 milliseconds per frame
HOW MONITORS WORK A A A A A TIME Frame Rate • Puts limits on the precision of our visual presentation • Cannot present something for shorter than the length of a single frame • Screen refresh timing is the anchor that PTB uses for all timing measurement
HOW MONITORS WORK A A AA A TIME Getting your frame duration in PTB frame. Duration = Screen('Get. Flip. Interval', window. Ptr)
HOW MONITORS WORK 1– 2– 3– 4– cathode ray tube electron gun electron beam deflection yoke The deflection yoke manipulates the electron beam, sweeping it across the screen, one horizontal line ("scanline") at a time
HOW MONITORS WORK Once one frame is completely drawn, there is a gap in time as the beam is blanked and sweeps back to the first scanline to start drawing the next frame. This gap between frames is called the Vertical BLank interval (VBL). The current position of the beam while it scans is called beamposition.
HOW MONITORS WORK To maintain backwards compatibility, LCD's also implement a VBL even though they don't technically need one. They also report a beamposition (the location of the current scanline), even though they don't use a beam.
TIME Frame 1 Frame 2 VBL Frame 3 VBL Frame 4 VBL PTB tries to swap the front and back buffers during the VBL, so that content is not being updated in the middle of a frame draw. This is called VBL Synchronization. If synchronization between buffer-swapping and VBL fails: - Visual artifacts like flicker and tearing may occur - Timing measurement will be less precise
SCREEN TIMING flip. Time = Screen('Flip', window. Ptr) >> w. Ptr = Screen('Open. Window’, 0, [255 255], [0 0 1024 768]); >> flip. Time = Screen('Flip', w. Ptr); flip. Time = 1. 1038 e+05 >>
FLIP TIMING VBLtime = Screen('Flip', window. Ptr [, when] [, dontclear]) Default is to flip now, i. e. at the next VBL interval. However, you can specify a time in the future for the flip to take place. Flip will wait until that time and then flip at the next VBL interval after the specified time. Default is to clear the back buffer. However in some cases you may want to leave the back buffer as is. Default is 0, set to 1 if you don't want it to clear.
SCREEN TIMING Get. Secs() tells you the current time >> now = Get. Secs() now = 1. 1058 e+05 >> a. Little. Later = Get. Secs() a. Litter. Later = 1. 1060 e+05 >> gap = a. Little. Later – now gap = 21. 2212
WORKING WITH COLOR Color pixels are combinations of red, blue, and green light Each R G B element can take on 256 levels of brightness
WORKING WITH COLOR the Color Lookup Table (CLUT) maps the values from your color indices to real voltage values use to light screen. In standard cases, 0 is mapped to the lowest value, and 255 to the highest, such that [0 0 0] is black and [255 255] is white If you want to get fancy, you can specify your own CLUT
WORKING WITH COLOR Specify colors using a vector of 3 integers my. Color = [ x, y, z] Examples: red= [ 255, 0, 0] aqua = [0, 200, 255]
WORKING WITH COLOR Grayscale colors can be specified using a single number: black = 0; white = 255; gray = 150; black = 0 is equivalent to black = [0 0 0]
DRAWING IN PSYCHTOOLBOX Psychtoolbox uses Open. GL for drawing to the screen. Open GL is the "Open Graphics Library". It is cross-platform software for specifying how to draw 2 D and 3 D graphics using the GPU to achieve hardware-accelerated processing. Psychtoolbox has its own functions for drawing that access lower-level Open. GL functions. But if you want to access actual Open. GL commands you can do that too, or use Open. GL objects created in a program like Blender.
DRAWING FUNCTIONS Screen('Draw. Line', window. Ptr [, color], from. H, from. V, to. H, to. V [, pen. Width]); Screen('Draw. Arc', window. Ptr, [color], [rect], start. Angle, arc. Angle) Screen('Frame. Arc', window. Ptr, [color], [rect], start. Angle, arc. Angle[, pen. Width] [, pen. Height] [, pen. Mode]) Screen('Fill. Arc', window. Ptr, [color], [rect], start. Angle, arc. Angle) Screen('Fill. Rect', window. Ptr [, color] [, rect] ); Screen('Frame. Rect', window. Ptr [, color] [, rect] [, pen. Width]); Screen('Fill. Oval', window. Ptr [, color] [, rect] [, perfect. Up. To. Max. Diameter]); Screen('Frame. Oval', window. Ptr [, color] [, rect] [, pen. Width] [, pen. Height] [, pen. Mode]); Screen('Frame. Poly', window. Ptr [, color], point. List [, pen. Width]); Screen('Fill. Poly', window. Ptr [, color], point. List [, is. Convex]); Screen('Draw. Dots', window. Ptr, xy [, size] [, color] [, center] [, dot_type]); Screen('Draw. Lines', window. Ptr, xy [, width] [, colors] [, center] [, smooth]);
Screen coordinate system (0, 0) y (x. Max, y. Max)
OPENWINDOW EXPANDED [window. Ptr, rect] = Screen('Open. Window', which. Window, bg. Color, rect) Set the screen's background color (default is white) Define a rect for the screen to appear in (default is to take up the whole physical screen) Note: timing may suffer if you are not using full screen
DRAWING SIMPLE SHAPES >> Screen Fill. Rect? Usage: Screen('Fill. Rect', window. Ptr [, color] [, rect] ) Fill "rect". "color" is the clut index (scalar or [r g b] triplet or [r g b a] quadruple) that you want to poke into each pixel; default produces white with the standard CLUT for this window's pixel. Size. Default "rect" is entire window, so you can use this function to clear the window. Please note that clearing the entire window will set the background color of the window to the clear color, ie. , future Screen('Flip') commands will clear to the new background clear color specified in Screen('Fill. Rect'). Instead of filling one rectangle, you can also specify a list of multiple rectangles to be filled - this is much faster when you need to draw many rectangles per frame. To fill n rectangles, provide "rect" as a 4 rows by n columns matrix, each column specifying one rectangle, e. g. , rect(1, 5)=left border of 5 th rectange, rect(2, 5)=top border of 5 th rectangle, rect(3, 5)=right border of 5 th rectangle, rect(4, 5)=bottom border of 5 th rectangle. If the rectangles should have different colors, then provide "color" as a 3 or 4 row by n column matrix, the i'th column specifiying the color of the i'th rectangle. See also: Frame. Rect
DRAWING SIMPLE SHAPES Screen('Fill. Rect', w. Ptr, color, rect); Define the rectangle(s) to fill in. If you leave this blank, the whole screen will be filled, and the background color will be set to that color (when you Flip the screen, the buffer will clear to this color)
function draw. Rect() [window. Ptr, rect]=Screen('Open. Window', 0, [255 255], [0 0 1024 768]); rect. One = [100 250 400]; rect. Two = [250 400 300 450]; both. Rects = [rect. One', rect. Two'] Screen('Fill. Rect', window. Ptr, [255 0 0], [100 500 500]); Screen('Flip', window. Ptr); Wait. Secs(5); Screen('Fill. Rect', window. Ptr, [255 0 0], both. Rects); Screen('Flip', window. Ptr); Wait. Secs(5); clear Screen;
DRAWING MULTIPLE RECTS >> rect. One = [100 250 400]; >> rect. Two = [250 400 300 450]; >> both. Rects = [rect. One', rect. Two'] both. Rects = 100 250 400 300 450 >> Screen('Fill. Rect', w, [0 255 0], both. Rects); >> Screen('Flip', w)
CENTERING Problem: Draw a 100 by 100 square exactly centered in the screen First step: find the center of the screen
CENTERING >> [w. Ptr, rect] = Screen('Open. Window’, 0, [255 255], [0 0 1024 768]); w. Ptr = 11 rect = 0 0 1024 768 >> screen. Width = rect(3); >> screen. Height = rect(4); >> screen. Center. X = screen. Width/2; >> screen. Center. Y = screen. Height/2; >> >> my. Rect. Width = 100; >> my. Rect. Height = 100; >> >> my. Rect. Left = screen. Center. X – my. Rect. Width/2; >> my. Rect. Top = screen. Center. Y – my. Rect. Height/2; >> my. Rect. Right = my. Rect. Left + my. Rect. Width; >> my. Rect. Bottom = my. Rect. Top + my. Rect. Height; >> my. Rect = [my. Rect. Left, my. Rect. Top, my. Rect. Right, my. Rect. Left]; >> >> Screen('Fill. Rect', w. Ptr, [0 0 255], my. Rect); >> Screen('Flip', w. Ptr);
CENTERING (screen. Center. X – rect. Width/2, screen. Center. Y – rect. Height/2) (screen. Center. X, screen. Center. Y) (rect. Left + rect. Width, rect. Top + rect. Height)
DRAWING Screen('Frame. Rect', w. Ptr, color, rect, pen. Width); Just like Fill. Rect, but the rectangle is not filled in. Change pen. Width to change thickness of the lines
DRAWING CIRCLES Screen('Fill. Oval', w. Ptr, color, rect); >> Screen('Fill. Rect', w. Ptr, [0 0 255], my. Rect) >> Screen('Fill. Oval', w. Ptr, [255 0 0 ], my. Rect) >> Screen('Flip', w. Ptr);
ALPHA BLENDING ENABLE BLENDING >> Screen Blend. Function? >>Screen('Blend. Function', w. Ptr, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); >> Screen('Blend. Function', w. Ptr, GL_ONE, GL_ZERO); DISABLE BLENDING
ALPHA BLENDING function Blend. Shapes() [a, b, c] = Kb. Wait(); dk = find(b); Disable. Keys. For. Kb. Check(dk); Screen('Preference', 'Skip. Sync. Tests', 2); [w. Ptr, rect]=Screen('Open. Window', 0, [128 128], [0 0 2560/2 1440/2]); Screen('Blend. Function', w. Ptr, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); color 1 = [0 255]; color 2 = [0 0 255 100]; Screen('Fill. Rect', w. Ptr, color 1, [300 400 400]); Screen('Fill. Rect', w. Ptr, color 2, [350 450 450]); Screen('Flip', w. Ptr); Kb. Wait(); clear Screen; end
USING DRAWDOTS Alternate method for drawing several shapes at once. Even though it is called Draw. Dots, it will draw squares as well as circles. Screen('Draw. Dots', window. Ptr, xy [, size] [, color] [, center] [, dot_type]); Matrix containing coordinates of dot centers. Two rows, x and y. Each column is another dot. Can either be a single value, or a vector of values, one per dot Define center coordinates Single color vector, or matrix with three rows (r, g, b) 0 (default) = squares 1 = circles 2 = circles with high quality anti-aliasing
>> Screen('Preference', 'Skip. Sync. Tests', 2); >> [w. Ptr, rect] = Screen('Open. Window', 0, [255 255], [0 0 1024 768]); >> x. Center = rect(3)/2; >> y. Center = rect(4)/2; >> >> colors = [255 0 0; 0 255 0; 0 0 255] colors = 255 0 0 0 255 >> locations = [-100 0 100; 0 0 0] locations = -100 0 0 0 >> sizes = [30 40 50]; >> Screen('Draw. Dots', w. Ptr, locations, sizes, colors, [x. Center, y. Center], 1); >> Screen('Flip', w. Ptr);
DRAWING LINES Screen('Draw. Line', w. Ptr, color, from. H, from. V, to. H, to. V, pen. Width); (from. H, from. V) (to. H, to. V)
USING DRAWLINES Screen('Draw. Lines', window. Ptr, xy [, width] [, colors] [, center] [, smooth]); Matrix containing coordinates. Two rows (x and y). Each pair of columns (start and end) specifies a line. Either a single color, or one color per point in xy. Three rows (r, g, b). 0 (default) = no smoothing 1 = smoothing 2 = high quality smoothing NOTE: smoothing requires blending to be on
>> lines = [-300 300; -50 50 50] lines = -300 300 -50 50 50 >> colors = [255 0 0 255; 0 0 ; 0 255 0] colors = 255 0 0 0 0 0 255 0 >> Screen('Draw. Lines', w. Ptr, lines, 10, colors, [x. Center, y. Center], 0) >> Screen('Flip', w. Ptr)
DRAWING A FIXATION CROSS function draw. Cross() cross. Length = 10; cross. Color = 0; cross. Width = 3; cross. Lines = [-cross. Length 0; 0 -cross. Length; 0 cross. Length]; cross. Lines = transpose(cross. Lines); [w. Ptr, rect] = Screen('Open. Window’, 0, [255 255], [0 0 1024 768]); x. Center = rect(3)/2; y. Center = rect(4)/2; Screen('Draw. Lines', w. Ptr, cross. Lines, cross. Width, cross. Color, [x. Center y. Center]); Screen('Flip', w. Ptr); Wait. Secs(5); sca; end
DRAWING A FIXATION CROSS function draw. Fixation. Cross(w. Ptr, rect, cross. Length, cross. Color, cross. Width) cross. Lines = [-cross. Length 0; 0 -cross. Length; 0 cross. Length]; cross. Lines = transpose(cross. Lines); x. Center = rect(3)/2; y. Center = rect(4)/2; Screen('Draw. Lines', w. Ptr, cross. Lines, cross. Width, cross. Color, [x. Center y. Center]); end
DRAWING A FIXATION CROSS cross. Width = 5; cross. Length = 20; cross. Color = [255 0 0]; [a, b, c] = Kb. Wait(); dk = find(b); Disable. Keys. For. Kb. Check(dk); [w. Ptr, rect] = Screen('Open. Window', 0, [255 255], [0 0 1024 768]); draw. Fixation. Cross(w. Ptr, rect, cross. Length, cross. Color, cross. Width); Kb. Wait(); fix. On = Screen('Flip', w. Ptr); Kb. Wait(); fix. Off = Screen('Flip', w. Ptr); fix. On. Time = fix. Off-fix. On
- Slides: 45