📄 extexture.htm
字号:
<HTML>
<HEAD>
<!-- Created by mktalk.pl on 5/4/99 at 4:41:12 PM -->
<TITLE>ExTexture.java</TITLE>
</HEAD>
<BODY
BGCOLOR=#000000
TEXT=#FFFFFF
LINK=#FFFFFF
ALINK=#00FF00
VLINK=#888888
>
<FONT COLOR=#FFFF00 SIZE=+0>
<CENTER>Introducing texture mapping</CENTER></FONT>
<FONT COLOR=#FFFF00 SIZE=+3>
<CENTER><B><I>ExTexture.java</I></B></CENTER></FONT>
<CENTER><IMG SRC="../images/red.jpg" HEIGHT=2 WIDTH=70% BORDER=0></CENTER>
<P>
<PRE>
<FONT COLOR=#00FF00 SIZE=+1>
//
// CLASS
// ExTexture - illustrate use of textures
//
// LESSON
// Use Texture2D and TextureAttributes to apply a texture image
// to a shape.
//
// AUTHOR
// David R. Nadeau / San Diego Supercomputer Center
//
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.*;
import java.net.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.image.*;
public class ExTexture
extends Example
{
//--------------------------------------------------------------
// SCENE CONTENT
//--------------------------------------------------------------
//
// Nodes (updated via menu)
//
private Shape3D shape = null; // overall scene shape
private Appearance app = null; // geometry appearance
private Appearance dummyApp = null; // temporary appearance
private TextureAttributes texatt = null;// texture attributes
private TextureAttributes dummyAtt = null;// temporary texture attributes
private Texture2D tex = null; // current texture
//
// Build scene
//
public Group buildScene( )
{
// Get the current menu choices for appearance attributes
int textureMode = ((Integer)modes[currentMode].value).intValue( );
Color3f color = (Color3f)colors[currentColor].value;
Color3f blendColor = (Color3f)colors[currentBlendColor].value;
// Turn on the example headlight
setHeadlightEnable( true );
// Default to examine navigation
setNavigationType( Examine );
// Disable scene graph compilation for this example
setCompilable( false );
// Create the scene group
Group scene = new Group( );
// BEGIN EXAMPLE TOPIC
// Set up a basic material
Material mat = new Material( );
mat.setAmbientColor( 0.2f, 0.2f, 0.2f );
mat.setDiffuseColor( 1.0f, 1.0f, 1.0f );
mat.setSpecularColor( 0.0f, 0.0f, 0.0f );
mat.setLightingEnable( true );
// Set up the texturing attributes with an initial
// texture mode, texture transform, and blend color
texatt = new TextureAttributes( );
texatt.setPerspectiveCorrectionMode( TextureAttributes.NICEST );
texatt.setTextureMode( textureMode );
texatt.setTextureTransform( new Transform3D( ) ); // Identity
texatt.setTextureBlendColor( blendColor.x, blendColor.y, blendColor.z, 0.5f );
// Enable changing these while the node component is live
texatt.setCapability( TextureAttributes.ALLOW_MODE_WRITE );
texatt.setCapability( TextureAttributes.ALLOW_BLEND_COLOR_WRITE );
texatt.setCapability( TextureAttributes.ALLOW_TRANSFORM_WRITE );
// Create an appearance using these attributes
app = new Appearance( );
app.setMaterial( mat );
app.setTextureAttributes( texatt );
app.setTexture( tex );
// And enable changing these while the node component is live
app.setCapability( Appearance.ALLOW_TEXTURE_WRITE );
app.setCapability( Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE );
// Build a shape and enable changing its appearance
shape = new Shape3D( buildGeometry( ), app );
shape.setCapability( Shape3D.ALLOW_APPEARANCE_WRITE );
// END EXAMPLE TOPIC
// Create some dummy appearance and tex attribute node components
// In response to menu choices, we quickly switch the shape to
// use one of these, then diddle with the main appearance or
// tex attribute, then switch the shape back. This effectively
// makes the appearance or tex attributes we want to change
// become un-live during a change. We have to do this approach
// because some texture features have no capability bits to set
// to allow changes while live.
dummyApp = new Appearance();
dummyAtt = new TextureAttributes( );
scene.addChild( shape );
return scene;
}
//--------------------------------------------------------------
// USER INTERFACE
//--------------------------------------------------------------
//
// Main
//
public static void main( String[] args )
{
ExTexture ex = new ExTexture( );
ex.initialize( args );
ex.buildUniverse( );
ex.showFrame( );
}
// Texture enable/disable
private boolean textureOnOff = true;
private CheckboxMenuItem textureOnOffMenu = null;
// Texture image choices
private NameValue[] images =
{
new NameValue( "Red bricks", "brick.jpg" ),
new NameValue( "Stone bricks", "stonebrk2.jpg" ),
new NameValue( "Marble", "granite07rev.jpg" ),
new NameValue( "Mud", "mud01.jpg" ),
new NameValue( "Wood", "flooring.jpg" ),
new NameValue( "Earth", "earthmap.jpg" ),
};
private CheckboxMenu imageMenu = null;
private int currentImage = 0;
// Texture boundary mode choices
private NameValue[] boundaries =
{
new NameValue( "CLAMP", new Integer( Texture.CLAMP ) ),
new NameValue( "WRAP", new Integer( Texture.WRAP ) ),
};
private CheckboxMenu boundaryMenu = null;
private int currentBoundary = 0;
// Texture boundary color choices
private NameValue[] colors = {
new NameValue( "White", White ),
new NameValue( "Gray", Gray ),
new NameValue( "Dark Gray", DarkGray ),
new NameValue( "Black", Black ),
new NameValue( "Red", Red ),
new NameValue( "Dark Red", DarkRed ),
new NameValue( "Green", Green ),
new NameValue( "Dark Green", DarkGreen ),
new NameValue( "Blue", Blue ),
new NameValue( "Dark Blue", DarkBlue ),
};
private CheckboxMenu colorMenu = null;
private int currentColor = 6;
// Texture filter choices
private NameValue[] filters =
{
new NameValue( "POINT", new Integer( Texture.BASE_LEVEL_POINT ) ),
new NameValue( "LINEAR", new Integer( Texture.BASE_LEVEL_LINEAR ) ),
};
private CheckboxMenu filterMenu = null;
private int currentFilter = 0;
// Texture attributes mode choices
private NameValue[] modes =
{
new NameValue( "BLEND", new Integer( TextureAttributes.BLEND ) ),
new NameValue( "DECAL", new Integer( TextureAttributes.DECAL ) ),
new NameValue( "MODULATE", new Integer( TextureAttributes.MODULATE ) ),
new NameValue( "REPLACE", new Integer( TextureAttributes.REPLACE ) ),
};
private CheckboxMenu modeMenu = null;
private int currentMode = 2;
// Texture attributes blend color choices
private CheckboxMenu blendColorMenu = null;
private int currentBlendColor = 6;
private NameValue[] xforms =
{
new NameValue( "Identity", new Integer( 0 ) ),
new NameValue( "Scale by 2", new Integer( 1 ) ),
new NameValue( "Scale by 4", new Integer( 2 ) ),
new NameValue( "Rotate by 45 degrees", new Integer( 3 ) ),
new NameValue( "Translate by 0.25", new Integer( 4 ) ),
};
private CheckboxMenu xformMenu = null;
private int currentXform = 0;
private Texture2D[] textureComponents;
//
// Initialize the GUI (application and applet)
//
public void initialize( String[] args )
{
// Initialize the window, menubar, etc.
super.initialize( args );
exampleFrame.setTitle( "Java 3D Texture Mapping Example" );
//
// Add a menubar menu to change texture parameters
// Image -->
// Boundary mode -->
// Boundary color -->
// Filter mode -->
//
Menu m = new Menu( "Texture" );
textureOnOffMenu = new CheckboxMenuItem(
"Texturing enabled", textureOnOff );
textureOnOffMenu.addItemListener( this );
m.add( textureOnOffMenu );
imageMenu = new CheckboxMenu( "Image", images,
currentImage, this );
m.add( imageMenu );
boundaryMenu = new CheckboxMenu( "Boundary mode", boundaries,
currentBoundary, this );
m.add( boundaryMenu );
colorMenu = new CheckboxMenu( "Boundary color", colors,
currentColor, this );
m.add( colorMenu );
filterMenu = new CheckboxMenu( "Filter mode", filters,
currentFilter, this );
m.add( filterMenu );
exampleMenuBar.add( m );
//
// Add a menubar menu to change texture attributes parameters
// Mode -->
// Blend color -->
//
m = new Menu( "TextureAttributes" );
modeMenu = new CheckboxMenu( "Mode", modes,
currentMode, this );
m.add( modeMenu );
blendColorMenu = new CheckboxMenu( "Blend color", colors,
currentBlendColor, this );
m.add( blendColorMenu );
xformMenu = new CheckboxMenu( "Transform", xforms,
currentXform, this );
m.add( xformMenu );
exampleMenuBar.add( m );
// Preload the texture images
// Use the texture loading utility to read in the texture
// files and process them into an ImageComponent2D
// for use in the Background node.
if( debug )
System.err.println( "Loading textures..." );
textureComponents = new Texture2D[images.length];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -