Skip to content

[pull] main from openlayers:main #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/ol/render/webgl/MixedGeometryBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,22 @@ class MixedGeometryBatch {

/**
* @param {Feature|RenderFeature} feature Feature
* @param {import("../../proj.js").TransformFunction} [projectionTransform] Projection transform.
*/
changeFeature(feature) {
changeFeature(feature, projectionTransform) {
// the feature is not present in the batch; do not add it to avoid unexpected behaviors
if (!this.uidToRef_.get(getUid(feature))) {
return;
}
this.removeFeature(feature);
const geometry = feature.getGeometry();
let geometry = feature.getGeometry();
if (!geometry) {
return;
}
if (projectionTransform) {
geometry = geometry.clone();
geometry.applyTransform(projectionTransform);
}
this.addGeometry_(geometry, feature);
}

Expand Down
7 changes: 4 additions & 3 deletions src/ol/renderer/webgl/VectorLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class WebGLVectorLayerRenderer extends WebGLLayerRenderer {
listen(
source,
VectorEventType.CHANGEFEATURE,
this.handleSourceFeatureChanged_,
this.handleSourceFeatureChanged_.bind(this, projectionTransform),
this,
),
listen(
Expand Down Expand Up @@ -295,12 +295,13 @@ class WebGLVectorLayerRenderer extends WebGLLayerRenderer {
}

/**
* @param {import("../../proj.js").TransformFunction} projectionTransform Transform function.
* @param {import("../../source/Vector.js").VectorSourceEvent} event Event.
* @private
*/
handleSourceFeatureChanged_(event) {
handleSourceFeatureChanged_(projectionTransform, event) {
const feature = event.feature;
this.batch_.changeFeature(feature);
this.batch_.changeFeature(feature, projectionTransform);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/ol/webgl/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ class WebGLHelper extends Disposable {
* @private
*/
this.startTime_ = Date.now();

/**
* @type {number}
* @private
*/
this.maxAttributeCount_ = this.gl_.getParameter(
this.gl_.MAX_VERTEX_ATTRIBS,
);
}

/**
Expand Down Expand Up @@ -894,6 +902,7 @@ class WebGLHelper extends Disposable {
* @param {import("../Map.js").FrameState} [frameState] Frame state.
*/
useProgram(program, frameState) {
this.disableAllAttributes_();
const gl = this.gl_;
gl.useProgram(program);
this.currentProgram_ = program;
Expand Down Expand Up @@ -1065,6 +1074,16 @@ class WebGLHelper extends Disposable {
this.gl_.uniformMatrix4fv(this.getUniformLocation(uniform), false, value);
}

/**
* Disable all vertex attributes.
* @private
*/
disableAllAttributes_() {
for (let i = 0; i < this.maxAttributeCount_; i++) {
this.gl_.disableVertexAttribArray(i);
}
}

/**
* Will set the currently bound buffer to an attribute of the shader program. Used by `#enableAttributes`
* internally.
Expand Down
31 changes: 31 additions & 0 deletions test/browser/spec/ol/render/webgl/MixedGeometryBatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import MultiPoint from '../../../../../../src/ol/geom/MultiPoint.js';
import MultiPolygon from '../../../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../../../src/ol/geom/Point.js';
import Polygon from '../../../../../../src/ol/geom/Polygon.js';
import {getTransform} from '../../../../../../src/ol/proj.js';
import RenderFeature from '../../../../../../src/ol/render/Feature.js';
import MixedGeometryBatch from '../../../../../../src/ol/render/webgl/MixedGeometryBatch.js';
import {getUid} from '../../../../../../src/ol/util.js';
Expand Down Expand Up @@ -1229,6 +1230,36 @@ describe('MixedGeometryBatch', function () {
});
});

describe('with projectionTransform', () => {
let geometry1, feature1, uid1, projectionTransform, transformedFlatCoordss;

beforeEach(() => {
projectionTransform = getTransform('EPSG:4326', 'EPSG:3857');

geometry1 = new Point([135, 35]);
feature1 = new Feature({
geometry: geometry1,
});

uid1 = getUid(feature1);
transformedFlatCoordss = [projectionTransform([135, 35])];

mixedBatch.addFeature(feature1, projectionTransform);
});

it('has the transformed flatCoords', () => {
expect(mixedBatch.pointBatch.entries[uid1].flatCoordss).to.eql(
transformedFlatCoordss,
);
});
it('has the same transformed flatCoords after changeFeature', () => {
mixedBatch.changeFeature(feature1, projectionTransform);
expect(mixedBatch.pointBatch.entries[uid1].flatCoordss).to.eql(
transformedFlatCoordss,
);
});
});

describe('#clear', () => {
beforeEach(() => {
const feature1 = new Feature(
Expand Down
18 changes: 18 additions & 0 deletions test/browser/spec/ol/webgl/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,22 @@ describe('ol/webgl/WebGLHelper', function () {
]);
});
});

describe('attributes disabling', () => {
let disableAttribSpy;
beforeEach(() => {
h = new WebGLHelper();
disableAttribSpy = sinonSpy(h.getGL(), 'disableVertexAttribArray');
const program = h.getProgram(FRAGMENT_SHADER, VERTEX_SHADER);
h.useProgram(program, SAMPLE_FRAMESTATE);
});
it('all active attributes are disabled when enabling programs, disregarding of previous state', () => {
const gl = h.getGL();
const max = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
expect(disableAttribSpy.getCalls().length).to.eql(max); // each possible attribute is disabled
for (let i = 0; i < max; i++) {
expect(disableAttribSpy.getCall(i).args[0]).to.eql(i);
}
});
});
});
Binary file modified test/rendering/cases/webgl-vector-geographic/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 37 additions & 4 deletions test/rendering/cases/webgl-vector-geographic/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Map from '../../../../src/ol/Map.js';
import View from '../../../../src/ol/View.js';
import GeoJSON from '../../../../src/ol/format/GeoJSON.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import WebGLVectorLayer from '../../../../src/ol/layer/WebGLVector.js';
import {useGeographic} from '../../../../src/ol/proj.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
Expand All @@ -12,6 +13,23 @@ const features = format.readFeatures({
type: 'FeatureCollection',
features: [
{
// for case 1: initially placed geometries should be transformed properly
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[0, -65],
[100, 0],
[0, 65],
[-100, 0],
[0, -65],
],
],
},
},
{
// for case 2: geometries changed after initial rendering should be transformed properly too
type: 'Feature',
geometry: {
type: 'Polygon',
Expand Down Expand Up @@ -40,7 +58,7 @@ const vector = new WebGLVectorLayer({
},
});

new Map({
const map = new Map({
layers: [vector],
target: 'map',
view: new View({
Expand All @@ -49,7 +67,22 @@ new Map({
}),
});

render({
message:
'Geometries using geographic coordinates are transformed before rendering',
map.once('rendercomplete', function () {
// case2: update geometry after initial rendering
features[1].setGeometry(
new Polygon([
[
[-100, 65],
[0, 65],
[-100, 0],
[-100, 65],
],
]),
);
map.renderSync();

render({
message:
'Geometries using geographic coordinates are always transformed properly',
});
});
Loading