My HUD problems

I am following the guide for huds for noobs on the wiki:



http://www.jmonkeyengine.com/wiki/doku.php?id=displaying_dynamic_values_in_the_hud



And it was going good until this article.



I am using JME 2.0 and it seems to be complaining about lines where i use :



.setTextureBuffer(FloatBuffer,int)



This function doesnt seem to exist - and I dont see any notes for it in the wiki jme 1 - 2 changes.

What is the new equivalent!?

Thanks

Andy

could be that this functionen doesnt exist or is named different in jme2 cause this tutorial refers to jme1

When Making the hud it could be easier to use feng gui this how i do it with jme1

I think the problem is that TriangleBatch-s were removed in jME 1 -> jME 2. Try .setTextureCoords()



When you find yourelf in a similar situation in the future, one easy way to find out how to change your code from jME 1 to jME 2 is by doing a search in the source:


  1. Type in .setTextureBuffer in the IDE search and let it run through jME 1 source. This gives us multiple matches. Lets pick TestGeometryInstancing. On line 203 we have:

batch.setTextureBuffer(BufferUtils.createVector2Buffer(geometryBatchCreator.getNumVertices()), 0);


2. Open up the same file in jME 2 source. Find the corresponding line (might not be exact match, but just look around that part of the code). Here we find:

mesh.setTextureCoords(new TexCoords(BufferUtils.createVector2Buffer(geometryBatchCreator.getNumVertices())), 0);



Happy porting :)

I'll add it to the wiki

help!

I cvhanged my code to conform to the snippet you posted. Now the bar appears in magenta, however nothing moves in the bar …



Even when I move the camera…

It probably did not find the texture file hudtutorial3.png. Make sure it is available for your code. Te pink tex you see is the default texture. If you look at the output on the console it will probably also tell you that this problem occurred.

thanks, however, thepic in the tutorial is huge, and seems to have 2 images in it, is this the correct hudtutorial3.png?

the image linked to from the tutorial works fine

for some reason it isnt being loaded properly on my end, you are talking about this :



http://www.jmonkeyengine.com/wiki/lib/exe/detail.php?id=displaying_dynamic_values_in_the_hud&cache=cache&media=hudtutorial3.png



if you look at that png, it has all sorts of rubbish in it :S

the image consists of two parts the gauge and the border, texture coordinates are used to display the correct parts when needed.

in the Buster class you have this:


hudNode.setLocalTranslation(new Vector3f(this.getLocalTranslation().x,
      this.getLocalTranslation().y+100,
      this.getLocalTranslation().z));



Here you try to set the Gauge quad above the Buster (this) which is a 3d model right ?
This wont work, because the Ortho and perspective modes have different coordinate systems.

Ortho goes from 0/0 (Bottom left) to 800/600 (top right).
Where as the Perspective Coordinate system goes from like -X/-X-/-x to +x/+x/+x.

I guess you need to either convert the perspective coords to ortho coords or use billboarded quads to display the gauges.

i think there are some posts in the forum on how to display name tags above characters.

oh…

hmmm…

I tried searching for them, but dont see them :frowning:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=5725.0

http://www.jmonkeyengine.com/jmeforum/index.php?topic=7852.0



:slight_smile:

am using the textlabel2d class, and it if works, ill make it work with the healthbar (will be similar no doubt) Thanks core.



However in the class :



import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Vector2f;
import com.jme.renderer.Renderer;
import com.jme.scene.BillboardNode;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.*;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.*;
import java.nio.FloatBuffer;
import java.util.Arrays;

public class TextLabel {
    private String text;
    private float blurIntensity = 0.1f;
    private int kernelSize = 5;
    private ConvolveOp blur;
    private Color foreground = new Color(1f, 1f, 1f);
    private Color background = new Color(0f, 0f, 0f);
    private float fontResolution = 40f;
    private int shadowOffsetX = 2;
    private int shadowOffsetY = 2;
    private Font font;
   
    public TextLabel(String text) {
        this.text = text;
        updateKernel();
        setFont(Font.decode("Sans PLAIN 40"));
    }
   
    public void setFont(Font font){
        this.font = font;
    }
   
    public void setShadowOffsetX(int offsetPixelX){
        shadowOffsetX = offsetPixelX;
    }
    public void setShadowOffsetY(int offsetPixelY){
        shadowOffsetY = offsetPixelY;
    }
    public void setBlurSize(int kernelSize){
        this.kernelSize = kernelSize;
        updateKernel();
    }
    public void setBlurStrength(float strength){
        this.blurIntensity = strength;
        updateKernel();
    }
    public void setFontResolution(float fontResolution){
        this.fontResolution = fontResolution;
    }

    private void updateKernel() {
        float[] kernel = new float[kernelSize*kernelSize];
        Arrays.fill(kernel, blurIntensity);
        blur = new ConvolveOp(new Kernel(kernelSize, kernelSize, kernel));
    }
   
