Java Programing

May 4, 2007

// add the geometry to the rendering engine…

Filed under: Java 3D Programming — webmaster @ 6:16 am

myJava3D.setTitle( “MyJava3D” ); myJava3D.setSize( 300, 300 ); myJava3D.setVisible( true ); } } 2.5 Summary The MyJava3D example application should have demystified some of the magic of 3D rendering and provided the opportunity to experiment with and test your own rendering functionality. A useful exercise would be to add some form of depth sorting or a Z-buffer to the AwtRenderingEngine. With some enhancements, it might be useful in its own right as a lightweight 100 percent Java rendering engine. The example reinforces how much more convenient it is to leverage a graphics API such as Java 3D. Not only does Java 3D handle (through OpenGL or Direct3D) low-level issues such as Z-buffering, but it also defines classes for specifying geometry and a rendering abstraction called the scenegraph. The next chapter steps you through creating your first simple Java 3D application, so let s go! 29

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

// add the geometry to the rendering engine…

Filed under: Java 3D Programming — webmaster @ 6:16 am

// add the geometry to the rendering engine… renderingEngine.addGeometry( (GeometryArray) shape.getGeometry() ); // create a rendering surface and bind the rendering engine renderingSurface = new RenderingSurface( renderingEngine, geometryUpdater ); // start the rendering surface and add it to the content panel renderingSurface.start(); getContentPane().add( renderingSurface ); // disable automatic close support for Swing frame. setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); // add the window listener addWindowListener( new WindowAdapter() { // handle the system exit window message public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } public static void main( String[] args ) { MyJava3D myJava3D = new MyJava3D(); 28

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

public RotatingGeometryUpdater ( ) { } public boolean

Filed under: Java 3D Programming — webmaster @ 1:24 am

