-
Notifications
You must be signed in to change notification settings - Fork 2
/
HordeHelper.m
executable file
·221 lines (188 loc) · 7.4 KB
/
HordeHelper.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
function varargout = HordeHelper(cmd, varargin)
% HordeHelper - Useful helper and utility functions for using Horde3D.
%
% Usage:
%
% HordeHelper('Initialize');
% - Initialize Horde3DCore and Horde3D engine. Call this while the target
% window for rendering is active and in 3D mode, ie., after
% Screen('BeginOpenGL', ...); was called for the window!
%
% This will also define a global struct variable 'global HE' which contains
% all interesting Horde engine constants.
%
% DEPRECATED FUNCTIONS - NO LONGER NEEDED:
%
% rendertarget = HordeHelper('GetRendertargetForWindow', win);
% - Setup onscreen or offscreen window 'win' for rendering and return the
% 'rendertarget' handle to be used after each render-pass in the call to
% HordeHelper('EndOpenGL', rendertarget);
% This is not strictly needed as long as you don't want to draw into
% offscreen windows (e.g., for image processing operations on the rendered
% scene) and as long as you don't use the Psychtoolbox imaging pipeline for
% postprocessing. Using it in the trivial case doesn't hurt however, it is
% basically a no operation then.
%
% HordeHelper('EndOpenGL', rendertarget);
% - Finalize rendering for 'rendertarget' window.
%
%
% History:
% 29.7.2009 MK Written.
% ??.?.2010 YNK Updated to Horde version 1.0.0 Beta4.
% 16.9.2015 MK Updated to Horde version 1.0.0 Beta5 - Current git master.
% This global variable will hold all global Horde data structures and
% enumerations of Horde3D parameters:
global HE;
% Need access to GL structs:
global GL;
if strcmpi(cmd, 'Initialize')
% Child protection: Psychtoolbox userspace rendering context active as
% required?
[targetwindow, IsOpenGLRendering] = Screen('GetOpenGLDrawMode');
if ~IsOpenGLRendering
% No: We fail, but not without closing the onscreen window and
% releasing other resources:
sca;
error('HordeHelper: Tried to call "Initialize" without calling Screen(''BeginOpenGL'',...) beforehand!');
end
% Call Initialize routine of Horde engine core to setup the engine for
% all further commands:
Horde3DCore('Initialize');
% Eat gl errors to cope with Apples fragile OSX:
while glGetError; end;
% Define all relevant enums for parameter passing to engine:
HE.H3DResTypes.Undefined = 0;
HE.H3DResTypes.SceneGraph = 1;
HE.H3DResTypes.Geometry = 2;
HE.H3DResTypes.Animation = 3;
HE.H3DResTypes.Material = 4;
HE.H3DResTypes.Code = 5;
HE.H3DResTypes.Shader = 6;
HE.H3DResTypes.Texture = 7;
HE.H3DResTypes.ParticleEffect = 8;
HE.H3DResTypes.Pipeline = 9;
HE.H3DRootNode = 1;
HE.H3DOptions.MaxLogLevel = 1;
HE.H3DOptions.MaxNumMessages = 2;
HE.H3DOptions.TrilinearFiltering = 3;
HE.H3DOptions.MaxAnisotropy = 4;
HE.H3DOptions.TexCompression = 5;
HE.H3DOptions.LoadTextures = 6;
HE.H3DOptions.FastAnimation = 7;
HE.H3DOptions.ShadowMapSize = 8;
HE.H3DOptions.SampleCount = 9;
HE.H3DOptions.WireframeMode = 10;
HE.H3DOptions.DebugViewMode = 11;
HE.H3DOptions.DumpFailedShaders = 12;
HE.H3DNodeTypes.Undefined = 0;
HE.H3DNodeTypes.Group = 1;
HE.H3DNodeTypes.Model = 2;
HE.H3DNodeTypes.Mesh = 3;
HE.H3DNodeTypes.Joint = 4;
HE.H3DNodeTypes.Light = 5;
HE.H3DNodeTypes.Camera = 6;
HE.H3DNodeTypes.Emitter = 7;
HE.H3DStats.TriCount = 100;
HE.H3DStats.BatchCount = 101;
HE.H3DStats.LightPassCount = 102;
HE.H3DStats.FrameTime = 103;
HE.H3DStats.AnimationTime = 104;
HE.H3DStats.GeoUpdateTime = 105;
HE.H3DStats.ParticleSimTime = 106;
HE.H3DStats.FwdLightsGPUTime = 107;
HE.H3DStats.DefLightsGPUTime = 108;
HE.H3DStats.ShadowsGPUTime = 109;
HE.H3DStats.ParticleGPUTime = 110;
HE.H3DStats.TextureVMem = 111;
HE.H3DStats.GeometryVMem = 112;
HE.H3DLight.MatResI = 500;
HE.H3DLight.RadiusF = 501;
HE.H3DLight.FovF = 502;
HE.H3DLight.ColorF3 = 503;
% HE.H3DLight.Col_G = 504; % TODO: figure out the colors
% HE.H3DLight.Col_B = 505;
HE.H3DLight.ShadowMapCountI = 504;
HE.H3DLight.ShadowSplitLambdaF = 505;
HE.H3DLight.ShadowMapBiasF = 506;
HE.H3DCamera.PipeResI = 600;
HE.H3DCamera.OutTexResI = 601;
HE.H3DCamera.OutBufIndexI = 602;
HE.H3DCamera.LeftPlaneF = 603;
HE.H3DCamera.RightPlaneF = 604;
HE.H3DCamera.BottomPlaneF = 605;
HE.H3DCamera.TopPlaneF = 606;
HE.H3DCamera.NearPlaneF = 607;
HE.H3DCamera.FarPlaneF = 608;
HE.H3DCamera.ViewportXI = 609;
HE.H3DCamera.ViewportYI = 610;
HE.H3DCamera.ViewportWidthI = 611;
HE.H3DCamera.ViewportHeightI = 612;
HE.H3DCamera.OrthoI = 613;
HE.H3DCamera.OccCullingI = 614;
HE.H3DModel.GeoResI = 200;
HE.H3DModel.SWSkinningI = 201;
HE.H3DModel.LodDist1F = 202;
HE.H3DModel.LodDist2F = 203;
HE.H3DModel.LodDist3F = 204;
HE.H3DModel.LodDist4F = 205;
HE.H3DNodeFlags.NoDraw = 1;
HE.H3DNodeFlags.NoCastShadow = 2;
HE.H3DNodeFlags.NoRayQuery = 4;
HE.H3DNodeFlags.Inactive = 7;
% Ready to rock!
return;
end
if strcmpi(cmd, 'GetRendertargetForWindow')
if nargin < 2
error('HordeHelper: GetRendertargetForWindow: Window handle missing!');
end
% Get 'win'dowhandle of target window:
win = varargin{1};
wk = Screen('WindowKind', win);
if ~ismember(wk, [-1, 1])
error('Invalid windowhandle passed! Not an onscreen or offscreen window handle!');
end
if wk == 1
% Onscreen window:
thingo.onscreen = 1;
% Perform dummy info query to get its drawBuffer bound:
winfo = Screen('GetWindowInfo', win);
% Onscreen with imaging pipeline enabled?
if ~bitand(winfo.ImagingMode, kPsychNeedFastBackingStore)
% Not a imaging pipeline backed onscreen window: No-Op. Just
% return windowhandle back to caller:
varargout{1} = win;
return;
end
% Real FBO backed onscreen window with imaging pipeline active:
% Query its bound FBO's color attachment texture handle:
thingo.wofftex = glGetFramebufferAttachmentParameterivEXT(GL.FRAMEBUFFER_EXT, GL.COLOR_ATTACHMENT0_EXT, GL.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT);
else
% Offscreen window: Query its texture handle directly:
thingo.wofftex = Screen('GetOpenGLTexture', win, win);
thingo.onscreen = 0;
end
% Assign remaining properties needed later for 'EndOpenGL' ops:
thingo.win = win;
[thingo.w thingo.h] = Screen('WindowSize', win);
varargout{1} = thingo;
return;
end
if strcmpi(cmd, 'EndOpenGL')
if nargin < 2
error('HordeHelper: EndOpenGL: Rendertarget parameter missing!');
end
% Get parameter:
thingo = varargin{1};
if isstruct(thingo)
% Real rendertarget. Do the end/unbind/rebind/copy op:
Screen('EndOpenGL', thingo.win);
else
% Normal window. Just do regular end op:
Screen('EndOpenGL', thingo);
end
return;
end
error('Unknown command %s specified to HordeHelper.', cmd);
return;