-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitShaders.js
101 lines (81 loc) · 2.6 KB
/
initShaders.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
"use strict";
/* * * * * * * * * * * * * * * * * * * * *
* Assignment 4
* By Jason Steck
* * * * * * * * * * * * * * * * * * * * */
// code skeleton borrowed from http://learningwebgl.com/blog/?p=239 rather than using the class's skeleton
var gl;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var program;
function useShader(name)
{
var vertexShader = getShader(gl, name + "-vs");
var fragmentShader = getShader(gl, name + "-fs");
var temp = gl.createProgram();
gl.attachShader(temp, vertexShader);
gl.attachShader(temp, fragmentShader);
gl.linkProgram(temp);
if (!gl.getProgramParameter(temp, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(temp);
/* Get attribute variables */
// Setup vPosition attribute
temp.vertexPositionAttribute = gl.getAttribLocation(temp, "vPosition");
gl.enableVertexAttribArray(temp.vertexPositionAttribute);
// Setup vNormal attribute
temp.vNormal = gl.getAttribLocation(temp, "vNormal");
gl.enableVertexAttribArray(temp.vNormal);
// Setup attribute vColor attribute
//temp.vertexColorAttribute = gl.getAttribLocation(temp, "vColor");
//gl.enableVertexAttribArray(temp.vertexColorAttribute);
/* Grab Uniform variables */
temp.modelViewMatrix = gl.getUniformLocation(temp, "modelViewMatrix");
temp.projectionMatrix = gl.getUniformLocation(temp, "projectionMatrix");
temp.normalMatrix = gl.getUniformLocation(temp, "normalMatrix");
program = temp;
}
function initShaders() {
//*
useShader("phong"); /*/
useShader("gouraud"); //*/
}