How to decide direction of directional light for a model in JMonkey

I want to add a model and rotate it by 180 along y axis.

  1. When I add a model and attach a directional light, it illuminates perfectly.

[java] golem = assetManager.loadModel(“Models/jeep/jeep.j3o”);
golem.scale(0.02f);
//golem.setLocalTranslation(0f, 5f, 0f);

// We must add a light to make the model visible
sun = new DirectionalLight();
sun.setDirection(new Vector3f(-1f, -1f, -1.0f).normalizeLocal());
golem.addLight(sun);

[/java]

The moment I adds rotation, it becomes completely dark. I tried playing with a few values of directional light but all in vain.
How to decide the direction of light??

keep in mind that since you added the light to the model, the light’s direction will move relative to the model. So perhaps attach it to another immovable node higher up the hierarchy if you want it to be static. Also add an ambient light for global illumination.

I have ambient as well as directional light attached to the rootNode as well…

[java] private void setUpLight() {
// We add light so we see the scene
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(1.3f));
rootNode.addLight(al);

DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);

}[/java]

Still I have a black model, when I rotate it. When I don’t rotate it illuminates well…

Does your model have proper normals and tangents?

@wezrule said: keep in mind that since you added the light to the model, the light's direction will move relative to the model. So perhaps attach it to another immovable node higher up the hierarchy if you want it to be static. Also add an ambient light for global illumination.

Pretty sure this is untrue. Adding a light to a model does not affect its parameters at all.

if you have a spot light and attach it to a model (a torch for example). If you move the model, the light doesn’t move with it? If so, ok my bad, I don’t use jME lighting very much

@wezrule said: if you have a spot light and attach it to a model (a torch for example). If you move the model, the light doesn't move with it? If so, ok my bad, I don't use jME lighting very much

Adding a light to a spatial only affects what spatials are lit by that light. You can add it all over the scene graph. If you want a light to move or change direction with a spatial then you have to use a control.

1 Like

…otherwise, Light would have been a spatial.

ah yeh, that makes sense. I did know that already (in my brain somewhere :P), but hadn’t used jME lighting properly in a long time (well i’ve never really used it apart from stealing light code templates from the palette just to make stuff appear occasionally ^_^)

So that makes sense. When the object s not rotated it illuminates well and when I rotate it it appears totally black.

  1. But it becomes black from all sides.
  2. How should I decide vector direction of light??

My end goal is to illuminate the rotated object. Thats it

Can you also post a picture of this? Just to be sure, does it work properly with the unshaded material? This should rule out a model with wrong triangle winding in blender and the same problem you had with ur opened cube. Have you generated tangents? (although I think this is only needed for normal mapping, could be wrong tho) no harm in trying. Otherwise, idk.

Hello,
I’m a beginner in this are yet, so I don’t understand shading and materials very well. Also I have limited knowledge of blender as I’m in the learning phase. I’m using models from internet (http://www.the3dmodelstore.com/free.php).

I’m having similar lighting problem in this post as well http://hub.jmonkeyengine.org/forum/topic/how-to-add-light-to-a-model-properly/#post-238708
Please point me some resources where I can get information how should I light up my models properly.

My current understanding is that when I import the model all materials etc gets imported automatically and I cant manipulate(change or edit) them. But if I import an unshaded model, then I can add materials etc.

The screenshot attached contains

  1. The unrotated image
  2. Rotated image from the same side
  3. Rotated image from the other side (which implies rotated image is black from all sides)

This is the related code that I’m calling form main function.

[java] private void setUpLight() {
// We add light so we see the scene
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(1.3f));
rootNode.addLight(al);

DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
rootNode.addLight(dl);

}

protected Spatial makeCharacter() {
// load a character from jme3test-test-data
//Spatial golem = assetManager.loadModel(“Models/Oto/Oto.mesh.xml”);
//Spatial golem = assetManager.loadModel(“Models/jeep/jeep.j3o”);
Spatial golem = assetManager.loadModel(“Models/axe/axe.j3o”);
golem.scale(0.08f);
golem.setLocalTranslation(0f, 0f, 0f);

// We must add a light to make the model visible
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
golem.addLight(sun);
shootables.attachChild (golem);

golem = assetManager.loadModel("Models/jeep/jeep.j3o"); 
golem.scale(0.02f);
golem.setLocalTranslation(0f, 5f, 0f);


// We must add a light to make the model visible
sun = new DirectionalLight();
sun.setDirection(new Vector3f(-1f, -1f, -1.0f).normalizeLocal());
golem.addLight(sun);

// Quaternion Quaternio = new Quaternion();
//golem.setLocalRotation(Quaternio.fromAngleAxis(FastMath.DEG_TO_RAD*15, new Vector3f(0,1,0)));

shootables.attachChild (golem);


return golem;

}[/java]

In the jMEDK open up the .j3o in the scenecomposer, find all the geometries, right click them and “generate tangents”, save the j3o. And try again, see if that does anything

Generating tangents does not make any difference. The model appears black from one side in scene composer as well.

Regarding the other post, http://hub.jmonkeyengine.org/forum/topic/how-to-add-light-to-a-model-properly/#post-238708 when I export the model in Orge XML format, it works perfectly. The same model when exported in obj format creates the problem.

There might be some problem with blender obj importer as well. As I have downloaded these objs from internet and the objs that I created behave the same. Some sides are all black or when rotated it becomes completely black.

Did u make sure to export the right options in the obj exporter? Triangulation, Normals, Uvs etc There’s also a Blender importer in the jMEDK which converts .blend files to .j3o directly.

In this post, I’m not using the obj exporter, just using the model from here Here
The Jeep model.

I will ensure all setting and then get back.

ah ok, when im back from work I can try this model out then (or someone else can). Are you using RC2 with the latest stable update btw?

Well after trying to convert the .obj directly, the output shows:

OBJ mesh jeep-geom-0 doesnt contain normals! It might not display correctly

So thats why it doesn’t work.

Converting the .blend file directly works fine, and so does exporting the .obj with normals selected.

Thnaks a lot for trying and helping me diagnose the problem.
But I din’t convert the obj file, there was a obj file already in the model folder…

Secondly, blender doesn’t show any log while converting the file to Obj.

However I’m still facing the same problem. This is what I did

  1. Started blender, a box appeared.
  2. I exported that box using Wavefront Obj.
  3. I imported the object in jME
  4. When not rotated, it appeared fine
  5. When I rotate with the code above, the box appeared black.
  6. When I zoom in very close to the box, the box regains its color but when I zoom out it again becomes black.
  7. In the scene explorer window as well, objects imported as objs (jeep and box) appear black from one side. However the room that I imported as Orge XML from google sketchup appears just fine from all sides. The same model (cube imported as Orge XML) appears well from all sides (silver).
  8. The Orge XML cube appears white in game (may be due to some material thing, dont know why). But rotating it doesn’t make it black. It works well…

All of this seems like a bug. Please have a look at it as well.

The output I quoted is from converting .obj to .j3o in the jMEDK.

In the .obj exporter you need to tick export normals!