ol 10.2.2-dev.1727715662036 → 10.2.2-dev.1727720556228

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @param {WebGLRenderingContext} gl Rendering Context.
3
+ * @param {string} fragmentSource Fragment shader source.
4
+ * @param {string} vertexSource Vertex shader source.
5
+ * @return {WebGLProgram} [progam] The program.
6
+ */
7
+ export function createProgram(gl: WebGLRenderingContext, fragmentSource: string, vertexSource: string): WebGLProgram;
8
+ /** @typedef {import("../transform.js").Transform} Matrix */
9
+ /**
10
+ * Canvas-like operations implemented in webgl.
11
+ */
12
+ export class Canvas {
13
+ /**
14
+ * @param {WebGLRenderingContext} gl Context to render in.
15
+ */
16
+ constructor(gl: WebGLRenderingContext);
17
+ /**
18
+ * @private
19
+ * @type {WebGLRenderingContext}
20
+ */
21
+ private gl_;
22
+ /**
23
+ * @private
24
+ * @type {WebGLProgram}
25
+ */
26
+ private program_;
27
+ positionLocation: number;
28
+ texcoordLocation: number;
29
+ matrixLocation: WebGLUniformLocation | null;
30
+ textureMatrixLocation: WebGLUniformLocation | null;
31
+ textureLocation: WebGLUniformLocation | null;
32
+ positionBuffer: WebGLBuffer | null;
33
+ positions: number[];
34
+ texcoordBuffer: WebGLBuffer | null;
35
+ texcoords: number[];
36
+ /**
37
+ * 2dContext drawImage call implemented in webgl.
38
+ * Unlike images, textures do not have a width and height associated
39
+ * with them so we'll pass in the width and height of the texture.
40
+ *
41
+ * @param {WebGLTexture} tex Image to draw.
42
+ * @param {number} texWidth Image width.
43
+ * @param {number} texHeight Image height.
44
+ * @param {number} srcX Top-left x-point to read src image.
45
+ * @param {number} srcY Top-left y-point to read src image.
46
+ * @param {number} [srcWidth] Width of source to read.
47
+ * @param {number} [srcHeight] Height of source to read.
48
+ * @param {number} [dstX] Top-left x-point of destination.
49
+ * @param {number} [dstY] Top-left y-point of destination.
50
+ * @param {number} [dstWidth] Width of written image in destination.
51
+ * @param {number} [dstHeight] Height of written image in destination.
52
+ * @param {number} [width] Width of canvas.
53
+ * @param {number} [height] Height of canvas.
54
+ */
55
+ drawImage(tex: WebGLTexture, texWidth: number, texHeight: number, srcX: number, srcY: number, srcWidth?: number | undefined, srcHeight?: number | undefined, dstX?: number | undefined, dstY?: number | undefined, dstWidth?: number | undefined, dstHeight?: number | undefined, width?: number | undefined, height?: number | undefined): void;
56
+ }
57
+ export type Matrix = import("../transform.js").Transform;
58
+ //# sourceMappingURL=Canvas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Canvas.d.ts","sourceRoot":"","sources":["Canvas.js"],"names":[],"mappings":"AAsNA;;;;;GAKG;AACH,kCALW,qBAAqB,kBACrB,MAAM,gBACN,MAAM,GACL,YAAY,CAuBvB;AAvMD,4DAA4D;AAE5D;;GAEG;AACH;IACE;;OAEG;IACH,gBAFW,qBAAqB,EA4C/B;IAzCC;;;OAGG;IACH,YAAa;IAEb;;;OAGG;IACH,iBAAiE;IAEjE,yBAAyE;IACzE,yBAAyE;IAEzE,4CAAsE;IACtE,mDAGC;IACD,6CAAwE;IAExE,mCAAuC;IAGvC,oBAAqD;IAOrD,mCAAuC;IAGvC,oBAAqD;IAQvD;;;;;;;;;;;;;;;;;;OAkBG;IACH,eAdW,YAAY,YACZ,MAAM,aACN,MAAM,QACN,MAAM,QACN,MAAM,qPAgFhB;CACF;qBA/Ia,OAAO,iBAAiB,EAAE,SAAS"}
@@ -0,0 +1,242 @@
1
+ import * as mat4 from '../vec/mat4.js';
2
+
3
+ /**
4
+ * @module ol/webgl/Canvas
5
+ */
6
+
7
+ const VERTEX_SHADER = `
8
+ attribute vec4 a_position;
9
+ attribute vec4 a_texcoord;
10
+
11
+ uniform mat4 u_matrix;
12
+ uniform mat4 u_textureMatrix;
13
+
14
+ varying vec2 v_texcoord;
15
+
16
+ void main() {
17
+ gl_Position = u_matrix * a_position;
18
+ vec2 texcoord = (u_textureMatrix * a_texcoord).xy;
19
+ v_texcoord = texcoord;
20
+ }
21
+ `;
22
+
23
+ const FRAGMENT_SHADER = `
24
+ precision mediump float;
25
+
26
+ varying vec2 v_texcoord;
27
+
28
+ uniform sampler2D u_texture;
29
+
30
+ void main() {
31
+ if (
32
+ v_texcoord.x < 0.0 ||
33
+ v_texcoord.y < 0.0 ||
34
+ v_texcoord.x > 1.0 ||
35
+ v_texcoord.y > 1.0
36
+ ) {
37
+ discard;
38
+ }
39
+ gl_FragColor = texture2D(u_texture, v_texcoord);
40
+ }
41
+ `;
42
+
43
+ /** @typedef {import("../transform.js").Transform} Matrix */
44
+
45
+ /**
46
+ * Canvas-like operations implemented in webgl.
47
+ */
48
+ export class Canvas {
49
+ /**
50
+ * @param {WebGLRenderingContext} gl Context to render in.
51
+ */
52
+ constructor(gl) {
53
+ /**
54
+ * @private
55
+ * @type {WebGLRenderingContext}
56
+ */
57
+ this.gl_ = gl;
58
+
59
+ /**
60
+ * @private
61
+ * @type {WebGLProgram}
62
+ */
63
+ this.program_ = createProgram(gl, FRAGMENT_SHADER, VERTEX_SHADER);
64
+
65
+ this.positionLocation = gl.getAttribLocation(this.program_, 'a_position');
66
+ this.texcoordLocation = gl.getAttribLocation(this.program_, 'a_texcoord');
67
+
68
+ this.matrixLocation = gl.getUniformLocation(this.program_, 'u_matrix');
69
+ this.textureMatrixLocation = gl.getUniformLocation(
70
+ this.program_,
71
+ 'u_textureMatrix',
72
+ );
73
+ this.textureLocation = gl.getUniformLocation(this.program_, 'u_texture');
74
+
75
+ this.positionBuffer = gl.createBuffer();
76
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
77
+
78
+ this.positions = [0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1];
79
+ gl.bufferData(
80
+ gl.ARRAY_BUFFER,
81
+ new Float32Array(this.positions),
82
+ gl.STATIC_DRAW,
83
+ );
84
+
85
+ this.texcoordBuffer = gl.createBuffer();
86
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.texcoordBuffer);
87
+
88
+ this.texcoords = [0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1];
89
+ gl.bufferData(
90
+ gl.ARRAY_BUFFER,
91
+ new Float32Array(this.texcoords),
92
+ gl.STATIC_DRAW,
93
+ );
94
+ }
95
+
96
+ /**
97
+ * 2dContext drawImage call implemented in webgl.
98
+ * Unlike images, textures do not have a width and height associated
99
+ * with them so we'll pass in the width and height of the texture.
100
+ *
101
+ * @param {WebGLTexture} tex Image to draw.
102
+ * @param {number} texWidth Image width.
103
+ * @param {number} texHeight Image height.
104
+ * @param {number} srcX Top-left x-point to read src image.
105
+ * @param {number} srcY Top-left y-point to read src image.
106
+ * @param {number} [srcWidth] Width of source to read.
107
+ * @param {number} [srcHeight] Height of source to read.
108
+ * @param {number} [dstX] Top-left x-point of destination.
109
+ * @param {number} [dstY] Top-left y-point of destination.
110
+ * @param {number} [dstWidth] Width of written image in destination.
111
+ * @param {number} [dstHeight] Height of written image in destination.
112
+ * @param {number} [width] Width of canvas.
113
+ * @param {number} [height] Height of canvas.
114
+ */
115
+ drawImage(
116
+ tex,
117
+ texWidth,
118
+ texHeight,
119
+ srcX,
120
+ srcY,
121
+ srcWidth,
122
+ srcHeight,
123
+ dstX,
124
+ dstY,
125
+ dstWidth,
126
+ dstHeight,
127
+ width,
128
+ height,
129
+ ) {
130
+ const gl = this.gl_;
131
+
132
+ if (dstX === undefined) {
133
+ dstX = srcX;
134
+ }
135
+ if (dstY === undefined) {
136
+ dstY = srcY;
137
+ }
138
+ if (srcWidth === undefined) {
139
+ srcWidth = texWidth;
140
+ }
141
+ if (srcHeight === undefined) {
142
+ srcHeight = texHeight;
143
+ }
144
+ if (dstWidth === undefined) {
145
+ dstWidth = srcWidth;
146
+ }
147
+ if (dstHeight === undefined) {
148
+ dstHeight = srcHeight;
149
+ }
150
+ if (width === undefined) {
151
+ width = gl.canvas.width;
152
+ }
153
+ if (height === undefined) {
154
+ height = gl.canvas.height;
155
+ }
156
+
157
+ gl.bindTexture(gl.TEXTURE_2D, tex);
158
+
159
+ gl.useProgram(this.program_);
160
+
161
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
162
+ gl.enableVertexAttribArray(this.positionLocation);
163
+ gl.vertexAttribPointer(this.positionLocation, 2, gl.FLOAT, false, 0, 0);
164
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.texcoordBuffer);
165
+ gl.enableVertexAttribArray(this.texcoordLocation);
166
+ gl.vertexAttribPointer(this.texcoordLocation, 2, gl.FLOAT, false, 0, 0);
167
+
168
+ // matrix for converting pixels to clip space
169
+ let matrix = mat4.orthographic(0, width, 0, height, -1, 1);
170
+ matrix = mat4.translate(matrix, dstX, dstY, 0);
171
+ matrix = mat4.scale(matrix, dstWidth, dstHeight, 1);
172
+ gl.uniformMatrix4fv(this.matrixLocation, false, matrix);
173
+
174
+ let texMatrix = mat4.translation(srcX / texWidth, srcY / texHeight, 0);
175
+ texMatrix = mat4.scale(
176
+ texMatrix,
177
+ srcWidth / texWidth,
178
+ srcHeight / texHeight,
179
+ 1,
180
+ );
181
+
182
+ gl.uniformMatrix4fv(this.textureMatrixLocation, false, texMatrix);
183
+ gl.uniform1i(this.textureLocation, 0);
184
+ gl.drawArrays(gl.TRIANGLES, 0, this.positions.length / 2);
185
+ }
186
+ }
187
+
188
+ /**
189
+ * @param {WebGLRenderingContext} gl Rendering Context.
190
+ * @param {GLenum} type Type of shader.
191
+ * @param {string} source source of shader.
192
+ * @return {WebGLShader} [progam] The program.
193
+ */
194
+ function createShader(gl, type, source) {
195
+ const shader = gl.createShader(type);
196
+
197
+ if (shader === null) {
198
+ throw new Error('Shader compilation failed');
199
+ }
200
+
201
+ gl.shaderSource(shader, source);
202
+
203
+ gl.compileShader(shader);
204
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
205
+ const log = gl.getShaderInfoLog(shader);
206
+ if (log === null) {
207
+ throw new Error('Shader info log creation failed');
208
+ }
209
+ throw new Error(log);
210
+ }
211
+
212
+ return shader;
213
+ }
214
+
215
+ /**
216
+ * @param {WebGLRenderingContext} gl Rendering Context.
217
+ * @param {string} fragmentSource Fragment shader source.
218
+ * @param {string} vertexSource Vertex shader source.
219
+ * @return {WebGLProgram} [progam] The program.
220
+ */
221
+ export function createProgram(gl, fragmentSource, vertexSource) {
222
+ const program = gl.createProgram();
223
+
224
+ const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource);
225
+ const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
226
+ if (program === null) {
227
+ throw new Error('Program creation failed');
228
+ }
229
+
230
+ gl.attachShader(program, vertexShader);
231
+ gl.attachShader(program, fragmentShader);
232
+
233
+ gl.linkProgram(program);
234
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
235
+ const log = gl.getProgramInfoLog(program);
236
+ if (log === null) {
237
+ throw new Error('Program info log creation failed');
238
+ }
239
+ throw new Error();
240
+ }
241
+ return program;
242
+ }