-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchino.java
86 lines (76 loc) · 3.8 KB
/
Echino.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package SimulazioneEsame;
import javax.media.j3d.*;
import javax.vecmath.Point3f;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.image.TextureLoader;
import java.awt.*;
public class Echino extends Shape3D {
public static final float TOP = 0.8f;
public static final float BOTTOM = -0.8f;
protected Point3f[] v = null;
protected TriangleStripArray triangleStrip;
protected PolygonAttributes polyAttrbutes;
protected Appearance appearance;
protected Geometry geometry;
protected Material material;
public Echino(int steps, float bottomRadius) {
v = new Point3f[(steps + 1) * 2];
for (int i = 0; i < steps; i++) {
double angle = 2.0 * Math.PI * (double) i / (double) steps;
float x = (float) Math.sin(angle);
float y = (float) Math.cos(angle);
float xInferiore = (float) Math.sin(angle) * bottomRadius;
float yInferiore = (float) Math.cos(angle) * bottomRadius;
v[i * 2 + 0] = new Point3f(xInferiore, yInferiore, BOTTOM);
v[i * 2 + 1] = new Point3f(x, y, TOP);
}
v[steps * 2 + 0] = new Point3f(0.0f, bottomRadius, BOTTOM);
v[steps * 2 + 1] = new Point3f(0.0f, 1.0f, TOP);
geometry = createGeometry(steps);
appearance = createAppearance();
setGeometry(geometry);
setAppearance(appearance);
}
protected Geometry createGeometry(int steps) {
int[] stripCounts = {(steps + 1) * 2};
triangleStrip = new TriangleStripArray((steps + 1) * 2,
GeometryArray.COORDINATES, stripCounts);
triangleStrip.setCoordinates(0, v);
triangleStrip.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
triangleStrip.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
GeometryInfo geometryInfo = new GeometryInfo(triangleStrip);
NormalGenerator normalGenerator = new NormalGenerator();
normalGenerator.generateNormals(geometryInfo);
setGeometry(triangleStrip);
setGeometry(geometryInfo.getGeometryArray());
return geometryInfo.getGeometryArray();
}
protected Appearance createAppearance() {
polyAttrbutes = new PolygonAttributes();
appearance = new Appearance();
material = new Material();
// Impostazione aspetto wireframe
polyAttrbutes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
polyAttrbutes.setCullFace(PolygonAttributes.CULL_NONE);
appearance.setPolygonAttributes(polyAttrbutes);
material.setCapability(Material.ALLOW_COMPONENT_WRITE);
material.setCapability(Material.ALLOW_COMPONENT_READ);
material.setCapability(Material.AMBIENT_AND_DIFFUSE);
material.setAmbientColor(210 / 255f, 180 / 255f, 140 / 255f);
material.setDiffuseColor(210 / 255f, 180 / 255f, 140 / 255f);
appearance.setCapability(Appearance.ALLOW_MATERIAL_READ);
appearance.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
// Caricamento Texture
TextureLoader loader = new TextureLoader("C:\\Users\\utente\\IdeaProjects\\Java3D\\src\\images\\wood.jpg", "RGB", new Container());
Texture texture = loader.getTexture();
TextureAttributes textureAttributes = new TextureAttributes();
textureAttributes.setTextureMode(TextureAttributes.COMBINE);
TexCoordGeneration texCoordGeneration = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_3);
appearance.setTexCoordGeneration(texCoordGeneration);
appearance.setTexture(texture);
appearance.setTextureAttributes(textureAttributes);
appearance.setMaterial(material);
return appearance;
}
}