|
Texturing Basics Example
Textures are a core part of every graphics applications. They can be used to
provide direct visual effects, indirect (such as environment mapping) or
even as inputs to programmable shaders. thus, it is important to be able
to use textures efficiently in any graphics API and understand the myriad of
options available. Aviatrix provides such an extensive range of texturing
capabilities that it can't all be covered in a single tutorial. This one
is aimed at introducing just the basic concepts, while the advanced
capabilities are given their own treatment. If you would like to
see the complete version of this code, it can be found in the
examples/basic directory and is named Setting up the applicationThe texture demo starts with just a slight variation of the Hello World demo. Instead of a triangle for the geometry, we will create a square so that you can see the complete texture. 101 Varieties of Textures
One of the nice things about texturing is that there are so many options to
choose from. At the C level, OpenGL provides just one way of handing texture
data to the rendering pipeline - through an array of ints, floats or bytes.
At the Java level, there are many different ways of providing texture data -
based on how it was loaded. A user could use the ImageIO libraries, URL
content handlers, through the AWT With Aviatrix3D, we've tried to cover as many bases as possible. One of the things that frustrated us most with Java3D was that textures were required to use the Java Image system. A lot of the time we wanted to generate dynamic textures and the overheads associated with the AWT image system was prohibitive - particularly for offscreen rendering techniques. At the same time, we wanted a consistent set of APIs regardless of how you created the texture or where you are using it.
Textures are divided into two sets of classes - data holders, which hold the
source for the pixels, and the rendering management object. By making this
separation we can separately control how to provide the raw pixels, yet keep
a single set of objects that are responsible for rendering. Classes that hold
pixel data are extended from the
For each texture, you have the option of defining 1D, 2D or 3D textures, along
with cubic environment maps and offscreen (pBuffer) textures. For the source
data, you can provide it as either Java AWT BufferedImages or as a raw byte
array. The extensibility of the system would allow you to define other types
if you desire too, these are the stock options available. The names of the
source Loading a TextureThere are many different ways to get a texture into the system from the raw data so this example will show just one option. The first thing that you need to do is to acquire the raw data that you want to render. Whether that source is a procedurally generated texture using some Perlin noise function or loaded from an external file, it does not matter. Here we are going to make use of the ImageIO libraries from JDK 1.4. Start by loading an instance of the image into the application using the normal routines TextureComponent2D img_comp = null; try { File f = new File("textures/test_image.png"); if(!f.exists()) { System.out.println("Can't find texture source file"); return; } FileInputStream is = new FileInputStream(f); BufferedInputStream stream = new BufferedInputStream(is); BufferedImage img = ImageIO.read(stream); .... } catch(IOException ioe) { System.out.println("Error reading image: " + ioe); return; }
Note that the image that is read is an instance of
After the image has been loaded, you need to create the wrapper for it with the
appropriate img_comp = new ImageTextureComponent2D(format, img);
Working out the format is a somewhat optional excercise since for the most
part it is automatically overridden based on the information from the supplied
image source. If the source is an instance of
With the Texture2D texture = new Texture2D();
And then fill it in with the source data using the texture.setImages(Texture.MODE_BASE_LEVEL, Texture.FORMAT_RGB, new TextureComponent[] { img_comp }, 1);
Note that Applying textures to an object
The last step of the texturing setup it to connect the
TextureUnit[] tu = new TextureUnit[1]; tu[0] = new TextureUnit(); tu[0].setTexture(texture); Appearance app = new Appearance(); app.setTextureUnits(tu, 1); And one last screenshot of what you should see if you run the demo application:
|
[ Home ]
[ License ]
[ javadoc ]
[ Online Examples ]
[ Download ]
[ j3d.org ] [ Aviatrix3D ] [ Code Repository ] [ Java3D ] [ OpenGL ] [ Books ] [ Contact Us ] Last Updated: $Date: 2010-05-01 04:20:35 -0700 (Sat, 01 May 2010) $ |