Last active
September 15, 2022 22:33
-
-
Save paretech/f5212dbb888c7d8a845b8934a0c53467 to your computer and use it in GitHub Desktop.
This MATLAB+Psychtoolbox example script contains naively approach of applying spatial calibration offsets using Psychtoolbox imaging pipeline. It is posted along with support post on the PTB website. Note that this is not production code, it is simply an example to demonstrate a specific concept.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Demonstrate attempt at applying x, y offsets via PTB processing hooks. | |
% | |
% Instructions | |
% ------------ | |
% After running, press any key to advance offset position. Position will | |
% not change until key-up event. The script will run indefinitely until the | |
% escape key is pressed. | |
% | |
% Known Issues | |
% ------------ | |
% 1) If there is not a task for FlipHorizontal in the pipeline, the demo | |
% fails. | |
% | |
% 2) No known way to exclude certain output (e.g. debug bar) from offsets | |
% being applied. | |
%% Setup | |
clear screen; | |
ListenChar(2); | |
Screen('Preference', 'VisualDebugLevel', 3); | |
Screen('Preference', 'SkipSyncTests', 1); | |
featureLevel = 2; | |
PsychDefaultSetup(featureLevel); | |
kb_escape = KbName('escape'); | |
PsychImaging('PrepareConfiguration'); | |
PsychImaging('AddTask', 'AllViews', 'FlipHorizontal'); | |
%% Create Window | |
% What happens to the pipeline if you provide a rect? | |
winParms = struct( ... | |
screenId=1, ... | |
color=0.5, ... | |
rect=[], ... | |
pixelSize=[], ... | |
numberOfBuffers=[], ... | |
stereoMode=4 ... | |
); | |
winParmsCell = struct2cell(winParms); | |
[winPtr, winRect] = PsychImaging('OpenWindow', winParmsCell{:}); | |
%% Draw dummy stimulus | |
rectParms = struct( ... | |
color=1.0, ... | |
rect=CenterRect([0 0 200 200], winRect), ... | |
strokeWidthPx=20 ... | |
); | |
rectParmsCell = struct2cell(rectParms); | |
textParms = struct( ... | |
text=char("E"), ... | |
scaleX='center', ... | |
scaleY='center', ... | |
color=rectParms.color, ... | |
wrapat=[], ... | |
flipHorizontal=[], ... | |
flipVertical=[], ... | |
vSpacing=[], ... | |
rightToLeft=[], ... | |
winRect=rectParms.rect ... | |
); | |
textParmsCell = struct2cell(textParms); | |
Screen('TextSize', winPtr, RectHeight(rectParms.rect)*.75); | |
stepSize = 40; | |
stepDelay = 1/4; | |
offsets = [0, 1; 1, 0; 0, -1; -1, 0;] .* stepSize; | |
i = 0; | |
try | |
while true | |
for bufferId = [0, 1] | |
Screen('SelectStereoDrawBuffer', winPtr, bufferId); | |
Screen('FrameRect', winPtr, rectParmsCell{:}); | |
DrawFormattedText(winPtr, textParmsCell{:}); | |
end | |
Screen('Flip', winPtr); | |
[~, keyCode, ~] = KbWait([], 2); | |
if keyCode(kb_escape) | |
break; | |
end | |
offset = num2cell(offsets(i+1, :)); | |
fprintf("Applying offset %i, %i\n", offset{:}); | |
applySpatialOffsets(winPtr, 'StereoLeftCompositingBlit', offset{:}); | |
i = mod(i + 1, 4); | |
end | |
catch exception | |
ListenChar(0); | |
rethrow(exception); | |
end | |
ListenChar(0); | |
clear screen; | |
%% Helper Functions | |
function applySpatialOffsets(winPtr, hookName, xo, yo) | |
[slot, ~, blittercfg, ~, ~, ~, ~] = ... | |
Screen('HookFunction', winPtr, 'Query', hookName, 0); | |
if slot > -1 | |
blitterParts = str2double(strsplit(blittercfg, ':')); | |
assert(length(blitterParts)==6); | |
blitterParams = num2cell(blitterParts(~isnan(blitterParts))); | |
[x, y, sx, sy] = blitterParams{:}; | |
Screen('HookFunction', winPtr, 'Remove', hookName , slot); | |
else | |
[x, y, sx, sy] = deal(0, 0, 1, 1); | |
end | |
Screen('HookFunction', winPtr, 'AppendBuiltin', hookName, 'Builtin:IdentityBlit', ... | |
sprintf('Offset:%i:%i:Scaling:%f:%f', x+xo, y+yo, sx, sy)); | |
Screen('HookFunction', winPtr, 'Enable', hookName); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See post 4604 on Psychtoolbox forum for additional context.