-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.java
More file actions
68 lines (59 loc) · 2.6 KB
/
Sphere.java
File metadata and controls
68 lines (59 loc) · 2.6 KB
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
/* This code is from exercise sheet written by Dr. Steve Maddock */
public final class Sphere {
// ***************************************************
/* THE DATA
*/
// anticlockwise/counterclockwise ordering
private static final int XLONG = 30;
private static final int YLAT = 30;
public static final float[] vertices = createVertices();
public static final int[] indices = createIndices();
private static float[] createVertices() {
double r = 0.5;
int step = 8;
//float[]
float[] vertices = new float[XLONG * YLAT * step];
for (int j = 0; j < YLAT; ++j) {
double b = Math.toRadians(-90 + 180 * (double) (j) / (YLAT - 1));
for (int i = 0; i < XLONG; ++i) {
double a = Math.toRadians(360 * (double) (i) / (XLONG - 1));
double z = Math.cos(b) * Math.cos(a);
double x = Math.cos(b) * Math.sin(a);
double y = Math.sin(b);
int base = j * XLONG * step;
vertices[base + i * step + 0] = (float) (r * x);
vertices[base + i * step + 1] = (float) (r * y);
vertices[base + i * step + 2] = (float) (r * z);
vertices[base + i * step + 3] = (float) x;
vertices[base + i * step + 4] = (float) y;
vertices[base + i * step + 5] = (float) z;
vertices[base + i * step + 6] = (float) (i) / (float) (XLONG - 1);
vertices[base + i * step + 7] = (float) (j) / (float) (YLAT - 1);
}
}
return vertices;
//debugging code:
//for (int i=0; i<vertices.length; i+=step) {
// System.out.println(vertices[i]+", "+vertices[i+1]+", "+vertices[i+2]);
//}
}
private static int[] createIndices() {
int[] indices = new int[(XLONG - 1) * (YLAT - 1) * 6];
for (int j = 0; j < YLAT - 1; ++j) {
for (int i = 0; i < XLONG - 1; ++i) {
int base = j * (XLONG - 1) * 6;
indices[base + i * 6 + 0] = j * XLONG + i;
indices[base + i * 6 + 1] = j * XLONG + i + 1;
indices[base + i * 6 + 2] = (j + 1) * XLONG + i + 1;
indices[base + i * 6 + 3] = j * XLONG + i;
indices[base + i * 6 + 4] = (j + 1) * XLONG + i + 1;
indices[base + i * 6 + 5] = (j + 1) * XLONG + i;
}
}
return indices;
//debugging code:
//for (int i=0; i<indices.length; i+=3) {
// System.out.println(indices[i]+", "+indices[i+1]+", "+indices[i+2]);
//}
}
}