java 加载3d模型_我的java3d学习(二)——载入模型的移动

夏侯和韵
2023-12-01

1 packagecom.zzl.j3d.viewer;2 3 importjava.applet.Applet;4 importjava.awt.BorderLayout;5 importjava.awt.GraphicsConfiguration;6 importjava.awt.event.ActionEvent;7 importjava.awt.event.KeyEvent;8 importjava.net.URL;9 importjava.util.Arrays;10 11 importjavax.media.j3d.Appearance;12 importjavax.media.j3d.BoundingSphere;13 importjavax.media.j3d.BranchGroup;14 importjavax.media.j3d.Canvas3D;15 importjavax.media.j3d.Group;16 importjavax.media.j3d.Texture;17 importjavax.media.j3d.TextureAttributes;18 importjavax.media.j3d.Transform3D;19 importjavax.media.j3d.TransformGroup;20 importjavax.swing.Timer;21 importjavax.vecmath.Point3d;22 importjavax.vecmath.Quat4f;23 importjavax.vecmath.Vector3d;24 importjavax.vecmath.Vector3f;25 26 importcom.sun.j3d.loaders.Scene;27 importcom.sun.j3d.utils.behaviors.vp.OrbitBehavior;28 importcom.sun.j3d.utils.geometry.Box;29 importcom.sun.j3d.utils.geometry.Primitive;30 importcom.sun.j3d.utils.image.TextureLoader;31 importcom.sun.j3d.utils.universe.SimpleUniverse;32 importcom.sun.j3d.utils.universe.ViewingPlatform;33 importcom.zzl.j3d.loader.J3DLoader;34 importcom.zzl.j3d.loader.KeyInputHandler;35 36 publicclassRenderextendsApplet {37 privatefinalfloatPI_180=(float) (Math.PI/180.0);38 39 privateSimpleUniverse universe ;40 privateCanvas3D canvas;41 privateBoundingSphere bounds=newBoundingSphere(newPoint3d(0.0,0.0,0.0),1000.0);42 privateTransform3D tans=newTransform3D();43 privateTransformGroup objTransG=null;44 privateTimer timer=null;45 46 privatefloatwalkbias=0;47 privatefloatwalkbiasangle=0;48 privatefloatheading;49 50 privatefloatxloc=0f;51 privatefloatyloc=0f;52 privatefloatzloc=0f;53 54 privatefloatyrot;//Y Rotation55 56 /**57 * 创建场景58 */59 publicRender()60 {61 setLayout(newBorderLayout());62 GraphicsConfiguration gc=SimpleUniverse.getPreferredConfiguration();63 canvas=newCanvas3D(gc);64 add("Center",canvas);65 66 universe=newSimpleUniverse(canvas);67 68 setupView();69 70 J3DLoader loader=newJ3DLoader();71 BranchGroup objRoot=newBranchGroup();72 73 URL texImage=loader.loadTexture("resource/flooring.jpg");74 Group group=this.createSceneBackGround(texImage);75 76 objTransG=newTransformGroup();77 objTransG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);78 Transform3D pos0=newTransform3D();79 pos0.setTranslation(newVector3f(0f,-1.1f,0f));80 objTransG.setTransform(pos0);81 objTransG.addChild(group);82 objRoot.addChild(objTransG);83 84 //载入人物模型85 Scene scene=loader.loadObj("Liit.obj");86 87 Transform3D pos1=newTransform3D();88 pos1.setTranslation(newVector3f(0f,0.0f,0f));89 //该 人物模型载入时默认为平行y=0平面,所以要绕x轴旋转90度90 pos1.rotX(Math.toRadians(-90));91 92 objTransG=newTransformGroup();93 94 objTransG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);95 objTransG.setTransform(pos1);96 97 objTransG.addChild(scene.getSceneGroup());98 99 objRoot.addChild(objTransG);100 101 universe.addBranchGraph(objRoot);102 }103 104 /**105 * 建立鼠标控制的视点106 */107 publicvoidsetupView()108 {109 /**Add some view related things to view branch side of scene graph*/110 //add mouse interaction to the ViewingPlatform111 OrbitBehavior orbit=newOrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL|OrbitBehavior.STOP_ZOOM);112 orbit.setSchedulingBounds(bounds);113 ViewingPlatform viewingPlatform=universe.getViewingPlatform();114 //This will move the ViewPlatform back a bit so the115 //objects in the scene can be viewed.116 viewingPlatform.setNominalViewingTransform();117 viewingPlatform.setViewPlatformBehavior(orbit);118 }119 120 publicvoidinit()121 {122 }123 124 publicvoiddestroy()125 {126 universe.removeAllLocales();127 }128 129 /**130 * 绘制一个地板131 *@paramtexImage132 *@return133 */134 publicGroup createSceneBackGround(URL texImage)135 {136 returncreateGeometry(Texture.MULTI_LEVEL_POINT ,-0.0f, texImage);137 }138 139 publicGroup createGeometry(intfilter,floaty, URL texImage)140 {141 Group topNode=newGroup();142 Appearance appearance=newAppearance();143 TextureLoader tex=newTextureLoader(texImage, TextureLoader.GENERATE_MIPMAP ,this);144 Texture texture=tex.getTexture();145 146 texture.setMinFilter(filter);147 appearance.setTexture(texture);148 TextureAttributes texAttr=newTextureAttributes();149 texAttr.setTextureMode(TextureAttributes.MODULATE);150 appearance.setTextureAttributes(texAttr);151 152 Transform3D pos2=newTransform3D();153 pos2.setTranslation(newVector3f(0f,y,0f));154 objTransG=newTransformGroup();155 objTransG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);156 objTransG.setTransform(pos2);157 158 159 Box box=newBox(6f,0.1f,8f,Primitive.GENERATE_NORMALS|Primitive.GENERATE_TEXTURE_COORDS,appearance);160 objTransG.addChild(box);161 topNode.addChild(objTransG);162 returntopNode;163 }164 165 /**166 * 键盘被按下时调用167 */168 publicvoidprocessKeyEvent(KeyEvent e)169 {170 if(!timer.isRunning())171 timer.start();172 173 if(e.getKeyChar()=='w')174 {175 xloc+=(float) Math.sin(heading*PI_180)*0.05f;176 zloc+=(float) Math.cos(heading*PI_180)*0.05f;177 if(walkbiasangle<=1.0f) {178 walkbiasangle=359.0f;179 }else{180 walkbiasangle-=10;181 }182 walkbias=(float) Math.sin(walkbiasangle*PI_180)/20.0f;183 }184 if(e.getKeyChar()=='s')185 {186 xloc-=(float) Math.sin(heading*PI_180)*0.05f;187 zloc-=(float) Math.cos(heading*PI_180)*0.05f;188 if(walkbiasangle>=359.0f) {189 walkbiasangle=0.0f;190 }else{191 walkbiasangle+=10;192 }193 walkbias=(float) Math.sin(walkbiasangle*PI_180)/20.0f;194 }195 if(e.getKeyChar()=='d') {196 xloc-=0.05f;197 }198 199 if(e.getKeyChar()=='a') {200 xloc+=0.05f;201 }202 }203 204 publicvoidprocessAction(ActionEvent e)205 {206 if(!timer.isRunning())207 timer.start();208 209 tans.setRotation(newQuat4f(1f,0f,0f,-1.0f));210 floatxtrans=-xloc;211 floatztrans=-zloc;212 floatytrans=-walkbias;213 floatsceneroty=360.0f-yrot;214 215 tans.setTranslation(newVector3d(xtrans, ytrans, ztrans));216 objTransG.setTransform(tans);217 }218 219 /**220 * 添加监听器221 *@paramkih222 */223 publicvoidregisterAction(KeyInputHandler kih)224 {225 canvas.addKeyListener(kih);226 227 timer=newTimer(100,kih);228 }229 }230

 类似资料: