Lightning doesn't work at all

does anybody know why my two boxes source1 and source2 aren't green and orange? they are only black, grey and white :frowning:

im really desperated!! thx


package src;

import java.net.URL;
import java.util.HashMap;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.input.ChaseCamera;

import com.jme.input.ThirdPersonHandler;
import com.jme.light.PointLight;
import com.jme.math.FastMath;
import com.jme.math.Ray;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.CullState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.util.TextureManager;

public class CP_Controller extends SimpleGame {
   
   private CP_Controller controller;
   private Node m_character;
   private ChaseCamera chaser;
   private PointLight light;
   private Node floorNode;
   private Node boxNode;
   
   public void init(CP_Controller controller) {
      this.controller = controller;
      controller.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
      controller.start();
   }

   @Override
   protected void simpleInitGame() {
      ZBufferState buf = display.getRenderer().createZBufferState();
      buf.setEnabled(true);
      buf.setFunction(ZBufferState.CF_LEQUAL);
      rootNode.setRenderState(buf);
      
      light = new PointLight();
      light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
      light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
      light.setLocation(new Vector3f(0f,10f,15f));
      light.setEnabled(true);
      
      LightState ls = display.getRenderer().createLightState();
      ls.attach(light);
      lightState.detachAll();
      boxNode = new Node("BoxNode");
         
      setupCharacter();
      setupTerrain();
      setupChaseCamera();
      setupInput();
      setupTestData();
      
      rootNode.setRenderState(ls);
      rootNode.attachChild(boxNode);
   }

   protected void simpleUpdate() {
      chaser.update(tpf);
      float camMinHeight = 2f;
        if (!Float.isInfinite(camMinHeight) && !Float.isNaN(camMinHeight)
                && cam.getLocation().y <= camMinHeight) {
            cam.getLocation().y = camMinHeight;
            cam.update();
        }

        float characterMinHeight = 2f;
        if (!Float.isInfinite(characterMinHeight) && !Float.isNaN(characterMinHeight)) {
            m_character.getLocalTranslation().y = characterMinHeight;
        }
   }

   private void setupInput() {
      HashMap<String, Object> handlerProps = new HashMap<String, Object>();
        handlerProps.put(ThirdPersonHandler.PROP_DOGRADUAL, "true");
        handlerProps.put(ThirdPersonHandler.PROP_TURNSPEED, ""+(1.0f * FastMath.PI));
        handlerProps.put(ThirdPersonHandler.PROP_LOCKBACKWARDS, "false");
        handlerProps.put(ThirdPersonHandler.PROP_CAMERAALIGNEDMOVE, "true");
        input = new ThirdPersonHandler(m_character, cam, handlerProps);
        input.setActionSpeed(100f);
   }

   private void setupChaseCamera() {
      Vector3f targetOffset = new Vector3f();
        targetOffset.y = (((BoundingBox) m_character.getWorldBound()).yExtent+10) * 1.5f;
        targetOffset.x = (((BoundingBox) m_character.getWorldBound()).xExtent+10) * 1.5f;
        targetOffset.z = (((BoundingBox) m_character.getWorldBound()).zExtent+10) * 1.5f;
        chaser = new ChaseCamera(cam, m_character);
        chaser.setTargetOffset(targetOffset);
   }

   private void setupTerrain() {
      rootNode.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
      fpsNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
      display.getRenderer().setBackgroundColor(ColorRGBA.blue);
      
      CullState cs = display.getRenderer().createCullState();
      cs.setCullMode(CullState.CS_BACK);
      cs.setEnabled(true);
      rootNode.setRenderState(cs);
      
      URL monkeyLoc;
      monkeyLoc = CP_Controller.class.getClassLoader().getResource("data/Grid.png");
      
      TextureState ts = display.getRenderer().createTextureState();
   
      Texture t = TextureManager.loadTexture(monkeyLoc, Texture.MM_LINEAR, Texture.FM_LINEAR);
      ts.setTexture(t);
      
      for (int i = 1; i < 5; i++) {
         for (int j = 1; j < 5; j++) {
            Box box = new Box("box", new Vector3f(i*50, 0, j*50), 50f, 0.01f, 50f);
            box.setRenderState(ts);
            rootNode.attachChild(box);
         }
      }
      
      Box y = new Box("y" ,new Vector3f(0,0,0), 0.1f, 100f, 0.1f);
      Box x = new Box("x", new Vector3f(0,0,0), 100f, 0.1f, 0.1f);
      Box z = new Box("z", new Vector3f(0,0,0), 0.1f, 0.1f, 100f);
      
      rootNode.attachChild(x);
      rootNode.attachChild(y);
      rootNode.attachChild(z);
   }

   private void setupCharacter() {
      Box b = new Box("b", new Vector3f(), 0.01f,0.01f,0.01f);
      b.setModelBound(new BoundingBox());
      b.updateModelBound();
      m_character = new Node("char Node");
      rootNode.attachChild(m_character);
      m_character.attachChild(b);
      m_character.updateWorldBound();
   }
   
   private void setupTestData() {
      Box source1 = new Box("source1", new Vector3f(10,15f,10f), 5f, 15f, 5f);
      Box source2 = new Box("source2", new Vector3f(40,30f,70f), 5f, 30f, 5f);
      source1.setDefaultColor(ColorRGBA.green);
      source2.setDefaultColor(ColorRGBA.orange);
      boxNode.attachChild(source1);
      boxNode.attachChild(source2);
   }
}

