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

Download this file

197 lines (170 with data), 5.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
%%
%% 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 contains opengl wrappers and utilites.
-module(wings_gl).
-compile(export_all). %% FIXME
-export([]).
-include("wings.hrl").
-include_lib("wx/include/gl.hrl").
%%%
%%% OpenGL extensions.
%%%
init_extensions() ->
?INFO("Opengl information: ~n",[]),
?INFO(" Red Size: ~p~n",
[element(1,gl:getIntegerv(?GL_RED_BITS))]),
?INFO(" Green Size: ~p~n",
[element(1,gl:getIntegerv(?GL_GREEN_BITS))]),
?INFO(" Blue Size: ~p~n",
[element(1,gl:getIntegerv(?GL_BLUE_BITS))]),
?INFO(" Alpha Size: ~p~n",
[element(1,gl:getIntegerv(?GL_ALPHA_BITS))]),
?INFO(" Stencil Size: ~p~n",
[element(1,gl:getIntegerv(?GL_STENCIL_BITS))]),
?INFO(" Depth Size: ~p~n",
[element(1,gl:getIntegerv(?GL_DEPTH_BITS))]),
ets:new(wings_gl_ext, [named_table,public,ordered_set]),
Exts0 = lists:sort(string:tokens(gl:getString(?GL_EXTENSIONS), " ")),
Exts = [{list_to_atom(E)} || E <- Exts0],
ets:insert(wings_gl_ext, Exts),
Ver = case catch get_version() of
{_,_,_}=V -> V;
_ -> {1,1,0}
end,
ets:insert(wings_gl_ext, {version,Ver}).
get_version() ->
case string:tokens(gl:getString(?GL_VERSION), ". ") of
[Major0,Minor0] ->
Patch = 0;
[Major0,Minor0,Patch0|_] ->
case catch list_to_integer(Patch0) of
{'EXIT',_} -> Patch = 0;
Patch -> Patch
end
end,
Major = list_to_integer(Major0),
Minor = list_to_integer(Minor0),
{Major,Minor,Patch}.
%% Either check for a given version (or higher), or
%% for that all the given extensions are implemented.
is_ext(Wanted) when is_tuple(Wanted), size(Wanted) >= 2 ->
[{_,Actual}] = ets:lookup(wings_gl_ext, version),
version_match(Wanted, Actual);
is_ext(Ext) when is_atom(Ext); is_list(Ext) ->
is_ext_1(Ext).
%% Must be Wanted version or higher, or the List of extensions must match.
is_ext(Wanted, []) ->
is_ext(Wanted);
is_ext(Wanted, List) ->
is_ext(Wanted) orelse is_ext_1(List).
is_ext_1([]) ->
true;
is_ext_1([Name|R]) ->
is_ext_1(Name) andalso is_ext(R);
is_ext_1(Name) ->
ets:member(wings_gl_ext, Name).
version_match({Ma1,_}, {Ma2,_,_})
when Ma1 < Ma2 ->
true;
version_match({Ma1,Mi1}, {Ma2,Mi2,_})
when Ma1 =< Ma2, Mi1 =< Mi2 -> true;
version_match({Ma1,Mi1,P1}, {Ma2,Mi2,P2})
when Ma1 =< Ma2, Mi1 =< Mi2, P1 =< P2 -> true;
version_match(_,_) ->
false.
error_string(0) -> no_error;
error_string(?GL_INVALID_VALUE) -> "GL_INVALID_VALUE";
error_string(?GL_INVALID_ENUM) -> "GL_INVALID_ENUM";
error_string(?GL_INVALID_OPERATION) -> "GL_INVALID_OPERATION";
error_string(?GL_STACK_OVERFLOW) -> "GL_STACK_OVERFLOW";
error_string(?GL_STACK_UNDERFLOW) -> "GL_STACK_UNDERFLOW";
error_string(?GL_OUT_OF_MEMORY) -> "GL_OUT_OF_MEMORY";
error_string(Error) -> "Error: "++integer_to_list(Error).
%%%
%%% Error checking in debug builds.
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Shader compilation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Use ARB functions until Mac OsX have been upgraded to opengl 2.0
support_shaders() ->
is_ext({2,0},['GL_ARB_fragment_shader', 'GL_ARB_vertex_shader']).
uloc(Prog, What) ->
try gl:getUniformLocation(Prog, What) of
-1 ->
io:format("Warning the uniform variable '~p' is not used~n",[What]),
-1;
Where ->
Where
catch _:_ ->
ErrStr = "ERROR The uniform variable ~p should be a string~n",
Err = io_lib:format(ErrStr, [What]),
throw(lists:flatten(Err))
end.
set_uloc(Prog, Var, Val) ->
case uloc(Prog, Var) of
-1 -> ok;
Pos -> set_uloc(Pos,Val)
end.
set_uloc(Pos, A) when is_integer(A) ->
gl:uniform1i(Pos,A);
set_uloc(Pos, A) when is_float(A) ->
gl:uniform1f(Pos,A);
set_uloc(Pos, {A,B}) when is_float(A),is_float(B) ->
gl:uniform2f(Pos,A,B);
set_uloc(Pos, {A,B,C}) when is_float(A),is_float(B),is_float(C) ->
gl:uniform3f(Pos,A,B,C).
compile(What, Bin) when is_binary(Bin) ->
compile(What, binary_to_list(Bin));
compile(vertex, Bin) when is_list(Bin) ->
compile2(?GL_VERTEX_SHADER, "Vertex", Bin);
compile(fragment, Bin) when is_list(Bin) ->
compile2(?GL_FRAGMENT_SHADER, "Fragment", Bin).
compile2(Type,Str,Src) ->
Handle = gl:createShaderObjectARB(Type),
%% io:format("~p ~p ~n", [Handle, Src]),
ok = gl:shaderSource(Handle, [Src]),
ok = gl:compileShader(Handle),
check_status(Handle,Str, ?GL_OBJECT_COMPILE_STATUS_ARB),
Handle.
link_prog(Objs) when is_list(Objs) ->
Prog = gl:createProgramObjectARB(),
[gl:attachObjectARB(Prog,ObjCode) || ObjCode <- Objs],
[gl:deleteShader(ObjCode) || ObjCode <- Objs],
gl:linkProgram(Prog),
check_status(Prog,"Link result", ?GL_OBJECT_LINK_STATUS_ARB),
Prog.
check_status(Handle,Str, What) ->
case gl:getObjectParameterivARB(Handle, What) of
1 ->
printInfo(Handle,Str), %% Check status even if ok
Handle;
_E ->
printInfo(Handle,Str),
throw("Compilation failed")
end.
printInfo(ShaderObj,Str) ->
Len = gl:getObjectParameterivARB(ShaderObj, ?GL_OBJECT_INFO_LOG_LENGTH_ARB),
case Len > 0 of
true ->
case catch gl:getInfoLogARB(ShaderObj, Len) of
[] ->
ok;
InfoStr when is_list(InfoStr) ->
io:format("Info: ~s:~n ~s ~n", [Str,InfoStr]),
case string:str(InfoStr, "oftware") of
0 -> ok;
_ ->
throw("Shader disabled would run in Software mode")
end;
Error ->
io:format("Internal error PrintInfo crashed with ~p ~n",
[Error])
end;
false ->
ok
end.