Java Programing

January 30, 2007

Figure 14.2 A texture-mapped cube (left); the texture

Filed under: Java 3D Programming — webmaster @ 3:33 pm

Figure 14.2 A texture-mapped cube (left); the texture image, actual size (middle); and the how the texture image was mapped onto one of the faces of the cube (right) Figure 14.3 Texture coordinates range from 0.0 to 1.0 with the origin at the bottom left of the texture image. The horizontal dimension is commonly called s and the vertical dimension is called t You should also be able to see that as the texture has been enlarged it has become pixilated. This is because several eventual screen pixels are all mapped to the same pixel within the texture image. This is a common problem with texture mapping and is visible in texture-mapped games such as Quake, as well. To discuss the details of mapping between 3D vertex coordinates and texture pixels, some terminology must be introduced. Figure 14.3 illustrates texture coordinates. Instead of mapping to pixel locations directly (which would be relative to the size of the texture image), we use texture coordinates. Texture coordinates range from 0.0 to 1.0 in each dimension, regardless of the size of the image. We know therefore that the coordinates s = 0.5, t = 0.25 are always located halfway across the image and three-quarters of the way down from the top of the image. Note that the origin of the texture coordinate system is at the bottom left of the image, in contrast to many windowing systems that define the origin at the top left. A pixel within an image that is used for texture mapping is often referred to as a texel. There are essentially two types of texture mapping, static and dynamic. Defining a static mapping is the most commonly used and easiest form of texture mapping and is the subject of section 14.1.1. 14.1.1 Static mapping using per-vertex texture coordinates Static mapping defines a static relationship between vertex coordinates and texture coordinates. This is usually implemented by simply assigning a texture coordinate to each vertex in the model (table 14.1). 230

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

Figure 14.2 A texture-mapped cube (left); the texture

Filed under: Java 3D Programming — webmaster @ 3:33 pm

Table 14.1 Static mapping Vertex 143: coordinate: 3, 6,7 color: red = 184, green = 242, blue = 32 normal vector 0.5, 0.2, -0.3 texture coordinate: 0.3, 0.6 Vertex 143 has been assigned a number of attributes: coordinate (position), color, normal vector, and a texture coordinate. The TextureTestexample that follows can be used to experiment with the relationship among images, texture coordinates, and 3D vertex coordinates (figure 14.4). TextureTestloads the following information from a simple ASCII text file: Name of texture image Size of geometry in the x direction Geometry y scaling factor Number of vertices Texture coordinates for Vertex 1 Texture coordinates for Vertex 2 Texture coordinates for Vertex N For example, the data for the image in figure 14.4 is shown in table 14.2. Table 14.2 Static mapping Width 400 Height 400 Vertex x y x’ y’ tx ty 0 159 99 159 301 0.40 0.75 1 125 126 125 274 0.31 0.69 2 110 163 110 237 0.28 0.59 3 102 243 102 157 0.26 0.39 4 118 304 118 96 0.30 0.24 5 179 363 179 37 0.45 0.09 6 220 364 220 36 0.55 0.09 7 264 335 264 65 0.66 0.16 8 287 289 287 111 0.72 0.28 9 295 204 295 196 0.74 0.49 10 279 132 279 268 0.70 0.67 11 253 104 253 296 0.63 0.74 12 207 95 207 305 0.52 0.76 231

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

CHAPTER 14 Using texture images 14.1 Introduction 14.2

Filed under: Java 3D Programming — webmaster @ 10:09 am

Figure 14.1 By applying a bitmap to the geometric model (left), very realistic results can be achieved even with a fairly coarse geometric mesh 14.1 Introduction Texture mapping is exactly what it says. As an application developer, you are defining a mapping from 3D coordinates into texture coordinates. Usually this equates to defining a coordinate mapping to go from a vertex s 3D coordinates to a 2D pixel location within an image. Defining coordinate mappings sounds pretty complicated, but in practice it can be as simple as saying the vertex located at position (1,1,1) should use the pixel located at (20,30) in the image named texture.jpg. Looking at figure 14.2 it should be obvious that the renderer does some pretty clever stuff when it maps a texture onto a geometric model. The texture used was 64 x 64 pixels in size, but when it was rendered, the faces of each cube were about 200 x 200 pixels. So, the renderer had to resize the texture image on the fly to fit the face of each cube. Even tougher, you can see that what started out as a square texture image turned into a parallelogram as perspective and rotation were applied to the cube. 229

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

