-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolidCylinder.java
115 lines (94 loc) · 2.89 KB
/
SolidCylinder.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* SolidCylinder.java
*
* William Kranich - [email protected]
*
* Used from supplied assignment template
*
* Unused
*
*/
import javax.media.opengl.*;
import com.jogamp.opengl.util.*;
import java.util.*;
public class SolidCylinder
{
private int DL; // display list
private double z_top, z_bottom; // z coords of the cylinder top and btm
private ArrayList<Coord> circle2D;
private ArrayList<Coord> circle2D_normal;
public SolidCylinder( double tx, double ty, double tz, double radius,
double height, int n_subdivision_steps )
{
circle2D = new ArrayList<Coord> ();
circle2D_normal = new ArrayList<Coord> ();
// Cylinder is centered at (0,0,0) and aligned along Z-axis
z_top = tz + height;
z_bottom = tz;
// Approximation of the 2D circle with polyline
double d_theta = 2*3.1415926/n_subdivision_steps;
double theta = 0;
for (int i = 0; i < n_subdivision_steps; ++i)
{
Coord normal = new Coord( radius*Math.cos(theta),
radius*Math.sin(theta), 0);
circle2D_normal.add( normal );
circle2D.add( new Coord ( normal.x + tx, normal.y + ty, 1));
theta += d_theta;
}
}
public void init( GL2 gl )
{
// Create the display list
DL = gl.glGenLists(1);
Coord n, p; // temp variables to store retrieved obj from ArrayList
gl.glNewList( DL, GL2.GL_COMPILE );
// Quad strip for cylinder sides
gl.glBegin( GL.GL_TRIANGLE_STRIP);
for (int i = 0; i < circle2D.size(); i++)
{
n = circle2D_normal.get(i);
p = circle2D.get(i);
gl.glNormal3d( n.x, n.y, 0 );
gl.glVertex3d( p.x, p.y, z_bottom );
gl.glNormal3d( n.x, n.y, 0 );
gl.glVertex3d( p.x, p.y, z_top );
}
n = circle2D_normal.get(0);
p = circle2D.get(0);
gl.glNormal3d( n.x, n.y, 0 );
gl.glVertex3d( p.x, p.y, z_bottom );
gl.glNormal3d( n.x, n.y, 0 );
gl.glVertex3d( p.x, p.y, z_top );
gl.glEnd(); // gl.glBegin(GL_TRIANGLE_STRIP)
// Top end cap polygon
gl.glBegin( GL2.GL_POLYGON );
for (int i = 0; i< circle2D.size(); i++ )
{
p = circle2D.get(i);
gl.glVertex3d( p.x, p.y, z_top );
gl.glNormal3d( 0, 0, 1 );
}
p = circle2D.get(0);
gl.glVertex3d( p.x, p.y, z_top );
gl.glNormal3d( 0, 0, 1 );
gl.glEnd(); // gl.glBegin(GL.GL_POLYGON);
// Bottom end cap polygon
gl.glBegin( GL2.GL_POLYGON );
for (int i = 0; i< circle2D.size(); i++ )
{
p = circle2D.get(i);
gl.glVertex3d( p.x, p.y, z_bottom );
gl.glNormal3d( 0, 0, 1 );
}
p = circle2D.get(0);
gl.glVertex3d( p.x, p.y, z_bottom );
gl.glNormal3d( 0, 0, 1 );
gl.glEnd(); // gl.glBegin(GL.GL_POLYGON);
gl.glEndList();
}
public void draw( GL2 gl )
{
gl.glCallList( DL );
}
}