1 Java 3D 1.1 1.2 1.3 ( ) 1.4 1.5 1.6
Java 3D Progra mming Java 3D Java 3 (Sun Microsystems) (application programming interface, API). Java 3D Java API. Sun API Abstract Windows Toolkit(AWT) Java Foundation Classes(JFC/Swing), (Graphical User Interface, GUI) Java. Java API Enterprise Java-Beans(EJB) Java 2 Enterprise Edition(J2EE) Sun API. 3, API,. Sun 3, 3. 3 OpenGL, (Silicon Graphics, SGI). OpenGL. OpenGL API C, Java., Java OpenGL OpenGL API Java,. GL4Java, http://www.jausoft.com/gl4java/. C OpenGL OpenGL Java., Java API, OpenGL Java Native Interface(JNI). Java 3D OpenGL DirectX, 3, Java. Sun OpenGL, Java 3D C Java. OpenGL 3, Java 3D. Sun OOP, JNI (scene optimization). 24
Cha pte r 1 Ja va 3D 1.1 Java Java 3D Java. 3., Java (UI). (Write-Once-Run-Anywhere) Sun, Java. 98/NT/2000(Microsoft Windows 98/NT/2000) (Sun Solaris), (LINUX) OS X(Macintosh OS X). Java. (RMI),,, (UNICODE) Java. Java 3D 3 Java. Java 3D API.,,, 3,,,. Java 3D (scenegraph),. Java 3D (head-mounted display, HMD) (screen projector) (view model). Java 3D,. 25
Java 3D Progra mming, Java 3D 3 3. API Java 3D. Java 3D, 3 3. 3 [ 1.1]. 1.1 Java 3D 3 VRML, C API OpenGL. 26
Cha pte r 1 Ja va 3D VRML(Virtual Reality Modeling Language) Java 3D. 3 OpenGL C, API., Java 3D. 1.2 Java 3D. OpenGL OpenGL Java 3D.,. OpenGL Java 3D, Java 3D. Java 3D, OpenGL C Java 3D Java. OpenGL API. Java Java. Java Java 3D., Java (JVM) GC. GC,.., GC. 27
Java 3D Progra mming Java API, Java 3D., Sun Java 2(JRE 1.2). JVM Java 2, Java 3D Java 2. Sun Java 2, Java 3D.,., OpenGL DirectX,.. Java 3D, Java 2., CLASSPATH., SDK 1.2 Java Java WebStart. XP JVM. Java 3D,,. Java 3D Java 3D. 1.3 ( ) Java, 3D PC. 3D 3D, 3 3., 3, PC. 28
Cha pte r 1 Ja va 3D 3D (, 3 ), 30 (FPS, frames per second, ).,,. PC,. 3 ( )... - -, -. 3D. 500MHz 16MB 3D. 3D. Java 3D OpenGL. Java 3D. 128MB 29
Java 3D Progra mming.,..,., 3. VMGEAR(http://www.vmgear.com) OptimizeIt. OptimizeIt JVM,. 1.4,.. 2., (AMD 850MHz nvidia GeForce II Ultra(64MB RAM) 256MB RAM) Java 3D Fly-Through (http://www.javasoft.com/products/java-media/3d/flythrough.html) 35FPS. ([ 1.2] ) 195,000 4,115 Shape3D 1,238 ( ). 30
Cha pte r 1 Ja va 3D 1.2 Sun Java 3D Fly-Through 1.4.1, Java. Swing/JFC GUI. Java 3D,. [ 1.1] Java 3D Fly-Through, Java 3D. Swing 25MB, 100MB. 1.1 Java 3D Fly-Through 25MB ( ) 108MB ( ) 3 1
Java 3D Progra mming. RAM.. (startup time).,. JVM,., 3D. RAM (JVM ) RAM.,.,. 3., Java 3D Fly-Through, [ 1.2]. 3, 3D 14. 14,. 1.2 Java 3D Fly-Through 3 14 1.5 Java 3D., Appendix A Java 3D. 32
Cha pte r 1 Ja va 3D 1.6 Java 3D API 3 API., Java 3D OpenGL, OpenGL JNI Java. Java API Java 3D, 1998., 1990 OpenGL Java 3D., OpenGL API, Java 3D 1.4, Java 3D. (Architecture Review Board, ARB) OpenGL., Java 3D Java Community Process(JCP) API. Java 3 Java 3D. Java C++, API Java 3D OpenGL/Direct3D,. Java 3D ( ), Sun API., 3, Java/Java3D C/OpenGL. 33
2 3 2.1 3 2.2 3 2 2.3 2.4 MyJava3D 2.5
Java 3D Progra mming 3, ( ), Java 3D OpenGL DirectX. 3.,., Mondo Media(http://www.mondomed.com/mlabs/glossary.html) 3Dgaming.com(http:/ /www.3dgaming.com/fps/techshop/glossary/) Chalmers Medialab(http://oss.medialab.chalmers. se/dictionary/) 3. 2.1 3, 3. 3. 10 3. C, 256KB RAM 80386. Java 3D OpenGL. BASIC BASIC. GKS API (Solaris). 3. ASCII 3.,.. 3 2.. 10., Java, Java 3D Java 3D. Java 3D OpenGL 3. 36
Cha pte r 2 3 3 2. 3, Java 3D. API, API., Java 3D MyJava3D. [ 2.1] Java 3D (LoaderTest ), Lightwave OBJ. [ 2.2] AWT 2 MyJava3D. 2.1 Java 3D application (LoaderTest) 2.2 MyJava3D - [ 2.1] 37
Java 3D Progra mming Java3D.., MyJava3D., 3 [ 2.2]., 3.., [ 2.1] 3..,. Java 3D, MyJava3D. 3. 1. 3.,. 2.., 2, 5, 7. 1. 3. 3 2.,. 4. 2., 1 3, 3 3. 5.. 1, 2, 4 5. 1 2, 4 5 Java AWT 2. 3 3., MyJava 3D Java 3D. Java 3D Lightwave OBJ, 3. 38
Cha pte r 2 3 2.2 3 2 3 2, ( ). 3. (FAQs, frequently asked questions) 3D Ark (http://www.3dark.com/resources/faqs.html). 3 Sébastien Loisel Zed3D, A compact reference for 3D computer graphics programming. http://www.math.mcgill.ca/~loisel/ ZIP. James Foley, Andries van Dam, Steven Feiner John Hughes Computer Graphics Principles and Practice (Addison-Wesley, 1990). 2.2.1 3 3. projectpoint 2 Point3d, 3, 3 2 ( z 0 ). Point3d Java 3D. Java 3D JavaDoc. public member x, y z. AwtRenderingEngine.java pr i vat e i nt xscreencent er = 320/ 2 ; pr i vat e i nt yscreencent er = 240/ 2 ; pr i vat e Vect or 3d screenposi t i on = new Vect or3d ( 0, 0, 20 ) ; pr i vat e Vect or 3d vi ewangl e = new Vect or3d ( 0, 90, 180 ) ; pr i vat e st at i c fi nal doubl e DEG_TO_RAD = 0. 017453292 ; 39
Java 3D Progra mming pr i vat e doubl e model Scale = 10 ; CT = Mat h.cos ( DEG_TO_RAD * vi ewangl e.x ); ST = Mat h.si n ( DEG_TO_RAD * vi ewangl e.x ); CP = Mat h.cos ( DEG_TO_RAD * vi ewangl e.y ); SP = Mat h.si n ( DEG_TO_RAD * vi ewangl e.y ); publi c void project Point ( Point 3d input, Point 3d output ) doubl e x = scr eenposi t i on.x + i nput.x * CT - i nput.y * ST; doubl e y = scr eenposi t i on.y + i nput.x * ST * SP + i nput.y * CT * SP + i nput. z * CP; doubl e t emp = vi ewangl e. z / (scr eenposi t i on. z + i nput.x * ST * CP + i nput.y * CT * CP - i nput. z * SP ); out put.x = xscreencent er + model Scal e * t emp * x; out put.y = yscreencent er - model Scal e * t emp * y; output. z = 0;. 7 3 projectpoint [ 2.1]. CT: 1 ST: 0 SP: 1 CP: 0 2.1 3 2 projectpoint WX WY WZ SX SY 1 1 0 250 30-1 1 0 70 30 1-1 0 250 210-1 - 1 0 70 210 0 0 0 160 120 1 1 1 255 25-1 - 1 1 65 215 40
Cha pte r 2 3 2.3 2 ([ 2.3] ). 0,0,0 (160,120)., Z (1,1,1-1,-1,1). projectpoint. x, y z sin cosine 3. Java 3D., (, HMD), ( ). 2.2.2 MyJava3D Java 3D ([ 2.4] )., Java 3D. 4 1
Java 3D Progra mming,.... 2.4 Java 3D( ) MyJava3D( 2.2.3 Java 3D.,. MyJava3D...,,... 3, n n 2. 42
Cha pte r 2 3,... hidden surface removal. (Painter's algorithm) Painter's algorithm.,. Chapel Hill North Carolina Computer Science Department (http://www.cs.unc.edu/ ~davemc/class/136/) Painter's algorithm (http://www.cs.unc.edu/~davemc/class/136/lecture19/painter.html). Painter's algorithm.,.,...,... 3.,. 43
Java 3D Progra mming, [ 2.5]. 2.5.,., (BSP tree, binary space partition tree). BSP tree. (Z- ), Z-. 3D. 640 480, ( Z- ).. xy, Z-. Z-. Z-. 44
Cha pte r 2 3.,.,.,,.,.,.,. 1024 800 Z- 32, 6.25MB., 64MB (, nvidia Geforce II/III). Z-., Z-., 16 Z-,. 16 65,536 65,536. 2, 100., 65,536. Z-,. 32 Z- (4,294,967,296),,., Z-.,,. Z- ( ),. z=2, z=10 2 10. 16 Z- 2 10 65,536.. z=50,000. 45
Java 3D Progra mming, 1,000. OpenGL OpenGL FAQ (http://www.frii.com/~martz/oglfaq/depthbuffer.htm). 2.3 MyJava3D.... MyJava3D, Phong ([ 2.6] ). 2.6 MyJava3D computeintensity. 3 ( ) GeometryArray. 46
Cha pte r 2 3, GeometryArray ( )., ( ). (beta). 0, 0. (theta). 0. theta,., (alpha). theta. Phong,. MyJava3D. Java3D, OpenGL, Direct3D,. Java 3D 10. AwtRenderingEngine.java pri vat e i nt comput eint ensi t y ( Geomet ryarray geomet ryarray, i nt i ndex, i nt numpoi nt s ) i nt i nt ensi t y = 0; i f ( comput eint ensity!= false ) / / ( ). i f ( (geomet r yar r ay.get Vert exformat ( ) &Geomet ryar ray.normals) == Geomet r yar r ay.normals ) doubl e cos_t het a; doubl e cos_al pha; doubl e cos_bet a ; 47
Java 3D Progra mming for ( i nt n = 0; n < numpoi nt s ; n++ ) geomet r yar r ay.get Normal ( i ndex+n, normal sar ray [n] ); / /. averagevect or ( sur f_norm, normal sar ray, numpoi nt s ); t emp. set ( vi ew ); t emp. scal e ( 1. 0f, sur f_norm ); cos_bet a = t emp.x + t emp.y + t emp. z; i f ( cos_bet a > 0. 0 ) cos_t het a = sur f_norm.dot ( l ight ); i f ( cos_t het a <= 0. 0 ) i nt ensi t y = (i nt ) (l i ght Max * l i ght Ambi ent ); el se t emp. set ( sur f_norm ); t emp. scal e ( (fl oat ) cos_t het a ); t emp.normal i ze ( ); t emp. sub ( l i ght ); t emp.normal i ze ( ); cos_alpha = vi ew.dot ( t emp ); i nt ensi t y = (i nt ) (l i ght Max * ( l i ght Ambi ent + l i ght Di f fuse * cos_t het a + l i ght Specul ar * Mat h.pow( cos_al pha, l i ght Gl ossi ness ) ) ); r et ur n i nt ensi t y; 48
Cha pte r 2 3 2.4 MyJa va 3D MyJava3D RenderingEngine, 3. RenderingEngine Java 3D GeometryArray 3. GeometryArray 3. GeometryArray RenderingEngine(addGeometry ) (setviewangle) (setlightangle), (setscale), (setscreensize).,, RenderingEngine GeometryArrays GeometryUpdater Graphics. GeometryUpdater. AwtRenderingEngine.java / ** * Render i ngengi ne. Render i ngengi ne 3 * (Java 3D Geomet ryar ray ) 2. */ publ i c i nt er face Render i ngengi ne / ** * Geomet r yar r ay Render i ngengi ne. * Geomet r yar r ays. */ publ i c voi d addgeomet ry ( Geomet ryarray geomet ryarray ); / ** */ * Gr aphi cs. public void render ( Graphics graphics, GeometryUpdater updater ); / ** 49
Java 3D Progra mming * RenderEngi ne. */ publ i c Vect or3d get ScreenPosi t i on (); / ** * RenderEngi ne (Vi ew Angl e ). *. */ publ ic Vector3d getviewangle (); / ** * RenderEngi ne. */ publ ic void setviewangle ( Vect or3d viewangle ); / ** * RenderEngi ne (Vi ew Angl e ). *. */ publ ic Vector3d getlightangle(); / ** * RenderEngi ne. */ publ ic void setlightangle( Vector3d angle ); / ** * RenderEngi ne. */ public void setscreensize ( int width, int height ); / ** * RenderEngi ne. */ public void setscale ( double scale ); / ** * RenderEngi ne. */ publ i c doubl e get Scal e (); 50
Cha pte r 2 3 RenderingEngine AwtRenderingEngine, Graphics (drawpolygon, setcolor, drawline, drawpoint) 3. RenderingEngine Graphics, JPanel RenderingSurface. RenderingSurface Animating- Surface,, JPanel repaint. Surface AnimatingSurface(Sun Java 2D ), (antialiasing) RenderingHints.VALUE_ANTIALIAS_OFF Graphics RenderingHints.VALUE_RENDER_SPEED Java 2D. API 3 ([ 2.4] on, [ 2.7] off ). 2.7 Java2D MyJava3D RotatingGeometryUpdater X-. 5 1
Java 3D Progra mming RotatingGeometryUpdater.java / ** * * Geomet r yupdat er */ publ i c cl ass Rot at i nggeomet ryupdat er impl ement s Geomet r yupdat er l ong l ast Frame = - 1; publ i c Rot at i nggeomet r yupdat er ( ) publi c boolean update ( Graphi cs graphics, RenderingEngine engine, Geomet ryarray geomet ry, int index, l ong framenumber ) i f ( l ast Fr ame!= f ramenumber ) l ast Frame = f ramenumber ; Vect or3d vi ewangl e = engi ne.get Vi ewangl e ( ); vi ewangl e.x += 1; engi ne.set Vi ewangl e ( vi ewangl e ); ret urn fal se ; MyJava3D. AwtRenderingEngine, Java 3D ObjectFile GeometryArray, GeometryArray AwtRenderingEngine, RotatingGeometryUpdater RenderingSurface RenderingSurface, JFrame. 52
Cha pte r 2 3 MyJava3D.java / ** * AWT * 3 3. */ publ i c cl ass MyJava3D ext ends JFrame privat e st at i c i nt m_kwidt h = 400; privat e st at i c i nt m_kheight = 400; pr ivat e Render i ngengi ne render i ngengi ne = new Awt Renderi ngengi ne (); privat e Geomet ryupdat er geomet ryupdat er = new Rot at i nggeomet ryupdat er (); pr i vat e Render i ngsur face r ender i ngsur face; publ i c MyJava3D( ) / /. Scene scene = nul l ; Shape3D shape = nul l ; / /. Obj ect Fi l e obj Fi l el oader = new Obj ect Fi le ( Obj ect Fi le.resize ); t ry scene = obj Fileloader. load ( "hand1.obj " ); cat ch ( Except ion e ) scene = nul l ; Syst em.er r.pr i nt l n ( e ); i f ( scene == nul l ) Syst em.exi t ( 1 ); / / Shape3D. 53
Java 3D Progra mming BranchGroup branchgroup = scene.get SceneGroup ( ); shape = (Shape3D) branchgroup.get Chi l d ( 0 ); / /. render i ngengi ne.addgeomet ry ( (Geomet ryar ray) shape.get Geomet ry () ); / / r ender i ngsur face,. renderingsur face = new Renderi ngsurface ( renderi ngengi ne, geomet r yupdat er ); / / r ender i ngsur face,. render i ngsur face.st art (); getcontent Pane ().add ( renderingsurface ); / / Swi ng. set Defaul t Cl oseoperat i on ( Wi ndowconst ant s.do_nothing_on_close ); / /. addwi ndowli st ener ( new Wi ndowadapt er () ); / /. publi c void windowclosing ( WindowEvent e ) Syst em.exi t ( 0 ); publ i c st at i c voi d mai n ( St r i ng [] args ) MyJava3DmyJava3D = newmyjava3d(); myjava3d.set Ti t l e ( "MyJava3D" ); myjava3d.set Si ze ( 300, 300 ); myjava3d.set Vi si bl e ( t rue ); 54
Cha pte r 2 3 2.5 MyJava3D 3,., Z- AwtRenderingEngine., 100 Java. Java 3D API. Java 3D(OpenGL Direct3D ) Z-,., Java 3D. 55