Skip to content

Instantly share code, notes, and snippets.

@iandol
Created December 19, 2012 19:39
Show Gist options
  • Save iandol/4339809 to your computer and use it in GitHub Desktop.
Save iandol/4339809 to your computer and use it in GitHub Desktop.
Weird bug. Run this and you should see a basic procedural garborium demo with correct blending (using normalised 0-1 colour range). Now uncomment line 24 (WhitIndex function) and rerun. What I see is the blending is now different (only the lighter parts of the gabor display); but I don't think WhiteIndex should effect the drawing mode.
function runGarborium()
s = struct();
s.srcMode = GL_ONE;
s.dstMode = GL_ONE;
s.blend = true;
s.backgroundColour = 0.5;
s.bitDepth = 'FloatingPoint32BitIfPossible';
s.screenid = max(Screen('Screens'));
%-----------------------------------------------------------
try%======This is our main TRY CATCH experiment display loop
%-----------------------------------------------------------
PsychImaging('PrepareConfiguration');
PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');
PsychImaging('AddTask', 'General', s.bitDepth);
PsychImaging('AddTask', 'General', 'NormalizedHighresColorRange');
[s.win, s.winRect] = PsychImaging('OpenWindow', s.screenid, s.backgroundColour);
if s.blend
Screen('BlendFunction', s.win, s.srcMode, s.dstMode);
end
s.isi = Screen('GetFlipInterval', s.win);
s.halfisi = s.isi/2;
s.black = BlackIndex(s.win);
%s.white = WhiteIndex(s.win);
sz = 65;
phase = 0;% Phase of underlying sine grating in degrees
sc = 10.0;% Spatial constant of the exponential "hull"
freq = .05;% Frequency of sine grating
contrast = 10.0;% Contrast of grating
aspectratio = 1.0;% Aspect ratio width vs. height
ngabors = 200;
mypars = repmat([phase, freq, sc, contrast, aspectratio, 0, 0, 0]', 1, ngabors);
gabortex = CreateProceduralGabor(s.win, sz, sz, 1);
Screen('DrawTexture', s.win, gabortex, [], [], [], [], [], [], [],kPsychDontDoRotation,...
[phase, freq, sc, contrast, aspectratio, 0, 0, 0]);
Screen('Flip', s.win);
WaitSecs(0.5)
texrect = Screen('Rect', gabortex);
inrect = repmat(texrect', 1, ngabors);
dstRects = zeros(4, ngabors);
scale = zeros(1,ngabors);
for i=1:ngabors
scale(i) = 1*(0.1 + 0.9 * randn);
dstRects(:, i) = CenterRectOnPointd(texrect * scale(i), rand * s.winRect(3), rand * s.winRect(3))';
end
% Preallocate array with rotation angles:
rotAngles = rand(1, ngabors) * 360;
% Initially sync us to VBL at start of animation loop.
vbl = Screen('Flip', s.win);
tstart = vbl;
count = 0;
% Animation loop: Run until any keypress:
while ~KbCheck
Screen('DrawTextures', s.win, gabortex, [], dstRects,...
rotAngles, [], [], [], [], kPsychDontDoRotation, mypars);
Screen('DrawingFinished', s.win);
%rotAngles = rotAngles + 1 * randn(1, ngabors);
mypars(1,:) = mypars(1,:) + 5;% Increment phase-shift
%mypars(5,:) = 1.0 + 0.25 * sin(count*0.1); % "Pulse" the aspect-ratio of each gabor with a sine-wave timecourse
[x, y] = RectCenterd(dstRects);
x = mod(x + 0.33 * cos(rotAngles/360*2*pi), s.winRect(3));
y = mod(y - 0.33 * sin(rotAngles/360*2*pi), s.winRect(4));
% Recompute dstRects destination rectangles for each patch, given the
% 'per gabor' scale and new center location (x,y):
dstRects = CenterRectOnPointd(inrect .* repmat(scale,4,1), x, y);
vbl = Screen('Flip', s.win, vbl + s.halfisi);
count = count + 1;
end
% Done. Last flip to take end timestamp and for stimulus offset:
vbl = Screen('Flip', s.win);
avgfps = count / (vbl - tstart);
fprintf('\n--->>> %g frames at %g FPS\n',count,avgfps);
Priority(0);
ListenChar(0);
ShowCursor;
Screen('CloseAll');
clear s;
catch ME
Priority(0);
ListenChar(0);
ShowCursor;
Screen('CloseAll');
clear s;
rethrow(ME)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment