-
Notifications
You must be signed in to change notification settings - Fork 1
/
shaders.js
88 lines (69 loc) · 3.66 KB
/
shaders.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
"use strict"
var Shaders = {
shaderSrc: null,
init: function(errorOutput)
{
if (Shaders.shaderSrc)
Shaders.onShadersRetrieved(Shaders.shaderSrc);
else
Helpers.ajaxGet("shaders.xml", Shaders.onShadersRetrieved.bind(Shaders));
Shaders.errorOutput = errorOutput;
},
onShadersRetrieved: function(xmlRaw)
{
Shaders.shaderSrc = xmlRaw;
var parser = new DOMParser();
var dom = parser.parseFromString(xmlRaw, "application/xml");
var scripts = dom.getElementsByTagName("script");
//console.log("%o", scripts);
Shaders.shaderSource = {};
for (var i = 0; i < scripts.length; i++)
{
var type = scripts[i].attributes["type"].value;
//console.log(type);
var id = scripts[i].attributes["id" ].value;
var shaderSrc = scripts[i].textContent;
if (type != "x-shader/x-vertex" && type != "x-shader/x-fragment")
{
console.log("[WARN] unknown script type: %s for script %o", type, scripts[i]);
continue;
}
if (id === undefined || id === null)
{
console.log("[WARN] shader %o has no id, skipping", scripts[i]);
continue;
}
Shaders.shaderSource[id] = shaderSrc;
}
//don't need the depth shader otherwise
if (glu.performShadowMapping)
Shaders.depth = glu.createShader(Shaders.shaderSource["depth-shader-vs"],
Shaders.shaderSource["depth-shader-fs"],
["vertexPosition"],
["modelViewProjectionMatrix", "lightPos"],
Shaders.errorOutput);
Shaders.building = glu.createShader( Shaders.shaderSource["building-shader-vs"],
Shaders.shaderSource["building-shader-fs"],
["vertexPosition","vertexTexCoords", "vertexNormal", "vertexColorIn"],
["modelViewProjectionMatrix", "tex", "cameraPos"],
Shaders.errorOutput);
Shaders.flat = glu.createShader( Shaders.shaderSource["flat-shader-vs"],
Shaders.shaderSource["flat-shader-fs"],
["vertexPosition"], ["modelViewProjectionMatrix", "color"],
Shaders.errorOutput);
Shaders.textured = glu.createShader( Shaders.shaderSource["texture-shader-vs"],
Shaders.shaderSource["texture-shader-fs"],
["vertexPosition","vertexTexCoords"],
["modelViewProjectionMatrix", "tex"],
Shaders.errorOutput);
Shaders.shadow = glu.performShadowMapping ?
glu.createShader( Shaders.shaderSource["shadowed-shader-vs"],
Shaders.shaderSource["shadowed-texture-shader-fs"],
["vertexPosition", "normalIn", "vertexTexCoords"],
["modelViewProjectionMatrix", "sunDir", "shadowMatrix", "tex", "shadowTex"],
Shaders.errorOutput) : Shaders.textured;
Shaders.ready = true;
scheduleFrameRendering();
//console.log(shaders);
}
}