[r137]: branches / wings2 / src / wings_setup_dc.erl  Maximize  Restore  History

Download this file

303 lines (276 with data), 10.8 kB

  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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
%%%-------------------------------------------------------------------
%%% File : wings_setup_dc.erl
%%% Author : Dan Gudmundsson <dgud@erix.ericsson.se>
%%% Description :
%%%
%%% Created : 4 Jul 2007 by Dan Gudmundsson <dgud@erix.ericsson.se>
%%%-------------------------------------------------------------------
-module(wings_setup_dc).
-export([build_faces/1, smooth_normals/3, color_id/1]).
-import(lists, [map/2,foreach/2,foldl/3,sort/1,keysort/2,
last/1,reverse/1,duplicate/2,seq/2,filter/2,zip/2]).
-include("wings.hrl").
-define(F64, 64/native-float).
%%-define(F64, 32/native-float).
-define(F32, 32/native-float).
-define(F, ?F32). %% Jesus!! using double killed performance
-define(UI32, 32/unsigned-native).
-define(INT_SZ, 4).
color_id(Id) when Id < 16777216 ->
<<R:8,G:8,B:8>> = <<Id:24>>,
{R,G,B}.
build_faces(We = #we{vp=OrigVpos,fs=Ftab}) ->
VisFs = wings_we:visible_fs(array:sparse_to_orddict(Ftab),We),
MatFs0 = wings_facemat:mat_faces(VisFs, We),
Init = {[],{0,0,[],[],[],[],[],[]}},
FaceInfo = fun(Finfo,Acc) -> build_face(Finfo,Acc,We) end,
{MatInfo,Buffers} =
foldl(fun({Mat,Fs},{MatFs,Acc}) ->
Start = element(1,Acc),
Next = foldl(FaceInfo,Acc,Fs),
Stop = element(1,Next),
{[{Mat,Start,Stop-Start}|MatFs],Next}
end, Init, MatFs0),
{EASize,Size,Fmap,Fnorm,Vpos,Vnor,FsInfo,FIds} = Buffers,
FaceIndex0 = foldl(fun({_,{_,_,Vs}}, Acc) -> Vs ++ Acc end, [], Fmap),
FaceIndex = list_to_binary([<<I:?UI32>>||I <- FaceIndex0]),
#dc{size = Size,
ev_bin = array_to_bin3(OrigVpos),%bind_binary(?GL_ARRAY_BUFFER),
ei = build_edge_index(We),
vi = build_vertex_index(We),
fv_bin = list_to_binary(reverse(Vpos)),%bind_binary(?GL_ARRAY_BUFFER),
fn_bin = list_to_binary(reverse(Vnor)),%bind_binary(?GL_ARRAY_BUFFER),
fi = {FaceIndex, %bind_binary(?GL_ELEMENT_ARRAY_BUFFER),
EASize,MatInfo},
fid = list_to_binary(reverse(FIds)),
hidden = wings_we:any_hidden(We),
matmode= We#we.matmode,
fmap = array:from_orddict(sort(Fmap))
% fnorm = array:from_orddict(sort(Fnorm)),
% fs = FsInfo
}.
build_face({_Face,undefined},{TVC,VC,Fmap,FNorm,Vpos,Vnor,Fs,FIds},_We) ->
{TVC,VC,[undefined|Fmap],[undefined|FNorm],Vpos,Vnor,Fs,FIds};
build_face({Face,Edge},{TVC,VC,Fmap,FNorm,Vpos,Vnor,Fs,FIds},We) ->
Pos = wings_face:vertex_positions(Face,Edge,We),
Normal = e3d_vec:normal(Pos),
{Vs,VsLen,VBin,VNBin,VsINo} = triangulate_face(Pos,VC,Normal),
%% io:format("~p ~p ~p~n",[Face, wings_face:vertices_ccw(Face,Edge,We), Pos]),
{TVC+VsLen,VC+VsINo,
[{Face,{TVC,VsLen,Vs}}|Fmap],
[{Face,Normal}|FNorm],
[VBin|Vpos],
[VNBin|Vnor],
[[VsINo|Face]|Fs],
[duplicate(VsINo,<<Face:24>>)|FIds]}.
triangulate_face([{X1,Y1,Z1},{X2,Y2,Z2},{X3,Y3,Z3}],I,{NX,NY,NZ}) ->
NormB = <<NX:?F,NY:?F,NZ:?F>>,
{[I,I+1,I+2],3,
<<X1:?F,Y1:?F,Z1:?F,
X2:?F,Y2:?F,Z2:?F,
X3:?F,Y3:?F,Z3:?F>>,
[NormB,NormB,NormB],
3};
triangulate_face([V1={X1,Y1,Z1},V2={X2,Y2,Z2},V3={X3,Y3,Z3},V4={X4,Y4,Z4}],
I0,N={NX,NY,NZ}) ->
NormB = <<NX:?F,NY:?F,NZ:?F>>,
I1 = I0+1, I2 = I0+2, I3 = I0+3,
Vlist = case good_triangulation(N,V1,V2,V3,V4) of
true -> [I0,I1,I2,I0,I2,I3];
false -> [I0,I1,I3,I3,I1,I2]
end,
{Vlist,6,
<<X1:?F,Y1:?F,Z1:?F,
X2:?F,Y2:?F,Z2:?F,
X3:?F,Y3:?F,Z3:?F,
X4:?F,Y4:?F,Z4:?F>>,
[NormB,NormB,NormB,NormB],
4};
triangulate_face(VPos,I0,N={NX,NY,NZ}) ->
NormB = <<NX:?F,NY:?F,NZ:?F>>,
{Fs,Ps} = glu:tesselate(N, VPos),
Count = size(Ps) div (3*8), %% 64bits
%% io:format("~p ~p ~p ~n",[Fs,Ps,Count]),
{[F+I0|| F <- Fs], % Fix indecies,
length(Fs), % Indexlist length
%% Posistion needs to converted to 32 bits
<< <<Ver:?F>> || <<Ver:?F64>> <= Ps>>,
duplicate(Count,NormB),% Add normals
Count}.
build_vertex_index(We) ->
Vis = wings_we:visible_vs(We),
Vs = foldl(fun(Vertex,Acc) -> [<<Vertex:?UI32>>|Acc] end, [], Vis),
Bin = list_to_binary(Vs), %% bind_binary(, ?GL_ELEMENT_ARRAY_BUFFER)
{Bin,size(Bin) div ?INT_SZ}.
build_edge_index(We = #we{es=Etab,he=He,hi=HidFs}) ->
case gb_sets:is_empty(He) andalso gb_sets:is_empty(HidFs) of
true ->
Eds = array:foldl(fun(_I, #edge{vs=Vs,ve=Ve}, Acc) ->
[<<Vs:?UI32,Ve:?UI32>>|Acc];
(_, _, Acc) -> Acc
end, [], Etab),
TEMPSz= length(Eds)*2,
{list_to_binary(Eds), %bind_binary(?GL_ELEMENT_ARRAY_BUFFER),
TEMPSz, 0};
false ->
Vis = gb_sets:from_ordset(wings_we:visible_es(We)),
VisSmooth = gb_sets:difference(Vis, He),
VisHard = gb_sets:intersection(Vis,He),
Collect = fun(Edge, Acc) ->
#edge{vs=Vs,ve=Ve} = array:get(Edge,Etab),
[<<Vs:?UI32,Ve:?UI32>>|Acc]
end,
SmoothVsI = lists:foldl(Collect,[],gb_sets:to_list(VisSmooth)),
HardVsI = lists:foldl(Collect,[],gb_sets:to_list(VisHard)),
{list_to_binary([SmoothVsI,HardVsI]),%bind_binary(?GL_ELEMENT_ARRAY_BUFFER),
gb_sets:size(VisSmooth)*2, gb_sets:size(VisHard)*2}
end.
array_to_bin3(Vpos) ->
Far = 99999999999999999999999.99,
Unseen = <<Far:?F,Far:?F,Far:?F>>,
array:foldr(fun(_,{X,Y,Z},Acc) ->
[<<X:?F,Y:?F,Z:?F>>|Acc];
(_,_,Acc) -> [Unseen|Acc]
end, [], Vpos).
%% good_triangulation(Normal, Point1, Point2, Point3, Point4) -> true|false
%% Return true if triangulation by connecting Point1 to Point3 is OK.
%% The normal Normal should be averaged normal for the quad.
good_triangulation({Nx,Ny,Nz}, {Ax,Ay,Az}, {Bx,By,Bz}, {Cx,Cy,Cz}, {Dx,Dy,Dz})
when is_float(Ax), is_float(Ay), is_float(Az) ->
CAx = Cx-Ax, CAy = Cy-Ay, CAz = Cz-Az,
ABx = Ax-Bx, ABy = Ay-By, ABz = Az-Bz,
DAx = Dx-Ax, DAy = Dy-Ay, DAz = Dz-Az,
D1 = Nx*(CAy*ABz-CAz*ABy) + Ny*(CAz*ABx-CAx*ABz) + Nz*(CAx*ABy-CAy*ABx),
D2 = Nx*(DAz*CAy-DAy*CAz) + Ny*(DAx*CAz-DAz*CAx) + Nz*(DAy*CAx-DAx*CAy),
good_triangulation_1(D1, D2).
good_triangulation_1(D1, D2) when D1 > 0, D2 > 0 -> true;
good_triangulation_1(_, _) -> false.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Calculate smooth_vertex normals
smooth_normals(Ns, Fs, #we{mirror=none,hi=Hi}=We) ->
case gb_sets:is_empty(Hi) of
true -> snormals_2(Ns,Fs,We);
false -> snormals_1(Ns,Fs,We)
end;
smooth_normals(Ns,Fs,We) -> snormals_1(Ns,Fs,We).
snormals_1(FaceNormals,Fs,#we{he=Htab0,hi=Hi}=We) ->
Edges = case {wings_we:visible_fs(We),gb_sets:size(Hi)} of
{Vis,HiSz} when length(Vis) < HiSz ->
wings_face:outer_edges(Vis, We);
{_,_} ->
InVis = gb_sets:to_list(Hi),
wings_face:outer_edges(InVis, We)
end,
Htab = gb_sets:union(Htab0, gb_sets:from_ordset(Edges)),
snormals_2(FaceNormals, Fs, We#we{he=Htab}).
snormals_2(FaceNormals,Fs,#we{he=He}=We) ->
%% wings_pb:start(?__(1,"calculating soft normals")), FIXME
Res = case FaceNormals of
[_,_] ->
two_faced(FaceNormals, We);
_ ->
case gb_sets:is_empty(He) of
true -> all_soft(FaceNormals,Fs,We);
false -> mixed_edges(FaceNormals,Fs,We)
end
end,
%% wings_pb:done(Res). FIXME
list_to_binary(Res).
all_soft(FaceNormals,Fs,#we{vp=Vtab}=We) ->
%% wings_pb:update(0.10, ?__(1,"preparing")),
VisVs = ?TC(wings_we:visible_vs(wings_util:keys(Vtab),We)),
VtxNormals = soft_vertex_normals(VisVs, FaceNormals, We),
GetNormal = fun(_F,V,Acc) ->
Normal = array:get(V, VtxNormals),
[Normal|Acc]
end,
%% wings_pb:update(0.6, ?__(2,"collecting")),
?TC(collect_normals(GetNormal,FaceNormals,Fs,We)).
collect_normals(CalcNormal, FaceNormals,Fs,We) ->
FaceFold = fun([Size|Face],Acc) ->
Fun = fun(V,_,_,[C|A]) ->
[C+1|CalcNormal(Face,V,A)]
end,
case wings_face:fold(Fun,[0|Acc], Face, We) of
[Size|Res] -> Res;
[Len|Res] ->
{X,Y,Z} = array:get(Face,FaceNormals),
lists:duplicate(Size-Len,<<X:?F,Y:?F,Z:?F>>) ++ Res
end
end,
foldl(FaceFold,[],Fs).
mixed_edges(FaceNormals,Fs,We) ->
%% wings_pb:update(0.20, ?__(1,"preparing")),
G = digraph:new(),
VtxNormals = vertex_normals(We, G, FaceNormals),
%% wings_pb:update(0.85, ?__(2,"vertex normals per face")),
Mixed = fun(Face,V,Acc) ->
case try array:get(V, VtxNormals) catch _:_ -> undefined end of
undefined ->
Normal = hard_vtx_normal(G, V, Face, FaceNormals),
[Normal|Acc];
Normal ->
[Normal|Acc]
end
end,
Ns = collect_normals(Mixed,FaceNormals,Fs,We),
digraph:delete(G),
Ns.
hard_vtx_normal(G, V, Face, FaceNormals) ->
Reachable = digraph_utils:reachable([{V,Face}], G),
case [array:get(AFace, FaceNormals) || {_,AFace} <- Reachable] of
[{X,Y,Z}] -> <<X:?F,Y:?F,Z:?F>>;
Ns ->
{X,Y,Z} = e3d_vec:norm(e3d_vec:add(Ns)),
Res = << X:?F,Y:?F,Z:?F >>,
Res
end.
%% FIXME I havn't looked at this at all
two_faced([{FaceA,Na},{FaceB,Nb}], We) ->
[{FaceA,two_faced_1(FaceA, Na, We)},
{FaceB,two_faced_1(FaceB, Nb, We)}].
two_faced_1(Face, Normal, We) ->
wings_face:fold_vinfo(fun (_, VInfo, Acc) ->
[[VInfo|Normal]|Acc]
end, [], Face, We).
vertex_normals(#we{vp=Vtab}=We, G, FaceNormals) ->
Vs = wings_we:visible_vs(wings_util:keys(Vtab),We),
vertex_normals_1(Vs, We, G, FaceNormals).
vertex_normals_1(Vs, #we{es=Etab,he=Htab}=We, G, FaceNormals) ->
He0 = gb_sets:to_list(Htab),
He = sofs:from_external(He0, [edge]),
Es0 = array:to_orddict(Etab),
Es1 = sofs:from_external(Es0, [{edge,data}]),
Es = sofs:image(Es1, He),
Hvs0 = foldl(fun(#edge{vs=Va,ve=Vb}, A) ->
[Va,Vb|A]
end, [], sofs:to_external(Es)),
Hvs = ordsets:from_list(Hvs0),
Svs = ordsets:subtract(Vs,Hvs),
foreach(fun(V) -> update_digraph(G, V, We) end, Hvs),
soft_vertex_normals(Svs, FaceNormals, We).
update_digraph(G, V, #we{he=Htab}=We) ->
wings_vertex:fold(
fun(_, Edge, #edge{lf=Lf0,rf=Rf0}, _) ->
case gb_sets:is_member(Edge, Htab) of
true -> ok;
false ->
Lf = {V,Lf0},
Rf = {V,Rf0},
digraph:add_vertex(G, Lf),
digraph:add_vertex(G, Rf),
digraph:add_edge(G, Lf, Rf),
digraph:add_edge(G, Rf, Lf)
end
end, [], V, We).
soft_vertex_normals(Vs, FaceNormals, We) ->
FoldFun = fun(Face, _, _, A) ->
Normal = array:get(Face, FaceNormals),
[Normal|A]
end,
Soft = ?TC(foldl(fun(V, Acc) ->
Ns = wings_vertex:fold(FoldFun, [], V, We),
{X,Y,Z} = e3d_vec:norm(e3d_vec:add(Ns)),
[{V,<<X:?F,Y:?F,Z:?F>>}|Acc]
end, [], Vs)),
array:from_orddict(reverse(Soft)).