Texture and Shading doesn't blend

Ok, so I’ve been working on this program for 3 years now. I’m using the old “trial and error” development method. I figured out how to create my own meshes and apply textures to them. Unfortunately, when I “look around” the objects, the shading gives away how the mesh is constructed. In the image below, the right face of the slotted cube is composed of 3 rectangles. When the texture/shading is applied, the main and lower left rectangles blend together very well. I suspect that is just coincidence. The upper left stand out. Is there some command to tell Jme3 to blend them all together? Any suggestion would be appreciated. Some of the code is listed below.

[java]
// Right Face top little rectangle
vertexVectors[++j] = new Vector3f( slotDepthUpperRightx, slotRearUpperRighty, slotRearUpperRightz );
textureVectors[j] = new Vector2f( slotDepthFromFront, slotRoof );

	vertexVectors[++j] = new Vector3f( midUpperRightx, midUpperRighty, midUpperRightz );                 
	textureVectors[j] = new Vector2f( slotDepthFromFront, 1 );      		    	

	vertexVectors[++j] = new Vector3f( frontUpperRightx, frontUpperRighty, frontUpperRightz );          
 	textureVectors[j] = new Vector2f( 0, 1 ); 

	vertexVectors[++j] = new Vector3f( slotRoofOuterRightx, slotFrontUpperRighty, slotFrontUpperRightz ); 
 	textureVectors[j] = new Vector2f( 0, slotRoof );    	

	vertexIndexes[++i] = 44;  	vertexIndexes[++i] = 45;   	vertexIndexes[++i] = 46;
	vertexIndexes[++i] = 46;  	vertexIndexes[++i] = 47;  	vertexIndexes[++i] = 44;    	

	//*********************************   	   	
	// Right Face Big Rectangle 
 	vertexVectors[++j] = new Vector3f( rearLowerRightx, rearLowerRighty, rearLowerRightz );              
	textureVectors[j] = new Vector2f( 1, 0 );      		    	     	
 	
	vertexVectors[++j] = new Vector3f( rearUpperRightx, rearUpperRighty, rearUpperRightz );              
	textureVectors[j] = new Vector2f( 1, 1 );      		    	
	
	vertexVectors[++j] = new Vector3f( midUpperRightx, midUpperRighty, midUpperRightz );                 
 	textureVectors[j] = new Vector2f( slotDepthFromFront, 1 );  
	
	vertexVectors[++j] = new Vector3f( midLowerRightx, midLowerRighty, midLowerRightz );   	            
 	textureVectors[j] = new Vector2f( slotDepthFromFront, 0 );    		    	

	vertexIndexes[++i] = 48;  	vertexIndexes[++i] = 49;  	vertexIndexes[++i] = 50;    	
	vertexIndexes[++i] = 50;  	vertexIndexes[++i] = 51;  	vertexIndexes[++i] = 48;   	
	
	//*********************************   	   	
	// Right Face bottom little rectangle
	vertexVectors[++j] = new Vector3f( midLowerRightx, midLowerRighty, midLowerRightz );                
	textureVectors[j] = new Vector2f( slotDepthFromFront,  0 );          

	vertexVectors[++j] = new Vector3f( slotDepthLowerRightx, slotRearLowerRighty, slotRearLowerRightz );  
	textureVectors[j] = new Vector2f( slotDepthFromFront, slotFloor );   
	
	vertexVectors[++j] = new Vector3f( slotFloorOuterRightx, slotFrontLowerRighty, slotFrontLowerRightz );
	textureVectors[j] = new Vector2f( 0,                  slotFloor );   

   	vertexVectors[++j] = new Vector3f( frontLowerRightx, frontLowerRighty, frontLowerRightz );           
	textureVectors[j] = new Vector2f( 0,                   0 );          
	
	vertexIndexes[++i] = 52;  	vertexIndexes[++i] = 53;  	vertexIndexes[++i] = 54;    	
	vertexIndexes[++i] = 54;  	vertexIndexes[++i] = 55;  	vertexIndexes[++i] = 52;
	
	setBuffer( Type.Position, 3,  BufferUtils.createFloatBuffer( vertexVectors ) );
	setBuffer( Type.Index,    3, BufferUtils.createIntBuffer( vertexIndexes) );
    setBuffer( Type.TexCoord, 2, BufferUtils.createFloatBuffer( textureVectors ) );   	

[/java]

You need normal- and tangent buffers, you can create the latter with the TangentBinormalGenerator but your normals have to be right in the first place ofc.

@normen said: You need normal- and tangent buffers, you can create the latter with the TangentBinormalGenerator but your normals have to be right in the first place ofc.

Thanks. I do have a generic Normal generating method. I am looking at the code to make sure it’s OK. I did notice references to TangentBinormalGenerator in various documentation. But I do not current employ it. I will try and see what happens.

With regard to the slotted box above, I reviewed my generic normal generator. It looks good. I added the code

TangentBinormalGenerator.generate(this); right after creating the vertexes, textures, indexes and normal buffers got created.

But the slotted box looks exactly the same. I recall now that I included this code in the past but dropped it because I could never see a difference. I guess I’m still missing something.

Well my suggestion is randomly changing stuff in your code and see if it affects the outcome at all or in the expected manner. Its hard to see bugs because you just wrote the code and you clearly see that it does what you intended it to do… or do you? (insert dramatic music) :wink:

On further testing, I am getting error messages from the TangentBinormalGenerator like

“WARNING: Normal and tangent are parallel for vertex 18.”

That looks like it is telling me my generic normal generator stinks. I have some work to do.

It’s just a warning.

I seem to have resolved the problem. I changed the code in my custom normal generator from cross product/normalize of a float variable to cross product/normalize of a double. For goodness sakes, it’s only calculation I do! Now the warning messages from TangentBinormalGenerator are gone and the object gets the right shading. Maybe the problem will return when I get to something more complicated like spheres.

But for now, I’m not even going to ask why.

Spoke too soon. Need more testing.

Now I think I have it. Correct Shading all around. This exercise taught me the following: ( I hope this is right)

To define a cube I need 8 position vertexes.

To apply a texture on all 6 faces, I need 24 position vertexes (because texture vertexes map one to one with position vertexes).

To include shading I need 36 position vertexes (because each triangle vertex needs a normal and normals map one to one with position vertexes).

I was using 24 position vertexes with 36 indexes. That gave me the right texture. But the wrong shading.

Thanks to pspeed and Norman. Focusing on the normal generator was the key.

For a cube you should only need 24 of everything. I’m not sure I understand why shading changes that.

If ever in doubt, you can look at JME’s Box class.

@pspeed said: For a cube you should only need 24 of everything. I'm not sure I understand why shading changes that.

If ever in doubt, you can look at JME’s Box class.

I just tested your statement. You are right. I defined a cube with 24 position vertexes, 24 normal vertexes and 24 texture vertexes. I still use 36 vertex indexes, one for each corner of each triangle, but that’s just an int array. It works! Thanks again.

And now I am going to study JME’s Box class.