File: renderer.js

package info (click to toggle)
basis-universal 2.0.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 216,436 kB
  • sloc: cpp: 163,224; ansic: 51,368; python: 2,824; javascript: 2,637; lisp: 1,026; sh: 161; makefile: 17
file content (326 lines) | stat: -rw-r--r-- 9,466 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
 * Constructs a renderer object.
 * @param {WebGLRenderingContext} gl The GL context.
 * @constructor
 */
var Renderer = function (gl) {
   /**
    * The GL context.
    * @type {WebGLRenderingContext}
    * @private
    */
   this.gl_ = gl;
         
   /**
    * The WebGLProgram.
    * @type {WebGLProgram}
    * @private
    */
   this.program_ = gl.createProgram();

   /**
    * @type {WebGLShader}
    * @private
    */
   this.vertexShader_ = this.compileShader_(
      Renderer.vertexShaderSource_, gl.VERTEX_SHADER);

   /**
    * @type {WebGLShader}
    * @private
    */
   this.fragmentShader_ = this.compileShader_(
      Renderer.fragmentShaderSource_, gl.FRAGMENT_SHADER);

   /**
    * Cached uniform locations.
    * @type {Object.<string, WebGLUniformLocation>}
    * @private
    */
   this.uniformLocations_ = {};

   /**
    * Cached attribute locations.
    * @type {Object.<string, WebGLActiveInfo>}
    * @private
    */
   this.attribLocations_ = {};

   /**
    * A vertex buffer containing a single quad with xy coordinates from [-1,-1]
    * to [1,1] and uv coordinates from [0,0] to [1,1].
    * @private
    */
   this.quadVertexBuffer_ = gl.createBuffer();
   gl.bindBuffer(gl.ARRAY_BUFFER, this.quadVertexBuffer_);

   var vertices = new Float32Array(
      [-1.0, -1.0, 0.0, 1.0,
      +1.0, -1.0, 1.0, 1.0,
      -1.0, +1.0, 0.0, 0.0,
         1.0, +1.0, 1.0, 0.0]);
		 
//   var vertices = new Float32Array(
//      [-1.0, -1.0, 0.0, .5,
//      +1.0, -1.0, .5, .5,
//      -1.0, +1.0, 0.0, 0.0,
//         1.0, +1.0, .5, 0.0]);
		 
   gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);


   // init shaders

   gl.attachShader(this.program_, this.vertexShader_);
   gl.attachShader(this.program_, this.fragmentShader_);
   gl.bindAttribLocation(this.program_, 0, 'vert');
   gl.linkProgram(this.program_);
   gl.useProgram(this.program_);
   gl.enableVertexAttribArray(0);

   gl.enable(gl.DEPTH_TEST);
   gl.disable(gl.CULL_FACE);

   var count = gl.getProgramParameter(this.program_, gl.ACTIVE_UNIFORMS);
   for (var i = 0; i < /** @type {number} */(count); i++) {
      var info = gl.getActiveUniform(this.program_, i);
      var result = gl.getUniformLocation(this.program_, info.name);
      this.uniformLocations_[info.name] = result;
   }

   count = gl.getProgramParameter(this.program_, gl.ACTIVE_ATTRIBUTES);
   for (var i = 0; i < /** @type {number} */(count); i++) {
      var info = gl.getActiveAttrib(this.program_, i);
      var result = gl.getAttribLocation(this.program_, info.name);
      this.attribLocations_[info.name] = result;
   }
};


Renderer.prototype.finishInit = function () {
   //this.draw();
};


Renderer.prototype.createDxtTexture = function (dxtData, width, height, format) {
   var gl = this.gl_;
   var tex = gl.createTexture();
   gl.bindTexture(gl.TEXTURE_2D, tex);
   gl.compressedTexImage2D(
      gl.TEXTURE_2D,
      0,
      format,
      width,
      height,
      0,
      dxtData);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
   gl.bindTexture(gl.TEXTURE_2D, null);
   return tex;
};

Renderer.prototype.createCompressedTexture = function (data, width, height, format) {
   var gl = this.gl_;
   var tex = gl.createTexture();
   gl.bindTexture(gl.TEXTURE_2D, tex);
   gl.compressedTexImage2D(
      gl.TEXTURE_2D,
      0,
      format,
      width,
      height,
      0,
      data);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
   gl.bindTexture(gl.TEXTURE_2D, null);
   return tex;
};

Renderer.prototype.createHalfRGBATexture = function (data, width, height, format) {
   var gl = this.gl_;
   var tex = gl.createTexture();
   gl.bindTexture(gl.TEXTURE_2D, tex);
   gl.texImage2D(
      gl.TEXTURE_2D,
      0,
      gl.RGBA,
      width,
      height,
      0,
      gl.RGBA,
      format,
      data);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
   gl.bindTexture(gl.TEXTURE_2D, null);
   return tex;
};

