This repository has been archived by the owner on Mar 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LcrPatternRenderer.m
65 lines (49 loc) · 2.07 KB
/
LcrPatternRenderer.m
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
% A renderer that draws primitives into a subset of color bits depending on the current pattern index.
classdef LcrPatternRenderer < Renderer
properties
numPatterns
patternBitDepth
colorBitDepth
end
properties (Access = private)
patternIndex
end
methods
function obj = LcrPatternRenderer(numPatterns, patternBitDepth, colorBitDepth)
if nargin < 3
colorBitDepth = 8;
end
obj = obj@Renderer();
obj.numPatterns = numPatterns;
obj.patternBitDepth = patternBitDepth;
obj.colorBitDepth = colorBitDepth;
obj.patternIndex = 0;
end
function drawArray(obj, array, mode, first, count, color, texture, mask, filter)
c = mean(color(1:3));
if c > 1
c = 1;
elseif c < 0
c = 0;
end
% Expand color to range of pattern bit depth.
patternColor = round(c * (2^obj.patternBitDepth - 1));
% Shift pattern color into pattern index position.
patternColor = bitshift(patternColor, obj.patternIndex * obj.patternBitDepth);
% Split shifted pattern color into GRB components.
bitMask = 2 ^ obj.colorBitDepth - 1;
g = bitand(bitshift(patternColor, -0 * obj.colorBitDepth), bitMask);
r = bitand(bitshift(patternColor, -1 * obj.colorBitDepth), bitMask);
b = bitand(bitshift(patternColor, -2 * obj.colorBitDepth), bitMask);
% Normalize and combine.
g = g / bitMask;
r = r / bitMask;
b = b / bitMask;
color = [r, g, b, color(4)];
drawArray@Renderer(obj, array, mode, first, count, color, texture, mask, filter);
end
function incrementPatternIndex(obj)
obj.patternIndex = rem(obj.patternIndex + 1, obj.numPatterns);
end
end
end