This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
glCompat.js
133 lines (108 loc) · 2.74 KB
/
glCompat.js
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
// Code based on "WebGL Lessons", http://learningwebgl.com
var gl;
function initGL()
{
var canvas = document.getElementById("arena");
try {
gl = canvas.getContext("webkit-3d");
} catch (e) {
// pass
}
if (!gl) {
try {
gl = canvas.getContext("experimental-webgl");
} catch (e) {
// pass
}
}
if (!gl) {
try {
gl = canvas.getContext("moz-webgl");
} catch (e) {
// pass
}
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
return;
}
// This temporary code provides support for Google Chrome, which
// as of 30 Nov 2009 does not support the new names for the
// functions to get shader/program parameters (among other things).
// It should be unnecessary soon, and is only a partial fix
// in the meantime (as, for example, there's no way to get shader
// or program parameters that are vectors of integers).
// See http://learningwebgl.com/blog/?p=1082 for details.
if (!gl.getProgramParameter) {
gl.getProgramParameter = gl.getProgrami
}
if (!gl.getShaderParameter) {
gl.getShaderParameter = gl.getShaderi
}
// End of Chrome compatibility code
}
var mvMatrix;
var mvMatrixStack = [];
function mvPushMatrix(m)
{
if (m) {
mvMatrixStack.push(m.dup());
mvMatrix = m.dup();
} else {
mvMatrixStack.push(mvMatrix.dup());
}
}
function mvPopMatrix()
{
if (mvMatrixStack.length == 0)
throw "Invalid popMatrix!";
mvMatrix = mvMatrixStack.pop();
return mvMatrix;
}
function loadIdentity()
{
mvMatrix = Matrix.I(4);
}
function multMatrix(m)
{
mvMatrix = mvMatrix.x(m);
}
function mvTranslate(v)
{
var m = Matrix.Translation($V([v[0], v[1], v[2]])).ensure4x4();
multMatrix(m);
}
function mvRotate(ang, v)
{
var arad = ang * Math.PI / 180.0;
var m = Matrix.Rotation(arad, $V([v[0], v[1], v[2]])).ensure4x4();
multMatrix(m);
}
var pMatrix;
function perspective(fovy, aspect, znear, zfar)
{
pMatrix = makePerspective(fovy, aspect, znear, zfar)
}
function getShader(gl, id)
{
var str = "";
var shader;
if (id == "fshader") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (id == "vshader") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
var request = new XMLHttpRequest();
request.open("GET", "estudo3d." + id, false);
request.send();
str = request.responseText;
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}