-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr2.js
120 lines (98 loc) · 4.18 KB
/
r2.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
var shaderProgram = null;
var vertexBuffer = null;
var indexBufferTriangles = null;
var indexBufferEdges = null;
var currentAngle = 0;
var incAngle = 0.3;
var sunLightDirection = SglVec4.normalize([1.5, 2.5, 0.5, 1.0]);
//// Initialize the buffers
////
function createObjectBuffers(gl, obj, n = true) {
if(n)ComputeNormals(obj)
else console.log("Not computing normals")
obj.vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, obj.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, obj.vertices, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
if (obj.textureCoordinates||0){
obj.texturePositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, obj.texturePositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, obj.textureCoordinates, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
obj.normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, obj.normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, obj.vertex_normal, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
obj.indexBufferTriangles = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indexBufferTriangles);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, obj.triangleIndices, gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
// create edges
var edges = new Uint16Array(obj.numTriangles * 3 * 2);
for (var i = 0; i < obj.numTriangles; ++i) {
edges[i * 6 + 0] = obj.triangleIndices[i * 3 + 0];
edges[i * 6 + 1] = obj.triangleIndices[i * 3 + 1];
edges[i * 6 + 2] = obj.triangleIndices[i * 3 + 0];
edges[i * 6 + 3] = obj.triangleIndices[i * 3 + 2];
edges[i * 6 + 4] = obj.triangleIndices[i * 3 + 1];
edges[i * 6 + 5] = obj.triangleIndices[i * 3 + 2];
}
obj.indexBufferEdges = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indexBufferEdges);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, edges, gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
};
///// Initialize the shaders
/////
function initShaders(gl) {
shaderProgram = lambertianSingleColorShader(gl)
// create the vertex shader
var vertexShader = shaderProgram.vertexShader
// create the fragment shader
var fragmentShader = shaderProgram.fragmentShader
// Create the shader program
}
function initialize(gl, primitive, n=true) {
createObjectBuffers(gl, primitive, n);
initShaders(gl);
gl.useProgram(shaderProgram)
}
///// Draw the given primitives with solid wireframe
/////
//
function getViewMatrix(gl){
var canvas = document.getElementById('canvas');
var width = canvas.clientWidth;
var height = canvas.clientHeight;
gl.viewport(0, 0, width, height);
projMat = SglMat4.perspective(0.8, width/height, 0.1, 1000.0);
viewMat = SglMat4.lookAt([0,2,6], [0,0,0], [0,1,0]);
}
function drawObject(gl, obj, shader, fillColor, stack) {
// Draw the primitive
gl.bindBuffer(gl.ARRAY_BUFFER, obj.vertexBuffer);
gl.enableVertexAttribArray(shader.aPositionIndex);
gl.vertexAttribPointer(shader.aPositionIndex, 3, gl.FLOAT, false, 0, 0);
if (shader.aColorIndex && obj.colorBuffer) {
gl.bindBuffer(gl.ARRAY_BUFFER, obj.colorBuffer);
gl.enableVertexAttribArray(shader.aColorIndex);
gl.vertexAttribPointer(shader.aColorIndex, 4, gl.FLOAT, false, 0, 0);
}
if (obj.textureCoordinates||0) {
gl.bindBuffer(gl.ARRAY_BUFFER, obj.texturePositionBuffer);
gl.enableVertexAttribArray(shader.aCheckerTexPositionIndex);
gl.vertexAttribPointer(shader.aCheckerTextPositionIndex, 2, gl.FLOAT, false, 0, 0);
}
gl.bindBuffer(gl.ARRAY_BUFFER, obj.normalBuffer);
gl.enableVertexAttribArray(shader.aNormalIndex);
gl.vertexAttribPointer(shader.aNormalIndex, 3, gl.FLOAT, false, 0, 0);
gl.uniform4fv(shader.uColorLocation, fillColor);
gl.uniform4fv(shader.uLightColorLocation, [1, 1, 1, 1]);
gl.uniform4fv(shader.uLightDirectionLocation, sunLightDirection);
gl.enable(gl.POLYGON_OFFSET_FILL);
gl.polygonOffset(1.0, 1.0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indexBufferTriangles);
gl.drawElements(gl.TRIANGLES, obj.triangleIndices.length, gl.UNSIGNED_SHORT, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
};