HelloMesh tutorial (JME3)

Hello everybody!



As I promised I am writing a HelloMesh tutorial for beginners so that to cover some of the basics. The source code provided below have 3 parts, each covering a different shader. I tried to explain everything in one tutorial, but as you see, I have not covered one part regarding vertex coloring, and here I am asking to help me.



Please post here any suggestions how to polish and finish this tutorial.



Thank you.



Here’s the source.



[java]package hellomesh;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector2f;

import com.jme3.scene.Mesh;

import com.jme3.scene.VertexBuffer.Type;

import com.jme3.util.BufferUtils;



public class HelloMesh extends SimpleApplication {



public static void main(String[] args){

HelloMesh app = new HelloMesh();

app.start();

}



@Override

public void simpleInitApp() {



Mesh m = new Mesh();



// Vertex positions in space

Vector3f [] vertexes = new Vector3f[4];

vertexes[0] = new Vector3f(0,0,0);

vertexes[1] = new Vector3f(3,0,0);

vertexes[2] = new Vector3f(0,3,0);

vertexes[3] = new Vector3f(3,3,0);



// Texture coordinates

Vector2f [] texCoord = new Vector2f[4];

texCoord[0] = new Vector2f(0,0);

texCoord[1] = new Vector2f(1,0);

texCoord[2] = new Vector2f(0,1);

texCoord[3] = new Vector2f(1,1);



// Indexes. We define the order in which mesh should be constructed

int [] indexes = {2,0,1,1,3,2};



// Setting buffers

m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertexes));

m.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));

m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));



// Creating a geometry, and apply a single color shader to it

Geometry geom = new Geometry(“OurMesh”, m);

Material mat = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

mat.setColor(“m_Color”, ColorRGBA.Blue);

geom.setMaterial(mat);



// Attaching our geometry to the root node.

rootNode.attachChild(geom);





// *************************************************************************

// Second mesh will use vertex colors to color each vertex

// *************************************************************************

Mesh cMesh = m.clone();

Geometry coloredMesh = new Geometry (“ColoredMesh”, cMesh);

Material matVC = new Material(assetManager, “Common/MatDefs/Misc/VertexColor.j3md”);



// Please fill here how to define vertex colors for each vertex using shader, because I haven’t found any info about it.

// The idea was to color each vertes in different color.



coloredMesh.setMaterial(matVC);



// move mesh a bit so that it don’t intersect with the first one

coloredMesh.setLocalTranslation(4, 0, 0);



rootNode.attachChild(coloredMesh);





// *************************************************************************

// Third mesh will use a wireframe shader to show wireframe

// *************************************************************************

Mesh wfMesh = m.clone();

Geometry wfGeom = new Geometry(“wireframeGeometry”, wfMesh);

Material matWireframe = new Material(assetManager, “Common/MatDefs/Misc/WireColor.j3md”);

matWireframe.setColor(“m_Color”, ColorRGBA.Green);

wfGeom.setMaterial(matWireframe);

wfGeom.setLocalTranslation(-4, 4, 0);

rootNode.attachChild(wfGeom);



}

}[/java]

4 Likes

I find this very useful! Thank you a lot Kay.

Hi,

I am getting some strange behavior when running this example. when i look arround and move by the FlyCam, the meshes disappear sometimes from the viewport. I have made some small experiments with more complex procedurally created geometry, where the impact of this is even bigger, at some angle or position it isn’t visible at all… And i don’t mean i just turn away from them :wink: they disappear even if they should be visible normally. This doen’t happen to predefined shape primitives like Box that i add to the scene…

The same happens under linux, windows, geforce7 and geforce8. I use the jMonkeyPlatform, newest updates, standard configuration and app-settings.

Can anybody reproduce this as well?

Otherwise it is a helpful tutorial!

Ok, after investigating the Box class i found out that the call of updateBound() is missing after setting the Buffers of the mesh. With that i can see clear again 8)

trival said:
Ok, after investigating the Box class i found out that the call of updateBound() is missing after setting the Buffers of the mesh. With that i can see clear again 8)

What do you mean? Look at the end at the method "duUpdateGeometryVertices()", there's an updateBound() there.

sorry for being unclear. Everything is fine with the Box class. I looked into it to understand how you are creating a Mesh object properly.



So i think the updateBound() is missing in this Tutorial. Adding it finally stopped the strange behavior that i tried to ged rid of for over 2 days…

Hi @KayTrance,



