Great problems with light control!

Hi, i'm gonna posting the entire code of a really simple class. In this class i have a cameraNode to move in the scene, load a 3DS model and try to set a light. The problem is that the light always moves with the camera and never stays in a fixed position and i don't know how to do that.



So my questions are:



1)Can anybody make changes to the code so i have a fixed light instead of a "following light"?

2)Can anybody show me how could i use a lightNode instead of a setRenderState(lightState)?

3)Is there a way to assign a name to a 3d model loaded so i can check the getTargetMesh on a PickResult?



Here is the code (i'm pretty sure that the problem is located only in the initGame method, so you may skip the rest of the code).

Remember i load a 3d model so if you run it you must change the line to load a 3ds model of yours!

Thanks to anybody for the help!





import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import com.jme.app.BaseGame;
import com.jme.bounding.BoundingBox;
import com.jme.input.AbsoluteMouse;
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.lwjgl.LWJGLKeyInput;
import com.jme.input.lwjgl.LWJGLMouseInput;
import com.jme.light.DirectionalLight;
import com.jme.light.LightNode;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.LightState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.system.JmeException;
import com.jme.util.Timer;
import com.jmex.model.XMLparser.JmeBinaryReader;
import com.jmex.model.XMLparser.Converters.MaxToJme;

/**
 * Tutorial 2 shows how to build our own Application framework for Flag Rush.
 * For Flag Rush Tutorial Series.
 *
 * @author Mark Powell
 */
public class SimpleExample extends BaseGame {

From the JavaDoc of DirectionalLight:


  • <code>DirectionalLight</code> defines a light that is assumed to be
  • infintely far away (something similar to the sun). This means the direction
  • of the light rays are all parallel. The direction the light is coming from
  • is defined by the class.



    So, use a different type of light.

As for the code that andretti1977 posted:

I can use both LightNode and SimpleLightNode to achive correct results in my own classes.

I don't do anything very different than you.

But when I take your code and make a class in my IDE from it, it shows lots of deprecated API calls and some unknown classes. Maybe your copy of jME is outdated or maybe you are using outdated commands with the current version.



We had a similar discussion before, in this thread:

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

Even though, mojomonk said that there is a bug, nothing happened up to now.

I still use a rotating SimpleLightNode with a DirectionalLight and still am correcting the angle of rotation manually.

Maybe I should post an issue for the developers - but I imagine they have more important things to do by now.



EDIT: changed order of statements according to importance

Yes… but note this problem has nothing to do with what andretti posted.

Deprecated methods? Strange! I've downloaded jme v0.9 no more than 3 weeks ago!

I know that a directional light comes with parallalels rays, and this is exactly what i want, but i would like that the light always come from one absolute direction to another fixed absolute direction (for example to x=-1,y=-1,z=-1) also if i rotate the camera.

In my code, if i rotate the cameraNode, light follows my rotation!! By this way, for example, i can never see the "black" side of my object, because lightState always lights my object!

Can anybody show me how to get that light direction fixed, also if i rotate the camera node?

My english is poor, i know, so i hope i have explained exactly what i need!

Thanks in advance!

there are many changes all over the time. the newest version is in the cvs.

but if you only download the 0.9 release you should not have problems now.

EDIT: the old GUI-system (Hudpanels etc.) has been dropped completely - do not learn it!



But I also have 2 possible solutions for you:



one solution:

here is a snipped of code, that I use normally, test it

(it is similar to yours, but uses SimpleLightNode class instead of LightNode):


      //create a light
      DirectionalLight pl=new DirectionalLight();
      
      //color the light white
      pl.setDiffuse(ColorRGBA.white);
      pl.setSpecular(ColorRGBA.white);
      pl.setAmbient(new ColorRGBA(0.1f, 0.1f, 0.1f, 1.0f));
      //enable the light
      pl.setEnabled(true);
      
      //configure the light state
      LightState globalLight = display.getRenderer().createLightState();
      globalLight.setEnabled(true);
      globalLight.detachAll();
      globalLight.attach(pl);
      
      //This node will hold my light
      SimpleLightNode ln = new SimpleLightNode("A node for my light",pl);

      //set the lightstate for the scene (used the root node from SimpleGame)
      rootNode.attachChild(ln);
      rootNode.setRenderState(globalLight);

      //remark: you can translate and rotate the light node "ln" now...
      //translations will work with point light / spot light only
      //rotations will work with spot light / directional light only

      rootNode.updateRenderState();



another solution:
There is a method "setDirection(Vector3f direction)" in DirectionalLight.
Try to pass some vectors into the method to play around and experiment with the method.

Ah, I thnk I misunderstood then, maybe it is the bug Ogli mentioned then!

Ok, thank you very much! I'll play with it!