Skip to content

Commit aab26d9

Browse files
committed
8217472: Add attenuation for PointLight
Reviewed-by: arapte, kcr
1 parent 31fce21 commit aab26d9

File tree

33 files changed

+1210
-108
lines changed

33 files changed

+1210
-108
lines changed

modules/javafx.graphics/src/main/java/com/sun/javafx/scene/PointLightHelper.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -56,6 +56,12 @@ protected NGNode createPeerImpl(Node node) {
5656
return pointLightAccessor.doCreatePeer(node);
5757
}
5858

59+
@Override
60+
protected void updatePeerImpl(Node node) {
61+
super.updatePeerImpl(node);
62+
pointLightAccessor.doUpdatePeer(node);
63+
}
64+
5965
public static void setPointLightAccessor(final PointLightAccessor newAccessor) {
6066
if (pointLightAccessor != null) {
6167
throw new IllegalStateException();
@@ -66,7 +72,6 @@ public static void setPointLightAccessor(final PointLightAccessor newAccessor) {
6672

6773
public interface PointLightAccessor {
6874
NGNode doCreatePeer(Node node);
75+
void doUpdatePeer(Node node);
6976
}
70-
7177
}
72-

modules/javafx.graphics/src/main/java/com/sun/javafx/sg/prism/NGPointLight.java

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,11 +26,94 @@
2626
package com.sun.javafx.sg.prism;
2727

2828
/**
29-
* TODO: 3D - Need documentation
29+
* The peer of the {@code PointLight} class. Holds the default values of {@code PointLight}'s
30+
* properties and updates the visuals via {@link NGNode#visualsChanged} when one of the current
31+
* values changes. The peer receives its changes by {@code PointLight.doUpdatePeer} calls.
3032
*/
3133
public class NGPointLight extends NGLightBase {
3234

35+
/** Constant attenuation factor default value */
36+
private static final float DEFAULT_CA = 1;
37+
/** Linear attenuation factor default value */
38+
private static final float DEFAULT_LA = 0;
39+
/** Quadratic attenuation factor default value */
40+
private static final float DEFAULT_QA = 0;
41+
/** Max range default value */
42+
private static final float DEFAULT_MAX_RANGE = Float.POSITIVE_INFINITY;
43+
3344
public NGPointLight() {
3445
}
3546

47+
public static float getDefaultCa() {
48+
return DEFAULT_CA;
49+
}
50+
51+
public static float getDefaultLa() {
52+
return DEFAULT_LA;
53+
}
54+
55+
public static float getDefaultQa() {
56+
return DEFAULT_QA;
57+
}
58+
59+
public static float getDefaultMaxRange() {
60+
return DEFAULT_MAX_RANGE;
61+
}
62+
63+
64+
private float ca = DEFAULT_CA;
65+
66+
public float getCa() {
67+
return ca;
68+
}
69+
70+
public void setCa(float ca) {
71+
if (this.ca != ca) {
72+
this.ca = ca;
73+
visualsChanged();
74+
}
75+
}
76+
77+
78+
private float la = DEFAULT_LA;
79+
80+
public float getLa() {
81+
return la;
82+
}
83+
84+
public void setLa(float la) {
85+
if (this.la != la) {
86+
this.la = la;
87+
visualsChanged();
88+
}
89+
}
90+
91+
92+
private float qa = DEFAULT_QA;
93+
94+
public float getQa() {
95+
return qa;
96+
}
97+
98+
public void setQa(float qa) {
99+
if (this.qa != qa) {
100+
this.qa = qa;
101+
visualsChanged();
102+
}
103+
}
104+
105+
106+
private float maxRange = DEFAULT_MAX_RANGE;
107+
108+
public float getMaxRange() {
109+
return maxRange;
110+
}
111+
112+
public void setMaxRange(float maxRange) {
113+
maxRange = maxRange < 0 ? 0 : maxRange;
114+
if (this.maxRange != maxRange) {
115+
this.maxRange = maxRange;
116+
visualsChanged();
117+
}
118+
}
36119
}

modules/javafx.graphics/src/main/java/com/sun/javafx/sg/prism/NGShape3D.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -109,14 +109,18 @@ private void renderMeshView(Graphics g) {
109109
int pointLightIdx = 0;
110110
if (g.getLights() == null || g.getLights()[0] == null) {
111111
// If no lights are in scene apply default light. Default light
112-
// is a single point white point light at camera eye position.
112+
// is a single white point light at camera eye position.
113113
meshView.setAmbientLight(0.0f, 0.0f, 0.0f);
114114
Vec3d cameraPos = g.getCameraNoClone().getPositionInWorld(null);
115115
meshView.setPointLight(pointLightIdx++,
116116
(float)cameraPos.x,
117117
(float)cameraPos.y,
118118
(float)cameraPos.z,
119-
1.0f, 1.0f, 1.0f, 1.0f);
119+
1.0f, 1.0f, 1.0f, 1.0f,
120+
NGPointLight.getDefaultCa(),
121+
NGPointLight.getDefaultLa(),
122+
NGPointLight.getDefaultQa(),
123+
NGPointLight.getDefaultMaxRange());
120124
} else {
121125
float ambientRed = 0.0f;
122126
float ambientBlue = 0.0f;
@@ -132,7 +136,7 @@ private void renderMeshView(Graphics g) {
132136
float gL = lightBase.getColor().getGreen();
133137
float bL = lightBase.getColor().getBlue();
134138
/* TODO: 3D
135-
* There is a limit on the number of lights that can affect
139+
* There is a limit on the number of point lights that can affect
136140
* a 3D shape. (Currently we simply select the first 3)
137141
* Thus it is important to select the most relevant lights.
138142
*
@@ -155,7 +159,11 @@ private void renderMeshView(Graphics g) {
155159
(float)lightWT.getMxt(),
156160
(float)lightWT.getMyt(),
157161
(float)lightWT.getMzt(),
158-
rL, gL, bL, 1.0f);
162+
rL, gL, bL, 1.0f,
163+
light.getCa(),
164+
light.getLa(),
165+
light.getQa(),
166+
light.getMaxRange());
159167
}
160168
} else if (lightBase instanceof NGAmbientLight) {
161169
// Accumulate ambient lights
@@ -173,7 +181,10 @@ private void renderMeshView(Graphics g) {
173181
// TODO: 3D Required for D3D implementation of lights, which is limited to 3
174182
while (pointLightIdx < 3) {
175183
// Reset any previously set lights
176-
meshView.setPointLight(pointLightIdx++, 0, 0, 0, 0, 0, 0, 0);
184+
meshView.setPointLight(pointLightIdx++,
185+
0, 0, 0, // x y z
186+
0, 0, 0, 0, // r g b w
187+
1, 0, 0, 0); // ca la qa maxRange
177188
}
178189

179190
meshView.render(g);

modules/javafx.graphics/src/main/java/com/sun/prism/MeshView.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -48,7 +48,8 @@ public interface MeshView {
4848

4949
public void setPointLight(int index,
5050
float x, float y, float z,
51-
float r, float g, float b, float w);
51+
float r, float g, float b, float w,
52+
float ca, float la, float qa, float maxRange);
5253

5354
public void render(Graphics g);
5455
}

modules/javafx.graphics/src/main/java/com/sun/prism/d3d/D3DContext.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ private static native void nSetWireframe(long pContext, long nativeMeshView,
426426
private static native void nSetAmbientLight(long pContext, long nativeMeshView,
427427
float r, float g, float b);
428428
private static native void nSetPointLight(long pContext, long nativeMeshView,
429-
int index, float x, float y, float z, float r, float g, float b, float w);
429+
int index, float x, float y, float z, float r, float g, float b, float w,
430+
float ca, float la, float qa, float maxRange);
430431
private static native void nRenderMeshView(long pContext, long nativeMeshView);
431432
private static native int nDrawIndexedQuads(long pContext,
432433
float coords[], byte colors[], int numVertices);
@@ -551,8 +552,9 @@ void setAmbientLight(long nativeMeshView, float r, float g, float b) {
551552
nSetAmbientLight(pContext, nativeMeshView, r, g, b);
552553
}
553554

554-
void setPointLight(long nativeMeshView, int index, float x, float y, float z, float r, float g, float b, float w) {
555-
nSetPointLight(pContext, nativeMeshView, index, x, y, z, r, g, b, w);
555+
void setPointLight(long nativeMeshView, int index, float x, float y, float z,
556+
float r, float g, float b, float w, float ca, float la, float qa, float maxRange) {
557+
nSetPointLight(pContext, nativeMeshView, index, x, y, z, r, g, b, w, ca, la, qa, maxRange);
556558
}
557559

558560
@Override

modules/javafx.graphics/src/main/java/com/sun/prism/d3d/D3DMeshView.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ public void setAmbientLight(float r, float g, float b) {
8181
}
8282

8383
@Override
84-
public void setPointLight(int index, float x, float y, float z, float r, float g, float b, float w) {
84+
public void setPointLight(int index, float x, float y, float z, float r, float g, float b, float w,
85+
float ca, float la, float qa, float maxRange) {
8586
// NOTE: We only support up to 3 point lights at the present
8687
if (index >= 0 && index <= 2) {
87-
context.setPointLight(nativeHandle, index, x, y, z, r, g, b, w);
88+
context.setPointLight(nativeHandle, index, x, y, z, r, g, b, w, ca, la, qa, maxRange);
8889
}
8990
}
9091

modules/javafx.graphics/src/main/java/com/sun/prism/es2/ES2Context.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -449,8 +449,9 @@ void setAmbientLight(long nativeHandle, float r, float g, float b) {
449449
glContext.setAmbientLight(nativeHandle, r, g, b);
450450
}
451451

452-
void setPointLight(long nativeHandle, int index, float x, float y, float z, float r, float g, float b, float w) {
453-
glContext.setPointLight(nativeHandle, index, x, y, z, r, g, b, w);
452+
void setPointLight(long nativeHandle, int index, float x, float y, float z, float r, float g, float b, float w,
453+
float ca, float la, float qa, float maxRange) {
454+
glContext.setPointLight(nativeHandle, index, x, y, z, r, g, b, w, ca, la, qa, maxRange);
454455
}
455456

456457
@Override

modules/javafx.graphics/src/main/java/com/sun/prism/es2/ES2Light.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,14 +32,19 @@ class ES2Light {
3232

3333
float x, y, z = 0;
3434
float r, g, b, w = 1;
35+
float ca, la, qa, maxRange;
3536

36-
ES2Light(float ix, float iy, float iz, float ir, float ig, float ib, float iw) {
37+
ES2Light(float ix, float iy, float iz, float ir, float ig, float ib, float iw, float ca, float la, float qa, float maxRange) {
3738
x = ix;
3839
y = iy;
3940
z = iz;
4041
r = ir;
4142
g = ig;
4243
b = ib;
4344
w = iw;
45+
this.ca = ca;
46+
this.la = la;
47+
this.qa = qa;
48+
this.maxRange = maxRange;
4449
}
4550
}

modules/javafx.graphics/src/main/java/com/sun/prism/es2/ES2MeshView.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ float getAmbientLightBlue() {
101101
}
102102

103103
@Override
104-
public void setPointLight(int index, float x, float y, float z, float r, float g, float b, float w) {
104+
public void setPointLight(int index, float x, float y, float z, float r, float g, float b, float w,
105+
float ca, float la, float qa, float maxRange) {
105106
// NOTE: We only support up to 3 point lights at the present
106107
if (index >= 0 && index <= 2) {
107-
lights[index] = new ES2Light(x, y, z, r, g, b, w);
108-
context.setPointLight(nativeHandle, index, x, y, z, r, g, b, w);
108+
lights[index] = new ES2Light(x, y, z, r, g, b, w, ca, la, qa, maxRange);
109+
context.setPointLight(nativeHandle, index, x, y, z, r, g, b, w, ca, la, qa, maxRange);
109110
}
110111
}
111112

modules/javafx.graphics/src/main/java/com/sun/prism/es2/ES2PhongShader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -204,10 +204,12 @@ static void setShaderParamaters(ES2Shader shader, ES2MeshView meshView, ES2Conte
204204
meshView.getAmbientLightGreen(), meshView.getAmbientLightBlue());
205205

206206
int i = 0;
207-
for(ES2Light light : meshView.getPointLights()) {
207+
for (ES2Light light : meshView.getPointLights()) {
208208
if (light != null && light.w > 0) {
209209
shader.setConstant("lights[" + i + "].pos", light.x, light.y, light.z, light.w);
210210
shader.setConstant("lights[" + i + "].color", light.r, light.g, light.b);
211+
shader.setConstant("lights[" + i + "].attn", light.ca, light.la, light.qa);
212+
shader.setConstant("lights[" + i + "].range", light.maxRange);
211213
i++;
212214
}
213215
}

modules/javafx.graphics/src/main/java/com/sun/prism/es2/GLContext.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -252,7 +252,8 @@ private static native void nSetWireframe(long nativeCtxInfo, long nativeMeshView
252252
private static native void nSetAmbientLight(long nativeCtxInfo, long nativeMeshViewInfo,
253253
float r, float g, float b);
254254
private static native void nSetPointLight(long nativeCtxInfo, long nativeMeshViewInfo,
255-
int index, float x, float y, float z, float r, float g, float b, float w);
255+
int index, float x, float y, float z, float r, float g, float b, float w,
256+
float ca, float la, float qa, float maxRange);
256257
private static native void nRenderMeshView(long nativeCtxInfo, long nativeMeshViewInfo);
257258
private static native void nBlit(long nativeCtxInfo, int srcFBO, int dstFBO,
258259
int srcX0, int srcY0, int srcX1, int srcY1,
@@ -808,8 +809,9 @@ void setAmbientLight(long nativeMeshViewInfo, float r, float g, float b) {
808809
nSetAmbientLight(nativeCtxInfo, nativeMeshViewInfo, r, g, b);
809810
}
810811

811-
void setPointLight(long nativeMeshViewInfo, int index, float x, float y, float z, float r, float g, float b, float w) {
812-
nSetPointLight(nativeCtxInfo, nativeMeshViewInfo, index, x, y, z, r, g, b, w);
812+
void setPointLight(long nativeMeshViewInfo, int index, float x, float y, float z, float r, float g, float b, float w,
813+
float ca, float la, float qa, float maxRange) {
814+
nSetPointLight(nativeCtxInfo, nativeMeshViewInfo, index, x, y, z, r, g, b, w, ca, la, qa, maxRange);
813815
}
814816

815817
void renderMeshView(long nativeMeshViewInfo) {

modules/javafx.graphics/src/main/java/javafx/scene/LightBase.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@
4444
import javafx.application.ConditionalFeature;
4545
import javafx.application.Platform;
4646
import javafx.beans.property.BooleanProperty;
47+
import javafx.beans.property.DoubleProperty;
4748
import javafx.beans.property.ObjectProperty;
4849
import javafx.beans.property.SimpleBooleanProperty;
50+
import javafx.beans.property.SimpleDoubleProperty;
4951
import javafx.beans.property.SimpleObjectProperty;
5052
import javafx.collections.ListChangeListener.Change;
5153
import javafx.collections.ObservableList;
@@ -277,6 +279,20 @@ void scenesChanged(final Scene newScene, final SubScene newSubScene,
277279
}
278280
}
279281

282+
/**
283+
* For use by implementing subclasses. Treat as protected.
284+
*
285+
* Creates and returns a SimpleDoubleProperty with an invalidation scheme.
286+
*/
287+
DoubleProperty getLightDoubleProperty(String name, double initialValue) {
288+
return new SimpleDoubleProperty(this, name, initialValue) {
289+
@Override
290+
protected void invalidated() {
291+
NodeHelper.markDirty(LightBase.this, DirtyBits.NODE_LIGHT);
292+
}
293+
};
294+
}
295+
280296
private void markOwnerDirty() {
281297
// if the light is part of the scene/subScene, we will need to notify
282298
// the owner to mark the entire scene/subScene dirty.

0 commit comments

Comments
 (0)