File: ProceduralSmoothedDiscMaskDemo.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (114 lines) | stat: -rw-r--r-- 3,433 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function ProceduralSmoothedDiscMaskDemo(ndiscs)
% ProceduralSmoothedDiscMaskDemo([ndiscs=700]) -- An aquarium full of discs
% within a centered mask.
%
% This demo shows how to use the Screen('DrawTextures') command to draw a
% large number of similar images quickly - in this case, smooth edged
% discs masked by another smoothed disc.

% History:
% 25/04/2018 created (Junxiang Luo)

% Setup defaults and unit color range:
PsychDefaultSetup(2);

% Disable synctests for this quick demo:
oldSyncLevel = Screen('Preference', 'SkipSyncTests', 2);

% Set number of discs to 700 if no number provided:
if nargin < 1 || isempty(ndiscs)
    ndiscs = 700;
end

randSizeFlag = 1;
maskFlag = 1;

% open our screen
PsychImaging('PrepareConfiguration');
PsychImaging('AddTask', 'General', 'FloatingPoint32BitIfPossible');
screenid = max(Screen('Screens'));
[win, winRect] = PsychImaging('OpenWindow', screenid, 0);
[width, height] = RectSize(winRect);
Screen('BlendFunction', win, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

% dot parameters
dotRadius = 5;
dotSize = 2*dotRadius+1;
dotSigma = 5;
% use alpha channel for smoothing edge of disc?
useAlpha = true;
% smoothing method: cosine (0), smoothstep (1), inverse smoothstep (2)
dotMethod = 1;

%mask parameters
maskRadius = height/2;
maskSigma = maskRadius;
% smoothing method: cosine (0), smoothstep (1), inverse smoothstep (2)
maskMethod = 2;

% create our procedural textures
[dotstex, dotsrect] = CreateProceduralSmoothedDisc(win,...
    dotSize, dotSize, [], dotRadius, dotSigma, useAlpha, dotMethod);

[masktex, maskrect] = CreateProceduralSmoothedDisc(win,...
    width, height, [], maskRadius, maskSigma, useAlpha, maskMethod);

% create our positions on screen
inrect = repmat(dotsrect', 1, ndiscs);
dotDstRects = zeros(4, ndiscs);
scale = zeros(1,ndiscs);
for i=1:ndiscs
    if randSizeFlag
        scale(i) = 1*(0.1 + 0.9 * randn);
    else
        scale(i) = 1;
    end
    dotDstRects(:, i) = CenterRectOnPointd(dotsrect * scale(i), rand * width, rand * height)';
end
maskDstRects = CenterRectOnPointd(maskrect, width/2, height/2)';

% the rotation angles for each disc
rotAngles = rand(1, ndiscs) * 360;

% create random alphas for yellow discs
colours = repmat([1 1 0 1]',1,ndiscs);
colours(4,:) = rand(ndiscs,1)';
myAlphas = colours(4,:);

% Initially sync us to VBL at start of animation loop.
count = 0;
vbl = Screen('Flip', win);
tstart = vbl;

while ~KbCheck
    % Screen('DrawTextures', windowPointer, texturePointer(s) [, sourceRect(s)]
    %[, destinationRect(s)] [, rotationAngle(s)] [, filterMode(s)] [, globalAlpha(s)]
    %[, modulateColor(s)] [, textureShader] [, specialFlags] [, auxParameters]);
    Screen('DrawTextures', win, dotstex, [], dotDstRects, rotAngles, [], [], colours, [], []);

    if maskFlag
        Screen('DrawTextures', win, masktex, [], maskDstRects, [], [], 1, [0, 0, 0, 1]', [], []);
    end

    Screen('DrawingFinished', win);

    rotAngles = rotAngles + 5 * randn(1, ndiscs);
    colours(4,:) = (1+sin(myAlphas*100-200 + count*0.05))/2;

    [x, y] = RectCenterd(dotDstRects);
    x = mod(x + 0.5 * cos(rotAngles/360*2*pi), width);
    y = mod(y - 0.5 * sin(rotAngles/360*2*pi), height);

    dotDstRects = CenterRectOnPointd(inrect .* repmat(scale,4,1), x, y);

    Screen('Flip', win);

    count = count + 1;
end

Screen('Flip', win);

sca;

% Restore old settings for sync-tests:
Screen('Preference', 'SkipSyncTests', oldSyncLevel);