-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderHub.js
144 lines (112 loc) · 3.61 KB
/
renderHub.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
// 'Hub' object for rendering
function getGLContext( canvas){
var gl = WebGLUtils.setupWebGL(canvas);
if(!gl){
throw ("The time is out of joint!");
return null;
}
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
return gl;
}
function RenderHub(window_width,window_height)
{
this.window_width = window_width;
this.window_height = window_height;
var neutralRotation = quat.create();
var startPosition = vec3.create();
startPosition = [0.0,0.0,2.0];
this.camera = new Camera(startPosition,neutralRotation,16.0/9.0,60.0*(9.0/16.0));
this.sky = new SkyDome();
this.ocean = new Ocean();
// terrain debugging
this.terrain = new Terrain(50.0,50.0,512,512);
// for debugging
this.debug_prgm = null;
this.debug_mesh = new Mesh();
this.debug_sphere = new Mesh();
}
RenderHub.prototype.loadScene=function()
{
this.sky.createResources();
this.ocean.createResources();
this.terrain.createResources();
}
RenderHub.prototype.createDebugResources=function()
{
// Load shader program for debugging
var vShader = loadShader(gl.VERTEX_SHADER,"./shaders/debug_v.glsl");
var fShader = loadShader(gl.FRAGMENT_SHADER,"./shaders/debug_f.glsl");
this.debug_prgm = gl.createProgram();
gl.attachShader(this.debug_prgm, vShader);
gl.attachShader(this.debug_prgm, fShader);
gl.bindAttribLocation(this.debug_prgm, 0, "v_position");
gl.linkProgram(this.debug_prgm);
if (!gl.getProgramParameter(this.debug_prgm, gl.LINK_STATUS))
{
alert("Could not initialise shaders");
}
// Create debug mesh
var debug_vertices = [];
var debug_indices = [];
debug_vertices.push(-1.0);debug_vertices.push(-1.0);debug_vertices.push(0.0);
debug_vertices.push(0.0);debug_vertices.push(1.0);debug_vertices.push(0.0);
debug_vertices.push(1.0);debug_vertices.push(-1.0);debug_vertices.push(0.0);
debug_indices.push(0);debug_indices.push(1);
debug_indices.push(1);debug_indices.push(2);
debug_indices.push(0);debug_indices.push(2);
this.debug_mesh.bufferDataFromArray(debug_vertices,debug_indices,gl.LINES);
}
RenderHub.prototype.renderDebug=function()
{
gl.clearColor(0.0,0.0,0.0,1.0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
gl.viewport(0,0,canvas.width,canvas.height);
var view_matrix = mat4.create();
var projection_matrix = mat4.create();
this.camera.computeViewMatrix(view_matrix);
this.camera.computeProjectionMatrix(projection_matrix);
gl.useProgram(this.debug_prgm);
gl.uniformMatrix4fv(gl.getUniformLocation(this.debug_prgm, "view_matrix"),false,view_matrix);
gl.uniformMatrix4fv(gl.getUniformLocation(this.debug_prgm, "projection_matrix"),false,projection_matrix);
this.debug_mesh.setVertexAttribPointer(0,3,gl.FLOAT,false,3*4,0);
this.debug_mesh.draw();
}
RenderHub.prototype.renderSky=function()
{
//TODO render to special sky framebuffer and merge in postprocessing?
gl.clearColor(0.0,0.0,0.0,1.0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
gl.viewport(0,0,canvas.width,canvas.height);
this.sky.render(this.camera);
}
RenderHub.prototype.renderOcean=function()
{
//TODO render to special ocean framebuffer and merge in postprocessing?
this.ocean.render(this.camera);
}
RenderHub.prototype.renderTerrain=function()
{
gl.clear(gl.DEPTH_BUFFER_BIT);
gl.enable(gl.DEPTH_TEST);
this.terrain.render(this.camera);
}
RenderHub.prototype.render = function()
{
this.renderSky();
//this.renderOcean();
this.renderTerrain();
//this.renderDebug();
}
/*
RenderHub.prototype.run=function()
{
var self = this;
(function updateFrame(){
requestAnimFrame(updateFrame);
//TODO call render methods
//self.renderDebug();
self.renderSky();
})();
}
*/