Java3D网络上的三维动画第一回初识Java3DWhatisJava3D?Internet的交互式三维图形应用编程接口(API)。让用户能够像浏览网页一样观看并操作三维动态图形一次编程,到处运行适应各种不同的软硬件平台Java3D=Java+3D提高了编写三维图形程序的层次是Java的嫡系高效的三维图形语言更大的优点它是网络语言,代码可以自由传输从产品设计、网上购物到各种游戏,“三维”将改变人们在屏幕上获取信息的习惯。Examples:ScientificVisualizationExamples:ScientificVisualizationExamples:ScientificVisualizationExamples:EducationExamples:AbstractData(Financial)Examples:MedicalEducationGeographicalinformationsystems(GIS)Examples:CADExamples:AnalysisExamples:3DLogosExamples:AstronomyExamples:Animations如何用Java3D编程安装Java3DJava2SDK/JREJava3DSDK/RTOpenGLDirect3DJava3D的编程空间VirtualUniverseLocaleBranchGroupBGBGTTSVPShape3DTransformGroupAppearanceGeometryViewPlatformVirtualUniverse:虚拟空间Locale:场所BranchGroup:分枝节点TransformGroup:转换节点Shape3D:三维形体Appearance:外观Geometry:几何形状ViewPlatform:观察平台Java3D的座标系第一个Java3D程序Hello.javaHello.javaImportspublicclassHello{publicHello(){Contents}publicstaticvoidmain(String[]args){newHello();}}importcom.sun.j3d.utils.geometry.*;importcom.sun.j3d.utils.universe.*;importjavax.media.j3d.*;importjavax.vecmath.*;ImportsContents:空间和小球//创建一个虚拟空间SimpleUniverseuniverse=newSimpleUniverse();//创建一个用来包含对象的数据结构BranchGroupgroup=newBranchGroup();//创建一个球并把它加入到group中Spheresphere=newSphere(0.5f);//小球的半径为0.5米group.addChild(sphere);Contents:灯光构件Color3flight1Color=newColor3f(1.8f,0.1f,0.1f);//设置光线的颜色BoundingSpherebounds=newBoundingSphere(newPoint3d(0.0,0.0,0.0),100.0);//设置光线的作用范围Vector3flight1Direction=newVector3f(4.0f,-7.0f,-12.0f);//设置光线的方向Contents:灯光形成DirectionalLightlight1=newDirectionalLight(light1Color,light1Direction);//指定颜色和方向,产生单向光源light1.setInfluencingBounds(bounds);//把光线的作用范围加入光源中group.addChild(light1);//将光源加入group组Contents:观察者//安放观察点universe.getViewingPlatform().setNominalViewingTransform();//把group加入到虚拟空间中universe.addBranchGraph(group);程序的逻辑结构观察点对象组:group虚拟空间:universe小球灯光范围方向颜色编译javacHello.java运行javaHello在网上发布Java3D程序第一步:编写Hello.htmlAPPLETCODE=Hello.classWIDTH=800HEIGHT=800ALIGN=middleblockquotehrIfyouwereusingaJava-capablebrowser,youwouldseeHelloUniverse!insteadofthisparagraph.hr/blockquote/APPLET第二步:转换HTML运行HTMLConverter.BATHTMLHEADTITLEHello/TITLE/HEADBODYBGCOLOR=#000000!--CONVERTED_APPLET--!--CONVERTERVERSION1.0--OBJECTclassid=clsid:8AD9C840-044E-11D1-B3E9-00805F499D93WIDTH=800HEIGHT=800ALIGN=middlecodebase==1,2,0,0PARAMNAME=CODEVALUE=Hello.classPARAMNAME=typeVALUE=application/x-java-applet;version=1.2COMMENTEMBEDtype=application/x-java-applet;version=1.2java_CODE=Hello.classWIDTH=800HEIGHT=800ALIGN=middlepluginspage=NOEMBED/COMMENTblockquotehrIfyouwereusingaJava-capablebrowser,youwouldseeHelloUniverse!insteadofthisparagraph.hr/blockquote/NOEMBED/EMBED/OBJECT/BODY/HTML开始用户浏览HTML用户端装有Java运行环境(JRE)?用户端装有Java3D运行环境?自动下载并安装JRE成功?自动下载并安装Java3D运行环境显示失败信息成功?启动Java3DApplet结束YYNNYNY执行流程里程碑什么是Java3D优点、用处用Java3D编程安装、程序、逻辑结构、编译、运行在网上发布Java3DApplet编HTML、转换、自动安装运行环境第二回基本形体的生成Conecone=newCone(.5f,1.0f,1,app)锥体Spheresphere=newSphere(.5f,app);球体Cylindercylinder=newCylinder(.5f,1.0f);圆柱体直线直线的生成(1)publicclasslineShapeextendsShape3D{//直线的定点坐标privatefloatvert[]={.5f,0.5f,0.0f,-0.5f,0.5f,0.0f,0.3f,0.0f,0.0f,-0.3f,0.0f,0.0f,-0.5f,-0.5f,0.0f,0.5f,-0.5f,0.0f};//各定点的颜色privatefloatcolor[]={0.0f,0.5f,1.0f,0.0f,0.5f,1.0f,0.0f,0.8f,2.0f,1.0f,0.0f,0.3f,0.0f,1.0f,0.3f,0.3f,0.8f,0.0f};直线的生成(2)publiclineShape(){//创建直线数组对象LineArrayline=newLineArray(6,LineArray.COORDINATES|LineArray.COLOR_3);//设置直线对象的坐标数组line.setCoordinates(0,vert);//设置直线对象的颜色数组line.setColors(0,color);直线的生成(3)//创建直线属性对象LineAttributeslinea=newLineAttributes();//设置线宽linea.setLineWidth(10.0f);//设置直线的渲染效果linea.setLineAntialiasingEnable(true);Appearanceapp=newAppearance();app.setLineAttributes(linea);this.setGeometry(line);this.setAppearance(app);}}直线的使用BranchGroupgroup=newBranchGroup();Shape3Dshape=newlineShape();group.addChild(shape);二维文字Shape3Dtext2D=newText2D(清华大学,newColor3f(1f,1f,0f),宋体,100,Font.BOLD);三维文字Stringtext=清华大学;FontExtrusionfe0=newFontExtrusion();QuadCurve2D.Doublecurve=newQuadCurve2D.Double();curve.setCurve(0,0,0.2,0.05,0.4,0);FontExtrusionfe1=newFontExtrusion();fe1.setExtrusionShape(curve);Font3Df3d=newFont3D(newFont(,Font.PLAIN,1),fe0);Text3Dtxt=newText3D(f3d,text,newPoint3f(0.0f,-1.f,-1.f),Text3D.ALIGN_CENTER,Text3D.PATH_RIGHT);Shape3Dsh=newShape3D();真实形体的生成用几何变换构造真实的物体Stool.htmlBranchGroupobjRoot=newBranchGroup();TransformGrouptrans=newTransformGroup();objRoot.addChild(trans);TransformGroupg1=newTransformGroup();g1.addChild(newColorCube(0.2));较简单的例子Transform3Dt=newTransform3D();t.setScale(0.5);t.setTranslation(newVector3f(0.0f,0.6f,0.0f));t.setRotation(newQuat4f(0.78f,0.78f,0.78f,0.0f));TransformGroupg2=newTransformGrou