well i read this source code that had the lighting done pretty good
but it was kind of hard to understand ( not the lighting part, but everything else )
so I wrote my own program but using the lighting part of the source code, and........my lighting doesnt work.
this is my class code for the geometry + lighting part
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| GeometryInfo gf = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); gf.setTextureCoordinateParams(1,2); gf.setTextureCoordinates(0,txt); gf.setStripCounts(strips); gf.setCoordinates(pts); gf.setContourCounts(contours); NormalGenerator ng = new NormalGenerator(); ng.setCreaseAngle ((float) Math.toRadians(30)); ng.generateNormals(gf); Appearance app = new Appearance(); Material mat = new Material(); mat.setShininess(0.01f); mat.setEmissiveColor(0.0f, 0.0f, 0.0f); mat.setAmbientColor(0.1f, 0.1f, 0.1f); app.setMaterial(mat); try{
TextureLoader txtLoader = new TextureLoader(new java.net.URL(texture),null); Texture2D textImg = (Texture2D) txtLoader.getTexture(); TextureAttributes texatt=new TextureAttributes(TextureAttributes.BLEND, new Transform3D(), new Color4f(1.0f, 1.0f, 1.0f, 1.0f), TextureAttributes.NICEST); app.setTextureAttributes(texatt); app.setTexture(textImg); }catch(java.net.MalformedURLException e){ System.out.println("he"); } setGeometry(gf.getGeometryArray()); setAppearance(app); |
and this is the tester class that I use for both the other source code and my code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| import java.applet.Applet; import java.awt.BorderLayout; import java.awt.event.*; import java.awt.GraphicsConfiguration; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.behaviors.vp.*; import javax.media.j3d.*; import javax.vecmath.*;
public class LandTest extends Applet { SimpleUniverse u; BoundingSphere bounds; ViewingPlatform ourView; OrbitBehavior B; public void init() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D c = new Canvas3D(config); u = new SimpleUniverse(c); BranchGroup scene = createSceneGraph(); u.addBranchGraph(scene); ourView = u.getViewingPlatform(); ourView.setNominalViewingTransform(); B = new OrbitBehavior(c); B.setSchedulingBounds(bounds); ourView.setViewPlatformBehavior(B); add("Center",c); }
public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); bounds = new BoundingSphere(); String texturePath="file://localhost/"+System.getProperty("user.dir")+"/stone.jpg"; Land s = new Land(6, 6, texturePath); Color3f alColour = new Color3f(0.4f, 0.4f, 0.4f); AmbientLight aLgt = new AmbientLight(alColour); aLgt.setInfluencingBounds(bounds); Color3f DirColour = new Color3f(0.35f, 0.35f, 0.3f); Vector3f DirVec = new Vector3f(0.5f, -0.2f, 0.6f); DirectionalLight DirLig = new DirectionalLight(DirColour, DirVec); DirLig.setInfluencingBounds(bounds); objRoot.addChild(DirLig); objRoot.addChild(aLgt); objRoot.addChild(s); objRoot.compile(); return objRoot; }
public static void main(String[] args) { new MainFrame(new LandTest(), 256, 256); } } |