|
| 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 | +} |
0 commit comments