This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawModels.js
188 lines (145 loc) · 5.71 KB
/
drawModels.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
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
//
// Functions to draw the models
//
// Diogo Ferreira
// Luís Leira
// 2017
function drawMainCube(
angleXX, angleYY, angleZZ,
sx, sy, sz,
tx, ty, tz,
mvMatrix){
applyMatrix(angleXX, angleYY, angleZZ, sx, sy, sz, tx, ty, tz, mvMatrix);
applyPhongIlluminationModel(kAmbiMainCube, kDiffMainCube, kSpecMainCube, nPhongMainCube);
// Do not apply texture
gl.uniform1f( gl.getUniformLocation(shaderProgram, "textureCoef"), 0.0 );
// Drawing
gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}
function drawMazeCube(
angleXX, angleYY, angleZZ,
sx, sy, sz,
tx, ty, tz,
mvMatrix){
applyMatrix(angleXX, angleYY, angleZZ, sx, sy, sz, tx, ty, tz, mvMatrix);
applyPhongIlluminationModel(kAmbiMazeCube, kDiffMazeCube, kSpecMazeCube, nPhongMazeCube);
// Apply texture
gl.uniform1f( gl.getUniformLocation(shaderProgram, "textureCoef"), 0.7 );
gl.bindTexture(gl.TEXTURE_2D, webGLTexture1);
// Drawing
gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}
function drawFloorSquare(color,
angleXX, angleYY, angleZZ,
sx, sy, sz,
tx, ty, tz,
mvMatrix){
applyMatrix(angleXX, angleYY, angleZZ, sx, sy, sz, tx, ty, tz, mvMatrix);
applyColor(color);
// Do not apply texture
gl.uniform1f( gl.getUniformLocation(shaderProgram, "textureCoef"), 0.0 );
// Drawing only the top face
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 24);
}
function drawCoin(angleXX, angleYY, angleZZ,
sx, sy, sz,
tx, ty, tz,
mvMatrix){
applyMatrix(angleXX, angleYY, angleZZ, sx, sy, sz, tx, ty, tz, mvMatrix);
applyPhongIlluminationModel(kAmbiCoin, kDiffCoin, kSpecCoin, nPhongCoin);
// Do not apply texture
gl.uniform1f( gl.getUniformLocation(shaderProgram, "textureCoef"), 0.0 );
// Drawing
gl.drawElements(gl.TRIANGLES, coinVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}
function drawPoisCoin(angleXX, angleYY, angleZZ,
sx, sy, sz,
tx, ty, tz,
mvMatrix){
applyMatrix(angleXX, angleYY, angleZZ, sx, sy, sz, tx, ty, tz, mvMatrix);
applyPhongIlluminationModel(kAmbiPoisCoin, kDiffPoisCoin, kSpecPoisCoin, nPhongPoisCoin);
// Do not apply texture
gl.uniform1f( gl.getUniformLocation(shaderProgram, "textureCoef"), 0.0 );
// Drawing
gl.drawElements(gl.TRIANGLES, coinVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}
function drawFlyingObstacle(angleXX, angleYY, angleZZ,
sx, sy, sz,
tx, ty, tz,
mvMatrix){
applyMatrix(angleXX, angleYY, angleZZ, sx, sy, sz, tx, ty, tz, mvMatrix);
applyPhongIlluminationModel(kAmbiFlyObj, kDiffFlyObj, kSpecFlyObj, nPhongFlyObj);
// Apply texture
gl.uniform1f( gl.getUniformLocation(shaderProgram, "textureCoef"), 0.7 );
gl.bindTexture(gl.TEXTURE_2D, webGLTexture2);
// Drawing
gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}
// Auxiliary Functions
function applyMatrix(angleXX, angleYY, angleZZ,
sx, sy, sz,
tx, ty, tz,
mvMatrix){
mvMatrix = mult( mvMatrix, translationMatrix( tx, ty, tz ) );
mvMatrix = mult( mvMatrix, rotationZZMatrix( angleZZ ) );
mvMatrix = mult( mvMatrix, rotationYYMatrix( angleYY ) );
mvMatrix = mult( mvMatrix, rotationXXMatrix( angleXX ) );
mvMatrix = mult( mvMatrix, scalingMatrix( sx, sy, sz ) );
// Passing the Model View Matrix to apply the current transformation
var mvUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
gl.uniformMatrix4fv(mvUniform, false, new Float32Array(flatten(mvMatrix)));
}
function applyPhongIlluminationModel(kAmbi, kDiff, kSpec, nPhong){
// Use phong illumination model
gl.uniform1i( gl.getUniformLocation(shaderProgram, "colortype"), 1 );
// Material properties
gl.uniform3fv( gl.getUniformLocation(shaderProgram, "k_ambient"), flatten(kAmbi) );
gl.uniform3fv( gl.getUniformLocation(shaderProgram, "k_diffuse"), flatten(kDiff) );
gl.uniform3fv( gl.getUniformLocation(shaderProgram, "k_specular"), flatten(kSpec) );
gl.uniform1f( gl.getUniformLocation(shaderProgram, "shininess"), nPhong);
// Light Sources
var numLights = lightSources.length;
gl.uniform1i( gl.getUniformLocation(shaderProgram, "numLights"),
numLights );
var j = 0;
for(var i = 0; i < lightSources.length; i++ )
{
if( lightSources[i].isOff() ) {
continue;
}
gl.uniform4fv( gl.getUniformLocation(shaderProgram, "allLights[" + String(j) + "].position"), flatten(lightSources[i].getPosition()) );
gl.uniform3fv( gl.getUniformLocation(shaderProgram, "allLights[" + String(j) + "].intensities"), flatten(lightSources[i].getIntensity()) );
j++;
}
}
function applyColor(color){
// Use specified color
gl.uniform1i( gl.getUniformLocation(shaderProgram, "colortype"), 0 );
gl.uniform3fv( gl.getUniformLocation(shaderProgram, "vertexColor"), flatten(color) );
}
function handleLoadedTexture(texture) {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.bindTexture(gl.TEXTURE_2D, null);
}
function initTexture() {
webGLTexture1 = gl.createTexture();
webGLTexture1.image = new Image();
webGLTexture1.image.onload = function () {
handleLoadedTexture(webGLTexture1)
}
webGLTexture1.image.crossOrigin = "";
webGLTexture1.image.src = "https://i.imgur.com/9Se16BR.jpg";
webGLTexture2 = gl.createTexture();
webGLTexture2.image = new Image();
webGLTexture2.image.onload = function () {
handleLoadedTexture(webGLTexture2)
}
webGLTexture2.image.crossOrigin = "";
webGLTexture2.image.src = "https://i.imgur.com/yAf53KU.jpg";
}