-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdmain.js
142 lines (118 loc) · 4.44 KB
/
oldmain.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
async function main() {
canvas = document.getElementById('webgl');
gl = canvas.getContext('webgl2');
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
program = compileShader(gl, VSHADER_SOURCE, FSHADER_SOURCE);
initProgram(program);
//setup shaders and prepare shader variables
shadowProgram = compileShader(gl, VSHADER_SHADOW_SOURCE, FSHADER_SHADOW_SOURCE);
shadowProgram.a_Position = gl.getAttribLocation(shadowProgram, 'a_Position');
shadowProgram.u_MvpMatrix = gl.getUniformLocation(shadowProgram, 'u_MvpMatrix');
programEnvCube = compileShader(gl, VSHADER_SOURCE_ENVCUBE, FSHADER_SOURCE_ENVCUBE);
programEnvCube.a_Position = gl.getAttribLocation(programEnvCube, 'a_Position');
programEnvCube.u_envCubeMap = gl.getUniformLocation(programEnvCube, 'u_envCubeMap');
programEnvCube.u_viewDirectionProjectionInverse =
gl.getUniformLocation(programEnvCube, 'u_viewDirectionProjectionInverse');
var quad = new Float32Array(
[
-1, -1, 1,
1, -1, 1,
-1, 1, 1,
-1, 1, 1,
1, -1, 1,
1, 1, 1
]); //just a quad
//load model;
// cubeMapTex = initCubeTexture("pos-x.jpg", "neg-x.jpg", "pos-y.jpg", "neg-y.jpg",
// "pos-z.jpg", "neg-z.jpg", 512, 512)
cubeMapTex = initCubeTexture("px.png", "nx.png", "py.png", "ny.png",
"pz.png", "nz.png", 256, 256);
quadObj = initVertexBufferForLaterUse(gl, quad);
// 調用parseModel函數來載入cube.obj
cubeObj = await parseModel('object/cube.obj');
//console.log(cubeObj);
bushObj = await parseModel('object/bushes/01/bush_01.obj');
rockObj = await parseModel('object/rocks/01/rock_01.obj');
onloadTexture("brick", "texture/brick.jpg")
onloadTexture("stone", "texture/stone_wall.png")
onloadTexture("bush", "object/bushes/01/diffuse.png");
onloadTexture("rock", "object/rocks/01/diffuse.png");
response = await fetch('object/sonic-the-hedgehog.mtl');
const mtlText = await response.text();
//console.log('mtlText'+mtlText)
mtl = parseMTL(mtlText);
imgNames = addTexturesToImgNames(mtl);
numTextures = imgNames.length
response = await fetch('object/sonic.obj');
text = await response.text();
var obj = parseOBJ(text);
objCompImgIndex = parsetexture(text, mtl);
for (let i = 0; i < obj.geometries.length; i++) {
let o = initVertexBufferForLaterUse(gl,
obj.geometries[i].data.position,
obj.geometries[i].data.normal,
obj.geometries[i].data.texcoord);
playerObj.push(o);
}
for (let i = 0; i < imgNames.length; i++) {
let image = new Image();
image.onload = function () { initTexture(gl, image, imgNames[i]); };
image.src = imgNames[i];
}
console.log(objCompImgIndex)
initGame();
gl.useProgram(program);
// putModel('location', objname);
// onloadTexture('tex', 'location')
//fboShadow = initFrameBuffer(gl);
//fbo = initFrameBuffer(gl);
console.log("done")
//x,z,y
mvpMatrix = new Matrix4();
modelMatrix = new Matrix4();
normalMatrix = new Matrix4();
rotateMatrix = new Matrix4();
draw();//draw it once before mouse move
canvas.onmousedown = function (ev) { mouseDown(ev) };
canvas.onmousemove = function (ev) { mouseMove(ev) };
canvas.onmouseup = function (ev) { mouseUp(ev) };
//canvas.onwheel = function (ev) { scroll(ev) };
// var menu = document.getElementById("menu");
// menu.onchange = function () {
// // if (this.value == "normal") normalMode = true;
// // else normalMode = false;
// draw();
// }
document.addEventListener('keydown', function (event) {
switch (event.key) {
case 'w':
case 'W':
move(1, 0);
break;
case 's':
case 'S':
move(-1, 0);
break;
case 'a':
case 'A':
move(0, -1);
break;
case 'd':
case 'D':
move(0, 1);
break;
}
draw(); // Redraw the scene with the new camera position
});
var tick = function () {
angle += speed;
lX = centerX + radius * Math.cos(angle);
lZ = centerY + radius * Math.sin(angle);
draw();
requestAnimationFrame(tick);
}
tick();
}