How to draw a line

You might still want to call these methods, especially updateBound() since the line might get culled when it is actually visible.

1 Like

Well i just tried to create a single Triangle, but failed miserably,



my idea is:



Mesh lineMesh = new Mesh();
//guess this mode then
lineMesh.setMode(Mesh.Mode.Triangles);

//what to put in place of the 3 ? and why was it a 3 anyways isn't a line only consisting of 2 points?
lineMesh.setBuffer(VertexBuffer.Type.Position, 3, new float[]{ 0,0,0, 0,1,0 0,1,1});

//well i think just adding a 2 is correct, nothing complicated here?
lineMesh.setBuffer(VertexBuffer.Type.Index, 3, new short[]{ 0, 1,2 });

lineMesh.updateBound();
lineMesh.updateCounts();

Geometry lineGeometry = new Geometry("line", lineMesh);

//jsut a hack/fix for the Absolut loading, copy pased out of some example
Material lineMaterial = manager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");

lineGeometry.setMaterial(lineMaterial);

rootNode.attachChild(lineGeometry);



1 Like

That looks fine, but you gotta have texture coordinates if you want texture mapping to work correctly.

1 Like

I hope it's the texture, because i'm unable to see it(however the debug obj count increases when lookin in that direction.)

Well how can I set Texture coordinates?



lineMesh.setBuffer(VertexBuffer.Type.TexCoord,3, new short[]{0,0, 1,1, 0,1});

3 points to give texture coordinates, some 2d vectors between 0 and 1 in each component. does that make sense?

1 Like

Well ok it seems to work fine as said above, but i today came to the conclusion that it would be far more effective, if I could create a convex object directly without having to create each single triangle, However i kinda miss any example on how I could do this… As well the implementation of the box is not really healpfull if you try to understand something…



        fpb.put(new float[] {
                v[0].x, v[0].y, v[0].z, v[1].x, v[1].y, v[1].z, v[2].x, v[2].y, v[2].z, v[3].x, v[3].y, v[3].z, // back
                v[1].x, v[1].y, v[1].z, v[4].x, v[4].y, v[4].z, v[6].x, v[6].y, v[6].z, v[2].x, v[2].y, v[2].z, // right
                v[4].x, v[4].y, v[4].z, v[5].x, v[5].y, v[5].z, v[7].x, v[7].y, v[7].z, v[6].x, v[6].y, v[6].z, // front
                v[5].x, v[5].y, v[5].z, v[0].x, v[0].y, v[0].z, v[3].x, v[3].y, v[3].z, v[7].x, v[7].y, v[7].z, // left
                v[2].x, v[2].y, v[2].z, v[6].x, v[6].y, v[6].z, v[7].x, v[7].y, v[7].z, v[3].x, v[3].y, v[3].z, // top
                v[0].x, v[0].y, v[0].z, v[5].x, v[5].y, v[5].z, v[4].x, v[4].y, v[4].z, v[1].x, v[1].y, v[1].z  // bottom
        });



As far as I understand it just passes each corner for each side, and thats it, ok, but what should I set here then?
lineMesh.setMode(Mesh.Mode.Triangles);
As triangles (at least for me) does not seem to fit anymore.
1 Like

The 2nd integer argument to the setbuffer calls is the number of components per vertex, there are two components for texture coordinates, U and V, then you should specify 2 instead of 3.

Also, using shorts for texcoords is not recommended, you should use floats instead



lineMesh.setBuffer(VertexBuffer.Type.TexCoord, 2[tt], new float[]{0,0, 1,1, 0,1});

1 Like

Yes that fixed a few missing textures, depending on orientation of the triangle.



I'm still stuck however to think of an efficient way to create a triangleshape out of a array of Vector3fs that form a convex shape :confused:

1 Like

What exactly are you trying to create? Did you look at how the shape classes work? It is not supposed to be very simple to create "triangleshapes"… Usually you would create them in a modeling tool and then import them into jME3.

1 Like

A vmf to jme compiler, however the vmf format specifies only convex objects, and gives all points for each corner :confused:

1 Like

Well, this isn't jME3's problem is it? My job is just to provide an interface so you could create vertex buffers using whether method you choose. Importing VMF files is a whole different story then.

1 Like

Yes, never said it's your job ^^



I'm just thought there may be a way to set the mode to a convex type, so i don't have to  calculate those triangles… But well seems like there is no such thing, so I have to think about how to calculate all those triangles manually…

1 Like

found a tut where everything is explained!



http://test.hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:custom_meshes

1 Like

sorry for bumping,

i am fairly new to jME3 and Java

i am trying to draw a route from an array of points (preferably in a 2D view as well) in a route editor for a train game. The player must be able to add/edit routes with a point and click method, had this working in a jFrame with AWT but poor frame rates made me experiment with jME3 and now i would like to have this working in jME3.

