Skip to content

Commit 3ccc518

Browse files
authored
solve issue 1555 (add API to determine max line width) (#1643)
* add glGetFloat() to the GL interface * add getMaxLineWidth() to the Renderer interface * add a simple test for getMaxLineWidth() * bugfix: core profile overrides GL_ALIASED_LINE_WIDTH_RANGE * GLRenderer: delete 2 debug printlns * TestLineWidth: refactor the line-drawing code into a new method
1 parent fb333fe commit 3ccc518

File tree

10 files changed

+196
-4
lines changed

10 files changed

+196
-4
lines changed

jme3-android/src/main/java/com/jme3/renderer/android/AndroidGL.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2020 jMonkeyEngine
2+
* Copyright (c) 2009-2021 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -317,6 +317,12 @@ public int glGetError() {
317317
return GLES20.glGetError();
318318
}
319319

320+
@Override
321+
public void glGetFloat(int parameterId, FloatBuffer storeValues) {
322+
checkLimit(storeValues);
323+
GLES20.glGetFloatv(parameterId, storeValues);
324+
}
325+
320326
@Override
321327
public void glGetInteger(int pname, IntBuffer params) {
322328
checkLimit(params);

jme3-core/src/main/java/com/jme3/renderer/Renderer.java

+7
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,13 @@ public void setTexture(int unit, Texture tex)
486486
*/
487487
public int getDefaultAnisotropicFilter();
488488

489+
/**
490+
* Determine the maximum allowed width for lines.
491+
*
492+
* @return the maximum width (in pixels)
493+
*/
494+
public float getMaxLineWidth();
495+
489496
/**
490497
* Test whether images with the sRGB flag will be linearized when read by a
491498
* shader.

jme3-core/src/main/java/com/jme3/renderer/opengl/GL.java

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646
public interface GL {
4747

48+
public static final int GL_ALIASED_LINE_WIDTH_RANGE = 0x846E;
4849
public static final int GL_ALPHA = 0x1906;
4950
public static final int GL_ALWAYS = 0x207;
5051
public static final int GL_ARRAY_BUFFER = 0x8892;
@@ -97,6 +98,7 @@ public interface GL {
9798
public static final int GL_LINEAR_MIPMAP_NEAREST = 0x2701;
9899
public static final int GL_LINES = 0x1;
99100
public static final int GL_LINE_LOOP = 0x2;
101+
public static final int GL_LINE_SMOOTH = 0xB20;
100102
public static final int GL_LINE_STRIP = 0x3;
101103
public static final int GL_LINK_STATUS = 0x8B82;
102104
public static final int GL_LUMINANCE = 0x1909;
@@ -829,6 +831,15 @@ public void glCompressedTexSubImage2D(int target, int level, int xoffset, int yo
829831
*/
830832
public int glGetError();
831833

834+
/**
835+
* Determine the current single-precision floating-point value(s) of the
836+
* specified parameter.
837+
*
838+
* @param parameterId which parameter
839+
* @param storeValues storage for the value(s)
840+
*/
841+
public void glGetFloat(int parameterId, FloatBuffer storeValues);
842+
832843
/**
833844
* <p><a target="_blank" href="http://docs.gl/gl4/glGetIntegerv">Reference Page</a></p>
834845
*

jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java

+25
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public final class GLRenderer implements Renderer {
8181
private static final Pattern GLVERSION_PATTERN = Pattern.compile(".*?(\\d+)\\.(\\d+).*");
8282

8383
private final ByteBuffer nameBuf = BufferUtils.createByteBuffer(250);
84+
private final FloatBuffer floatBuf16 = BufferUtils.createFloatBuffer(16);
8485
private final StringBuilder stringBuf = new StringBuilder(250);
8586
private final IntBuffer intBuf1 = BufferUtils.createIntBuffer(1);
8687
private final IntBuffer intBuf16 = BufferUtils.createIntBuffer(16);
@@ -3325,6 +3326,30 @@ public int getDefaultAnisotropicFilter() {
33253326
return this.defaultAnisotropicFilter;
33263327
}
33273328

3329+
/**
3330+
* Determine the maximum allowed width for lines.
3331+
*
3332+
* @return the maximum width (in pixels)
3333+
*/
3334+
@Override
3335+
public float getMaxLineWidth() {
3336+
// Since neither JMonkeyEngine nor LWJGL ever enables GL_LINE_SMOOTH,
3337+
// all lines are aliased, but just in case...
3338+
assert !gl.glIsEnabled(GL.GL_LINE_SMOOTH);
3339+
3340+
// When running with OpenGL 3.2+ core profile,
3341+
// compatibility features such as multipixel lines aren't available.
3342+
if (caps.contains(Caps.CoreProfile)) {
3343+
return 1f;
3344+
}
3345+
3346+
floatBuf16.clear();
3347+
gl.glGetFloat(GL.GL_ALIASED_LINE_WIDTH_RANGE, floatBuf16);
3348+
float result = floatBuf16.get(1);
3349+
3350+
return result;
3351+
}
3352+
33283353
/**
33293354
* Test whether images with the sRGB flag will be linearized when read by a
33303355
* shader.

jme3-core/src/main/java/com/jme3/system/NullRenderer.java

+10
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ public void readFrameBufferWithFormat(FrameBuffer fb, ByteBuffer byteBuf, Image.
252252
public void setDefaultAnisotropicFilter(int level) {
253253
}
254254

255+
/**
256+
* Determine the maximum allowed width for lines.
257+
*
258+
* @return the maximum width (in pixels)
259+
*/
260+
@Override
261+
public float getMaxLineWidth() {
262+
return Float.MAX_VALUE;
263+
}
264+
255265
@Override
256266
public boolean getAlphaToCoverage() {
257267
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2021 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package jme3test.renderer;
33+
34+
import com.jme3.app.SimpleApplication;
35+
import com.jme3.font.BitmapFont;
36+
import com.jme3.font.BitmapText;
37+
import com.jme3.material.Material;
38+
import com.jme3.material.Materials;
39+
import com.jme3.math.ColorRGBA;
40+
import com.jme3.math.Vector3f;
41+
import com.jme3.scene.Geometry;
42+
import com.jme3.scene.Mesh;
43+
import com.jme3.scene.shape.Line;
44+
import com.jme3.system.AppSettings;
45+
46+
/**
47+
* Display the renderer's maximum line width.
48+
*
49+
* @author Stephen Gold [email protected]
50+
*/
51+
public class TestLineWidth extends SimpleApplication {
52+
53+
public static void main(String... args) {
54+
TestLineWidth app = new TestLineWidth();
55+
AppSettings set = new AppSettings(true);
56+
set.setRenderer(AppSettings.LWJGL_OPENGL2);
57+
app.setSettings(set);
58+
app.start();
59+
}
60+
61+
@Override
62+
public void simpleInitApp() {
63+
/*
64+
* Generate a message to report (1) which renderer is selected
65+
* and (2) the maximum line width.
66+
*/
67+
String rendererName = settings.getRenderer();
68+
float maxWidth = renderer.getMaxLineWidth();
69+
String message = String.format(
70+
"using %s renderer%nmaximum line width = %.1f pixel%s",
71+
rendererName, maxWidth, (maxWidth == 1f) ? "" : "s");
72+
/*
73+
* Display the message, centered near the top of the display.
74+
*/
75+
BitmapFont font = assetManager.loadFont("Interface/Fonts/Default.fnt");
76+
BitmapText text = new BitmapText(font, false);
77+
text.setSize(font.getCharSet().getRenderedSize());
78+
text.setText(message);
79+
float leftX = (cam.getWidth() - text.getLineWidth()) / 2;
80+
float topY = cam.getHeight();
81+
text.setLocalTranslation(leftX, topY, 0f);
82+
guiNode.attachChild(text);
83+
/*
84+
* Display a vertical green line on the left side of the display.
85+
*/
86+
float lineWidth = Math.min(maxWidth, leftX);
87+
drawVerticalLine(lineWidth, leftX / 2, ColorRGBA.Green);
88+
}
89+
90+
private void drawVerticalLine(float lineWidth, float x, ColorRGBA color) {
91+
Material material = new Material(assetManager, Materials.UNSHADED);
92+
material.setColor("Color", color.clone());
93+
material.getAdditionalRenderState().setLineWidth(lineWidth);
94+
95+
float viewportHeight = cam.getHeight();
96+
Vector3f startLocation = new Vector3f(x, 0.1f * viewportHeight, 0f);
97+
Vector3f endLocation = new Vector3f(x, 0.9f * viewportHeight, 0f);
98+
Mesh wireMesh = new Line(startLocation, endLocation);
99+
Geometry wire = new Geometry("wire", wireMesh);
100+
wire.setMaterial(material);
101+
guiNode.attachChild(wire);
102+
}
103+
}

jme3-ios/src/main/java/com/jme3/renderer/ios/IosGL.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2020 jMonkeyEngine
2+
* Copyright (c) 2009-2021 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -52,6 +52,7 @@
5252
*/
5353
public class IosGL implements GL, GL2, GLES_30, GLExt, GLFbo {
5454

55+
private final float[] tmpFloatArray = new float[16];
5556
private final int[] temp_array = new int[16];
5657
private final IntBuffer tmpBuff = BufferUtils.createIntBuffer(1);
5758

@@ -100,6 +101,15 @@ private int toArray(IntBuffer buffer) {
100101
return remain;
101102
}
102103

104+
private void fromArray(int n, float[] array, FloatBuffer buffer) {
105+
if (buffer.remaining() < n) {
106+
throw new BufferOverflowException();
107+
}
108+
int pos = buffer.position();
109+
buffer.put(array, 0, n);
110+
buffer.position(pos);
111+
}
112+
103113
private void fromArray(int n, int[] array, IntBuffer buffer) {
104114
if (buffer.remaining() < n) {
105115
throw new BufferOverflowException();
@@ -355,6 +365,13 @@ public int glGetError() {
355365
return JmeIosGLES.glGetError();
356366
}
357367

368+
@Override
369+
public void glGetFloat(int parameterId, FloatBuffer storeValues) {
370+
checkLimit(storeValues);
371+
JmeIosGLES.glGetFloatv(parameterId, tmpFloatArray, 0);
372+
fromArray(storeValues.remaining(), tmpFloatArray, storeValues);
373+
}
374+
358375
@Override
359376
public void glGetInteger(int pname, IntBuffer params) {
360377
checkLimit(params);

jme3-ios/src/main/java/com/jme3/renderer/ios/JmeIosGLES.java

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ private JmeIosGLES() {
193193
public static native int glGetAttribLocation(int program, String name);
194194
public static native void glGetBoolean(int pname, ByteBuffer params);
195195
public static native int glGetError();
196+
public static native void glGetFloatv (int pname, float[] params, int offset);
196197
public static native void glGetFramebufferAttachmentParameteriv(int target, int attachment, int pname, int[] params, int offset);
197198
public static native void glGetIntegerv (int pname, int[] params, int offset);
198199
public static native String glGetProgramInfoLog(int program);

jme3-lwjgl/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,13 @@ public void glGetBufferSubData(int target, long offset, ByteBuffer data) {
290290
public int glGetError() {
291291
return GL11.glGetError();
292292
}
293-
293+
294+
@Override
295+
public void glGetFloat(int parameterId, FloatBuffer storeValues) {
296+
checkLimit(storeValues);
297+
GL11.glGetFloat(parameterId, storeValues);
298+
}
299+
294300
@Override
295301
public void glGetInteger(int param1, IntBuffer param2) {
296302
checkLimit(param2);

jme3-lwjgl3/src/main/java/com/jme3/renderer/lwjgl/LwjglGL.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2020 jMonkeyEngine
2+
* Copyright (c) 2009-2021 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -319,6 +319,12 @@ public int glGetError() {
319319
return GL11.glGetError();
320320
}
321321

322+
@Override
323+
public void glGetFloat(int parameterId, FloatBuffer storeValues) {
324+
checkLimit(storeValues);
325+
GL11.glGetFloatv(parameterId, storeValues);
326+
}
327+
322328
@Override
323329
public void glGetInteger(final int pname, final IntBuffer params) {
324330
checkLimit(params);

0 commit comments

Comments
 (0)