You need to color your objects with a MaterialState.



You can either set the ambient,diffuse and specular colors in the material state.

Or tell it to use the geometry colors for ambient and diffuse.


    private void setupTestData() {
        MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
//        ms.setDiffuse(ColorRGBA.red);
//        ms.setAmbient(ColorRGBA.blue);
//        ms.setSpecular(ColorRGBA.white);
//        ms.setShininess(90);
        ms.setColorMaterial(MaterialState.CM_AMBIENT_AND_DIFFUSE);
       
        Box source1 = new Box("source1", new Vector3f(10,15f,10f), 5f, 15f, 5f);
        Box source2 = new Box("source2", new Vector3f(40,30f,70f), 5f, 30f, 5f);
        source1.setRenderState(ms);
        source2.setRenderState(ms);
        source1.setDefaultColor(ColorRGBA.green);
        source2.setDefaultColor(ColorRGBA.orange);
        boxNode.attachChild(source1);
        boxNode.attachChild(source2);
    }

thx a lot! its workin know!

know i have a new problem: i draw a line wich connects these boxes. the line should be plain white. but unfortunately in on corner, the line is grey and black:







is there any possibility to solve this? this is my code:



   MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
      ms.setColorMaterial(MaterialState.CM_AMBIENT_AND_DIFFUSE);
      
      PointLight light = new PointLight();
      LightState lightstate = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
      
         light.setDiffuse(new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
      
       light.setLocation(new Vector3f(0,0,0));
       light.setEnabled(true);
      
      
       lightstate.setEnabled(true);
       lightstate.attach(light);
      
      int length = boxes.size();
      Vector3f[] vertex = new Vector3f[length];
      ColorRGBA[] color = new ColorRGBA[length];
      Vector3f center = new Vector3f();
      
      /**
       * only use boxes which are of a specific author
       */
      
      System.out.println("Checking for: " + author);
      for (int i = 0; i < boxes.size(); i++) {
         if (authors.elementAt(i).equals(author) == true) {
            boxes2draw.add(boxes.elementAt(i));
         }
      }
      
      for (int j = 0; j < boxes2draw.size(); j++) {
         center = boxes2draw.elementAt(j).getCenter();
         vertex[j] = new Vector3f(center.x, center.y + boxes2draw.elementAt(j).yExtent, center.z);
         color[j] = new ColorRGBA(1,1,1,1);
      }
      
      Line connectingLine = new Line("Connecting Line", vertex, null, color, null);
      connectingLine.setMode(Line.CONNECTED);
      connectingLine.setLineWidth(LINE_WIDTH);
      connectingLine.setRenderState(ms);
      lineNode.setRenderState(ms);
      lineNode.setRenderState(lightstate);
      lineNode.attachChild(connectingLine);
      nodes.add(lineNode);

That would be because you applied the same material you just created in order to allow lighting on your boxes to your line, which should not be affected by light.

hmm the materialstate for the boxes is created in another class i think. that cant be true?



then i should attach no materialstate to the lines?

further explanations would be great. i can't getting it to work

At the end of your initialization you call:



nodes.add(lineNode);



What is that "nodes" and how is it related to the scene graph?



Perhaps you are attaching your lines to a node that already has some lighting configuration.


node is a separate node which i created only for these purpose and afterwards it is added directly to the root node. there should be no alternative light configuration.

Use Nymon's Scene Monitor (search for it in the forums) and check what renderstates and lighting configuration it is being applied.

thx for the answer. do you mean this helper tool?  http://www.jmonkeyengine.com/wiki/doku.php?id=scenegraphdump



i tested it, and as far as i can see, lineNode and ConnectingLine are the only ones with Renderstate L




Line: Connecting Line
Transform: (-0.6571431, -0.49047637, 24.0)[0.0, 0.0, 0.0]{1.0}
.......................
LineBatch: null
Batches set: VCT0
Vertex: 322


19.10.2008 21:30:59 com.jme.app.BaseSimpleGame cleanup
INFO: Cleaning up resources.
19.10.2008 21:30:59 com.jme.app.BaseGame start
INFO: Application ending.

is L (for light) even the correct state? or should it be M for RenderState??


I can't remember whether you need a MaterialState for something to be lighted. Try adding a MaterialState to your lines but... the rest of the lines look good so I don't think that's a problem.

I was talking about this tool: http://www.jmonkeyengine.com/jmeforum/index.php?topic=8159.msg64486#msg64486

yes i already tried to add an materials tate but nothing changes. it's weird because its always the same line which is darker than the others, even if i draw 100 lines or so its always the lines which are near to (0,0,0).



thy for the link, i will test it tonight!

i really hope we can solve this odd problem :smiley:

The only thing I can think of is a missing updateRenderState call or something similar.

shouldn't be the case…



could it be a problem that im using chase camera and thirdperson handler in combination with a point light? should i use a lightnode or something similair?

i detected that if i move the camera, in some positions the line becomes grey instead of black. thats why i think it has something to do with the chasing camera and the light settings…

the problem is solved. thx @ all for the great help.

the problem was related to this one: http://www.jmonkeyengine.com/jmeforum/index.php?topic=9472.0