I could draw a single line using the above method in my function:

private void add2DLine( Position A, Position B, Node myNode ) {

    Mesh lineMesh = new Mesh();

    lineMesh.setMode(Mesh.Mode.Lines);

    lineMesh.setBuffer(VertexBuffer.Type.Position, 3, new float[]{ (float) A.getXPos(), (float) A.getYPos(), (float) A.getZPos(), (float) B.getXPos(), (float) B.getYPos(), (float) B.getZPos()});

    lineMesh.setBuffer(VertexBuffer.Type.Index, 2, new short[]{ 0, 1 });

    lineMesh.updateBound();

    lineMesh.updateCounts();

    Geometry lineGeometry = new Geometry("line", lineMesh);

    Material lineMaterial = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
    
    lineGeometry.setMaterial(lineMaterial);

    myNode.attachChild(lineGeometry);

};

but when i init my world:

add2DLine( new Position ( 0.0, 0.0, 0.0), new Position (0.0, 1.0, 0.0), View2D );
add2DLine( new Position ( 0.0, 1.0, 0.0), new Position (0.0, 1.0, 1.0), View2D );
add2DLine( new Position ( 0.0, 1.0, 1.0), new Position (0.0, 2.0, 1.0), View2D );

i only see one line, where i expected to see 3 connected lines
(View2D is a Node which gets attached to rootNode upon entering edit mode, default mu View3D node is displayed)

1 Like
  1. Why don’t you just use the Line class? It’s much easier.

  2. It could be because of multiple reasons, try making a single class testate case

  3. Besides, the code seems quite messy (you’re using Position in a place where Vector3f would be expected). Try to keep your code separated.

  4. You can look here if you don’t know what to do else : https://jmonkeyengine.github.io/wiki/jme3/advanced/custom_meshes.html

1 Like

Hello MisterCavespider,

  1. thanks for the input, i will look into the Line class.

  2. i will look into Line class first, it sounds more like what i need

  3. i started out without any game engine and build my own Position and Vector class and much more, some of which i discovered later were already implemented. Much of the more sophisticated classes are build upon my own implementation, for now i stick with them.

  4. i’ll have a look into that although i am doubting at the moment if meshes are the right solution for a 2d representation
    of a track layout.

1 Like

Nothing in your code jumps out as incorrect. It’s possible that the problem is elsewhere.

If you can’t find the issue then simplify the test into a single class test case that exhibits the issue and then post that.

1 Like

Mesh is a general term for the surface of something which is displayed. Along with a Material it makes up a Geometry.

Now jme being a 3d engine you have to do pretty much anything in 3d and use the guiNode. You have some simple 2d classes but for example there is no Sprite Sheet Support or 2d Physics. It might not be needed for a Track Editor so you are fine with jme but if you need some heavy 2d stuff you might need another engine or Code much stuff.

1 Like

I followed an example on stack exchange on how to use Line and it is now producing lines:

in init:

Node line1 = new Node();
        Node line2 = new Node();
        Node line3 = new Node();
        add2DLine( new Position ( 0.0, 1.0, 0.0), new Position (1.0, 0.0, 0.0), line1, 1 );
        add2DLine( new Position ( 0.0, 0.0, 0.0), new Position (0.0, 0.0, 1.0), line2, 2 );
        add2DLine( new Position ( 0.0, 0.0, 0.0), new Position (0.0, 1.0, 0.0), line3, 3 );
        View2D.attachChild( line1 );
        View2D.attachChild( line2 );
        View2D.attachChild( line3 );

and the line function with color added for debugging

private void add2DLine( Position A, Position B, Node myNode, int color ) {
        Line line = new Line(new Vector3f( (float) A.getXPos(), (float) A.getYPos(), (float) A.getZPos()), 
                             new Vector3f( (float) B.getXPos(), (float) B.getYPos(), (float) B.getZPos()));
        line.setLineWidth(2);
        Geometry geometry = new Geometry("Bullet", line);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        if ( color == 1 ) {
            mat.setColor("Color", ColorRGBA.Blue);
        };
        if ( color == 2 ) {
            mat.setColor("Color", ColorRGBA.Green);
        };
        if ( color == 3 ) {
            mat.setColor("Color", ColorRGBA.Red);
        };
        if ( color > 3 ) {
            mat.setColor("Color", ColorRGBA.Pink);
        };
        
        geometry.setMaterial( mat );           
        mat.getAdditionalRenderState().setFaceCullMode( RenderState.FaceCullMode.Off );
        geometry.setCullHint(Spatial.CullHint.Never); 
        geometry.updateGeometricState();  //unsure about this is necessary
         geometry.updateModelBound();     //unsure about this is necessary 
        myNode.attachChild(geometry);
    };

1 Like

For the future:

…and it’s a sticky at the top of the forum, too.

1 Like

Thanks, i changed the code post

1 Like