Skip to main content

[JAVA3D-INTEREST] Setting the appearance on the top side of a cylinder

4 replies [Last post]
Anonymous

Hello everyone,

I am having problems trying to set the appearance of the top side of a
cylinder. I use a picture to indicate the texture I want to apply to my cylinder,
but when I do a setAppearance on the top side of the cylinder--setAppearance.(cyl.TOP,
theApp)-- all I see are lines but no texture(the texture render fine on the
round surface).

I use a TexCoordGeneration plus a TextureLoader to obtaint the texture and
the corresponding coordinates. But nothing seems to work. Can some one give
me a hint on how to achive this? Please? What am I omitting?

Below I post the code I am using. Thanks all in advance,
JC

=============================================
Code starts here
=============================================
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.image.TextureLoader;

public class myCylinder extends Frame implements ActionListener
{
protected Canvas3D displayCanvas;

protected SimpleUniverse myVWorld;

protected Button Quit;

public myCylinder()
{
displayCanvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
myVWorld = new SimpleUniverse(displayCanvas);
Quit = new Button("Quit");

Quit.addActionListener(this);

this.setLayout(new BorderLayout());
this.add("Center", displayCanvas);

Panel ButtonsPanel = new Panel();
ButtonsPanel.setLayout(new GridLayout(1, 3));
ButtonsPanel.add(Quit);

this.add("South", ButtonsPanel);

Appearance theApp = new Appearance();

TexCoordGeneration texCoordGen = new
TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR,
TexCoordGeneration.TEXTURE_COORDINATE_2);

TextureLoader textureLoader = new TextureLoader("turf.jpg", null);

ImageComponent2D img = textureLoader.getImage();

Texture2D wallsTexture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
img.getWidth(), img.getHeight());

wallsTexture.setBoundaryModeS(Texture.WRAP);
wallsTexture.setBoundaryModeT(Texture.WRAP);

wallsTexture.setImage(0, img);
wallsTexture.setEnable(true);
wallsTexture.setMagFilter(Texture.NICEST);
wallsTexture.setMinFilter(Texture.NICEST);

PolygonAttributes polyAttr =
new PolygonAttributes(PolygonAttributes.POLYGON_FILL,
PolygonAttributes.CULL_NONE, 0.0f);

theApp.setPolygonAttributes(polyAttr);
theApp.setTexCoordGeneration(texCoordGen);
theApp.setTexture(wallsTexture);

PointLight sunLight = new PointLight(new Color3f(0.9f, 0.9f, 0.9f),
new Point3f(0.0f, 1.0f, 1.0f),
new Point3f(1.5f, 0.0f, 0.0f));

BoundingSphere inflZone =
new BoundingSphere(new Point3d(0.0f, 0.0f, 0.0f), 300.0f);

sunLight.setInfluencingBounds(inflZone);

TransformGroup trGrpMain = new TransformGroup();
trGrpMain.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
trGrpMain.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

trGrpMain.addChild(sunLight);

Cylinder cyl = new Cylinder(1.0f, 0.1f, Cylinder.GENERATE_NORMALS,
64, 1, theApp);

cyl.setAppearance(cyl.TOP, theApp);

trGrpMain.addChild(cyl);

MouseZoom mZoom = new MouseZoom(trGrpMain);
MouseRotate mRot = new MouseRotate(trGrpMain);
MouseTranslate mTransl = new MouseTranslate(trGrpMain);

mZoom.setSchedulingBounds(inflZone);
mRot.setSchedulingBounds(inflZone);
mTransl.setSchedulingBounds(inflZone);

BranchGroup brGrp = new BranchGroup();

brGrp.addChild(trGrpMain);
brGrp.addChild(mZoom);
brGrp.addChild(mRot);
brGrp.addChild(mTransl);
myVWorld.getLocale().addBranchGraph(brGrp);

Transform3D tr3D = new Transform3D();
tr3D.setTranslation(new Vector3f(0.0f, 0.0f, 2.0f));

myVWorld.getViewingPlatform().
getViewPlatformTransform().setTransform(tr3D);

this.setSize(400, 600);
this.setVisible(true);

}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand() == "Quit")
{
this.dispose();
System.exit(0);
}

}

public static void main(String[] _args)
{
myCylinder cylind = new myCylinder();
}

}
=============================================
Code ends here
=============================================

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@java3d.dev.java.net
For additional commands, e-mail: interest-help@java3d.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
theeagle
Offline
Joined: 2006-05-12
Points: 0

nevermind ... as I expected as soon as I asked some1 for help, I found the solution myself. If anyone needs to know ... just leave all the "TexCoordGeneration" stuff away - and it works fine (I'm using the Cylinder.GENERATE_TEXTURE_COORDS tho).

brian30004
Offline
Joined: 2004-08-03
Points: 0

Try changing the primflags parameter in your Cylinder instantiation from Cylinder.GENERATE_NORMALS to Cylinder.GENERATE_TEXTURE_COORDS. You can also OR them together.

theeagle
Offline
Joined: 2006-05-12
Points: 0

Hello, it's been a year since this post was made, but I also have the same problem as Carmen here. A Cylinder, trying to texture only its top (with TexCoordGeneration.OBJECT_LINEAR). And the texture only appears as "stripes" on top (or bottom also). I tried Carmen's code above, same result. I also tried setting the suggested Cylinder primflags, no success. Tried it in J3d 1.3.2 (ogl and d3d) and 1.4.0build6 . No differences, no success anywhere.

Any1 got an idea what I'm doing wrong? Or any1 got working sample code for texturing a j3d standard cylinder?

Thx in advance ...

geoff
Offline
Joined: 2004-03-04
Points: 0

hi carmen.

the code works ok for me. in fact its the first time i
ever got a texture on any j3d object.
obviously i used my own texture. i did it with .gif
and that was ok. i did it with .jpg thats was ok as well.
i can vaguely remember image size being important, i think they have to be a multiple of something.
maybe 64, 128, 32, 256, and stuff.
the images i used were pretty basic as well.
i hope this helps. cause it sure helpes me.
i cant believe i got the html tags out of it(the long way) and it compiled first time. doing that did wonders for my guitaring as well. i am so impressed this will probably be the last time i come here. unless anybody
needs any help with there particle system that is.
geoff.

Message was edited by: geoff