Flat vs. smooth shading

Have jReality programming problems or questions? Post them here.
Post Reply
Joe
Posts: 61
Joined: Fri 11. Jul 2008, 19:29

Flat vs. smooth shading

Post by Joe » Sun 1. Feb 2009, 13:53

Hi,
I would like to apply a nice shading to my surfaces and so I activated SMOOTH_SHADING. But it works not with my "IndexedFaceSetFactory", only with the "IndexedFaceSet" of Primitives.sphere(). Why doesn't work SMOOTH_SHADING with my own geometry?

The appended source code shows the problem.

Many thanks and best regards, Joe

Code: Select all

import javax.swing.JFrame;

import de.jreality.geometry.IndexedFaceSetFactory;
import de.jreality.geometry.Primitives;
import de.jreality.scene.Appearance;
import de.jreality.scene.SceneGraphComponent;
import de.jreality.shader.CommonAttributes;
import de.jreality.ui.viewerapp.ViewerApp;

public class Test {
	public static void main(String[] arguments){
		double[][] verticesArray = new double[][]{
				{-1.5, -1.0, -1.0}, {-1.0, -1.0, 0.0}, {0.0, -1.0, 0.5}, { 1.0, -1.0, 0.0}, { 1.5, -1.0, -1.0},
				{ 1.5,  1.0, -1.0}, { 1.0,  1.0, 0.0}, {0.0,  1.0, 0.5}, {-1.0,  1.0, 0.0}, {-1.5,  1.0, -1.0}};

		int[][] faceIndicesArray = new int[][]{{0, 1, 8, 9}, {1, 2, 7, 8}, {2, 3, 6, 7}, {3, 4, 5, 6}};

		IndexedFaceSetFactory indexedFaceSetFactory = new IndexedFaceSetFactory();
		indexedFaceSetFactory.setVertexCount(verticesArray.length);
		indexedFaceSetFactory.setVertexCoordinates(verticesArray);
		indexedFaceSetFactory.setFaceCount(faceIndicesArray.length);
		indexedFaceSetFactory.setFaceIndices(faceIndicesArray);
		indexedFaceSetFactory.setGenerateFaceNormals(true);
		indexedFaceSetFactory.update();

		SceneGraphComponent sceneGraphComponent = new SceneGraphComponent();
		sceneGraphComponent.setGeometry(indexedFaceSetFactory.getGeometry());
		//sceneGraphComponent.setGeometry(Primitives.sphere(5));

		Appearance appearance = new Appearance();
		appearance.setAttribute(CommonAttributes.VERTEX_DRAW, false);
		appearance.setAttribute(CommonAttributes.EDGE_DRAW, false);
		appearance.setAttribute(CommonAttributes.TUBES_DRAW, false);
		appearance.setAttribute(CommonAttributes.FACE_DRAW, true);
		appearance.setAttribute(CommonAttributes.LIGHTING_ENABLED, true);
		appearance.setAttribute(CommonAttributes.POLYGON_SHADER + "." + CommonAttributes.SMOOTH_SHADING, true);
 
		sceneGraphComponent.setAppearance(appearance);

		ViewerApp viewerApp = new ViewerApp(sceneGraphComponent);

		JFrame frame = new JFrame();
		frame.getContentPane().add(viewerApp.getViewingComponent());
		frame.setSize(512, 512);
		frame.validate();
		frame.setVisible(true);
	}
}

STRESS
Posts: 141
Joined: Mon 19. Jan 2009, 12:10

Post by STRESS » Mon 2. Feb 2009, 11:42

The problem is I think you do not have vertex normals you need a normal at each vertex to get Gouraud aka smooth shading to work. Try to add this line:

indexedFaceSetFactory.setGenerateVertexNormals(true)

I assume this does neighbour weighting of all the Face normals to generate a vertex normal for each vertex

Joe
Posts: 61
Joined: Fri 11. Jul 2008, 19:29

Post by Joe » Mon 2. Feb 2009, 19:35

Thanks, now it works fine.

Post Reply