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

Download this file

218 lines (197 with data), 6.2 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
%%
%% 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 keeps session data. Controls all objects and it's data
-module(wings_session).
-export([start/2, init/2, loop/2,
call/2, cast/2,
refresh/2]).
-include("wings.hrl").
-include_lib("wx/include/gl.hrl").
call(S,Fun) ->
wings_server:call(S,Fun).
cast(S,Fun) ->
wings_server:cast(S,Fun).
refresh(Id,DC0) ->
%% Hmm do we forget old binds here
DC = wings_draw:gl_send_bins(DC0),
put(dc,array:set(Id,DC,get(dc))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start(DEnv, Args) ->
spawn(?MODULE, init, [DEnv, Args]).
init(#d_env{wx=Wx,gl=GL, size={W,H}}, _Args) ->
link(whereis(wings)),
wings ! {session, self()},
wx:set_env(Wx),
wxGLCanvas:setCurrent(GL),
%% {R,G,B} = wxWindow:getBackgroundColour(GL),
gl:clearColor(0.6,0.6,0.7,1.0),
put(win_size, {W,H}),
put(gl_win, GL),
gl:viewport(0,0,W,H),
gl:matrixMode(?GL_PROJECTION),
gl:loadIdentity(),
gl:ortho( -2.0, 2.0, -2.0*H/W, 2.0*H/W, -20.0, 20.0),
gl:matrixMode(?GL_MODELVIEW),
gl:loadIdentity(),
Light = hemi(),
Select = select(),
put(light_shader, Light),
put(select_shader, Select),
gl:enable(?GL_DEPTH_TEST),
gl:depthFunc(?GL_LESS),
put(dc, array:new()),
GS = #gs{selmode=face,mat=wings_material:default(),
ssels=gb_trees:empty()},
loop(GS,undefined).
loop(State, Last) ->
receive
{repaint, #view{} = View} ->
DEnv = Last#d_env{view=View},
repaint(DEnv,State),
loop(State, DEnv);
{repaint, DEnv} ->
repaint(DEnv,State),
loop(State, DEnv);
{size, #d_env{size={W,H}} = DEnv} ->
put(win_size, DEnv#d_env.size),
gl:viewport(0,0,W,H),
loop(State, DEnv);
close ->
normal;
Op ->
NewState = wings_server:invoke(Op, State),
%% Maybe not call repaint everytime..:-)
repaint(Last,NewState),
loop(NewState, Last)
end.
repaint(DEnv, State) ->
DCs0 = get(dc),
gl:clearColor(0.6,0.6,0.7,1.0),
gl:clear(?GL_COLOR_BUFFER_BIT bor ?GL_DEPTH_BUFFER_BIT),
wings_view:load_matrices(DEnv#d_env.view, false),
%%gl:rotatef(Deg, 0.0, 1.0, 0.0),
SelMode = vertex,
Shading = flat,
WinProp = [wires,vertex_color,material],
gl:useProgram(get(light_shader)),
DCs = wings_draw:all(SelMode,Shading,WinProp,State,DCs0),
gl:useProgram(get(select_shader)),
gl:depthFunc(?GL_LEQUAL),
wings_draw:draw_selection(face, State, DCs),
gl:depthFunc(?GL_LESS),
gl:useProgram(0),
mini_axis_icon(),
wxGLCanvas:swapBuffers(DEnv#d_env.gl),
put(dc,DCs).
mini_axis_icon() ->
gl:pushAttrib(?GL_ALL_ATTRIB_BITS),
{W,H} = get(win_size),
Matrix1 = gl:getDoublev(?GL_MODELVIEW_MATRIX),
Matrix2 = setelement(13, Matrix1, -W/H+0.11),
Matrix3 = setelement(14, Matrix2, -1.0+0.11),
Matrix4 = setelement(15, Matrix3, 0.0),
gl:matrixMode(?GL_PROJECTION),
gl:pushMatrix(),
gl:loadIdentity(),
gl:ortho(-W/H, W/H, -1, 1, 0.00001, 10000000.0),
gl:matrixMode(?GL_MODELVIEW),
gl:pushMatrix(),
gl:loadIdentity(),
gl:loadMatrixd(Matrix4),
draw_mini_axis(),
gl:popMatrix(),
gl:matrixMode(?GL_PROJECTION),
gl:popMatrix(),
gl:matrixMode(?GL_MODELVIEW),
gl:popAttrib().
draw_mini_axis() ->
{PA,PB} = {0.08,0.01},
{Red,Green,Blue} = {{1,0,0},{0,1,0},{0,0,1}},
gl:'begin'(?GL_LINES),
%% X Axis
gl:color3fv(Red),
gl:vertex3f(0,0,0),
gl:vertex3f(0.1,0.0,0.0),
%% Y Axis
gl:color3fv(Green),
gl:vertex3f(0,0,0),
gl:vertex3f(0.0,0.1,0.0),
%% Z Axis
gl:color3fv(Blue),
gl:vertex3f(0,0,0),
gl:vertex3f(0.0,0.0,0.1),
%% X Arrows
gl:color3fv(Red),
gl:vertex3f(PA,0.0,-PB),
gl:vertex3f(0.1,0.0,0.0),
gl:vertex3f(PA,0.0,PB),
gl:vertex3f(0.1,0.0,0.0),
%% Y Arrows
gl:color3fv(Green),
gl:vertex3f(-PB,PA,0.0),
gl:vertex3f(0.0,0.1,0.0),
gl:vertex3f(PB,PA,0.0),
gl:vertex3f(0.0,0.1,0.0),
%% Z Arrows
gl:color3fv(Blue),
gl:vertex3f(-PB,0.0,PA),
gl:vertex3f(0.0,0.0,0.1),
gl:vertex3f(PB,0.0,PA),
gl:vertex3f(0.0,0.0,0.1),
gl:'end'().
hemi() ->
Sh = wings_gl:compile(vertex, light_shader_src()),
Prog = wings_gl:link_prog([Sh]),
gl:useProgram(Prog),
wings_pref:set_default(hl_lightpos, {3.0,10.0,1.0}),
wings_pref:set_default(hl_skycol, {0.95,0.95,0.90}),
wings_pref:set_default(hl_groundcol, {0.026,0.024,0.021}),
wings_gl:set_uloc(Prog, "LightPosition", wings_pref:get_value(hl_lightpos)),
wings_gl:set_uloc(Prog, "SkyColor", wings_pref:get_value(hl_skycol)),
wings_gl:set_uloc(Prog, "GroundColor", wings_pref:get_value(hl_groundcol)),
Prog.
select() ->
Sh = wings_gl:compile(vertex, select_shader_src()),
Prog = wings_gl:link_prog([Sh]),
gl:useProgram(Prog),
DepthBits = element(1,gl:getIntegerv(?GL_DEPTH_BITS)),
Depth = 1 bsl DepthBits,
wings_gl:set_uloc(Prog, "DepthBias", Depth),
Prog.
select_shader_src() ->
<<"
uniform float DepthBias;
void main()
{
vec4 pos = ftransform();
// pos.xyz /= pos.w; // Pre-calc
// pos.w = 1.0; //
// pos.z += 10*DepthBias; // Offset selection in Z dir
gl_Position = pos;
gl_FrontColor = vec4(1.0,0.0,0.0,1.0);
}
">>.
light_shader_src() ->
<<"
uniform vec3 LightPosition;
uniform vec3 SkyColor;
uniform vec3 GroundColor;
void main()
{
vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);
vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);
vec3 lightVec = normalize(LightPosition - ecPosition);
float costheta = dot(tnorm, lightVec);
float a = 0.5 + 0.5 * costheta;
// ATI needs this for vcolors to work
vec4 color = gl_FrontMaterial.diffuse * gl_Color;
gl_FrontColor = color * vec4(mix(GroundColor, SkyColor, a),
1.0);
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
">>.