Problems with Custom meshes and lighting

Hey all,



I have the strangest issue where my custom meshes are appearing inconsistently, sometimes disappearing entirely, depending on the camera angle. I’ve tried a lot of things, making sure I update bounds, turning off backface culling, excluding them from being culled at all, but to no avail. I would really like some feedback on this.



First off, here’s the mesh I’m creating.



[java]public class TileMesh extends Mesh

{

public TileMesh()

{

Vector3f [] vertices = new Vector3f[7];

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

vertices[1] = new Vector3f(-0.5f,0,-0.25f);

vertices[2] = new Vector3f(-0.5f,0,0.25f);

vertices[3] = new Vector3f(0,0,0.5f);

vertices[4] = new Vector3f(0.5f,0,0.25f);

vertices[5] = new Vector3f(0.5f,0,-0.25f);

vertices[6] = new Vector3f(0,0,-0.5f);





Vector2f[] texCoord = new Vector2f[7];

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

texCoord[1] = new Vector2f(-0.5f,-0.25f);

texCoord[2] = new Vector2f(-0.5f,0.25f);

texCoord[3] = new Vector2f(0,0.5f);

texCoord[4] = new Vector2f(0.5f, 0.25f);

texCoord[5] = new Vector2f(0.5f, -0.25f);

texCoord[6] = new Vector2f(0,-0.5f);



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



this.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));

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

this.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes));



this.updateBound();

}

}[/java]



The first issue arose with having 1 such object in the scene in conjuction with a standard box and setting their materials to “Common/MatDefs/Light/Lighting.j3md” so that they are affected by lighting. I added a directional light to the scene and things got interesting.



From most angles, it looks fine





From other angles, it suddenly looks to be receiveing shadows of sort





Note that at this point I’m only using basic materials and NO shadow techniques at all, I’m still learning jme3 and didn’t want to get in over my head right off the bat. First things first :slight_smile:



Having no idea what was up at this time, I decided to see how things would react with 2 of my custom objects in the scene. These results were even stranger! It seems that, at any one time, only one of them is rendered at all! And only one of the objects seems to be affected by lighting when I move around it.



Just the Blue one, which is not affected by light.





And the Red one. My camera only changed direction (as indicated by the value printed on the top) a slight bit to make this happen.





I figured, since this started happening when I changed my materials to be affected by lighting, I’d turn that off for one of them and, Presto! Both objects are now drawn, though the one that still uses the ‘Phong’ material looks a bit… funny.







So this where it doesn’t make any sense for me anymore. I’m hoping someone here can help me make more sense of this all.



For reference, here’s the application as I have it here.



[java]public class Main extends SimpleApplication

{

BitmapText displaytext; // For displaying a text line



/** Start the jMonkeyEngine application /

public static void main(String[] args)

{

Main app = new Main();



AppSettings newSetting = new AppSettings(true);

newSetting.setFrameRate(120);

app.setSettings(newSetting);



app.start();

}



@Override

public void simpleInitApp()

{

TileMesh genericTile = new TileMesh();

Geometry blue = new Geometry(“OurTile”, genericTile);

// Create a red box straight above the blue one at (1,3,1)

//Box box1 = new Box( Vector3f.ZERO, 1,1,1);

//Geometry blue = new Geometry(“Box”, box1);

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

/mat1.setBoolean(“UseMaterialColors”, true);

mat1.setColor(“Ambient”, ColorRGBA.Blue);

mat1.setColor(“Diffuse”, ColorRGBA.Blue);

mat1.setColor(“Specular”, ColorRGBA.White);
/

mat1.setColor(“Color”,ColorRGBA.Blue);

mat1.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);

//mat1.getAdditionalRenderState().setWireframe(true);

blue.setMaterial(mat1);

blue.move(1,1,1);



TileMesh otherTile = new TileMesh();

Geometry red = new Geometry(“OurTile”, otherTile);

// Create a red box straight above the blue one at (1,3,1)

//Box box2 = new Box( Vector3f.ZERO, 1,1,1);

//Geometry red = new Geometry(“Box”, box2);

Material mat2 = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

mat2.setBoolean(“UseMaterialColors”, true);

mat2.setColor(“Ambient”, ColorRGBA.Red);

mat2.setColor(“Diffuse”, ColorRGBA.Red);

mat2.setColor(“Specular”, ColorRGBA.White);

mat2.setFloat(“Shininess”, 12);

//mat2.setColor(“Color”,ColorRGBA.Red);

mat2.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);

//mat2.getAdditionalRenderState().setWireframe(true);

red.setMaterial(mat2);

red.move(3,3,1);



// Add a light

DirectionalLight myLight = new DirectionalLight();

myLight.setColor(ColorRGBA.White);

myLight.setDirection(new Vector3f(-.5f, -.5f, -.5f));



blue.setCullHint(CullHint.Never); // always drawn

red.setCullHint(CullHint.Never); // always drawn

rootNode.attachChild(blue);

rootNode.attachChild(red);



rootNode.addLight(myLight);



/
* Load the HUD*/

BitmapFont guiFont = assetManager.loadFont(

“Interface/Fonts/Default.fnt”);

displaytext = new BitmapText(guiFont);

displaytext.setSize(guiFont.getCharSet().getRenderedSize());

displaytext.move(5, settings.getHeight(), 0);

displaytext.setText(“Default On-Creation String”);

guiNode.attachChild(displaytext);



// Place the camera ‘above’ our scene

Vector3f cameraPos = new Vector3f(0,10,10);

cam.setLocation(cameraPos);

cam.lookAt(new Vector3f(0,0,0), new Vector3f(0,1,0));

flyCam.setMoveSpeed(20);



}



@Override

public void simpleUpdate(float tpf)

{

displaytext.setText("Camera direction: " + cam.getDirection());

}[/java]

You need tangents to use lighting, you can generate them with the TangentBinormalFactory.

@normen said:
You need tangents to use lighting, you can generate them with the TangentBinormalFactory.


You only need tangents to do bump mapping or normal mapping. Regular lighting works just fine without tangents.

You DO need normals, though, and I don't see any code that sets up the vertex normals.

Actually you need Normals in the first place if you want lighting. Tangents are mandatory only if you use normal maps.



Your mesh has no normal buffer that’s why you have unexpected results with lighting.

You have to create it like you did for the other buffers.

For your case it should be easy normals should be 0,1,0. but it can be tricky to compute for a complex mesh.



EDIT : ninjaed by Paul

@nehon said:
EDIT : ninjaed by Paul


Maybe I should go as a Ninja for Halloween.

If someone comes to your door and rings the bell and there's no one there... that's me. I'm a ninja. :)

Oh yeah, that seemed to have solved it!! :smiley:



Thanks so much! For the lazy, and posterity, this is what I added:

[java]float [] normals = { 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0};

this.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));[/java]



And, if I understand correctly, for tangents I simply do this as well.

[java]TangentBinormalGenerator.generate(this);[/java]



I’m curious though, before this my objects had specularity that you could see if you moved around them, but it seems to have gone completely flat now. I’m not sure why that is.



Thanks again though, this really helped me out! ^^

ouch, thought i replied first to this! :confused: did someone delete my reply?



i said:

“didn’t read it all, but you don’t have normals?”