-
Notifications
You must be signed in to change notification settings - Fork 0
/
heightfield.js
110 lines (90 loc) · 4 KB
/
heightfield.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
var heightfieldShader;
function initHeightfieldShader() {
heightfieldShader = initShaders("heightfield-vs","heightfield-fs");
// active ce shader
gl.useProgram(heightfieldShader);
// recupere la localisation de l'attribut dans lequel on souhaite acceder aux positions
heightfieldShader.vertexPositionAttribute = gl.getAttribLocation(heightfieldShader, "aVertexPosition");
gl.enableVertexAttribArray(heightfieldShader.vertexPositionAttribute); // active cet attribut
// pareil pour les coordonnees de texture
heightfieldShader.vertexCoordAttribute = gl.getAttribLocation(heightfieldShader, "aVertexCoord");
gl.enableVertexAttribArray(heightfieldShader.vertexCoordAttribute);
// adresse de la variable uniforme uOffset dans le shader
heightfieldShader.offsetUniform = gl.getUniformLocation(heightfieldShader, "uOffset");
heightfieldShader.amplitudeUniform = gl.getUniformLocation(heightfieldShader, "uAmplitude");
heightfieldShader.frequencyUniform = gl.getUniformLocation(heightfieldShader, "uFrequency");
heightfieldShader.persistenceUniform = gl.getUniformLocation(heightfieldShader, "uPersistence");
console.log("heightfield shader initialized");
}
function Heightfield() {
this.initParameters();
// cree un nouveau buffer sur le GPU et l'active
this.vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
// un tableau contenant les positions des sommets (sur CPU donc)
var vertices = [
-1.0,-1.0, 0.0,
1.0,-1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0
];
// on envoie ces positions au GPU ici (et on se rappelle de leur nombre/taille)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
this.vertexBuffer.itemSize = 3;
this.vertexBuffer.numItems = 4;
// meme principe pour les couleurs
this.coordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.coordBuffer);
var coords = [
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(coords), gl.STATIC_DRAW);
this.coordBuffer.itemSize = 2;
this.coordBuffer.numItems = 4;
// creation des faces du cube (les triangles) avec les indices vers les sommets
this.triangles = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.triangles);
var tri = [0,1,2,0,2,3];
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(tri), gl.STATIC_DRAW);
this.triangles.numItems = 6;
console.log("heightfield initialized");
}
Heightfield.prototype.initParameters = function() {
this.timer = 0.0;
this.offset = [0.0,0.0];
this.amplitude = 1.3;
this.frequency = 3.5;
this.persistence = 0.5;
}
Heightfield.prototype.setParameters = function(elapsed) {
// un exemple d'animation, a vous de changer ca en fonction
// de ce que vous souhaitez obtenir
this.timer = this.timer+elapsed*0.0004;
var speed = Math.min(4.0, 1.0 + (this.timer * 0.1));
this.offset[1] = this.offset[1]+elapsed*0.0004*speed;
this.amplitude = 1.5;
this.frequency = 3.5;
}
Heightfield.prototype.shader = function() {
return heightfieldShader;
}
Heightfield.prototype.sendUniformVariables = function() {
gl.uniform2fv(heightfieldShader.offsetUniform,this.offset);
gl.uniform1f(heightfieldShader.amplitudeUniform,this.amplitude);
gl.uniform1f(heightfieldShader.frequencyUniform,this.frequency);
gl.uniform1f(heightfieldShader.persistenceUniform,this.persistence);
}
Heightfield.prototype.draw = function() {
// active le buffer de position et fait le lien avec l'attribut aVertexPosition dans le shader
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.vertexAttribPointer(heightfieldShader.vertexPositionAttribute, this.vertexBuffer.itemSize, gl.FLOAT, false, 0, 0);
// active le buffer de coords
gl.bindBuffer(gl.ARRAY_BUFFER, this.coordBuffer);
gl.vertexAttribPointer(heightfieldShader.vertexCoordAttribute, this.coordBuffer.itemSize, gl.FLOAT, false, 0, 0);
// dessine les buffers actifs
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.triangles);
gl.drawElements(gl.TRIANGLES, this.triangles.numItems, gl.UNSIGNED_SHORT, 0);
}