实验三Java3D的三角网格与四边网格定义与生成建议3学时一、实验前的准备工作认真阅读并熟悉教材第三章例3.6到例3.16实例程序的程序结构及相关类的功能。二、目的及要求(1)掌握一个平面法向量的概念与计算;(2)熟练掌握PolygonAttributes类的功能与各参数的含义;(3)掌握三角网格类TriangleArray类的几何数据定义方式;(4)掌握TriangleStripArray类的定义格式与使用;(5)掌握TriangleFanArray类的定义格式与使用;(6)掌握IndexedTriangleArray类的定义格式与使用;(7)掌握QuadArray类的定义格式与使用;(8)掌握线型数据、平面型数据与三维层状立体数据的多维数组定义;(9)掌握几何数据的多维数组表示与Java3D中各种类所使用的一维数组的转换方法;(10)掌握一个体或者一个面的三角网格、四边网格表示与显示技术;(11)掌握三角网格面或四边网格面的颜色等属性设置与图形显示。三、实验内容(1)上机调试教材第三章例3.6到例3.16实例程序;(2)创意设计,在掌握以上程序功能的基础上,自己给定几何数据,生成一个旋转体,分别用三角网格与四边网格实现该旋转体的图形显示,并定义相关属性;四、实验报告要求对自己的创意程序设计部分,有如下要求:(1)画出算法的流程图;(2)写出在机器上能运行的完整源程序;(3)附完成的图形。3.6源代码:importjava.applet.Applet;importjava.awt.BorderLayout;importcom.sun.j3d.utils.applet.MainFrame;importcom.sun.j3d.utils.geometry.Primitive;importcom.sun.j3d.utils.universe.*;importjavax.media.j3d.*;importjavax.vecmath.*;importcom.sun.j3d.utils.behaviors.mouse.*;publicclassDisplayTriangleextendsApplet{publicBranchGroupcreateBranchGroupSceneGraph(){BranchGroupBranchGroupRoot=newBranchGroup();BoundingSpherebounds=newBoundingSphere(newPoint3d(0.0,0.0,0.0),100.0);Color3fbgColor=newColor3f(1.0f,1.0f,1.0f);Backgroundbg=newBackground(bgColor);bg.setApplicationBounds(bounds);BranchGroupRoot.addChild(bg);Color3fdirectionalColor=newColor3f(1.f,0.f,0.f);Vector3fvec=newVector3f(0.f,0.f,-1.0f);DirectionalLightdirectionalLight=newDirectionalLight(directionalColor,vec);directionalLight.setInfluencingBounds(bounds);BranchGroupRoot.addChild(directionalLight);Appearanceapp=newAppearance();Materialmaterial=newMaterial();material.setDiffuseColor(newColor3f(1.0f,1.0f,0.0f));app.setMaterial(material);TransformGrouptransformgroup=newTransformGroup();transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);BranchGroupRoot.addChild(transformgroup);MouseRotatemouserotate=newMouseRotate();mouserotate.setTransformGroup(transformgroup);BranchGroupRoot.addChild(mouserotate);mouserotate.setSchedulingBounds(bounds);MouseZoommousezoom=newMouseZoom();mousezoom.setTransformGroup(transformgroup);BranchGroupRoot.addChild(mousezoom);mousezoom.setSchedulingBounds(bounds);MouseTranslatemousetranslate=newMouseTranslate();mousetranslate.setTransformGroup(transformgroup);BranchGroupRoot.addChild(mousetranslate);mousetranslate.setSchedulingBounds(bounds);Shape3Dshapetrianglearray=newShapeTriangleArray();transformgroup.addChild(shapetrianglearray);BranchGroupRoot.compile();returnBranchGroupRoot;}publicDisplayTriangle(){setLayout(newBorderLayout());Canvas3Dc=newCanvas3D(null);add(Center,c);BranchGroupBranchGroupScene=createBranchGroupSceneGraph();SimpleUniverseu=newSimpleUniverse(c);u.getViewingPlatform().setNominalViewingTransform();u.addBranchGraph(BranchGroupScene);}publicstaticvoidmain(String[]args){newMainFrame(newDisplayTriangle(),500,500);}}classShapeTriangleArrayextendsShape3D{publicShapeTriangleArray(){intvCount=12;floatvertexes[]={-0.6f,0.6f,0.0f,-0.6f,-0.6f,0.2f,-0.2f,0.6f,-0.2f,-0.2f,-0.4f,0.2f,0.0f,0.3f,-0.2f,0.0f,-0.3f,0.2f,0.2f,0.7f,0.0f,0.2f,0.2f,0.3f,0.5f,0.6f,-0.3f,0.6f,-0.2f,0.0f,0.8f,0.6f,0.2f,0.8f,-0.3f,0.3f};floatcolors[]={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.5f,0.9f,1.0f,0.0f,0.5f,0.0f,1.0f,0.0f,0.5f,1.0f,1.0f,0.5f,0.0f,1.0f,0.0f,0.5f,1.0f,0.8f,0.0f,1.0f,0.5f,0.0f};TriangleArraytrianglearray=newTriangleArray(vCount,TriangleArray.COORDINATES|TriangleArray.COLOR_3);trianglearray.setCoordinates(0,vertexes);trianglearray.setColors(0,colors);PolygonAttributespolygonattributes=newPolygonAttributes();polygonattributes.setCullFace(PolygonAttributes.CULL_NONE);//应用参数显示Appearanceapp=newAppearance();app.setPolygonAttributes(polygonattributes);this.setGeometry(trianglearray);this.setAppearance(app);}}实验效果图:应用CULLNONE参数显示:应用CULLBACK参数显示:应用CULLFRONT参数显示:3.7源代码:importjava.applet.Applet;importjava.awt.BorderLayout;importcom.sun.j3d.utils.applet.MainFrame;importcom.sun.j3d.utils.geometry.Primitive;importcom.sun.j3d.utils.universe.*;importjavax.media.j3d.*;importjavax.vecmath.*;importcom.sun.j3d.utils.behaviors.mouse.*;publicclassDisplayStripTriangleextendsApplet{publicBranchGroupcreateBranchGroupSceneGraph(){BranchGroupBranchGroupRoot=newBranchGroup();BoundingSpherebounds=newBoundingSphere(newPoint3d(0.0,0.0,0.0),100.0);Color3fbgColor=newColor3f(1.0f,1.0f,1.0f);Backgroundbg=newBackground(bgColor);bg.setApplicationBounds(bounds);BranchGroupRoot.addChild(bg);Color3fdirectionalColor=newColor3f(1.f,0.f,0.f);Vector3fvec=newVector3f(0.f,0.f,-1.0f);DirectionalLightdirectionalLight=newDirectionalLight(directionalColor,vec);directionalLight.setInfluencingBounds(bounds);BranchGroupRoot.addChild(directionalLight);Appearanceapp=newAppearance();Materialmaterial=newMaterial();material.setDiffuseColor(newColor3f(1.0f,1.0f,0.0f));app.setMaterial(material);TransformGrouptransformgroup=newTransformGroup();transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);BranchGroupRoot.addChi