-
Notifications
You must be signed in to change notification settings - Fork 1
/
quad.js
122 lines (96 loc) · 5.15 KB
/
quad.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
import { gl } from './game.js';
class Quad {
constructor(shader) {
this.shader = shader;
// Attribute Location
this.posLocation = gl.getAttribLocation(this.shader.program, 'a_pos');
// Uniform Location
this.cameraLocation = gl.getUniformLocation(this.shader.program, 'u_camera');
this.transformLocation = gl.getUniformLocation(this.shader.program, 'u_transform');
this.viewLocation = gl.getUniformLocation(this.shader.program, 'u_view');
this.colorLocation = gl.getUniformLocation(this.shader.program, 'u_color');
this.textureLocation = gl.getUniformLocation(this.shader.program, 'u_texture');
this.skyColorLocation = gl.getUniformLocation(this.shader.program, 'u_skyColor');
const vertexData = new Float32Array(12);
vertexData.set([0.0, 0.0, 0.0], 0);
vertexData.set([0.0, 1.0, 0.0], 3);
vertexData.set([1.0, 1.0, 0.0], 6);
vertexData.set([1.0, 0.0, 0.0], 9);
const indexData = new Int16Array(6);
indexData.set([0, 1, 2, 0, 2, 3], 0);
gl.useProgram(this.shader.program);
const vertexBuffer = gl.createBuffer();
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.enableVertexAttribArray(this.posLocation);
gl.bufferData(gl.ARRAY_BUFFER, vertexData, gl.STATIC_DRAW);
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indexData, gl.STATIC_DRAW);
gl.vertexAttribPointer(this.posLocation, 3, gl.FLOAT, false, 0, 0);
}
setTexture = (texture) => {
this.texture = texture;
gl.bindTexture(gl.TEXTURE_2D, this.texture.texture);
}
setCamera = (viewMatrix, cameraMatrix) => {
gl.uniformMatrix4fv(this.viewLocation, false, viewMatrix);
gl.uniformMatrix4fv(this.cameraLocation, false, cameraMatrix);
}
renderPlayer = (pos, w, h, uo, vo, color) => {
this.objectMatrix = glMatrix.mat4.create();
this.textureMatrix = glMatrix.mat4.create();
glMatrix.mat4.translate(this.objectMatrix, this.objectMatrix, pos);
glMatrix.mat4.scale(this.objectMatrix, this.objectMatrix, [4 * 1.0, 4 * 1.0, 0.0]);
gl.uniformMatrix4fv(this.transformLocation, false, this.objectMatrix);
// console.log(this.texture.width)
glMatrix.mat4.scale(this.textureMatrix, this.textureMatrix, [1.0 / this.texture.width, 1.0 / this.texture.height, 0.0]);
glMatrix.mat4.translate(this.textureMatrix, this.textureMatrix, [uo * 1.0, vo * 1.0, 0.0]);
glMatrix.mat4.scale(this.textureMatrix, this.textureMatrix, [w, h, 0.0]);
gl.uniformMatrix4fv(this.textureLocation, false, this.textureMatrix);
gl.uniform4fv(this.colorLocation, color);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
}
renderString = (pos, w, h, uo, vo, color) => {
this.objectMatrix = glMatrix.mat4.create();
this.textureMatrix = glMatrix.mat4.create();
glMatrix.mat4.translate(this.objectMatrix, this.objectMatrix, pos);
glMatrix.mat4.scale(this.objectMatrix, this.objectMatrix, [2 * 1.0, 2 * 1.0, 0.0]);
gl.uniformMatrix4fv(this.transformLocation, false, this.objectMatrix);
glMatrix.mat4.scale(this.textureMatrix, this.textureMatrix, [1.0 / this.texture.width, 1.0 / this.texture.height, 0.0]);
glMatrix.mat4.translate(this.textureMatrix, this.textureMatrix, [uo * 1.0, vo * 1.0, 0.0]);
glMatrix.mat4.scale(this.textureMatrix, this.textureMatrix, [w, h, 0.0]);
gl.uniformMatrix4fv(this.textureLocation, false, this.textureMatrix);
gl.uniform4fv(this.colorLocation, color);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
}
renderStringCopyrights = (pos, w, h, uo, vo, color) => {
this.objectMatrix = glMatrix.mat4.create();
this.textureMatrix = glMatrix.mat4.create();
glMatrix.mat4.translate(this.objectMatrix, this.objectMatrix, pos);
glMatrix.mat4.scale(this.objectMatrix, this.objectMatrix, [0.3, 0.3, 0.0]);
gl.uniformMatrix4fv(this.transformLocation, false, this.objectMatrix);
glMatrix.mat4.scale(this.textureMatrix, this.textureMatrix, [1.0 / this.texture.width, 1.0 / this.texture.height, 0.0]);
glMatrix.mat4.translate(this.textureMatrix, this.textureMatrix, [uo * 1.0, vo * 1.0, 0.0]);
glMatrix.mat4.scale(this.textureMatrix, this.textureMatrix, [w, h, 0.0]);
gl.uniformMatrix4fv(this.textureLocation, false, this.textureMatrix);
gl.uniform4fv(this.colorLocation, color);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
}
render = (pos, w, h, uo, vo, color) => {
this.objectMatrix = glMatrix.mat4.create();
this.textureMatrix = glMatrix.mat4.create();
glMatrix.mat4.translate(this.objectMatrix, this.objectMatrix, pos);
glMatrix.mat4.scale(this.objectMatrix, this.objectMatrix, [w * 1.0, h * 1.0, 0.0]);
gl.uniformMatrix4fv(this.transformLocation, false, this.objectMatrix);
// console.log(this.texture.width)
glMatrix.mat4.scale(this.textureMatrix, this.textureMatrix, [1.0 / this.texture.width, 1.0 / this.texture.height, 0.0]);
glMatrix.mat4.translate(this.textureMatrix, this.textureMatrix, [uo * 1.0, vo * 1.0, 0.0]);
glMatrix.mat4.scale(this.textureMatrix, this.textureMatrix, [w, h, 0.0]);
gl.uniformMatrix4fv(this.textureLocation, false, this.textureMatrix);
gl.uniform4fv(this.colorLocation, color);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
}
}
export { Quad };