CHAPTER 14 Using texture images 14.1 Introduction 14.2

Filed under: Java 3D Programming — webmaster @ 10:09 am

CHAPTER 14 Using texture images 14.1 Introduction 14.2 3D texture coordinates 14.3 Texture and multiple levels of detail 14.4 TextureAttributes 14.5 Using transparent geometry with transparent texture images 14.6 Animated (video) texture mapping 14.7 Summary The process of applying a bitmap to geometry is called texture mapping and is often a highly effective way of achieving apparent scene complexity while still using a relatively modest number of vertices. By the end of this chapter, you should be able to generate texture coordinates and apply a texture image to your geometry (e.g., figure 14.1). If you are familiar with the process of texture mapping and texture coordinates, you may want to skim the first few sections and jump straight to the specifics of the Java 3D implementation. As colors can only be associated with vertices in the model, if texture mapping was not used, a vertex would have to be located at every significant surface color transition. For highly textured surfaces such as wood or stone, this would quickly dominate the positions of the vertices rather than the geometric shape of the object itself. By applying an image to the geometric model, the apparent complexity of the model is increased while preserving the function of vertices for specifying relative geometry within the model. Modern 3D computer games have used texture mapping extensively for a number of years, and first-person-perspective games such as Quake by Id software immerses the user in a richly texture-mapped world. 228

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

BehaviorTestexample application. 13.5.1 Calculating the rendered FPS using

Filed under: Java 3D Programming — webmaster @ 3:28 am

} } //assign the next WakeUpCondition, so we are notified again wakeupOn( m_WakeupCondition ); } } 13.6 Summary The BehaviorTestexample allows many behaviors to affect a single texture-mapped Sphere. RotationInterpolatorrotates the entire scene, ObjectSizeBehaviorprints the size of the Sphereevery 20 frames, ExplodeBehaviorexplodes the Sphereevery 10 seconds, StretchBehaviormodels the vertices of the Sphereas weights attached to springs anchored at the origin, and BoundsBehaviortracks the Boundsof the Sphere. Tying all these behaviors together into a single application allows complex application logic to be built up from relatively simple building blocks. The interactions between the behaviors can be explored by running the example and switching the behaviors on and off using the AWT buttons at the bottom of the Frame. I hope the examples presented in this section have demystified Java 3D s behaviors. You should now start breaking down your application logic into good, reusable, OO chunks and distributing them across your scenegraph. You should aim to empower your scenegraph objects with the abilities to detect, process, and respond to user interactions. Keep a careful eye on application performance at all times, because excessive behavior processing can slow your frame rate or make your application appear unresponsive. Do not be afraid of writing more complex behaviors that can affect whole classes of objects within your scenegraph. In this way you may be able to limit the number of behaviors in the scenegraph and use a manager design philosophy, where each behavior manages a given class of objects within the scenegraph, instead of attaching single instances of a behavior to a single object. 227

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

BehaviorTestexample application. 13.5.1 Calculating the rendered FPS using

Filed under: Java 3D Programming — webmaster @ 3:28 am

BehaviorTestexample application. 13.5.1 Calculating the rendered FPS using a behavior A useful method of displaying the rendered FPS in a Java 3D application is to add the following Behavior class anywhere within the scenegraph. A behavior-based calculation is easier to add and remove to a program than overriding the Canvas3D postSwapmethod. If accuracy is paramount, postSwap NOTE may provide more accurate results because Behavior processing typically runs on an independent thread to rendering. From FpsBehavior //this class implements a simple behavior //that output the rendered Frames Per Second. public class FpsBehavior extends Behavior { //the wake up condition for the behavior protected WakeupCondition m_WakeupCondition = null; protected long m_StartTime = 0; private final int m_knReportInterval = 100; public FpsBehavior() { //save the WakeupCriterion for the behavior m_WakeupCondition = new WakeupOnElapsedFrames( m_knReportInterval ); } public void initialize() { //apply the initial WakeupCriterion wakeupOn( m_WakeupCondition ); } public void processStimulus( java.util.Enumeration criteria ) { while( criteria.hasMoreElements() ) { WakeupCriterion wakeUp = (WakeupCriterion) criteria.nextElement(); //every N frames, report the FPS if( wakeUp instanceof WakeupOnElapsedFrames ) { if( m_StartTime > 0 ) { final long interval = System.currentTimeMillis() - m_StartTime; System.out.println( “FPS: ” + (double) m_knReportInterval / ( interval / 1000.0)); } m_StartTime = System.currentTimeMillis(); 226

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