private static int m_kWidth = 400; private static int m_kHeight = 400; private RenderingEngine renderingEngine = new AwtRenderingEngine(); private GeometryUpdater geometryUpdater = new RotatingGeometryUpdater(); private RenderingSurface renderingSurface; public MyJava3D( ) { // load the object file Scene scene = null; Shape3D shape = null; // read in the geometry information from the data file ObjectFile objFileloader = new ObjectFile( ObjectFile.RESIZE ); try { scene = objFileloader.load( “hand1.obj” ); } catch ( Exception e ) { scene = null; System.err.println( e ); } if( scene == null ) System.exit( 1 ); // retrieve the Shape3D object from the scene BranchGroup branchGroup = scene.getSceneGroup( ); shape = (Shape3D) branchGroup.getChild( 0 ); 27

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

public RotatingGeometryUpdater ( ) { } public boolean

Filed under: Java 3D Programming — webmaster @ 1:24 am

public RotatingGeometryUpdater ( ) { } public boolean update( Graphics graphics, RenderingEngine engine, GeometryArray geometry, int index, long frameNumber ) { if ( lastFrame != frameNumber ) { lastFrame = frameNumber; Vector3d viewAngle = engine.getViewAngle( ); viewAngle.x += 1; engine.setViewAngle( viewAngle ); } return false; } } The MyJava3Dclass pulls all of these elements together. It creates an AwtRenderingEngineinstance, loads a GeometryArrayfrom disk using a Java 3D ObjectFileobject loader, adds the GeometryArrayto the AwtRenderingEngine, constructs a RenderingSurfacesupplying a RotatingGeometryUpdater, starts the RenderingSurface, and then adds it to the content pane of the JFramethat hosts the application. From MyJava3D.java /** * Render a 3D shape using a 3D rendering engine * that was written from scratch using AWT for * graphics operations. */ public class MyJava3D extends JFrame { 26

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

May 3, 2007

/** * Get the current View Angle used

Filed under: Java 3D Programming — webmaster @ 6:41 pm

repainton the JPanel, forcing the scene to be continuously redrawn. The Surfaceclass, which is the base class for AnimatingSurface(both taken from Sun Java 2D demos), allows you to specify Java 2D rendering hints such as RenderingHints.VALUE_ANTIALIAS_OFF, which switches off antialiasing, and RenderingHints.VALUE_RENDER_SPEED, which tells the Graphicsobject to optimize for speed rather than rendering quality. It is interesting to see the effect of switching on antialiasing (figure 2.4 is on, figure 2.7 is off), as rendering APIs or 3D graphics hardware does not commonly support this functionality. Figure 2.7 MyJava3D rendering with Java2D antialiasing enabled The RotatingGeometryUpdaterclass is used to increase the X-angle of the viewer after each subsequent frame. From RotatingGeometryUpdater.java /** * Implementation of the GeometryUpdater interface * that rotates the scene by changing the viewer position * and the scale factor for the model. */ public class RotatingGeometryUpdater implements GeometryUpdater { long lastFrame = -1; 25

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

/** * Get the current View Angle used

Filed under: Java 3D Programming — webmaster @ 6:41 pm

/** * Get the current View Angle used by the RenderEngine. View * angles are expressed in degrees. */ public Vector3d getLightAngle(); /** * Set the current View Angle used by the RenderEngine. */ public void setLightAngle( Vector3d angle ); /** * Set the Screen size used by the RenderEngine. */ public void setScreenSize( int width, int height ); /** * Set the scale used by the RenderEngine. */ public void setScale( double scale ); /** * Get the scale used by the RenderEngine. */ public double getScale(); } The RenderingEngineinterface is implemented by the AwtRenderingEngineclass, which uses simple Graphicsrendering calls (drawPolygon, setColor, drawLine, drawPoint) to render the 3D models. The RenderingEngineinstance is driven by a RenderingSurface, an instance of a JPanelthat provides a Graphicsobject for its client area and receives the frames of the rendered scene. The RenderingSurfaceextends AnimatingSurface, which creates a rendering thread, and calls 24

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

temp.normalize( ); temp.sub( light ); temp.normalize( ); cos_alpha

Filed under: Java 3D Programming — webmaster @ 1:42 pm

temp.normalize( ); temp.sub( light ); temp.normalize( ); cos_alpha = view.dot( temp ); intensity = (int) (lightMax * ( lightAmbient + lightDiffuse * cos_theta + lightSpecular * Math.pow( cos_alpha, lightGlossiness ))); } } } } return intensity; } 2.4 Putting it together MyJava3D The MyJava3D example defines the RenderingEngineinterface. This interface defines a simple rendering contract between a client and a 3D renderer implementation. The RenderingEngineinterface defines a simple renderer that can render 3D geometry described using a Java 3D GeometryArray. The GeometryArraycontains the 3D points and normal vectors for the 3D model to be rendered. In addition to adding GeometryArraysto the RenderingEngine(addGeometrymethod), the viewpoint of the viewer can be specified (setViewAngle), the direction of a single light can be specified (setLightAngle), the scaling factor to be applied to the model can be varied (setScale), and the size of the rendering screen defined (setScreenSize). To render all the GeometryArraysadded to the RenderingEngineusing the current light, screen, scale, and view parameters, clients can call the render method, supplying a Graphicsobject to render into, along with an optional GeometryUpdater. The GeometryUpdaterallows a client to modify the positions of points or rendering parameters prior to rendering. From AwtRenderingEngine.java /** * Definition of the RenderingEngine interface. A RenderingEngine 22

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

temp.normalize( ); temp.sub( light ); temp.normalize( ); cos_alpha

Filed under: Java 3D Programming — webmaster @ 1:42 pm

* can render 3D geometry (described using a Java 3D GeometryArray) * into a 2D Graphics context. */ public interface RenderingEngine { /** * Add a GeometryArray to the RenderingEngine. All GeometryArrays * will be rendered. */ public void addGeometry( GeometryArray geometryArray ); /** * Render a single frame into the Graphics. */ public void render( Graphics graphics, GeometryUpdater updater ); /** * Get the current Screen position used by the RenderEngine. */ public Vector3d getScreenPosition(); /** * Get the current View Angle used by the RenderEngine. View * angles are expressed in degrees. */ public Vector3d getViewAngle(); /** * Set the current View Angle used by the RenderEngine. */ public void setViewAngle( Vector3d viewAngle ); 23

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Figure 2.6 MyJava3D rendering without light intensity calculations

Filed under: Java 3D Programming — webmaster @ 8:26 am

// if we have a normal vector, compute the intensity // under the lighting if ( (geometryArray.getVertexFormat( ) GeometryArray.NORMALS) == GeometryArray.NORMALS ) { double cos_theta; double cos_alpha; double cos_beta; for( int n = 0; n 0.0 ) { cos_theta = surf_norm.dot( light ); if ( cos_theta <= 0.0 ) { intensity = (int) (lightMax * lightAmbient); } else { temp.set( surf_norm ); temp.scale( (float) cos_theta ); 21

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

Figure 2.6 MyJava3D rendering without light intensity calculations

Filed under: Java 3D Programming — webmaster @ 8:26 am

Figure 2.6 MyJava3D rendering without light intensity calculations The computeIntensitymethod calculates the color intensity to use when rendering a triangle. It accepts a GeometryArraycontaining the 3D points for the geometry, an index that is the first point to be rendered, and a count of the number of points (vertices) that compose the item to be rendered. The method then computes the average normal vector for the points to be rendered by inspecting the normal vectors stored within the GeometryArray. For a triangle (three vertices) this will be the vector normal to the plane of the surface. The angle between the surface normal and the viewer is then calculated (beta). If the cosine of this angle is less than or equal to zero, the facet cannot be seen by the viewer and an intensity of zero will be returned. Otherwise, the method computes the angle between the light source position vector and the surface normal vector of the surface (theta). If the cosine of this angle is less than or equal to zero, none of the light from the light source illuminates the surface, so its light intensity is set to that of the ambient light. Otherwise, the surface normal vector is multiplied by the cosine of theta, the resulting vector is normalized, and then the light vector subtracted from it and the resulting vector normalized again. The angle between this vector and the viewer vector (alpha) is then determined. The intensity of the surface is the sum of the ambient light, the diffuse lighting from the surface multiplied by the cosine of the theta, and the specular light from the surface multiplied by the cosine of alpha raised to the glossiness power. The last term is the Phong shading, which creates the highlights that are seen in illuminated curved objects. Note that in this simple MyJava3D example only one light is being used to illuminate the scene in Java3D, OpenGL, or Direct3D many lights can be positioned within the scene and the rendering engine will compute the combined effects of all the lights on every surface. Please refer to chapter 10 for a further discussion of lighting equations and example illustrations created using Java 3D. From AwtRenderingEngine.java private int computeIntensity( GeometryArray geometryArray, int index, int numPoints ) { int intensity = 0; if ( computeIntensity != false ) { 20

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

« Previous PageNext Page »

Powered by Java Web Hosting