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

Download this file

397 lines (358 with data), 12.1 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
%%
%% Copyright (c) 2007 Wings Dev
%%
%% See the file "license.terms" for information on usage and redistribution
%% of this file, and for a DISCLAIMER OF ALL WARRANTIES.
%%
%% @doc This module handles the selection and mouse over highlight
%% Most of this code runs in the session process.
-module(wings_pick).
-compile(binary_comprehension).
-include("wings.hrl").
-include_lib("wx/include/gl.hrl").
-include_lib("wx/include/wx.hrl").
-export([pick/5]).
-export([event/2]).
-import(lists, [map/2,min/1,sort/1,member/2, reverse/2]).
-define(B24, 8388608).
-define(BFFF, 16777215).
-record(pick,
{session, % Session
op, % Operation: add/delete
view % View data
}).
%% For marquee picking.
-record(marquee,
{ox,oy, %Original X,Y.
cx,cy, %Current X,Y.
st
}).
%% For highlighting.
-record(hl,
{st, %Saved state.
redraw, %Redraw function.
always_dirty = false, %Always redraw on mousemotion?
filter, %Filter fun, allow hl? true/false
prev=none %Previous hit ({Id,Item}).
}).
%% Marquee select will be a mess :-) later...
%% Will not work with color buffer selection used below.
%%% event is called by a gui process.
event(#wx{event=#wxMouse{type=motion}=Mm}, #gstate{selmode=Mode}) ->
case hilite_enabled(Mode) of
false -> next;
true ->
%% FIXME {seq,push,handle_hilite_event(Mm, #hl{st=St,redraw=Redraw})}
next
end;
event(#wx{event=#wxMouse{type=left_down,x=X,y=Y}},
#gstate{session=Sess, d_env=DEnv}) ->
wings_session:call(Sess, fun(GS) -> pick(X,Y,fIXME_MOD,GS,DEnv) end);
event(_, _) -> next.
hilite_enabled(vertex) -> wings_pref:get_value(vertex_hilite);
hilite_enabled(edge) -> wings_pref:get_value(edge_hilite);
hilite_enabled(face) -> wings_pref:get_value(face_hilite);
hilite_enabled(body) -> wings_pref:get_value(body_hilite);
hilite_enabled(object) -> wings_pref:get_value(object_hilite).
%% pick(X, Y, Mod, St) -> %% FIXME when Mod band (?SHIFT_BITS bor ?CTRL_BITS) =/= 0 ->
%% Pick = #marquee{ox=X,oy=Y,st=St},
%% clear_hilite_marquee_mode(Pick);
pick(X, Y, _, GS0, DEnv) ->
case do_pick(X, Y, GS0, DEnv) of
none ->
%% Pick = #marquee{ox=X,oy=Y,st=St0},
%% clear_hilite_marquee_mode(Pick);
{next,GS0};
{PickOp,GS} ->
% wings_wm:dirty(),
% wings_draw:refresh_dlists(St),
Pick = #pick{op=PickOp,session=self(),view=DEnv},
{{seq,push,get_pick_event(Pick)}, GS}
end.
%% Runned by gui process
get_pick_event(Pick) ->
{replace,fun(Ev) -> pick_event(Ev, Pick) end}.
pick_event(redraw, #pick{}) ->
% wings:redraw(St),
keep;
pick_event(#wx{event=#wxMouse{type=motion,x=X,y=Y}},
#pick{op=Op, session=Session, view=DEnv}) ->
DoPick = fun(GS0) ->
case do_pick(X,Y,GS0,DEnv) of
none -> {keep, GS0};
{Op,GS} ->
%% wings_wm:dirty(),
%% wings_draw:refresh_dlists(St),
{keep,GS};
{_,_} -> {keep, GS0}
end
end,
wings_session:call(Session, DoPick);
pick_event(#wx{event=#wxMouse{type=left_up}}, #pick{}) ->
%% wings_wm:later({new_state,St}),
pop;
pick_event(_, _) -> keep.
do_pick(X, Y, GS, DEnv) ->
case raw_pick(X, Y, GS, DEnv) of
none -> none;
Hit -> update_selection(Hit, GS)
end.
raw_pick(X0,Y0,GS=#gs{selmode=SMode},#d_env{size={_,H},view=View}) ->
gl:clearColor(1.0,1.0,1.0,1.0),
gl:clear(?GL_COLOR_BUFFER_BIT bor ?GL_DEPTH_BUFFER_BIT),
gl:disable(?GL_TEXTURE_2D),
gl:disable(?GL_DITHER), %% ?
gl:disable(?GL_FOG), %% ?
gl:disable(?GL_LIGHTING),
gl:enable(?GL_COLOR_MATERIAL),
gl:shadeModel(?GL_FLAT),
gl:matrixMode(?GL_PROJECTION),
gl:loadIdentity(),
X = X0,
Y = H-Y0,
S = 7, Sd2 = S div 2,
wings_view:projection(View),
wings_view:modelview(View),
%% case wings_wm:lookup_prop(select_backface) of FIXME
%% {value,true} ->
%% draw();
%% _ ->
%% gl:enable(?GL_CULL_FACE),
%% draw(),
%% gl:disable(?GL_CULL_FACE)
%% end,
DC = get(dc),
%% We do this in two steps to get enough color/id bits.
gl:disableClientState(?GL_COLOR_ARRAY),
draw_select(GS,DC),
gl:bindBuffer(?GL_ELEMENT_ARRAY_BUFFER, 0),
Bin = wx:create_memory(S*S*3),
gl:readBuffer(?GL_BACK),
gl:pixelStorei(?GL_PACK_ALIGNMENT, 1),
gl:readPixels(X-Sd2,Y-Sd2,S,S,?GL_RGB,?GL_UNSIGNED_BYTE, Bin),
%% This can proberly be faster on cards with fbo and float buffers..
%% We should be able to draw it once on them.
case get_id(S,wx:get_memory_bin(Bin)) of
none ->
none;
MObj when SMode =:= object ->
{object, {unmirrored_id(MObj), 0}};
MObj when SMode =:= body ->
Obj = unmirrored_id(MObj),
#object{body=Body} = array:get(Obj,GS#gs.objects),
{body, {Obj, Body}};
MObj -> % when in face/vertex/edge mode
%% Find body
gl:clear(?GL_COLOR_BUFFER_BIT bor ?GL_DEPTH_BUFFER_BIT),
Mxs = draw_select_face(unmirrored_id(MObj),GS#gs.objects,DC),
gl:disableClientState(?GL_COLOR_ARRAY),
gl:readPixels(X-Sd2,Y-Sd2,S,S,?GL_RGB,?GL_UNSIGNED_BYTE, Bin),
Face = get_id(S,wx:get_memory_bin(Bin)),
convert_hit(MObj,Face,SMode,X,Y,GS,Mxs,View)
end.
draw_select(#gs{top=ObjList,objects=Obs},DC) ->
draw_select_1(gb_sets:to_list(ObjList),Obs,DC).
draw_select_1([O|Os],Obs,DC) ->
#object{matrix=M,body=Body} = array:get(O, Obs),
gl:pushMatrix(),
gl:multMatrixf(e3d_mat:expand(M)),
draw_select_2(Body,O,Obs,DC,object),
gl:popMatrix(),
draw_select_1(Os, Obs, DC);
draw_select_1([],_Obs,_DC) ->
ok.
draw_select_2(Os,_,Obs,DC,_) when is_list(Os) ->
draw_select_1(Os,Obs,DC);
draw_select_2(Body,Object,_Obs,DC,Mode) when is_integer(Body) ->
#dc{fv_bin=VsBin,size=Max,fi={FI,Size,_},fid=Fid,mirror=MM}
= array:get(Body, DC),
case Mode of
face ->
gl:color3ubv({255,0,0}),
wings_draw:activate_face_ids(Fid);
_ -> ok
end,
wings_draw:activate_vertices(VsBin),
wings_draw:activate_index(FI),
gl:color3ubv(wings_setup_dc:color_id(Object)),
gl:drawRangeElements(?GL_TRIANGLES,0,Max,Size,?GL_UNSIGNED_INT,0),
case MM of
none -> none;
_ ->
gl:pushMatrix(),
gl:multMatrixf(e3d_mat:expand(MM)),
gl:color3ubv(wings_setup_dc:color_id(Object+?B24)),
gl:drawRangeElements(?GL_TRIANGLES,0,Max,Size,
?GL_UNSIGNED_INT,0),
gl:popMatrix(),
MM
end.
draw_select_face(Obj,Obs,DC) ->
#object{body=Body} = array:get(Obj, Obs),
true = is_integer(Body), %% Assert
gl:pushMatrix(),
TransMat = e3d_mat:expand(wings_u:trans_mat(Obj,Obs)),
gl:multMatrixf(TransMat),
MM = draw_select_2(Body,Obj,ignore,DC,face),
gl:popMatrix(),
{TransMat,MM}.
%% print([A,B,C,D,E|R]) ->
%% print(R),
%% io:format("~p ~p ~p ~p ~p~n", [A,B,C,D,E]);
%% print([]) -> ok.
get_id(S, Bin) ->
Pixels = [Id || <<Id:24>> <= Bin],
%% print(Pixels),
N = S div 2,
get_id(-N,-N,Pixels,N+1,[]).
get_id(R,C,[Id|Ids],Max,Acc) when R =< Max ->
get_id(R+1,C,Ids,Max,[{R*R+C*C,Id}|Acc]);
get_id(_R,Max, [], Max, Acc) ->
get_id(sort(Acc));
get_id(_,C,Ids,Max,Acc) when C =< Max ->
get_id(-Max,C+1,Ids,Max,Acc).
get_id([{_,?BFFF}|List]) ->
get_id(List);
get_id([{_,Id}|_]) -> Id;
get_id([]) -> none.
unmirrored_id(Obj) when Obj < ?B24 ->
Obj;
unmirrored_id(Obj) ->
Obj - ?B24.
convert_hit(Obj,Face,SMode,X,Y,#gs{objects=Os,bodies=Bs, sel=Sel, sh=Sh},{TM,MM},View) ->
Mode = if Sh, SMode =/= body, Sel == [] ->
{auto,SMode};
true -> SMode
end,
Id = unmirrored_id(Obj),
#object{body=Body} = array:get(Id,Os),
WePid = array:get(Body, Bs),
if Mode =:= face ->
{face, {Id, Face}};
Obj < ?B24 ->
Ms = wings_u:get_matrices(TM,none,View),
Convert = fun(We) -> {convert_hit_1(Mode,X,Y,Id,Face,Ms,We),We} end,
wings_we:call(WePid, Convert);
true -> %% >= ?B24 i.e. Mirrored
Ms = wings_u:get_matrices(TM,MM,View),
Convert = fun(We) -> {convert_hit_1(Mode,X,Y,Id,Face,Ms,We),We} end,
wings_we:call(WePid, Convert)
end.
convert_hit_1({auto,_}, X, Y, Id, Face, Trans, We) ->
Vs = sort(find_vertex(Face, We, X, Y, Trans)),
[{Vdist0,{Xva,Yva},V},{_,{Xvb,Yvb},_}|_] = Vs,
Vdist = math:sqrt(Vdist0),
Es = find_edge(Face, We, X, Y, Trans),
{Edist0,_,Edge} = min(Es),
Edist = math:sqrt(Edist0),
Xd = Xva-Xvb,
Yd = Yva-Yvb,
Lim0 = math:sqrt(Xd*Xd+Yd*Yd) / 4,
Lim1 = min([math:sqrt(L) || {_,L,_} <- Es]) / 4,
Lim = min([20.0,Lim0,Lim1]),
Hilite = if
Vdist < Lim -> {vertex,{Id,V}};
Edist < Lim -> {edge,{Id,Edge}};
true -> {face,{Id,Face}}
end,
check_restriction(Hilite, Id, V, Edge, Face);
convert_hit_1(Mode, X, Y, Id, Face, Trans, We) ->
case Mode of
vertex ->
{_,_,V} = min(find_vertex(Face, We, X, Y, Trans)),
{vertex,{Id,V}};
edge ->
{_,_,E} = min(find_edge(Face, We, X, Y, Trans)),
{edge,{Id,E}}
end.
find_vertex(Face, We, X, Y, Trans) ->
Vs0 = wings_face:vertices_ccw(Face, We),
map(fun(V) ->
{Xs,Ys} = Pos = project_vertex(V, We, Trans),
Dx = X-Xs,
Dy = Y-Ys,
{Dx*Dx+Dy*Dy,Pos,V}
end, Vs0).
find_edge(Face, We, Cx, Cy, Trans) ->
wings_face:fold(
fun(_, Edge, #edge{vs=Va,ve=Vb}, A) ->
{Ax,Ay} = project_vertex(Va, We, Trans),
{Bx,By} = project_vertex(Vb, We, Trans),
if
is_float(Ax), is_float(Ay),
is_float(Bx), is_float(By) ->
Xdist = Bx-Ax,
Ydist = By-Ay,
L = Xdist*Xdist+Ydist*Ydist,
{Px,Py} =
try ((Cx-Ax)*Xdist+(Cy-Ay)*Ydist)/L of
R when R =< 0 -> {Ax,Ay};
R when R >= 1 -> {Bx,By};
R -> {Ax+R*Xdist,Ay+R*Ydist}
catch
error:badarith -> {Ax,Ay}
end,
Xdiff = Px-Cx,
Ydiff = Py-Cy,
DistSqr = Xdiff*Xdiff + Ydiff*Ydiff,
[{DistSqr,L,Edge}|A]
end
end, [], Face, We).
project_vertex(V, We, {ModelMatrix,ProjMatrix,ViewPort}) ->
{Px,Py,Pz} = wings_vertex:pos(V, We),
{Xs,Ys,_} = glu:project(Px, Py, Pz, ModelMatrix,
ProjMatrix, ViewPort),
{Xs,Ys}.
check_restriction({Mode,MM,_}=Hilite, Id, V, Edge, Face) ->
case wings:get_mode_restriction() of
all -> Hilite;
Modes ->
case member(Mode, Modes) of
true -> Hilite;
false -> restrict_hilite(Mode, Modes, Id, V, Edge, Face, MM)
end
end.
restrict_hilite(vertex, Modes, Id, _V, Edge, Face, MM) ->
case member(edge, Modes) of
true -> {edge,MM,{Id,Edge}};
false ->
true = member(face, Modes),
{face,MM,{Id,Face}}
end;
restrict_hilite(edge, Modes, Id, V, _Edge, Face, MM) ->
case member(vertex, Modes) of
true -> {vertex,MM,{Id,V}};
false ->
true = member(face, Modes),
{face,MM,{Id,Face}}
end;
restrict_hilite(face, Modes, Id, V, Edge, _Face, MM) ->
case member(edge, Modes) of
true -> {edge,MM,{Id,Edge}};
false ->
true = member(vertex, Modes),
{vertex,MM,{Id,V}}
end.
update_selection({Mode,{Id,Item}}, #gs{sel=Sel0}=GS) ->
{Type,Sel} = update_selection(Id, Item, Sel0, []),
%%io:format("Sel ~p ~p~n",[Mode, Sel]),
{Type,GS#gs{selmode=Mode,sel=Sel,sh=false}}.
update_selection(Id, Item, [{I,_}=H|T], Acc) when Id > I ->
update_selection(Id, Item, T, [H|Acc]);
update_selection(Id, Item, [{I,_}|_]=T, Acc) when Id < I ->
{add,reverse(Acc, [{Id,gb_sets:singleton(Item)}|T])};
update_selection(Id, Item, [{_,Items0}|T0], Acc) -> %Id == I
case gb_sets:is_member(Item, Items0) of
true ->
Items = gb_sets:delete(Item, Items0),
T = case gb_sets:is_empty(Items) of
true -> T0;
false -> [{Id,Items}|T0]
end,
{delete,reverse(Acc, T)};
false ->
Items = gb_sets:insert(Item, Items0),
{add,reverse(Acc, [{Id,Items}|T0])}
end;
update_selection(Id, Item, [], Acc) ->
{add,reverse(Acc, [{Id,gb_sets:singleton(Item)}])}.