sorry I don’t read the forums regularily, just saw your post. Cool, very useful!



I don’t know the answer to the vertex coloring question, but I will ask around.



I wonder, do beginners ever care to create custom meshes? I thought this was rather something that developers look into after they have figured out the basics of the Engine. Or at least it was like that for me: For jME2, I went through all the beginner tutorials, but I skipped the “hello trimesh” one, because I didn’t see what I would ever need it for. A year later I needed a custom mesh, and I didn’t even remember that this info had been in the beginner tuts.



What I want to say is, I would like to use your code sample for the “advanced” section of the documentation page. What do you think?



(When I named this section “advanced”, I didn’t mean it in the way of “horribly difficult”, but just, “rather specific details” and “not used by everyone”. In comparison, in the beginner tutorials I tried to include the things that everyone uses; and the intermediate ones will show how to connect the learned things together.)

Many people dont come here and want to learn the engine and then go about applying what they learned to create something but they directly want to do a certain thing like “create a mmo”, “make a clone of game x” and they soon realize they need info like this to do it. So i guess yes, newbies will want this info from time to time :wink:

Cheers,

Normen

OK, survey time – How often have you (or anyone else) created a custom mesh? And when in the process of creating a game was that? :stuck_out_tongue:

I never did, its not how I would work ;p Oh, for bullet mesh debug shapes I had to create my own mesh :slight_smile:

@trival



Same problem here. However calling updateBound() doesn’t resolve the problem…Any thoughts? Spent 4 days trying to resolve this :frowning:



[java]



[ init vertices here ]



Vector3f[] verticesArray = new Vector3f[14];

verticesArray = vertices.toArray(verticesArray);

hull.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(verticesArray));

hull.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indices));

hull.updateBound();



Material material = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

material.setColor(“m_Color”, ColorRGBA.Black);

hullGeometry = new Geometry(“Vessel Hull”, hull);

hullGeometry.setMaterial(material);

hullGeometry.updateModelBound();

[/java]

The problem that the meshes disappar, you mean? I noticed that too.



Does the backside of a or mesh “exist” at all? When you’re inside a box or sphere, you don’t see the inside either, or do you?

I think you have to define the same two triangles per Quad again, but in the opposite order, then you’ll have a backside. (That – or you’ll get some great Z-fighting… Hm…)

You could set the rendering to front and back faces, just to try:

Matial.getAdditionalRenderStates().setCullState or something like that.

@zathras Yes, some meshes or parts of the mesh disappear. As I move they might reappear just to have others disappear…

Thanks, I will try what you said :slight_smile:



@norman

Danke. I already used my_material.getAdditionalRenderState().setFaceCullMode(FaceCullMode.FaceAndBack);, however to no avail. I presume that this is what you meant with Matial.getAdditionalRenderStates().setCullState? So, that doesn’t work…unless you are referring to a different class?

Hi javatar, i guess that this problem could be caused by the frustum culling mechanism of the scene graph or render engine. I think the scene graph culls away each object whose boundingvolume lies outside the view frustum of the camera. So if your mesh doesn’t have its boundingvolume set correctly, it is culled out incorrectly or randomly. That would explain why calling updateBound() solved the problem for me. Maybe you should try to set a new boundingVolume, or something went wrong in setting a bound to your mesh, don’t know, but maybe this is the direction where to look. you could maybe check it somehow with getModelBound() or something… hope it helps, and please correct me if i’m wrong…



Btw, this code is running correctly for me:

[java]

//… fill c and v buffers here…



Mesh m = new Mesh();

m.setMode(Mesh.Mode.Points);

m.setBuffer(VertexBuffer.Type.Color, 4, BufferUtils.createFloatBuffer©);

m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(v));

m.setPointSize(10f);

//m.updateCounts();

m.updateBound();

m.setStatic();



Geometry points = new Geometry(“Points”, m);

points.setMaterial(mat);

rootNode.attachChild(points);

[/java]

Have you tried this in your main initialization?



rootNode.setCullHint(CullHint.Never);



It looks overkill, and probably is, but its a start.

javatar said:
...unless you are referring to a different class?

No, that was what I meant :/ Sorry, no real idea here..

@ jiyarza – Thank you. I’m afraid that this didn’t work though. :frowning: Any more thoughts? Much appreciated.

@trivia – Still no luck I’m afraid… :frowning:

One more question: What does setPointSize() do? I can’t find anything on it in the javadoc…

Thanks for your time.

I think it only sets the point size for when you display the mesh as points.