January 29, 2007

public void processStimulus( java.util.Enumeration criteria ) { //update

Filed under: Java 3D Programming — webmaster @ 9:12 pm

else { //otherwise, print the average acceleration System.out.print( “Average acceleration:t” + accel_sum/m_AccelerationArray.length + “n” ); } } //assign the next WakeUpCondition, so we are notified again wakeupOn( m_WakeupCondition ); } After pressing a key has disturbed the equilibrium of the model, it can take a considerable length of time to return to equilibrium. In figure 13.4 the model took over 500 frames to stabilize. Figure 13.4 The StretchBehavior causes the Sphere to oscillate in size. By plotting the average vertex acceleration, you can see that the model took in excess of 500 frames to stabilize. The parameters used were Spring Constant 0.8, Acceleration Loss Factor 0.98, and Vertex Mass 50 + 2.5 (average) 13.5 Using behaviors for debugging A library of custom Behaviorclasses can be a very useful debugging aid, as they can be quickly added and removed from the scenegraph as needed. It is a simple step to conditionally add the debugging behaviors for development builds and remove them for production builds. For example, I have used the following two behaviors extensively: 1. BoundsBehavioris attached to a scenegraph Nodeand creates a wire frame ColorCubeor Sphereto graphically represent the Bounds(BoundingBoxor BoundingSphere) for the object at runtime. 2. FpsBehaviorcan be added anywhere in the scenegraph and writes the rendered FPS to the standard output window. Both behaviors can be found in the org.selman.java3d.bookpackage and are illustrated in the 225

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

public void processStimulus( java.util.Enumeration criteria ) { //update

Filed under: Java 3D Programming — webmaster @ 9:12 pm

public void processStimulus( java.util.Enumeration criteria ) { //update the positions of the vertices regardless of criteria float elongation = 0; float force_spring = 0; float force_mass = 0; float force_sum = 0; float timeFactor = 0.1f; float accel_sum = 0; //loop over every vertex and calculate its new position //based on the sum of forces due to acceleration and the spring for( int n = 0; n

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

m_TransparencyAttributes. setTransparency( ((float) m_nFrameNumber) / ((float) m_nNumFrames) );

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

m_TransparencyAttributes. setTransparency( ((float) m_nFrameNumber) / ((float) m_nNumFrames) ); m_Shape3D.getAppearance(). setTransparencyAttributes( m_TransparencyAttributes ); for( int n = 0; n

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

m_TransparencyAttributes. setTransparency( ((float) m_nFrameNumber) / ((float) m_nNumFrames) );

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

the lighter ones. A damping effect is modeled by losing a portion of the vertex acceleration after each iteration. See figure 13.3. Figure 13.3 The StretchBehavior: Frame 1, the original Shape3D; frames 2 4, the vertices within the Shape3D s geometry are oscillating as the vertices are affected by the springs from each vertex to the origin. The model is a Sphere primitive with an applied texture image. The Sphere was created with a resolution value of 32 This is a computationally expensive NOTE behavior. StretchBehaviorresponds to two WakeUpconditions: after every frame and after a key press. The WakeUpconditions for the behavior are specified as follows: //create the WakeupCriterion for the behavior WakeupCriterion criterionArray[] = new WakeupCriterion[2]; criterionArray[0] = new WakeupOnAWTEvent( KeyEvent.KEY_PRESSED ); criterionArray[1] = new WakeupOnElapsedFrames( 1 ); //save the WakeupCriterion for the behavior m_WakeupCondition = new WakeupOr( criterionArray ); As usual, the WakeupCriterionis passed to the behavior inside the initializemethod: public void initialize() { //apply the initial WakeupCriterion wakeupOn( m_WakeupCondition ); } The processStimulusmethod of the behavior, which is called on every frame and in response to a key press, performs all the basic physics calculations and updates the positions of the coordinates within the GeometryArray. From StretchBehavior.java 223

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

« Previous PageNext Page »

Powered by Java Web Hosting