I manage to add the texture, but I can't for the life of me figure out what TextureCoordinates to use...
I've tried to use the coordinates gotten from the disk FaceSet, but then the texture is applied 4 times, not once like I want it too.
Posting an example code here to show what I've tried:
Code: Select all
public class CompassGeometry {
private static Appearance diskAppearance;
public static void main(String[] args) {
SceneGraphComponent myscene = SceneGraphUtility.createFullSceneGraphComponent("myscene");
double zmin = 0;
double zmax = 0.1;
double radius = 1;
IndexedFaceSet cylinder = Primitives.cylinder(20, radius, radius, zmin, zmax, Math.PI*2);
SceneGraphComponent cylinderComponent = SceneGraphUtility.createFullSceneGraphComponent("compassCylinder");
cylinderComponent.setGeometry(cylinder);
SceneGraphComponent topDisk = createDisk("Top");
SceneGraphComponent bottomDisk = createDisk("Bottom");
MatrixBuilder.euclidean().translate(0,0,zmin).scale(radius,radius,1).assignTo(topDisk);
MatrixBuilder.euclidean().translate(0,0,zmax).rotateX(Math.PI).scale(radius,radius,1).assignTo(bottomDisk);
Appearance appearance = myscene.getAppearance();
appearance.setAttribute(POLYGON_SHADER + "." + CommonAttributes.DIFFUSE_COLOR, Color.white);
appearance.setAttribute(CommonAttributes.EDGE_DRAW, false);
appearance.setAttribute(CommonAttributes.VERTEX_DRAW, false);
myscene.addChildren(cylinderComponent, topDisk, bottomDisk);
JRViewer.display(myscene);
}
private static SceneGraphComponent createDisk(String name) {
SceneGraphComponent diskComponent = SceneGraphUtility.createFullSceneGraphComponent(name);
IndexedFaceSetFactory disk = Primitives.regularPolygonFactory(20,0.0);
double[][] coordinates = disk.getIndexedFaceSet().getVertexAttributes(Attribute.COORDINATES).toDoubleArrayArray(null);
disk.setVertexTextureCoordinates(coordinates);
disk.update();
diskComponent.setGeometry(disk.getIndexedFaceSet());
diskAppearance = diskComponent.getAppearance();
URL imagePath = CompassGeometry.class.getResource("labels.png");
applyTexture(imagePath);
return diskComponent;
}
private static void applyTexture(URL imagePath) {
Texture2D tex2d = (Texture2D) AttributeEntityUtility.createAttributeEntity(Texture2D.class,
POLYGON_SHADER + "." + TEXTURE_2D, diskAppearance, true);
ImageData imageData = null;
try {
imageData = ImageData.load(new Input(imagePath));
} catch (IOException ee) {
ee.printStackTrace();
}
tex2d.setImage(imageData);
Matrix textureMatrix = new Matrix();
MatrixBuilder.euclidean().scale(1, 1, 1).assignTo(textureMatrix);
tex2d.setTextureMatrix(textureMatrix);
}
}