// WebGL requires each row of rgb565Data to be aligned on a 4-byte boundary.            
Renderer.prototype.createRgb565Texture = function (rgb565Data, width, height) {
   var gl = this.gl_;
   var tex = gl.createTexture();
   gl.bindTexture(gl.TEXTURE_2D, tex);
   
   gl.texImage2D(
      gl.TEXTURE_2D,
      0,
      gl.RGB,
      width,
      height,
      0,
      gl.RGB,
      gl.UNSIGNED_SHORT_5_6_5,
      rgb565Data);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
   gl.bindTexture(gl.TEXTURE_2D, null);
   return tex;
};

Renderer.prototype.createRgbaTexture = function (rgbaData, width, height) {
   var gl = this.gl_;
   var tex = gl.createTexture();
   gl.bindTexture(gl.TEXTURE_2D, tex);
   gl.texImage2D(
      gl.TEXTURE_2D,
      0,
      gl.RGBA,
      width,
      height,
      0,
      gl.RGBA,
      gl.UNSIGNED_BYTE,
      rgbaData);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
   gl.bindTexture(gl.TEXTURE_2D, null);
   return tex;
};


Renderer.prototype.drawTexture = function (texture, width, height, mode, scale, linearToSRGBFlag, useLinearFiltering) {
   var gl = this.gl_;
   // draw scene
   gl.clearColor(0, 0, 0, 1);
   gl.clearDepth(1.0);
   gl.viewport(0, 0, width, height);
   gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);

   gl.activeTexture(gl.TEXTURE0);
   gl.bindTexture(gl.TEXTURE_2D, texture);
   
   // Point vs. bilinear sampling (no mipmaps involved here)
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, useLinearFiltering ? gl.LINEAR : gl.NEAREST);
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, useLinearFiltering ? gl.LINEAR : gl.NEAREST);
      
   gl.uniform1i(this.uniformLocations_.texSampler, 0);

   var x = 0.0;
   var y = 0.0;
   if (mode == 1)
      x = 1.0;
   else if (mode == 2)
      y = 1.0;

   gl.uniform4f(this.uniformLocations_.control, x, y, scale, linearToSRGBFlag ? 1.0 : 0.0);

   var a = 1.0 / width;
   var b = 1.0 / height;
   var c = 0;
   var d = 0;
   gl.uniform4f(this.uniformLocations_.control2, a, b, c, d);

   gl.enableVertexAttribArray(this.attribLocations_.vert);
   gl.bindBuffer(gl.ARRAY_BUFFER, this.quadVertexBuffer_);
   gl.vertexAttribPointer(this.attribLocations_.vert, 4, gl.FLOAT,
      false, 0, 0);
   gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};


/**
 * Compiles a GLSL shader and returns a WebGLShader.
 * @param {string} shaderSource The shader source code string.
 * @param {number} type Either VERTEX_SHADER or FRAGMENT_SHADER.
 * @return {WebGLShader} The new WebGLShader.
 * @private
 */
Renderer.prototype.compileShader_ = function (shaderSource, type) {
   var gl = this.gl_;
   var shader = gl.createShader(type);
   gl.shaderSource(shader, shaderSource);
   gl.compileShader(shader);
   
     // Check for errors
   const compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
   if (!compiled) {
        const errorLog = gl.getShaderInfoLog(shader);
        console.error(`Error compiling ${type === gl.VERTEX_SHADER ? 'vertex' : 'fragment'} shader:\n${errorLog}`);
        gl.deleteShader(shader); // Cleanup shader object
        throw new Error('Shader compilation failed');
   }
    
   return shader;
};


/**
 * @type {string}
 * @private
 */
Renderer.vertexShaderSource_ = [
   'attribute vec4 vert;',
   'varying vec2 v_texCoord;',
   'void main() {',
   '  gl_Position = vec4(vert.xy, 0.0, 1.0);',
   '  v_texCoord = vert.zw;',
   '}'
].join('\n');


/**
 * @type {string}
 * @private '  gl_FragColor = texture2D(texSampler, v_texCoord);',
 */
Renderer.fragmentShaderSource_ = [
   'precision highp float;',
   'uniform sampler2D texSampler;',
   'uniform vec4 control;',
   'uniform vec4 control2;',
   'varying vec2 v_texCoord;',
   'vec3 linearToSrgb(vec3 linearRGB) {',
   '  vec3 srgbLow = linearRGB * 12.92;',
   '  vec3 srgbHigh = 1.055 * pow(linearRGB, vec3(1.0/2.4)) - 0.055;',
   '  return clamp(mix(srgbLow, srgbHigh, step(0.0031308, linearRGB)), 0.0, 1.0);',
   '}',
   'void main() {',
   '  vec4 c;',
   '  c = texture2D(texSampler, v_texCoord);',
   '  c.rgb *= control.z;',
   '  if (control.x > 0.0)',
   '  {',
   '   	c.w = 1.0;',
   '  }',
   '	 else if (control.y > 0.0)',
   '	 {',
   '   	c.rgb = c.aaa; c.w = 1.0;',
   '  }',
   '  if (control.w > 0.0)',
   '     c.rgb = linearToSrgb(c.rgb);',
   '  gl_FragColor = c;',
   '}'
].join('\n');