PageSourceSearch

https://binomial.biz/texture_test/renderer.js

js binomial.biz collected 2026-09-25 22:09:12 UTC 8,360 bytes, 296 lines download raw bytes

1/**
2 * Constructs a renderer object.
3 * @param {WebGLRenderingContext} gl The GL context.
4 * @constructor
5 */
6var Renderer = function (gl) {
7   /**
8    * The GL context.
9    * @type {WebGLRenderingContext}
10    * @private
11    */
12   this.gl_ = gl;
13
14   /**
15    * The WebGLProgram.
16    * @type {WebGLProgram}
17    * @private
18    */
19   this.program_ = gl.createProgram();
20
21   /**
22    * @type {WebGLShader}
23    * @private
24    */
25   this.vertexShader_ = this.compileShader_(
26      Renderer.vertexShaderSource_, gl.VERTEX_SHADER);
27
28   /**
29    * @type {WebGLShader}
30    * @private
31    */
32   this.fragmentShader_ = this.compileShader_(
33      Renderer.fragmentShaderSource_, gl.FRAGMENT_SHADER);
34
35   /**
36    * Cached uniform locations.
37    * @type {Object.<string, WebGLUniformLocation>}
38    * @private
39    */
40   this.uniformLocations_ = {};
41
42   /**
43    * Cached attribute locations.
44    * @type {Object.<string, WebGLActiveInfo>}
45    * @private
46    */
47   this.attribLocations_ = {};
48
49   /**
50    * A vertex buffer containing a single quad with xy coordinates from [-1,-1]
51    * to [1,1] and uv coordinates from [0,0] to [1,1].
52    * @private
53    */
54   this.quadVertexBuffer_ = gl.createBuffer();
55   gl.bindBuffer(gl.ARRAY_BUFFER, this.quadVertexBuffer_);
56   var vertices = new Float32Array(
57      [-1.0, -1.0, 0.0, 1.0,
58      +1.0, -1.0, 1.0, 1.0,
59      -1.0, +1.0, 0.0, 0.0,
60         1.0, +1.0, 1.0, 0.0]);
61   gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
62
63
64   // init shaders
65
66   gl.attachShader(this.program_, this.vertexShader_);
67   gl.attachShader(this.program_, this.fragmentShader_);
68   gl.bindAttribLocation(this.program_, 0, 'vert');
69   gl.linkProgram(this.program_);
70   gl.useProgram(this.program_);
71   gl.enableVertexAttribArray(0);
72
73   gl.enable(gl.DEPTH_TEST);
74   gl.disable(gl.CULL_FACE);
75
76   var count = gl.getProgramParameter(this.program_, gl.ACTIVE_UNIFORMS);
77   for (var i = 0; i < /** @type {number} */(count); i++) {
78      var info = gl.getActiveUniform(this.program_, i);
79      var result = gl.getUniformLocation(this.program_, info.name);
80      this.uniformLocations_[info.name] = result;
81   }
82
83   count = gl.getProgramParameter(this.program_, gl.ACTIVE_ATTRIBUTES);
84   for (var i = 0; i < /** @type {number} */(count); i++) {
85      var info = gl.getActiveAttrib(this.program_, i);
86      var result = gl.getAttribLocation(this.program_, info.name);
87      this.attribLocations_[info.name] = result;
88   }
89};
90
91
92Renderer.prototype.finishInit = function () {
93   this.draw();
94};
95
96
97Renderer.prototype.createDxtTexture = function (dxtData, width, height, format) {
98   var gl = this.gl_;
99   var tex = gl.createTexture();
100   gl.bindTexture(gl.TEXTURE_2D, tex);
101   gl.compressedTexImage2D(
102      gl.TEXTURE_2D,
103      0,
104      format,
105      width,
106      height,
107      0,
108      dxtData);
109   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
110   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
111   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
112   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
113   gl.bindTexture(gl.TEXTURE_2D, null);
114   return tex;
115};
116
117Renderer.prototype.createCompressedTexture = function (data, width, height, format) {
118   var gl = this.gl_;
119   var tex = gl.createTexture();
120   gl.bindTexture(gl.TEXTURE_2D, tex);
121   gl.compressedTexImage2D(
122      gl.TEXTURE_2D,
123      0,
124      format,
125      width,
126      height,
127      0,
128      data);
129   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
130   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
131   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
132   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
133   gl.bindTexture(gl.TEXTURE_2D, null);
134   return tex;
135};
136
137Renderer.prototype.createHalfRGBATexture = function (data, width, height, format) {
138   var gl = this.gl_;
139   var tex = gl.createTexture();
140   gl.bindTexture(gl.TEXTURE_2D, tex);
141   gl.texImage2D(
142      gl.TEXTURE_2D,
143      0,
144      gl.RGBA,
145      width,
146      height,
147      0,
148      gl.RGBA,
149      format,
150      data);
151   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
152   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
153   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
154   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
155   gl.bindTexture(gl.TEXTURE_2D, null);
156   return tex;
157};
158
159Renderer.prototype.createRgb565Texture = function (rgb565Data, width, height) {
160   var gl = this.gl_;
161   var tex = gl.createTexture();
162   gl.bindTexture(gl.TEXTURE_2D, tex);
163   gl.texImage2D(
164      gl.TEXTURE_2D,
165      0,
166      gl.RGB,
167      width,
168      height,
169      0,
170      gl.RGB,
171      gl.UNSIGNED_SHORT_5_6_5,
172      rgb565Data);
173   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
174   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
175   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
176   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
177   gl.bindTexture(gl.TEXTURE_2D, null);
178   return tex;
179};
180
181Renderer.prototype.createRgbaTexture = function (rgbaData, width, height) {
182   var gl = this.gl_;
183   var tex = gl.createTexture();
184   gl.bindTexture(gl.TEXTURE_2D, tex);
185   gl.texImage2D(
186      gl.TEXTURE_2D,
187      0,
188      gl.RGBA,
189      width,
190      height,
191      0,
192      gl.RGBA,
193      gl.UNSIGNED_BYTE,
194      rgbaData);
195   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
196   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
197   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
198   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
199   gl.bindTexture(gl.TEXTURE_2D, null);
200   return tex;
201};
202
203
204Renderer.prototype.drawTexture = function (texture, width, height, mode, scale, linearToSRGBFlag) {
205   var gl = this.gl_;
206   // draw scene
207   gl.clearColor(0, 0, 0, 1);
208   gl.clearDepth(1.0);
209   gl.viewport(0, 0, width, height);
210   gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
211
212   gl.activeTexture(gl.TEXTURE0);
213   gl.bindTexture(gl.TEXTURE_2D, texture);
214   gl.uniform1i(this.uniformLocations_.texSampler, 0);
215
216   var x = 0.0;
217   var y = 0.0;
218   if (mode == 1)
219      x = 1.0;
220   else if (mode == 2)
221      y = 1.0;
222
223   gl.uniform4f(this.uniformLocations_.control, x, y, scale, linearToSRGBFlag ? 1.0 : 0.0);
224
225   gl.enableVertexAttribArray(this.attribLocations_.vert);
226   gl.bindBuffer(gl.ARRAY_BUFFER, this.quadVertexBuffer_);
227   gl.vertexAttribPointer(this.attribLocations_.vert, 4, gl.FLOAT,
228      false, 0, 0);
229   gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
230};
231
232
233/**
234 * Compiles a GLSL shader and returns a WebGLShader.
235 * @param {string} shaderSource The shader source code string.
236 * @param {number} type Either VERTEX_SHADER or FRAGMENT_SHADER.
237 * @return {WebGLShader} The new WebGLShader.
238 * @private
239 */
240Renderer.prototype.compileShader_ = function (shaderSource, type) {
241   var gl = this.gl_;
242   var shader = gl.createShader(type);
243   gl.shaderSource(shader, shaderSource);
244   gl.compileShader(shader);
245   return shader;
246};
247
248
249/**
250 * @type {string}
251 * @private
252 */
253Renderer.vertexShaderSource_ = [
254   'attribute vec4 vert;',
255   'varying vec2 v_texCoord;',
256   'void main() {',
257   '  gl_Position = vec4(vert.xy, 0.0, 1.0);',
258   '  v_texCoord = vert.zw;',
259   '}'
260].join('\n');
261
262
263/**
264 * @type {string}
265 * @private '  gl_FragColor = texture2D(texSampler, v_texCoord);',
266 */
267Renderer.fragmentShaderSource_ = [
268   'precision highp float;',
269   'uniform sampler2D texSampler;',
270   'uniform vec4 control;',
271   'varying vec2 v_texCoord;',
272
273   // Function to convert linear RGB to sRGB
274   'vec3 linearToSrgb(vec3 linearRGB) {',
275   '  vec3 srgbLow = linearRGB * 12.92;',
276   '  vec3 srgbHigh = 1.055 * pow(linearRGB, vec3(1.0/2.4)) - 0.055;',
277   '  return clamp(mix(srgbLow, srgbHigh, step(0.0031308, linearRGB)), 0.0, 1.0);',
278   '}',
279
280   'void main() {',
281   '  vec4 c;',
282   '  c = texture2D(texSampler, v_texCoord);',
283   '  c.rgb *= control.z;',
284   '  if (control.x > 0.0)',
285   '  {',
286   '   	c.w = 1.0;',
287   '  }',
288   '	 else if (control.y > 0.0)',
289   '	 {',
290   '   	c.rgb = c.aaa; c.w = 1.0;',
291   '  }',
292   '  if (control.w > 0.0)',
293   '     c.rgb = linearToSrgb(c.rgb);',
294   '  gl_FragColor = c;',
295   '}'
296].join('\n');

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.