    /**
     *
     * @param scaleFactors is set to the factors needed to adjust texture coords
     * to the next-power-of-two- sized resulting image
     */
    private BufferedImage getImage(Vector2f scaleFactors){
        BufferedImage tmp0 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) tmp0.getGraphics();
        Font drawFont = font.deriveFont(fontResolution);
        g2d.setFont(drawFont);
        Rectangle2D b = g2d.getFontMetrics().getStringBounds(text, g2d);
       
        int actualX = (int)b.getWidth()+kernelSize+1+shadowOffsetX;
        int actualY = (int)b.getHeight()+kernelSize+1+shadowOffsetY;
       
        int desiredX = FastMath.nearestPowerOfTwo(actualX);
        int desiredY = FastMath.nearestPowerOfTwo(actualY);
       
        if(scaleFactors != null){
            scaleFactors.x = (float)actualX/desiredX;
            scaleFactors.y = (float)actualY/desiredY;
        }
       
        tmp0 = new BufferedImage(desiredX, desiredY, BufferedImage.TYPE_INT_ARGB);
       
        g2d = (Graphics2D) tmp0.getGraphics();
        g2d.setFont(drawFont);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       
        int textX = kernelSize/2;
        int textY = g2d.getFontMetrics().getMaxAscent() - kernelSize/2;
       
        g2d.setColor(background);
        g2d.drawString(text, textX + shadowOffsetX, textY + shadowOffsetY);
       
        BufferedImage ret = blur.filter(tmp0, null);
       
        g2d = (Graphics2D) ret.getGraphics();
        g2d.setFont(drawFont);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
       
        g2d.setColor(foreground);
        g2d.drawString(text, textX, textY);
       
        return ret;
    }
   
    public Quad getQuad(float height){
        Vector2f scales = new Vector2f();
        BufferedImage img = getImage(scales);
        float w = img.getWidth() * scales.x;
        float h = img.getHeight() * scales.y;
        float factor = height / h;
        Quad ret = new Quad("textLabel2d", w * factor , h * factor);
        TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        Texture tex = TextureManager.loadTexture(img, Texture.MinificationFilter.Trilinear , Texture.MagnificationFilter.Bilinear , true);
       
        FloatBuffer texCo = ret.getTextureBuffer(0, 0);
        FloatBuffer newTC = BufferUtils.createFloatBuffer(texCo.limit());
        texCo.rewind();
        for(int i=0; i<texCo.limit(); i+=2){
            float u = texCo.get();
            float v = texCo.get();
            newTC.put(u*scales.x);
            newTC.put(v*scales.y);
        }
        ret.setTextureBuffer(0, newTC);
        ret.updateGeometricState(0, true);
       
//        tex.setScale(new Vector3f(scales.x, scales.y, 1));
        ts.setTexture(tex);
        ts.setEnabled(true);
        ret.setRenderState(ts);
       
        ret.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
       
        AlphaState as = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
        as.setBlendEnabled(true);
        as.setTestEnabled(true);
        as.setTestFunction(AlphaState.TF_GREATER);
        as.setEnabled(true);
        ret.setRenderState(as);
       
        ret.setLightCombineMode(LightState.OFF);
        ret.updateRenderState();
        return ret;
    }
   
    public BillboardNode getBillboard(float height){
        BillboardNode bb = new BillboardNode("bb");
        Quad q = getQuad(height);
        bb.attachChild(q);
        return bb;
    }

    public void setForeground(Color foreground) {
        this.foreground = foreground;
    }

    public void setBackground(Color background) {
        this.background = background;
    }
}



The :
  FloatBuffer texCo = ret.getTextureBuffer(0, 0);
        FloatBuffer newTC = BufferUtils.createFloatBuffer(texCo.limit());
        texCo.rewind();
        for(int i=0; i<texCo.limit(); i+=2){
            float u = texCo.get();
            float v = texCo.get();
            newTC.put(u*scales.x);
            newTC.put(v*scales.y);
        }
        ret.setTextureBuffer(0, newTC);
        ret.updateGeometricState(0, true);

code doesnt work

It is because createTextureBuffer isnt used, its now createTextureCoords, but i dont know how to change the bits of code!

anybody help! :smiley:

renegadeandy said:

It is because createTextureBuffer isnt used, its now createTextureCoords, but i dont know how to change the bits of code!

Try to describe in more detail, I don't understand quite what your problem is! I can't spot a createTextureBuffer or createTextureCoords in the code you posted, for starters.
You might of course refer to setTextureBuffer / setTextureCoords, if that's your problem, it is really easy to convert - just construct a TexCoords from the float buffer, as seen in  the huds tutorial.

Please! Somebody! :smiley:

see my earlier reply, you cannot use the location of your enemy to set the location of the HP Bar.

Those two objects use different coordinate systems.



this would only work if the Buster is rendered in ORTHO Queue which, i guess, isn't.


      hudNode.setLocalTranslation(new Vector3f(this.getLocalTranslation().x,this.getLocalTranslation().y+100,this.getLocalTranslation().z));

argh yes - so how DO i do it then!? in reference to my example code!