-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocean.js
57 lines (43 loc) · 1.81 KB
/
ocean.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
function Ocean()
{
this.oceanSurface_prgm = null;
this.oceanSurface_mesh = new Mesh();
//TODO find working single channel texture format
//this.ocean_heighmap = new Texture2D("ocean_heightmap",gl.LUMINANCE,1,1,gl.LUMINANCE,gl.FLOAT,null);
}
Ocean.prototype.createResources = function()
{
// Load shader for ocean surface
var vShader = loadShader(gl.VERTEX_SHADER,"./shaders/ocean_v.glsl");
var fShader = loadShader(gl.FRAGMENT_SHADER,"./shaders/ocean_f.glsl");
this.oceanSurface_prgm = gl.createProgram();
gl.attachShader(this.oceanSurface_prgm, vShader);
gl.attachShader(this.oceanSurface_prgm, fShader);
gl.bindAttribLocation(this.oceanSurface_prgm, 0, "v_position");
gl.bindAttribLocation(this.oceanSurface_prgm, 1, "v_uv");
gl.linkProgram(this.oceanSurface_prgm);
if (!gl.getProgramParameter(this.oceanSurface_prgm, gl.LINK_STATUS))
{
alert("Could not initialise shaders");
}
// Create base quad
var vertices = [-100.0,-1.0,-100.0,-1.0,-1.0,
-100.0,-1.0,100.0,-1.0,1.0,
100.0,-1.0,-100.0,1.0,-1.0,
100.0,-1.0,100.0,1.0,1.0];
var indices = [0,1,2,1,3,2];
this.oceanSurface_mesh.bufferDataFromArray(vertices,indices,gl.TRIANGLES);
}
Ocean.prototype.render = function(camera)
{
var view_matrix = mat4.create();
var projection_matrix = mat4.create();
camera.computeViewMatrix(view_matrix);
camera.computeProjectionMatrix(projection_matrix);
gl.useProgram(this.oceanSurface_prgm);
gl.uniformMatrix4fv(gl.getUniformLocation(this.oceanSurface_prgm, "view_matrix"),false,view_matrix);
gl.uniformMatrix4fv(gl.getUniformLocation(this.oceanSurface_prgm, "projection_matrix"),false,projection_matrix);
this.oceanSurface_mesh.setVertexAttribPointer(0,3,gl.FLOAT,false,5*4,0);
this.oceanSurface_mesh.setVertexAttribPointer(1,2,gl.FLOAT,false,5*4,3*4);
this.oceanSurface_mesh.draw();
}