⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extexture.htm

📁 详细介绍Java 3D编程的一本电子书
💻 HTM
📖 第 1 页 / 共 2 页
字号:
		String value = null;
		for ( int i = 0; i < images.length; i++ )
		{
			value = (String)images[i].value;
			textureComponents[i] = loadTexture( value );
		}

		tex = textureComponents[ currentImage ];

	}


	//
	//  Handle checkboxes and menu choices
	//
	public void checkboxChanged( CheckboxMenu menu, int check )
	{
		if ( menu == imageMenu )
		{
			// Change the texture image
			currentImage = check;
			Texture tex = textureComponents[currentImage];
			int mode = ((Integer)boundaries[currentBoundary].value).intValue();
			Color3f color = (Color3f)colors[currentColor].value;
			int filter = ((Integer)filters[currentFilter].value).intValue( );

			shape.setAppearance( dummyApp );
			tex.setEnable( textureOnOff );
			tex.setBoundaryModeS( mode );
			tex.setBoundaryModeT( mode );
			tex.setBoundaryColor( color.x, color.y, color.z, 0.0f );
			tex.setMagFilter( filter );
			tex.setMinFilter( filter );
			app.setTexture( tex );
			shape.setAppearance( app );

			return;
		}

		if ( menu == boundaryMenu )
		{
			// Change the texture boundary mode
			currentBoundary = check;
			Texture tex = textureComponents[currentImage];
			int mode = ((Integer)boundaries[currentBoundary].value).intValue();

			shape.setAppearance( dummyApp );
			tex.setBoundaryModeS( mode );
			tex.setBoundaryModeT( mode );
			app.setTexture( tex );
			shape.setAppearance( app );

			return;
		}

		if ( menu == colorMenu )
		{
			// Change the boundary color
			currentColor = check;
			Color3f color = (Color3f)colors[currentColor].value;
			Texture tex = textureComponents[currentImage];

			shape.setAppearance( dummyApp );
			tex.setBoundaryColor( color.x, color.y, color.z, 0.0f );
			app.setTexture( tex );
			shape.setAppearance( app );

			return;
		}

		if ( menu == filterMenu )
		{
			// Change the filter mode
			currentFilter = check;
			int filter = ((Integer)filters[currentFilter].value).intValue( );
			Texture tex = textureComponents[currentImage];

			shape.setAppearance( dummyApp );
			tex.setMagFilter( filter );
			tex.setMinFilter( filter );
			app.setTexture( tex );
			shape.setAppearance( app );

			return;
		}

		if ( menu == modeMenu )
		{
			// Change the texture mode
			currentMode = check;
			int mode = ((Integer)modes[currentMode].value).intValue( );

			app.setTextureAttributes( dummyAtt );
			texatt.setTextureMode( mode );
			app.setTextureAttributes( texatt );

			return;
		}

		if ( menu == blendColorMenu )
		{
			// Change the boundary color
			currentBlendColor = check;
			Color3f color = (Color3f)colors[currentBlendColor].value;

			app.setTextureAttributes( dummyAtt );
			texatt.setTextureBlendColor( color.x, color.y, color.z, 0.5f );
			app.setTextureAttributes( texatt );

			return;
		}

		if ( menu == xformMenu )
		{
			// Change the texture transform
			currentXform = check;
			Transform3D tt = new Transform3D( );
			switch ( currentXform )
			{
			default:
			case 0:
				// Identity
				texatt.setTextureTransform( tt );	
				return;

			case 1:
				// Scale by 2
				tt.setScale( 2.0 );
				texatt.setTextureTransform( tt );	
				return;

			case 2:
				// Scale by 4
				tt.setScale( 4.0 );
				texatt.setTextureTransform( tt );	
				return;

			case 3:
				// Z rotate by 45 degrees
				tt.rotZ( Math.PI/4.0 );
				texatt.setTextureTransform( tt );	
				return;

			case 4:
				// Translate by 0.25
				tt.set( new Vector3f( 0.25f, 0.0f, 0.0f ) );
				texatt.setTextureTransform( tt );	
				return;
			}
		}

		// Handle all other checkboxes
		super.checkboxChanged( menu, check );
	}


	public void itemStateChanged( ItemEvent event )
	{
		Object src = event.getSource( );

		// Check if it is the texture on/off choice
		if ( src == textureOnOffMenu )
		{
			textureOnOff = textureOnOffMenu.getState( );
			Texture tex = textureComponents[currentImage];
			tex.setEnable( textureOnOff );
		}

		// Handle all other checkboxes
		super.itemStateChanged( event );
	}





	//--------------------------------------------------------------
	//  UTILITY METHODS
	//--------------------------------------------------------------

	//
	//  Handle loading a texture and setting up its attributes
	//
	private Texture2D loadTexture( String filename )
	{
		// Load the texture image file
		if ( debug )
			System.err.println( "Loading texture '" + filename + "'" );
		TextureLoader texLoader = new TextureLoader( filename, this);

		// If the image is NULL, something went wrong
		ImageComponent2D ic = texLoader.getImage( );
		if ( ic == null )
		{
			System.err.println( "Cannot load texture '" + filename + "'" );
			return null;
		}

		// Configure a Texture2D with the image
		Texture2D t = (Texture2D)texLoader.getTexture( );

		int mode = ((Integer)boundaries[currentBoundary].value).intValue();
		t.setBoundaryModeS( mode );
		t.setBoundaryModeT( mode );

		Color3f color = (Color3f)colors[currentColor].value;
		t.setBoundaryColor( color.x, color.y, color.z, 0.0f );

		int filter = ((Integer)filters[currentFilter].value).intValue( );
		t.setMagFilter( filter );
		t.setMinFilter( filter );

		t.setMipMapMode( Texture.BASE_LEVEL );

		// Turn it on and allow future changes
		t.setEnable( true );
		t.setCapability( Texture.ALLOW_ENABLE_WRITE );

		return t;
	}

	//
	//  Build a cube using a QuadArray
	//
	public QuadArray buildGeometry( )
	{
		QuadArray cube = new QuadArray(
					24,
					GeometryArray.COORDINATES |
					GeometryArray.NORMALS     |
					GeometryArray.TEXTURE_COORDINATE_2 );
		cube.setCapability( GeometryArray.ALLOW_COORDINATE_WRITE );
		cube.setCapability( GeometryArray.ALLOW_TEXCOORD_WRITE );

		VertexList vl = new VertexList( cube );

		float MAX =  1.0f;
		float MIN =  0.0f;

		//           Coordinate             Normal               Texture
		//             X      Y      Z       I      J      K      S    T

		// Front
		vl.xyzijkst( -1.0f, -1.0f,  1.0f,   0.0f,  0.0f,  1.0f,  MIN, MIN );
		vl.xyzijkst(  1.0f, -1.0f,  1.0f,   0.0f,  0.0f,  1.0f,  MAX, MIN );
		vl.xyzijkst(  1.0f,  1.0f,  1.0f,   0.0f,  0.0f,  1.0f,  MAX, MAX );
		vl.xyzijkst( -1.0f,  1.0f,  1.0f,   0.0f,  0.0f,  1.0f,  MIN, MAX );

		// Back
		vl.xyzijkst(  1.0f, -1.0f, -1.0f,   0.0f,  0.0f, -1.0f,  MAX, MIN );
		vl.xyzijkst( -1.0f, -1.0f, -1.0f,   0.0f,  0.0f, -1.0f,  MIN, MIN );
		vl.xyzijkst( -1.0f,  1.0f, -1.0f,   0.0f,  0.0f, -1.0f,  MIN, MAX );
		vl.xyzijkst(  1.0f,  1.0f, -1.0f,   0.0f,  0.0f, -1.0f,  MAX, MAX );

		// Right
		vl.xyzijkst(  1.0f, -1.0f,  1.0f,   1.0f,  0.0f,  0.0f,  MIN, MAX );
		vl.xyzijkst(  1.0f, -1.0f, -1.0f,   1.0f,  0.0f,  0.0f,  MIN, MIN );
		vl.xyzijkst(  1.0f,  1.0f, -1.0f,   1.0f,  0.0f,  0.0f,  MAX, MIN );
		vl.xyzijkst(  1.0f,  1.0f,  1.0f,   1.0f,  0.0f,  0.0f,  MAX, MAX );

		// Left
		vl.xyzijkst( -1.0f, -1.0f, -1.0f,  -1.0f,  0.0f,  0.0f,  MIN, MIN );
		vl.xyzijkst( -1.0f, -1.0f,  1.0f,  -1.0f,  0.0f,  0.0f,  MIN, MAX );
		vl.xyzijkst( -1.0f,  1.0f,  1.0f,  -1.0f,  0.0f,  0.0f,  MAX, MAX );
		vl.xyzijkst( -1.0f,  1.0f, -1.0f,  -1.0f,  0.0f,  0.0f,  MAX, MIN );

		// Top
		vl.xyzijkst( -1.0f,  1.0f,  1.0f,   0.0f,  1.0f,  0.0f,  MIN, MAX );
		vl.xyzijkst(  1.0f,  1.0f,  1.0f,   0.0f,  1.0f,  0.0f,  MAX, MAX );
		vl.xyzijkst(  1.0f,  1.0f, -1.0f,   0.0f,  1.0f,  0.0f,  MAX, MIN );
		vl.xyzijkst( -1.0f,  1.0f, -1.0f,   0.0f,  1.0f,  0.0f,  MIN, MIN );

		// Bottom
		vl.xyzijkst( -1.0f, -1.0f, -1.0f,   0.0f, -1.0f,  0.0f,  MIN, MIN );
		vl.xyzijkst(  1.0f, -1.0f, -1.0f,   0.0f, -1.0f,  0.0f,  MAX, MIN );
		vl.xyzijkst(  1.0f, -1.0f,  1.0f,   0.0f, -1.0f,  0.0f,  MAX, MAX );
		vl.xyzijkst( -1.0f, -1.0f,  1.0f,   0.0f, -1.0f,  0.0f,  MIN, MAX );

		return cube;
	}

	//
	//  Use a helper class to manage the coordinate lists
	//
	private class VertexList
	{
		private int index = 0;
		private GeometryArray ga = null;

		public VertexList( GeometryArray newga )
		{
			index = 0;
			ga = newga;
		}

		public void xyzst( float x, float y, float z, float s, float t )
		{
			ga.setCoordinate( index, new Point3f( x, y, z ) );
			ga.setTextureCoordinate( index, new Point2f( s, t ) );
			index++;
		}

		public void xyzijkst(	float x, float y, float z,
					float i, float j, float k,
					float s, float t )
		{
			ga.setCoordinate( index, new Point3f( x, y, z ) );
			ga.setNormal( index, new Vector3f( i, j, k ) );
			ga.setTextureCoordinate( index, new Point2f( s, t ) );
			index++;
		}
	}
}
</FONT></PRE>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -