Custom mesh light glitch [Solved]

i made a trianlge geometry by using a custom mesh, here is the code :

public class Triangle extends Mesh
	public static final float SIZE = 12f;
	private Mesh mesh = new Mesh();
	public Triangle(float a, float b, float c)
    	Vector3f [] vertices = new Vector3f[3];
        vertices[0] = new Vector3f(0.0f, a ,0.0f);
        vertices[1] = new Vector3f(SIZE, c ,0.0f);
        vertices[2] = new Vector3f(SIZE/2, b ,SIZE); //pointe
        Vector2f [] texCoord = new Vector2f[3];
        texCoord[0] = new Vector2f(0,0);
        texCoord[1] = new Vector2f(1,0);
        texCoord[2] = new Vector2f(0,1);
        short[] indexes = {1, 0, 2};
        mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
        mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
        mesh.setBuffer(Type.Index, 1, BufferUtils.createShortBuffer(indexes));
	public Mesh mesh()
		return mesh;

And i use it in this scene :

public class test2 extends SimpleApplication

	public void simpleInitApp()
		assetManager.registerLocator("res/", FileLocator.class);
		Triangle triangle = new Triangle(0,0,0);
		Mesh mesh = triangle.mesh();
		Geometry geo;

		Vector3f vec;
		for(int x=0 ; x<=1 ; x++)
			for(int z=0 ; z<=0 ; z++)
				boolean reverse = false;
				vec = new Vector3f(x*Triangle.SIZE/2, 0, z*Triangle.SIZE);
				geo = new Geometry("Triangle", mesh);
				Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
			    mat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/grass.jpg"));
			    mat.setFloat("Shininess", 64f);
				if (x%2 != 0)
					reverse = true;
				if (z%2 == 0)
					reverse = !reverse;
				if (reverse)
					geo.rotate(0,  FastMath.PI, 0);
					vec.x += Triangle.SIZE;
					vec.z += Triangle.SIZE;

        lamp_light = new PointLight();
    PointLight lamp_light;
    public void simpleUpdate(float tps)
	public static void main(String[] args)
		test2 app = new test2();

In this sceen there’s 2 triangles but i can’t see both at the same time, depend of the camera direction i can only see one of them.
But if i change the material to :

Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue); 

They both appear. What i did wrong ?
(you need to turn the camera to the right to see them)

with light:

without light:

thx guys !

Lighting won’t work without normals. Else how will it know which way the thing is facing?

1 Like

Also, why are you doing this? If you just replace calls to mesh by this, you can directly pass in your triangle:

geo = new Geometry("Triangle", triangle);

Ok, i search and find this post
But the link with the answer is broken :sob:
i’ll try to made it myself.

I use the triangles as ground so they always face upward.

The shaders can’t know that because you didn’t give the vertexes any normals. What direction do they face? answer: random garbage in memory in your current implementation.

Did you look at the tutorials?

100% unrelated. You have to have normals already to generate tangents.

Yes but i totaly skiped the section about normal :open_mouth:
i update my triangle code:

    float[] normals = new float[12];
    normals = new float[]{0,1,0, 0,1,0, 0,1,0, 0,1,0};
    mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));

And it’s woked ! But now my triangle look cambered on the edge :neutral_face:
but i think i need to work on the normal coords.
thx for the replies

Your normals should point orthongonally into the air. You can achieve that by applying the cross product of vector(b,c) and vector(b,a) of your a,b,c 3D points (which are represented as vectors too in your case).

I think you should learn basic vector math, cartesian coordinates, etc. - basic math and geometry stuff (don’t know if our favorite “math for dummies” (see main page of this forum) covers that stuff or maybe it’s already too advanced). I learned most of these things in highschool - before we had internet and when DOS games where the thing for nerds… :chimpanzee_closedlaugh:

I learn this stuff in school but I don’t know how normal use vector or how they’re applied.
Here for my normals I got 4 times 3 coords but for a triangle it seems wierd :neutral_face:

Well, just know that the normal should have length 1 (normalized) and must have 90° (DEGREE) or PI0.5 (RADIANS) angle to the surface (orthogonal). That part with the 90° (PI0.5) may vary for other cases (e.g. spheres can not have such normals). But in your case it’s that simple.

If you know what a cross product is, what a vector and a point are in 3D, then you can quickly count 1 and 1 together and make use of that knowledge. But from where you are it’s a long long way. Have you read the “math for dummies” ? It covers basics (just looked into it). Then you need to google a site about 3D graphics (what is a normal what is a vertex - this kind of level of explanation with lots of images) - might be any site, not just those related to jME…

I guess you copied the code for a quad… you only need three normals if you have three vertexes. Just like you only needed three texture coordinates. They all go together one to one.

Is your terrain ultimately supposed to look smooth or flat-shaded? Because they approach you are taking now will lead to flat-shading.

If flat-shading is desired then you need to take the cross product between vertices[1] - vertices[0] and vertices[2] - vertices[0] (translated into real subtract() call and so on.) As it’s flat-shaded you can use that same normal for all of the vertexes.

Ultimately, for terrain all of these separate objects aren’t going to work and you’ll eventually want a whole terrain mesh.

I started reading the math for dummies guide but never finish it (still in my bookmark) but now i plan to read it to the end.

Indeed I want my terrain look flat-shaded. I use separate object for the moment but can I merge them using BatchNode ?

Yeah, people often use batch node to get around learning how to make proper meshes themselves. It works for a while.

You will create about 6x or more as many objects as you ultimately need… versus just creating a buffer of triangles in the first place.

Okay, I will release you from that pitiful situation.
Since I can write that code in much fewer time units than you.
I’m currently having lunch and I’m in the mood to make that.
Expect a little demo programm today or tomorrow.
You owe me one beer for that… :chimpanzee_wink:

ok, i’ll use BatchNode for the moment but I keep your advice in a corner.

No prob Ogli :stuck_out_tongue: :beers:

Thx for taking time to help me guys

Hm, I had some strange bugs and a lot of Real Life activity going on. I’ve done the regular terrain with flat shading, the triangle soup (arbitrary triangles) is in the making. Are you looking for something like this: ??

Okay, that was easy - I’ve done triangle soup terrain (any triangle you want). Looks like that:

I’ve just used pure random height points. A real terrain would use a height map or a procedural algorithm like perlin noise or errosion simulation. :chimpanzee_smile:

And here is the code for this and for some other custom meshes. Please respect the original @author tag if you contribute any of that code to the engine or to any other open source project. The license shall be the same BSD license found within the jME source code.

I look into your code and learn a lot !
A big beer pint for you Ogli :chimpanzee_laugh: your demo must be on the tutorial topic !
I’ll keep post update if I have trouble.
Thx guys !

Glad that it helps you. It’s btw less of a tutorial, more in the style of the “test project” (when using the SDK: File > New Project... > JME3 > JME3 Tests). These are really great - one of the finest features of the SDK. I think you can get the tests separate too from the github repo. But it’s not as comfortable when doing it that way. :chimpanzee_smile: