-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.js
203 lines (175 loc) · 6.98 KB
/
function.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function initCubeTexture(posXName, negXName, posYName, negYName,
posZName, negZName, imgWidth, imgHeight) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
const faceInfos = [
{
target: gl.TEXTURE_CUBE_MAP_POSITIVE_X,
fName: posXName,
},
{
target: gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
fName: negXName,
},
{
target: gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
fName: posYName,
},
{
target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
fName: negYName,
},
{
target: gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
fName: posZName,
},
{
target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Z,
fName: negZName,
},
];
faceInfos.forEach((faceInfo) => {
const { target, fName } = faceInfo;
// setup each face so it's immediately renderable
gl.texImage2D(target, 0, gl.RGBA, imgWidth, imgHeight, 0,
gl.RGBA, gl.UNSIGNED_BYTE, null);
var image = new Image();
image.onload = function () {
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
gl.texImage2D(target, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
};
image.src = fName;
});
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
return texture;
}
async function loadOBJtoCreateVBO(objFile) {
let objComponents = [];
response = await fetch(objFile);
text = await response.text();
obj = parseOBJ(text);
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);
objComponents.push(o);
}
return objComponents;
}
function initFrameBufferForCubemapRendering(gl) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
// 6 2D textures
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
for (let i = 0; i < 6; i++) {
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0,
gl.RGBA, offScreenWidth, offScreenHeight, 0, gl.RGBA,
gl.UNSIGNED_BYTE, null);
}
//create and setup a render buffer as the depth buffer
var depthBuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16,
offScreenWidth, offScreenHeight);
//create and setup framebuffer: linke the depth buffer to it (no color buffer here)
var frameBuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT,
gl.RENDERBUFFER, depthBuffer);
frameBuffer.texture = texture;
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
return frameBuffer;
}
function renderCubeMap(camX, camY, camZ)
{
//camera 6 direction to render 6 cubemap faces
var ENV_CUBE_LOOK_DIR = [
[1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, -1.0]
];
//camera 6 look up vector to render 6 cubemap faces
var ENV_CUBE_LOOK_UP = [
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, -1.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0]
];
gl.useProgram(program);
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.viewport(0, 0, offScreenWidth, offScreenHeight);
gl.clearColor(0.4, 0.4, 0.4,1);
for (var side = 0; side < 6;side++){
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0,
gl.TEXTURE_CUBE_MAP_POSITIVE_X+side, fbo.texture, 0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
let vpMatrix = new Matrix4();
vpMatrix.setPerspective(90, 1, 1, 100);
vpMatrix.lookAt(camX, camY, camZ,
camX + ENV_CUBE_LOOK_DIR[side][0],
camY + ENV_CUBE_LOOK_DIR[side][1],
camZ + ENV_CUBE_LOOK_DIR[side][2],
ENV_CUBE_LOOK_UP[side][0],
ENV_CUBE_LOOK_UP[side][1],
ENV_CUBE_LOOK_UP[side][2]);
drawoffscreen(vpMatrix);
///////////////////////////////////////////////////////////
gl.useProgram(programEnvCube);
gl.depthFunc(gl.LEQUAL);
var vpFromCameraInverse=vpMatrix.invert();
gl.uniformMatrix4fv(programEnvCube.u_viewDirectionProjectionInverse,
false, vpFromCameraInverse.elements);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeMapTex);
gl.uniform1i(programEnvCube.u_envCubeMap, 0);
initAttributeVariable(gl, programEnvCube.a_Position, quadObj.vertexBuffer);
gl.drawArrays(gl.TRIANGLES, 0, quadObj.numVertices);
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
/////BEGIN:///////////////////////////////////////////////////////////////////////////////////////////////
/////The folloing three function is for creating vertex buffer, but link to shader to user later//////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
function initAttributeVariable(gl, a_attribute, buffer) {
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.vertexAttribPointer(a_attribute, buffer.num, buffer.type, false, 0, 0);
gl.enableVertexAttribArray(a_attribute);
}
function initArrayBufferForLaterUse(gl, data, num, type) {
// Create a buffer object
var buffer = gl.createBuffer();
if (!buffer) {
console.log('Failed to create the buffer object');
return null;
}
// Write date into the buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
// Store the necessary information to assign the object to the attribute variable later
buffer.num = num;
buffer.type = type;
return buffer;
}
function initVertexBufferForLaterUse(gl, vertices, normals, texCoords) {
var nVertices = vertices.length / 3;
var o = new Object();
o.vertexBuffer = initArrayBufferForLaterUse(gl, new Float32Array(vertices), 3, gl.FLOAT);
if (normals != null) o.normalBuffer = initArrayBufferForLaterUse(gl, new Float32Array(normals), 3, gl.FLOAT);
if (texCoords != null) o.texCoordBuffer = initArrayBufferForLaterUse(gl, new Float32Array(texCoords), 2, gl.FLOAT);
//you can have error check here
o.numVertices = nVertices;
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
return o;
}
/////END://///////////////////////////////////////////////////////////////////////////////////////////////
/////The folloing three function is for creating vertex buffer, but link to shader to user later//////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////