Skip to main content

TextureUnitState

5 replies [Last post]
jjones7947
Offline
Joined: 2004-10-16

Have done several loaders for XFiles gradually increasing the complexity. In the latest version, I used TextureUnitState in the Appearances, with a view to loading secondary textures specifically light maps. The geometry loads fine, but no textures. Since this was the only change, have to assume I did something wrong. The appearances are build in the following code:

public void buildAppearance() {
  // Create the Appearance object
  appear = new Appearance();
  //Create ColoringAttributes and set to Appearance bject
  ColoringAttributes col_attr = new ColoringAttributes();
  col_attr.setShadeModelColoringAttributes.SHADE_GOURAUD);
  appear.setColoringAttributes(col_attr);
  //Create PolygonAttributes and set to Appearance object
  PolygonAttributes rend_attr = new PolygonAttributes();
  rend_attr.setCullFace(PolygonAttributes.CULL_NONE);
  rend_attr.setPolygonModePolygonAttributes.POLYGON_FILL);
  appear.setPolygonAttributes(rend_attr);
  //Create TextureAttributes for TextureUnitState Objects
  TextureAttributes tex_attr = new TextureAttributes();
  tex_attr.setTextureMode(TextureAttributes.REPLACE);
  tex_attr.setPerspectiveCorrectionMode(TextureAttributes.FASTEST);
  appear.setTextureAttributes(tex_attr);
  //Create TexCoordGeneration for TextureUnitState Objects
  TexCoordGeneration tcg = new TexCoordGeneration(
TexCoordGeneration.OBJECT_LINEAR,
TexCoordGeneration.TEXTURE_COORDINATE_2);
  //TextureUnitState[] the size of the textureFiles[]
  int len = textureFiles.length;
  TextureUnitState[] tus = new TextureUnitState[len];
  TextureLoader tLoader = null;
  for(int i=0; i<len; i++) {
tLoader = new TextureLoader(textureFiles[i],
                           TextureLoader.Y_UP, null);
Texture tex = tLoader.getTexture();
tus[i] = new TextureUnitState(tex, tex_attr, tcg);
  }
  appear.setMaterial(materialSet[0]);
  appear.setTextureUnitState(tus);
}

Any ideas?
Jim

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aces
Offline
Joined: 2003-07-17

Try a better encapsulation of your method. There are too many references in outside and is very hard to track bug on code like this.

Try something like this:

[code]
public Appearance buildAppearance(Material mat,
String[] textureFiles)
{
// Create the Appearance object
Appearance appear = new Appearance();
appear.setMaterial(mat);

//Create ColoringAttributes and set to Appearance bject
ColoringAttributes col_attr = new ColoringAttributes();
col_attr.setShadeModelColoringAttributes.SHADE_GOURAUD);
appear.setColoringAttributes(col_attr);

//Create PolygonAttributes and set to Appearance object
PolygonAttributes rend_attr = new PolygonAttributes();
rend_attr.setCullFace(PolygonAttributes.CULL_NONE);
rend_attr.setPolygonModePolygonAttributes.POLYGON_FILL);
appear.setPolygonAttributes(rend_attr);

//Create TextureAttributes for TextureUnitState Objects
TextureAttributes tex_attr = new TextureAttributes();
tex_attr.setTextureMode(TextureAttributes.MODULATE);
appear.setTextureAttributes(tex_attr);

//Create TexCoordGeneration for TextureUnitState Objects
TexCoordGeneration tcg = null;

//TextureUnitState[] the size of the textureFiles[]
int len = textureFiles.length;
TextureUnitState[] tus = new TextureUnitState[len];
TextureLoader tLoader = null;
for(int i=0; i {
try
{
tLoader = new TextureLoader(textureFiles[i],
TextureLoader.Y_UP, null);
Texture tex = tLoader.getTexture();
tus[i] = new TextureUnitState(tex, tex_attr, tcg);
}
catch(Exception e)
{
System.err.println("error loading texture: " +
textureFiles[i] );
e.printStackTrace();
}
}
appear.setTextureUnitState(tus);

return appear;
}
[/code]

I hope it helps.
Alessandro

jjones7947
Offline
Joined: 2004-10-16

Run a little test
[code]
for(int i=0; i<10; i++) {
dosomething;
}
[/code]
cool it's square brackets
the code is formated,
just used the wrong markup

Alessandro,
I've spent most of my programming life writing code to parse text as fast as possible. One of the things that makes Jave fast is using void return methods, because the compiler inlines them. This cuts time for building a calling stack and cuts memory use as well. Try to avoid passing in parameters as well, choosing to operate on instance variables.

Thanks for moving it into the correct format
Any ideas on why the textures don't show up?
I can see them loaded in the variable tree in debug mode.
Jim

aces
Offline
Joined: 2003-07-17

Hi,

> Alessandro,
> I've spent most of my programming life writing code to parse text as fast as
> possible. One of the things that makes Jave fast is using void return
> methods, because the compiler inlines them. This cuts time for building a
> calling stack and cuts memory use as well. Try to avoid passing in parameters
> as well, choosing to operate on instance variables.

Ok. I understand and respect your point. But there is not much gain when you call it just once in your entire application. Objects and arrays as parameters are passed by reference(fast), just primitives (int, float, char, etc) are passed by copy/value (slower).

>
> Thanks for moving it into the correct format
> Any ideas on why the textures don't show up?
> I can see them loaded in the variable tree in debug mode.
> Jim
> ---

Did you try that code ? if that code fails too, try my AppearanceEditor with code generation for appearance.
The output code is simple and fairly to use and you can see the result in real time.
I hope it helps you.

http://planeta.terra.com.br/educacao/alessandroborges/appeditor/j3d_appe...

http://planeta.terra.com.br/educacao/alessandroborges/java3d.html

Alessandro

jjones7947
Offline
Joined: 2004-10-16

Alessandro,
I did use your code to no effect. I downloaded your editor, but couldn't get your textures to show up or mine either. I ran all your demos and they all seemed to work. I'm using j2SE 1.5 and 1.3.1 j3d. I saw the picture you included it looked great. Let's see I have a 5800 series Nvidia video card and have all my settings are on "application control". Given that it may not be a code problem at all. I commented out the buildAppearance method I posted and went back to the way I did it last time(not using TextureUnitState) and it worked. So there is something about TSU I'm missing.
Nice editor by the way, when did/do you finish your PhD/MS?
Jim

aces
Offline
Joined: 2003-07-17

In demos/java3d/TextureTest/ folder there is a example of multitexturing using texture units: MultiTextureText.java
Check it .
About the editor: thank you. I am planning a upgrade, featuring Java 1.4 goodies ;)
I finish it my post graduation in July/04.